From c5689fa3301ffc5596d4ca4b6b6f0cc0e62f525d Mon Sep 17 00:00:00 2001 From: zugdev Date: Sat, 14 Dec 2024 20:03:04 -0300 Subject: [PATCH] feat: add commit scraping --- src/directory/directory.ts | 1 + src/directory/get-repository-pull-requests.ts | 22 +++++++++++++++++++ src/git.ts | 10 ++++++++- src/main.ts | 13 +++++++++-- 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 src/directory/get-repository-pull-requests.ts diff --git a/src/directory/directory.ts b/src/directory/directory.ts index 00119964c..3ce9f60e8 100644 --- a/src/directory/directory.ts +++ b/src/directory/directory.ts @@ -20,6 +20,7 @@ if (typeof DEVPOOL_OWNER_NAME !== "string" || typeof DEVPOOL_REPO_NAME !== "stri export type GitHubIssue = RestEndpointMethodTypes["issues"]["get"]["response"]["data"]; export type GitHubLabel = RestEndpointMethodTypes["issues"]["listLabelsOnIssue"]["response"]["data"][0]; +export type GitHubPullRequest = RestEndpointMethodTypes["pulls"]["get"]["response"]["data"]; export type StateChanges = { [key: string]: { diff --git a/src/directory/get-repository-pull-requests.ts b/src/directory/get-repository-pull-requests.ts new file mode 100644 index 000000000..ec46fe54d --- /dev/null +++ b/src/directory/get-repository-pull-requests.ts @@ -0,0 +1,22 @@ +import { GitHubPullRequest, octokit } from "./directory"; + +export async function getRepositoryPullRequests(ownerName: string, repoName: string) { + // Check if the repository is archived + const { data: repo } = await octokit.rest.repos.get({ + owner: ownerName, + repo: repoName, + }); + + if (repo.archived) { + console.warn(`Warning: Repository ${ownerName}/${repoName} is archived. Skipping pull request retrieval.`); + return []; + } + + // get all pull requests (opened and closed) + const pullRequests: GitHubPullRequest[] = await octokit.paginate({ + method: "GET", + url: `/repos/${ownerName}/${repoName}/pulls`, + }); + + return pullRequests; +} diff --git a/src/git.ts b/src/git.ts index 4c5810ee6..1b601d483 100644 --- a/src/git.ts +++ b/src/git.ts @@ -1,4 +1,4 @@ -import { DEVPOOL_OWNER_NAME, DEVPOOL_REPO_NAME, GitHubIssue, octokit } from "./directory/directory"; +import { DEVPOOL_OWNER_NAME, DEVPOOL_REPO_NAME, GitHubIssue, GitHubPullRequest, octokit } from "./directory/directory"; import { Statistics } from "./directory/statistics"; let gitChanges: Array<{ path: string; content: string }> = []; @@ -121,6 +121,14 @@ export async function commitTasks(tasks: GitHubIssue[]) { } } +export async function commitPullRequests(tasks: GitHubPullRequest[]) { + try { + await gitCommit(tasks, "devpool-pull-requests.json"); + } catch (error) { + console.error(`Error preparing devpool pull requests for github file: ${error}`); + } +} + export async function commitTwitterMap(twitterMap: TwitterMap) { try { await gitCommit(twitterMap, "twitter-map.json"); diff --git a/src/main.ts b/src/main.ts index d19a6facc..f291e671d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,10 +1,12 @@ import { calculateStatistics } from "./directory/calculate-statistics"; -import { DEVPOOL_OWNER_NAME, DEVPOOL_REPO_NAME, GitHubIssue } from "./directory/directory"; +import { DEVPOOL_OWNER_NAME, DEVPOOL_REPO_NAME, GitHubIssue, GitHubPullRequest } from "./directory/directory"; import { getPartnerUrls as getPartnerRepoUrls } from "./directory/get-partner-urls"; +import { getRepoCredentials } from "./directory/get-repo-credentials"; import { getRepositoryIssues } from "./directory/get-repository-issues"; +import { getRepositoryPullRequests } from "./directory/get-repository-pull-requests"; import { Statistics } from "./directory/statistics"; import { syncPartnerRepoIssues } from "./directory/sync-partner-repo-issues"; -import { commitStatistics, commitTasks } from "./git"; +import { commitPullRequests, commitStatistics, commitTasks } from "./git"; import { initializeTwitterMap, TwitterMap } from "./twitter/initialize-twitter-map"; export async function main() { @@ -12,15 +14,22 @@ export async function main() { const directoryIssues: GitHubIssue[] = await getRepositoryIssues(DEVPOOL_OWNER_NAME, DEVPOOL_REPO_NAME); const partnerRepoUrls = await getPartnerRepoUrls(); const taskList: GitHubIssue[] = []; + const pullRequestList: GitHubPullRequest[] = []; // for each project URL for (const partnerRepoUrl of partnerRepoUrls) { // get owner and repository names from project URL const result: GitHubIssue[] = await syncPartnerRepoIssues({ partnerRepoUrl, directoryIssues, twitterMap }); taskList.push(...result); + + // get all pull requests (opened and closed) + const [ownerName, repoName] = getRepoCredentials(partnerRepoUrl); + const pullRequests: GitHubPullRequest[] = await getRepositoryPullRequests(ownerName, repoName); + pullRequestList.push(...pullRequests); } await commitTasks(taskList); + await commitPullRequests(pullRequestList); // Calculate total rewards from devpool issues const { rewards, tasks } = await calculateStatistics(await getRepositoryIssues(DEVPOOL_OWNER_NAME, DEVPOOL_REPO_NAME));