forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.test.js
282 lines (238 loc) · 7.69 KB
/
helper.test.js
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2023 Pants project contributors (see CONTRIBUTORS.md).
// Licensed under the Apache License, Version 2.0 (see LICENSE).
const Helper = require("./helper.js");
const OWNER = "pantsbuild";
const REPO = "pants";
function expect_owner_repo(obj) {
expect(obj.owner).toBe(OWNER);
}
function get_octokit(milestone) {
return {
rest: {
actions: {
listRepoWorkflows: jest.fn((obj) => {
return {
data: {
workflows: [
{
name: "Auto Cherry-Picker",
url: "<WORKFLOW_URL>",
},
{
name: "CI Green",
url: "<OTHER_URL>",
},
],
},
};
}),
},
pulls: {
get: jest.fn((obj) => {
expect_owner_repo(obj);
return {
data: {
merge_commit_sha: "5b01f3797d102ca97969bef746bfa8d72c75832a",
milestone: milestone,
merged_by: {
login: "steve_buscemi",
},
},
};
}),
list: jest.fn(),
},
issues: {
addLabels: jest.fn((obj) => {
// NB: GitHub's actual code forwarded extra keys to the API, which rejected the data.
expect(Object.keys(obj)).toEqual([
"owner",
"repo",
"issue_number",
"labels",
]);
}),
removeLabel: jest.fn(),
createComment: jest.fn(),
listMilestones: jest.fn(),
},
},
};
}
function get_context() {
return {
serverUrl: "https://github.com",
repo: {
owner: OWNER,
repo: REPO,
},
issue: {
owner: OWNER,
repo: REPO,
number: 19214,
},
runId: 5148273558,
workflow: "Auto Cherry-Picker",
};
}
function get_core() {
return {
setFailed: jest.fn(),
};
}
test("get_prereqs fails when no milestone", async () => {
const helper = new Helper({
octokit: get_octokit(null),
context: get_context(),
core: get_core(),
});
const result = await helper.get_prereqs();
expect(result).toBe(null);
expect(helper.octokit.rest.issues.addLabels).toBeCalledTimes(1);
expect(helper.octokit.rest.issues.addLabels.mock.calls[0][0].labels).toEqual([
"auto-cherry-picking-failed",
]);
expect(helper.octokit.rest.issues.createComment).toBeCalledTimes(1);
expect(
helper.octokit.rest.issues.createComment.mock.calls[0][0].body
).toEqual(
`I was unable to cherry-pick this PR; the milestone seems to be missing.
@steve_buscemi: Please add the milestone to the PR and re-run the [Auto Cherry-Picker job](<WORKFLOW_URL>) using the "Run workflow" button.
:robot: [Beep Boop here's my run link](https://github.com/pantsbuild/pants/actions/runs/5148273558)`
);
expect(helper.core.setFailed).toBeCalledTimes(1);
});
test("get_prereqs ok_with_no_relevant_milestones", async () => {
const helper = new Helper({
octokit: get_octokit({ title: "2.16.x" }),
context: get_context(),
core: get_core(),
});
let project_milestones = [];
helper.octokit.rest.issues.listMilestones.mockImplementation((obj) => {
return {
data: project_milestones.map((title) => {
return {
title,
};
}),
};
});
project_milestones = ["2.16.x"];
expect(await helper.get_prereqs()).toEqual({
merge_commit: "5b01f3797d102ca97969bef746bfa8d72c75832a",
milestones: ["2.16.x"],
pr_num: 19214,
});
project_milestones = ["2.16.x", "2.17.x"];
expect(await helper.get_prereqs()).toEqual({
merge_commit: "5b01f3797d102ca97969bef746bfa8d72c75832a",
milestones: ["2.16.x", "2.17.x"],
pr_num: 19214,
});
// Some weird ordering
project_milestones = ["2.17.x", "2.16.x", "2.15.x"];
expect(await helper.get_prereqs()).toEqual({
merge_commit: "5b01f3797d102ca97969bef746bfa8d72c75832a",
milestones: ["2.16.x", "2.17.x"],
pr_num: 19214,
});
// Odd looking milestones
project_milestones = ["2.17.x", "2.16.x", "2023"];
expect(await helper.get_prereqs()).toEqual({
merge_commit: "5b01f3797d102ca97969bef746bfa8d72c75832a",
milestones: ["2.16.x", "2.17.x"],
pr_num: 19214,
});
// PR milestone is no longer open. That's OK, assume all milestones
project_milestones = ["2.17.x", "2.18.x"];
expect(await helper.get_prereqs()).toEqual({
merge_commit: "5b01f3797d102ca97969bef746bfa8d72c75832a",
milestones: ["2.17.x", "2.18.x"],
pr_num: 19214,
});
expect(helper.core.setFailed).not.toBeCalled();
});
test("cherry_pick_finished one pass one fail", async () => {
const helper = new Helper({
octokit: get_octokit(),
context: get_context(),
core: get_core(),
});
helper.octokit.rest.pulls.list.mockImplementation((obj) => {
if (obj.head === "pantsbuild:cherry-pick-19214-to-2.17.x") {
return {
data: [{ html_url: `<URL for 2.17.x>` }],
};
} else {
return {
data: [],
};
}
});
await helper.cherry_pick_finished("1234ABCD", [
{ branch_name: "cherry-pick-19214-to-2.16.x", milestone: "2.16.x" },
{ branch_name: "cherry-pick-19214-to-2.17.x", milestone: "2.17.x" },
]);
expect(helper.octokit.rest.issues.addLabels).toBeCalledTimes(1);
expect(helper.octokit.rest.issues.addLabels.mock.calls[0][0].labels).toEqual([
"auto-cherry-picking-failed",
]);
expect(
helper.octokit.rest.issues.createComment.mock.calls[0][0].body
).toEqual(
`I tried to automatically cherry-pick this change back to each relevant milestone, so that it is available in those older releases of Pants.
## :x: 2.16.x
I was unable to cherry-pick this PR to 2.16.x, likely due to merge-conflicts.
<details>
<summary>Steps to Cherry-Pick locally</summary>
To resolve:
1. (Ensure your git working directory is clean)
2. Run the following script to reproduce the merge-conflicts:
\`\`\`bash
git fetch https://github.com/pantsbuild/pants main \\
&& git fetch https://github.com/pantsbuild/pants 2.16.x \\
&& git checkout -b cherry-pick-19214-to-2.16.x FETCH_HEAD \\
&& git cherry-pick 1234ABCD
\`\`\`
3. Fix the merge conflicts and commit the changes
4. Run \`build-support/cherry_pick/make_pr.sh "19214" "2.16.x"\`
Please note that I cannot re-run CI if a job fails. Please work with your PR approver(s) to re-run CI if necessary.
</details>
## :heavy_check_mark: 2.17.x
Successfully opened <URL for 2.17.x>.
---
When you're done manually cherry-picking, please remove the \`needs-cherrypick\` label on this PR.
Thanks again for your contributions!
:robot: [Beep Boop here's my run link](https://github.com/pantsbuild/pants/actions/runs/5148273558)`
);
});
test("cherry_pick_finished all pass", async () => {
const helper = new Helper({
octokit: get_octokit(),
context: get_context(),
core: get_core(),
});
helper.octokit.rest.pulls.list.mockImplementation((obj) => {
return {
data: [{ html_url: `<URL for 2.16.x>` }],
};
});
await helper.cherry_pick_finished("1234ABCD", [
{ branch_name: "cherry-pick-19214-to-2.16.x", milestone: "2.16.x" },
]);
expect(helper.octokit.rest.issues.removeLabel).toBeCalledTimes(1);
expect(helper.octokit.rest.issues.removeLabel.mock.calls[0][0].name).toEqual(
"needs-cherrypick"
);
expect(
helper.octokit.rest.issues.createComment.mock.calls[0][0].body
).toEqual(
`I tried to automatically cherry-pick this change back to each relevant milestone, so that it is available in those older releases of Pants.
## :heavy_check_mark: 2.16.x
Successfully opened <URL for 2.16.x>.
---
Thanks again for your contributions!
:robot: [Beep Boop here's my run link](https://github.com/pantsbuild/pants/actions/runs/5148273558)`
);
});