diff --git a/scraper/src/github-scraper/parseEvents.ts b/scraper/src/github-scraper/parseEvents.ts index dd62526a..1b6fdc8b 100644 --- a/scraper/src/github-scraper/parseEvents.ts +++ b/scraper/src/github-scraper/parseEvents.ts @@ -8,24 +8,8 @@ import { calculateTurnaroundTime } from "./utils.js"; import { parseISO } from "date-fns"; import { isBlacklisted } from "./utils.js"; import { octokit } from "./config.js"; - const processedData: ProcessData = {}; -const defaultBranches: Record = {}; - -async function getDefaultBranch(owner: string, repo: string) { - if (defaultBranches[repo] == null) { - try { - const { data } = await octokit.request("GET /repos/{owner}/{repo}", { - owner, - repo, - }); - defaultBranches[repo] = data.default_branch; - } catch (e) { - console.error(`Error fetching default branch for ${owner}/${repo} `); - } - } - return defaultBranches[repo]; -} + function appendEvent(user: string, event: Activity) { console.log(`Appending event for ${user}`); if (!processedData[user]) { @@ -50,20 +34,11 @@ const emailUserCache: { [key: string]: string } = {}; async function addCollaborations(event: PullRequestEvent, eventTime: Date) { const collaborators: Set = new Set(); - const [owner, repo] = event.repo.name.split("/"); - const defaultBranch = await getDefaultBranch(owner, repo); - if (event.payload.pull_request.base.ref !== defaultBranch) { - return; - } + const url: string | undefined = event.payload.pull_request?.commits_url; - const url = event.payload.pull_request?.commits_url; - const { data: commits } = await octokit.request("GET " + url); + const response = await octokit.request("GET " + url); + const commits = response.data; for (const commit of commits) { - // Merge commits has more than 1 parent commits; skip merge commit authors from being counted as collaborators - if (commit.parents.length > 1) { - continue; - } - let authorLogin = commit.author && commit.author.login; if (!authorLogin) { authorLogin = commit.commit.author.name; @@ -75,11 +50,12 @@ async function addCollaborations(event: PullRequestEvent, eventTime: Date) { collaborators.add(authorLogin); - const coAuthors = commit.commit.message.match( - /Co-authored-by: (.+) <(.+)>/, + const coAuthors = commit.commit.message.matchAll( + /Co-authored-by: (.+?) <(.+?)>/g, ); if (coAuthors) { - for (const [name, email] of coAuthors) { + //First Element of the Array is full match and not the name + for (const [, name, email] of coAuthors) { if (isBlacklisted(name)) { continue; } @@ -124,10 +100,11 @@ async function addCollaborations(event: PullRequestEvent, eventTime: Date) { } if (collaborators.size > 1) { - const collaboratorArray = Array.from(collaborators); + const collaboratorArray = Array.from(collaborators); // Convert Set to Array for (const user of collaboratorArray) { const others = new Set(collaborators); const othersArray = Array.from(others); + others.delete(user); appendEvent(user, { type: "pr_collaborated", @@ -202,15 +179,13 @@ export const parseEvents = async (events: IGitHubEvent[]) => { } break; case "PullRequestReviewEvent": - if (event.payload.pull_request.user.login !== user) { - appendEvent(user, { - type: "pr_reviewed", - time: eventTime?.toISOString(), - title: `${event.repo.name}#${event.payload.pull_request.number}`, - link: event.payload.review.html_url, - text: event.payload.pull_request.title, - }); - } + appendEvent(user, { + type: "pr_reviewed", + time: eventTime?.toISOString(), + title: `${event.repo.name}#${event.payload.pull_request.number}`, + link: event.payload.review.html_url, + text: event.payload.pull_request.title, + }); break; default: break;