forked from pytorch/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrcrOncallBot.test.ts
More file actions
243 lines (220 loc) · 6.51 KB
/
Copy pathcrcrOncallBot.test.ts
File metadata and controls
243 lines (220 loc) · 6.51 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import nock from "nock";
import { Probot } from "probot";
import crcrOncallBot from "../lib/bot/crcrOncallBot";
import * as crcrAllowlist from "../lib/crcrAllowlist";
import { clearAllowlistCache, CrcrAllowlist } from "../lib/crcrAllowlist";
import { handleScope } from "./common";
import * as utils from "./utils";
nock.disableNetConnect();
const OWNER = "pytorch";
const REPO = "pytorch";
const PR_NUMBER = 42;
const HEAD_SHA = "abc123def456789";
function mockAllowlist() {
const mockAl = CrcrAllowlist.fromYaml(`
L3:
xpu:
intel/torch-xpu-ops: [oncall_xpu]
L4:
- apple/mps-backend: oncall_mps
`);
jest.spyOn(crcrAllowlist, "fetchCrcrAllowlist").mockResolvedValue(mockAl);
}
describe("crcrOncallBot", () => {
let probot: Probot;
beforeEach(() => {
clearAllowlistCache();
probot = utils.testProbot();
probot.load(crcrOncallBot);
nock("https://api.github.com")
.post("/app/installations/2/access_tokens")
.reply(200, { token: "test" });
mockAllowlist();
});
afterEach(() => {
nock.cleanAll();
jest.restoreAllMocks();
});
function checkRunPayload(overrides: Record<string, unknown> = {}) {
return {
action: "completed",
check_run: {
name: "crcr/intel/torch-xpu-ops/CI/build",
head_sha: HEAD_SHA,
status: "completed",
conclusion: "failure",
html_url: "https://github.com/pytorch/pytorch/runs/12345",
pull_requests: [{ number: PR_NUMBER }],
...overrides,
},
repository: {
owner: { login: OWNER },
name: REPO,
},
};
}
test("posts comment on L3 CRCR check run failure", async () => {
const scope = nock("https://api.github.com")
.get(`/repos/${OWNER}/${REPO}/issues/${PR_NUMBER}/comments`)
.reply(200, [])
.post(
`/repos/${OWNER}/${REPO}/issues/${PR_NUMBER}/comments`,
(body: any) => {
expect(body.body).toContain(
"<!-- crcr-oncall:intel/torch-xpu-ops -->"
);
expect(body.body).toContain("intel/torch-xpu-ops");
expect(body.body).toContain("@oncall_xpu");
expect(body.body).toContain(HEAD_SHA.slice(0, 7));
return true;
}
)
.reply(200);
await probot.receive({
name: "check_run" as any,
payload: checkRunPayload() as any,
id: "1",
});
handleScope(scope);
});
test("posts comment on L4 CRCR check run failure", async () => {
const scope = nock("https://api.github.com")
.get(`/repos/${OWNER}/${REPO}/issues/${PR_NUMBER}/comments`)
.reply(200, [])
.post(
`/repos/${OWNER}/${REPO}/issues/${PR_NUMBER}/comments`,
(body: any) => {
expect(body.body).toContain("apple/mps-backend");
expect(body.body).toContain("@oncall_mps");
return true;
}
)
.reply(200);
await probot.receive({
name: "check_run" as any,
payload: checkRunPayload({
name: "crcr/apple/mps-backend/CI/build",
}) as any,
id: "2",
});
handleScope(scope);
});
test("does not comment on non-CRCR check runs", async () => {
await probot.receive({
name: "check_run" as any,
payload: checkRunPayload({
name: "some-other-check",
}) as any,
id: "3",
});
});
test("does not comment on successful check runs", async () => {
await probot.receive({
name: "check_run" as any,
payload: checkRunPayload({
conclusion: "success",
}) as any,
id: "4",
});
});
test("does not comment on neutral check runs", async () => {
await probot.receive({
name: "check_run" as any,
payload: checkRunPayload({
conclusion: "neutral",
}) as any,
id: "5",
});
});
test("dedup: does not comment twice if marker already exists", async () => {
const scope = nock("https://api.github.com")
.get(`/repos/${OWNER}/${REPO}/issues/${PR_NUMBER}/comments`)
.reply(200, [
{
id: 100,
body: "<!-- crcr-oncall:intel/torch-xpu-ops -->\nprevious comment",
},
]);
await probot.receive({
name: "check_run" as any,
payload: checkRunPayload() as any,
id: "6",
});
handleScope(scope);
});
test("does not comment when allowlist has no oncalls for repo", async () => {
const mockAl = CrcrAllowlist.fromYaml(`
L3:
xpu:
intel/no-oncall-repo: []
`);
jest.spyOn(crcrAllowlist, "fetchCrcrAllowlist").mockResolvedValue(mockAl);
await probot.receive({
name: "check_run" as any,
payload: checkRunPayload({
name: "crcr/intel/no-oncall-repo/CI/build",
}) as any,
id: "7",
});
});
test("does not comment when output has no PR number", async () => {
// output.summary exists but regex doesn't match (empty pr_number);
// falls through to Search API which also returns nothing
const scope = nock("https://api.github.com")
.get("/search/issues")
.query(true)
.reply(200, { total_count: 0, items: [] });
await probot.receive({
name: "check_run" as any,
payload: checkRunPayload({
pull_requests: [],
output: {
title: "In progress",
summary: "intel/torch-xpu-ops workflow for PR : https://example.com",
},
}) as any,
id: "8",
});
handleScope(scope);
});
test("posts comment when output contains PR number for cross-fork PR", async () => {
const scope = nock("https://api.github.com")
.get(`/repos/${OWNER}/${REPO}/issues/${PR_NUMBER}/comments`)
.reply(200, [])
.post(
`/repos/${OWNER}/${REPO}/issues/${PR_NUMBER}/comments`,
(body: any) => {
expect(body.body).toContain(
"<!-- crcr-oncall:intel/torch-xpu-ops -->"
);
expect(body.body).toContain("@oncall_xpu");
return true;
}
)
.reply(200);
await probot.receive({
name: "check_run" as any,
payload: checkRunPayload({
pull_requests: [], // empty — simulates cross-fork PR
output: {
title: "Failure",
summary: `intel/torch-xpu-ops workflow for PR ${PR_NUMBER}: https://example.com`,
},
}) as any,
id: "10",
});
handleScope(scope);
});
test("does nothing for unsupported org", async () => {
const payload = checkRunPayload();
payload.repository = {
owner: { login: "some-other-org" },
name: REPO,
};
await probot.receive({
name: "check_run" as any,
payload: payload as any,
id: "9",
});
});
});