Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,64 @@ describe('listCommentReactions', () => {
repo: 'repo',
comment_id: 12345,
per_page: 100,
page: 1,
});
});

it('follows pagination when the reporter reacted after the first 100 reactions', async () => {
const firstPage = Array.from({ length: 100 }, (_, index) => ({
content: '+1',
user: { login: `user-${index}` },
}));
const listForIssueComment = vi
.fn()
.mockResolvedValueOnce({ data: firstPage })
.mockResolvedValueOnce({
data: [{ content: '-1', user: { login: 'reporter-user' } }],
});
const mockClient = { reactions: { listForIssueComment } };

const result = await listCommentReactions(mockClient as never, 'owner', 'repo', 12345);

expect(result).toHaveLength(101);
expect(result.at(-1)).toEqual({ content: '-1', login: 'reporter-user' });
expect(listForIssueComment).toHaveBeenNthCalledWith(1, {
owner: 'owner',
repo: 'repo',
comment_id: 12345,
per_page: 100,
page: 1,
});
expect(listForIssueComment).toHaveBeenNthCalledWith(2, {
owner: 'owner',
repo: 'repo',
comment_id: 12345,
per_page: 100,
page: 2,
});
});

it('requests another page when the previous page has exactly 100 reactions', async () => {
const firstPage = Array.from({ length: 100 }, (_, index) => ({
content: '+1',
user: { login: `user-${index}` },
}));
const listForIssueComment = vi
.fn()
.mockResolvedValueOnce({ data: firstPage })
.mockResolvedValueOnce({ data: [] });
const mockClient = { reactions: { listForIssueComment } };

const result = await listCommentReactions(mockClient as never, 'owner', 'repo', 12345);

expect(result).toHaveLength(100);
expect(listForIssueComment).toHaveBeenCalledTimes(2);
expect(listForIssueComment).toHaveBeenNthCalledWith(2, {
owner: 'owner',
repo: 'repo',
comment_id: 12345,
per_page: 100,
page: 2,
});
});
});
Expand Down
41 changes: 28 additions & 13 deletions packages/outpost/shared/src/integrations/github-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface GithubReactionClient {
repo: string;
comment_id: number;
per_page?: number;
page?: number;
}): Promise<{ data: Array<{ content: string; user: { login: string } | null }> }>;
};
}
Expand All @@ -49,27 +50,41 @@ export interface CommentReaction {
}

/**
* List reactions on a GitHub issue comment. Only works for issue comments
* (numeric REST comment IDs) β€” discussion-comment reactions are addressed
* by GraphQL node_ids, not numeric REST IDs, and require the GraphQL API,
* which is out of scope for this client.
* List every page of reactions on a GitHub issue comment. Only works for issue
* comments (numeric REST comment IDs) β€” discussion-comment reactions are addressed
* by GraphQL node_ids, not numeric REST IDs, and require the GraphQL API, which
* is out of scope for this client.
*/
export async function listCommentReactions(
client: GithubReactionClient,
owner: string,
repo: string,
commentId: number,
): Promise<CommentReaction[]> {
const response = await client.reactions.listForIssueComment({
owner,
repo,
comment_id: commentId,
per_page: 100,
});
const reactions: CommentReaction[] = [];
const perPage = 100;
let page = 1;

return response.data
.filter((r) => r.user !== null)
.map((r) => ({ content: r.content, login: r.user!.login }));
while (true) {
const response = await client.reactions.listForIssueComment({
owner,
repo,
comment_id: commentId,
per_page: perPage,
page,
});

reactions.push(
...response.data
.filter((r) => r.user !== null)
.map((r) => ({ content: r.content, login: r.user!.login })),
);

if (response.data.length < perPage) break;
page += 1;
}

return reactions;
}

/**
Expand Down