-
Notifications
You must be signed in to change notification settings - Fork 319
Expand file tree
/
Copy pathgithubPushPrReviews.test.ts
More file actions
149 lines (125 loc) · 4.95 KB
/
Copy pathgithubPushPrReviews.test.ts
File metadata and controls
149 lines (125 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { expect, test, vi, describe } from 'vitest';
import { Octokit } from 'octokit';
import { githubPushPrReviews } from './githubPushPrReviews';
import { sourcebot_pr_payload, sourcebot_file_diff_review } from '../types';
vi.mock('@sourcebot/shared', () => ({
createLogger: () => ({
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
}),
}));
const MOCK_PAYLOAD: sourcebot_pr_payload = {
title: 'Test PR',
description: 'desc',
hostDomain: 'github.com',
owner: 'my-org',
repo: 'my-repo',
file_diffs: [],
number: 7,
head_sha: 'sha_abc123',
};
const SINGLE_REVIEW: sourcebot_file_diff_review[] = [
{
filename: 'src/foo.ts',
reviews: [{ line_start: 10, line_end: 10, review: 'Missing null check' }],
},
];
function makeMockOctokit(createReviewCommentResult: 'resolve' | 'reject' = 'resolve') {
return {
rest: {
pulls: {
createReviewComment: createReviewCommentResult === 'resolve'
? vi.fn().mockResolvedValue({})
: vi.fn().mockRejectedValue(new Error('Unprocessable Entity')),
},
},
} as unknown as Octokit;
}
describe('githubPushPrReviews', () => {
test('posts a review comment for each review', async () => {
const octokit = makeMockOctokit();
await githubPushPrReviews(octokit, MOCK_PAYLOAD, SINGLE_REVIEW);
expect(octokit.rest.pulls.createReviewComment).toHaveBeenCalledOnce();
expect(octokit.rest.pulls.createReviewComment).toHaveBeenCalledWith(
expect.objectContaining({
owner: 'my-org',
repo: 'my-repo',
pull_number: 7,
commit_id: 'sha_abc123',
body: 'Missing null check',
path: 'src/foo.ts',
side: 'RIGHT',
line: 10,
}),
);
});
test('uses line for a single-line review', async () => {
const octokit = makeMockOctokit();
await githubPushPrReviews(octokit, MOCK_PAYLOAD, SINGLE_REVIEW);
const call = octokit.rest.pulls.createReviewComment.mock.calls[0][0];
expect(call).toHaveProperty('line', 10);
expect(call).not.toHaveProperty('start_line');
});
test('uses start_line and line for a multi-line review', async () => {
const multiLineReviews: sourcebot_file_diff_review[] = [
{
filename: 'src/bar.ts',
reviews: [{ line_start: 5, line_end: 15, review: 'Refactor this block' }],
},
];
const octokit = makeMockOctokit();
await githubPushPrReviews(octokit, MOCK_PAYLOAD, multiLineReviews);
const call = octokit.rest.pulls.createReviewComment.mock.calls[0][0];
expect(call).toHaveProperty('start_line', 5);
expect(call).toHaveProperty('line', 15);
expect(call).toHaveProperty('start_side', 'RIGHT');
});
test('posts multiple reviews across multiple files', async () => {
const multiFileReviews: sourcebot_file_diff_review[] = [
{
filename: 'src/a.ts',
reviews: [
{ line_start: 1, line_end: 1, review: 'Comment A1' },
{ line_start: 5, line_end: 5, review: 'Comment A2' },
],
},
{
filename: 'src/b.ts',
reviews: [{ line_start: 3, line_end: 3, review: 'Comment B1' }],
},
];
const octokit = makeMockOctokit();
await githubPushPrReviews(octokit, MOCK_PAYLOAD, multiFileReviews);
expect(octokit.rest.pulls.createReviewComment).toHaveBeenCalledTimes(3);
});
test('continues posting remaining reviews when one fails', async () => {
const twoReviews: sourcebot_file_diff_review[] = [
{
filename: 'src/foo.ts',
reviews: [
{ line_start: 1, line_end: 1, review: 'First' },
{ line_start: 2, line_end: 2, review: 'Second' },
],
},
];
const mockCreate = vi.fn()
.mockRejectedValueOnce(new Error('422'))
.mockResolvedValueOnce({});
const octokit = { rest: { pulls: { createReviewComment: mockCreate } } } as unknown as Octokit;
await githubPushPrReviews(octokit, MOCK_PAYLOAD, twoReviews);
expect(mockCreate).toHaveBeenCalledTimes(2);
});
test('does not throw when all review comments fail', async () => {
const octokit = makeMockOctokit('reject');
await expect(
githubPushPrReviews(octokit, MOCK_PAYLOAD, SINGLE_REVIEW),
).resolves.toBeUndefined();
});
test('does nothing when file_diff_reviews is empty', async () => {
const octokit = makeMockOctokit();
await githubPushPrReviews(octokit, MOCK_PAYLOAD, []);
expect(octokit.rest.pulls.createReviewComment).not.toHaveBeenCalled();
});
});