Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"fix: update regex and use matchall for multiple coauthors" #492 #540

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 17 additions & 42 deletions scraper/src/github-scraper/parseEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {};

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]) {
Expand All @@ -50,20 +34,11 @@ const emailUserCache: { [key: string]: string } = {};
async function addCollaborations(event: PullRequestEvent, eventTime: Date) {
const collaborators: Set<string> = 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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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;
Expand Down
Loading