diff --git a/package-lock.json b/package-lock.json index 9d8d6f7..52f98e7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -859,7 +859,6 @@ "integrity": "sha512-zWW5KPngR/yvakJgGOmZ5vTBemDoSqF3AcV/LrO5u5wTWyEAVVh+IT39G4gtyAkh3CtTZs8aX/yRM82OfzHJRg==", "dev": true, "license": "MIT", - "peer": true, "dependencies": { "undici-types": "~7.16.0" } @@ -1792,7 +1791,6 @@ "integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==", "dev": true, "license": "Apache-2.0", - "peer": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" diff --git a/solutions/get-pr-comments/README.md b/solutions/get-pr-comments/README.md index 577c258..c6d1272 100644 --- a/solutions/get-pr-comments/README.md +++ b/solutions/get-pr-comments/README.md @@ -7,13 +7,22 @@ A CLI and library to fetch all comments for a GitHub pull request using github-r CLI usage ```bash -get-pr-comments +get-pr-comments [username] ``` +- `owner`: Repository owner +- `repo`: Repository name +- `prNumber`: Pull request number +- `username` (optional): Filter comments by this GitHub username + NPM usage ```bash +# Get all comments npm run start -- dfberry gh 16 + +# Filter comments by username +npm run start -- dfberry gh 16 copilot ``` ## Authentication @@ -45,8 +54,17 @@ npm run start -- owner repo 123 ```ts import { fetchPRComments } from 'get-pr-comments'; -const comments = await fetchPRComments('owner', 'repo', 123, 'ghp_...'); -console.log(comments); +// Get all comments +const allComments = await fetchPRComments('owner', 'repo', 123); +console.log(allComments); + +// Filter comments by username +const userComments = await fetchPRComments('owner', 'repo', 123, 'username'); +console.log(userComments); + +// Filter comments by username with a token +const userCommentsAuth = await fetchPRComments('owner', 'repo', 123, 'username', 'ghp_...'); +console.log(userCommentsAuth); ``` ## Output diff --git a/solutions/get-pr-comments/src/cli.ts b/solutions/get-pr-comments/src/cli.ts index 9303e88..b5c621f 100644 --- a/solutions/get-pr-comments/src/cli.ts +++ b/solutions/get-pr-comments/src/cli.ts @@ -2,9 +2,9 @@ import { fetchPRComments } from './index.js'; async function main() { - const [owner, repo, prNumberStr] = process.argv.slice(2); + const [owner, repo, prNumberStr, username] = process.argv.slice(2); if (!owner || !repo || !prNumberStr) { - console.error('Usage: get-pr-comments '); + console.error('Usage: get-pr-comments [username]'); process.exit(1); } const prNumber = Number(prNumberStr); @@ -15,13 +15,17 @@ async function main() { const token = process.env.GH_TOKEN || process.env.GITHUB_TOKEN; if (!token) { - console.error('Warning: No GitHub token provided. You may hit rate limits.'); + if (username) { + console.error('Warning: No GitHub token provided. Username filtering may not work correctly without authentication.'); + } else { + console.error('Warning: No GitHub token provided. You may hit rate limits.'); + } } else { console.log('Using GitHub token from environment variable.'); } try { - const comments = await fetchPRComments(owner, repo, prNumber, token); + const comments = await fetchPRComments(owner, repo, prNumber, username, token); console.log(JSON.stringify(comments, null, 2)); } catch (err) { console.error('Error fetching PR comments:', err); diff --git a/solutions/get-pr-comments/src/index.ts b/solutions/get-pr-comments/src/index.ts index 50434c1..f93d278 100644 --- a/solutions/get-pr-comments/src/index.ts +++ b/solutions/get-pr-comments/src/index.ts @@ -1,6 +1,27 @@ import { GitHubClient, getPullRequestComments } from 'github-rest'; -export async function fetchPRComments(owner: string, repo: string, prNumber: number, token?: string) { +export async function fetchPRComments( + owner: string, + repo: string, + prNumber: number, + username?: string, + token?: string +) { const client = new GitHubClient({ token }); - return await getPullRequestComments(client, owner, repo, prNumber); + const comments = await getPullRequestComments(client, owner, repo, prNumber); + + // If no username filter is provided, return all comments + if (!username) { + return comments; + } + + // Filter comments by username + return { + issueComments: comments.issueComments.filter( + (comment: any) => comment.user?.login === username + ), + reviewComments: comments.reviewComments.filter( + (comment: any) => comment.user?.login === username + ), + }; }