Skip to content
Draft
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 21 additions & 3 deletions solutions/get-pr-comments/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <owner> <repo> <prNumber>
get-pr-comments <owner> <repo> <prNumber> [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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 8 additions & 4 deletions solutions/get-pr-comments/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <owner> <repo> <prNumber>');
console.error('Usage: get-pr-comments <owner> <repo> <prNumber> [username]');
process.exit(1);
}
const prNumber = Number(prNumberStr);
Expand All @@ -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);
Expand Down
25 changes: 23 additions & 2 deletions solutions/get-pr-comments/src/index.ts
Original file line number Diff line number Diff line change
@@ -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
),
};
}