Support multiple repositories parameters via the CLI

This commit is contained in:
Jimmy Royer 2024-12-30 14:39:36 -05:00
parent 960b6f51a9
commit d0abf2f802
2 changed files with 27 additions and 40 deletions

3
.vscode/launch.json vendored
View file

@ -12,8 +12,9 @@
"src/exportComments.ts", "src/exportComments.ts",
"--owner", "--owner",
"cds-snc", "cds-snc",
"--repo", "--repos",
"notification-terraform", "notification-terraform",
"notification-manifests",
"--author", "--author",
"github-actions[bot]", "github-actions[bot]",
"--since", "--since",

View file

@ -57,12 +57,14 @@ const argv = yargs(hideBin(process.argv))
description: "Repository owner", description: "Repository owner",
demandOption: true, demandOption: true,
}) })
.option("repo", { .option("repos", {
alias: "r", alias: "r",
type: "string", type: "array",
description: "Repository name", description: "List of repository names",
demandOption: true, demandOption: true,
}) })
.array("repos")
.string("repos")
.option("author", { .option("author", {
alias: "a", alias: "a",
type: "string", type: "string",
@ -136,25 +138,10 @@ function extractCategories(reactions: Record<string, any>): string[] {
return nonEmptyCategory; return nonEmptyCategory;
} }
async function fetchCommentsForPRs(prNumbers: string): Promise<void> { async function fetchCommentsForPR(owner: string, repository: string, prNumber: number, author?: string): Promise<Comment[]> {
const intPrNumbers = prNumbers
.split(",")
.map((pr: string) => parseInt(pr.trim(), 10));
let allComments: Comment[] = [];
for (const prNumber of intPrNumbers) {
const comments = await fetchCommentsForPR(prNumber);
allComments = allComments.concat(comments);
}
await csvWriter.writeRecords(allComments);
console.log("CSV file written successfully");
}
async function fetchCommentsForPR(prNumber: number): Promise<Comment[]> {
try { try {
const response = await axios.get( const response = await axios.get(
`https://api.github.com/repos/${argv.owner}/${argv.repo}/pulls/${prNumber}/comments`, `https://api.github.com/repos/${owner}/${repository}/pulls/${prNumber}/comments`,
{ {
headers: { headers: {
Authorization: `token ${argv.token}`, Authorization: `token ${argv.token}`,
@ -165,11 +152,11 @@ async function fetchCommentsForPR(prNumber: number): Promise<Comment[]> {
let comments: Comment[] = await Promise.all( let comments: Comment[] = await Promise.all(
response.data.map(async (comment: Record<string, any>) => { response.data.map(async (comment: Record<string, any>) => {
const categories = extractCategories(comment.reactions); const categories = extractCategories(comment.reactions);
console.debug(`Categories for comment ${argv.repo}/pull/${prNumber}/${comment.id}:`, categories); console.debug(`Categories for comment ${repository}/pull/${prNumber}/${comment.id}:`, categories);
return { return {
date: comment.created_at, date: comment.created_at,
author: comment.user.login, author: comment.user.login,
repository: `${argv.owner}/${argv.repo}`, repository: `${owner}/${repository}`,
prNumber: prNumber.toString(), prNumber: prNumber.toString(),
category: categories, category: categories,
comment: comment.body, comment: comment.body,
@ -178,8 +165,8 @@ async function fetchCommentsForPR(prNumber: number): Promise<Comment[]> {
}) })
); );
if (argv.author) { if (author) {
comments = comments.filter((comment) => comment.author === argv.author); comments = comments.filter((comment) => comment.author === author);
} }
return comments; return comments;
@ -189,23 +176,20 @@ async function fetchCommentsForPR(prNumber: number): Promise<Comment[]> {
} }
} }
async function fetchComments(since: Date, until: Date = new Date()): Promise<Comment[]> { // Function to fetch comments for multiple repositories
let comments: Comment[] = []; async function fetchCommentsForRepos(owner: string, repositories: string[], since: Date, until: Date, author?: string): Promise<Comment[]> {
try { let allComments: Comment[] = [];
const pullRequests = await fetchPullRequests(argv.owner, argv.repo, since, until); for (const repository of repositories) {
console.log(`Fetching comments for repository ${owner}/${repository}...`);
const pullRequests = await fetchPullRequests(owner, repository, since, until);
for (const pr of pullRequests) { for (const pr of pullRequests) {
console.log(`Fetching comments for PR ${argv.repo}/pull/${pr.number}...`); const prComments = await fetchCommentsForPR(owner, repository, pr.number, author);
const prComments = await fetchCommentsForPR(pr.number); allComments = allComments.concat(prComments);
comments = comments.concat(prComments);
} }
} }
finally { await csvWriter.writeRecords(allComments);
console.debug(`comments=${comments}`);
await csvWriter.writeRecords(comments);
console.log("CSV file written successfully"); console.log("CSV file written successfully");
} return allComments;
return comments;
} }
async function fetchPullRequests(owner: string, repo: string, since: Date, until: Date) { async function fetchPullRequests(owner: string, repo: string, since: Date, until: Date) {
@ -213,6 +197,7 @@ async function fetchPullRequests(owner: string, repo: string, since: Date, until
let page = 1; let page = 1;
let hasMore = true; let hasMore = true;
console.debug(`Fetching pull requests for ${owner}/${repo}...`);
while (hasMore) { while (hasMore) {
const response = await axios.get(`https://api.github.com/repos/${owner}/${repo}/pulls`, { const response = await axios.get(`https://api.github.com/repos/${owner}/${repo}/pulls`, {
headers: { headers: {
@ -238,7 +223,8 @@ async function fetchPullRequests(owner: string, repo: string, since: Date, until
} }
} }
console.debug(`Fetched ${pullRequests.length} pull requests for ${owner}/${repo}`);
return pullRequests; return pullRequests;
} }
fetchComments(argv.since, argv.until); fetchCommentsForRepos(argv.owner, argv.repos, argv.since, argv.until, argv.author);