-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.js
498 lines (459 loc) · 15.4 KB
/
action.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
const core = require("@actions/core");
const github = require("@actions/github");
const getOctokit = require("./lib/actions/octokit");
const {
updateAsanaTask,
getTask,
moveTaskToProjectSection,
getProjectSections,
} = require("./lib/actions/asana");
const { getMobsuccessYMLFromRepo } = require("./lib/mobsuccessyml");
const customFieldLive = require("./lib/asana/custom-fields/live");
const customFieldStorybook = require("./lib/asana/custom-fields/storybook");
const customFieldPR = require("./lib/asana/custom-fields/asana-pr");
const customFieldPRStatus = require("./lib/asana/custom-fields/asana-pr-status");
const asanaMagics = require("@mobsuccess-devops/asana-magics");
const asanaSprintProjectId = asanaMagics.projects.currentSprint.gid;
const asanaSprintSectionIds = asanaMagics.projects.currentSprint.sections;
const octokit = getOctokit();
exports.getPullReviewStatuses = async function getPullReviewStatuses({
pullRequest,
}) {
const { data: reviews } = await octokit.pulls.listReviews({
...github.context.repo,
pull_number: pullRequest.number,
});
reviews.sort(({ submitted_at: a }, { submitted_at: b }) => {
return -a.localeCompare(b);
});
console.log("Sorted Reviews", JSON.stringify(reviews, undefined, 4));
const stateByUserId = reviews
.filter(
({ state }) => ["APPROVED", "CHANGES_REQUESTED"].indexOf(state) >= 0
)
.reduce(
(s, { user: { id: userId }, state }) => ({ [userId]: state, ...s }),
{}
);
console.log("State by user ID", stateByUserId);
return Object.entries(stateByUserId).reduce(
(s, [, state]) => ({
...s,
...(state === "APPROVED" ? { isApproved: true } : {}),
...(state === "CHANGES_REQUESTED" ? { isRejected: true } : {}),
}),
{}
);
};
exports.getPullIsMerged = async function getPullIsMerged({ pullRequest }) {
const { merged_at: mergedAt } = pullRequest;
return !!mergedAt;
};
exports.getPullIsDraft = async function getPullIsDraft({ pullRequest }) {
const { draft } = pullRequest;
return !!draft;
};
exports.getAsanaPRStatus = async function getAsanaPRStatus({ pullRequest }) {
const { isApproved, isRejected } = await exports.getPullReviewStatuses({
pullRequest,
});
const numberOfRequestedReviewers = (
pullRequest.requested_reviewers || []
).filter(({ login }) => login !== "ms-testers").length;
const isMerged = await exports.getPullIsMerged({ pullRequest });
const isDraft = await exports.getPullIsDraft({ pullRequest });
console.log("Asana status", {
isApproved,
isRejected,
isMerged,
numberOfRequestedReviewers,
isDraft,
});
if (isMerged) {
return customFieldPRStatus.values.merged;
} else if (isDraft) {
return customFieldPRStatus.values.inProgress;
} else if (isRejected) {
return customFieldPRStatus.values.rejected;
} else if (numberOfRequestedReviewers) {
return customFieldPRStatus.values.inReview;
} else if (isApproved) {
return customFieldPRStatus.values.approved;
} else {
return customFieldPRStatus.values.inProgress;
}
};
exports.findAsanaTaskId = function findAsanaTaskId({
triggerPhrase,
pullRequest,
}) {
const { body } = pullRequest;
const regexString = `${triggerPhrase}(?:\\s*)https:\\/\\/app.asana.com\\/(\\d+)\\/(?<project>\\d+)\\/(?<task>\\d+)`;
const regex = new RegExp(regexString, "gi");
console.info("looking in body", body, "regex", regexString);
let foundAsanaTasks = [];
let parseAsanaURL;
while ((parseAsanaURL = regex.exec(body)) !== null) {
const taskId = parseAsanaURL.groups.task;
foundAsanaTasks.push(taskId);
}
console.info(
`found ${foundAsanaTasks.length} taskIds:`,
foundAsanaTasks.join(",")
);
return foundAsanaTasks.shift();
};
exports.getActionParameters = function getActionParameters() {
const repository = github.context.payload.repository;
const pullRequest = github.context.payload.pull_request;
const mergeGroup = github.context.payload.merge_group;
const action = core.getInput("action", { required: true });
const triggerPhrase = core.getInput("trigger-phrase") || "";
const amplifyUri = core.getInput("amplify-uri") || "";
const storybookAmplifyUri = core.getInput("storybook-amplify-uri") || "";
return {
repository,
pullRequest,
action,
triggerPhrase,
amplifyUri,
storybookAmplifyUri,
mergeGroup,
};
};
async function getTaskDestination({ taskId, pullRequest }) {
const { merged_at: mergedAt } = pullRequest;
if (mergedAt) {
// do not move pulls in draft or already merged
return;
}
const { requested_reviewers: requestedReviewers, assignees } = pullRequest;
console.log("Requested reviewers:", requestedReviewers);
// if review has been requested from ms-testers, this is probably bogus:
// someone wanted to assign the task to them and failed- update the
// task for them
if (requestedReviewers.some(({ login }) => login === "ms-testers")) {
console.log(
`Found the pull requested to have a review requested from ms-testers, fix it`
);
await octokit.pulls.removeRequestedReviewers({
...github.context.repo,
pull_number: pullRequest.number,
reviewers: ["ms-testers"],
});
await octokit.issues.addAssignees({
...github.context.repo,
issue_number: pullRequest.number,
assignees: ["ms-testers"],
});
assignees.push({ login: "ms-testers" });
}
if (assignees.some(({ login }) => login === "ms-testers")) {
// user ms-testers has been assigned
// just to make sure, what is the current section of this task?
const task = await getTask(taskId, {
opt_fields: ["memberships", "completed"],
});
const { completed, memberships } = task;
if (completed) {
console.log(`Task ${taskId} is completed, not moving task`);
return;
}
const sprintProjectMembership = memberships.find(
({ project: { gid: projectId } }) => projectId === asanaSprintProjectId
);
if (!sprintProjectMembership) {
console.log(
`Task ${taskId} is not included in the current sprint, not moving task`
);
return;
}
const {
section: { gid: sprintSectionId },
} = sprintProjectMembership;
if (sprintSectionId === asanaSprintSectionIds.toTest) {
console.log(
`Task ${taskId} is already in the To Test section of the current sprint, not moving task`
);
return;
}
if (sprintSectionId === asanaSprintSectionIds.ready) {
console.log(
`Task ${taskId} is in the Ready section of the current sprint, not moving task`
);
return;
}
return {
destination: asanaSprintSectionIds.toTest,
shouldRemoveAssignee: true,
shouldAssignToAsanaCreator: true,
};
} else {
// user ms-testers is not currently assigned
// if the PR is still open and the task is in the section “to test”, move it back
// to “in progress”
const task = await getTask(taskId, {
opt_fields: ["memberships", "completed"],
});
const { completed, memberships } = task;
if (completed) {
console.log(`Task ${taskId} is completed, not moving task`);
return;
}
const sprintProjectMembership = memberships.find(
({ project: { gid: projectId } }) => projectId === asanaSprintProjectId
);
if (!sprintProjectMembership) {
console.log(
`Task ${taskId} is not included in the current sprint, not moving task`
);
return;
}
const {
section: { gid: sprintSectionId },
} = sprintProjectMembership;
if (sprintSectionId === asanaSprintSectionIds.toTest) {
console.log(
`Task ${taskId} is still in the “To Test” section of the current sprint, moving task to “In Progress”`
);
return {
destination: asanaSprintSectionIds.inProgress,
shouldRemoveAssignee: true,
};
}
if (
sprintSectionId === asanaSprintSectionIds.design ||
sprintSectionId === asanaSprintSectionIds.readyToDo
) {
console.log(
`Task ${taskId} is still in the ${sprintSectionId} section of the current sprint, moving task to “In Progress”`
);
return {
destination: asanaSprintSectionIds.inProgress,
shouldRemoveAssignee: false,
};
}
}
}
async function moveTaskToSprintAndEpicSection({ taskId, sectionId }) {
console.log(`Moving task ${taskId} to section ${sectionId}`);
const task = await getTask(taskId, {
opt_fields: ["memberships"],
});
const { memberships } = task;
console.log(
`Found the following memberships: ${JSON.stringify(memberships)}`
);
for (const {
project: { gid: projectId },
} of memberships) {
if (projectId === asanaSprintProjectId) {
// move task to section
console.log(
`Found the Current Sprint project, moving to section ${sectionId}`
);
await moveTaskToProjectSection({ taskId, projectId, sectionId });
} else {
console.log(
`Found a project that is not the Current Sprint: ${projectId}`
);
// this project is not the current sprint, see if we have a matching section
const sections = await getProjectSections({ projectId });
console.log(`Sections for this project: ${JSON.stringify(sections)}`);
const matchingSection = sections.find(({ name }) => {
switch (sectionId) {
case asanaSprintSectionIds.design:
return name.match(/design/i);
case asanaSprintSectionIds.readyToDo:
return name.match(/to ?do/i);
case asanaSprintSectionIds.inProgress:
return name.match(/in ?progress/i);
case asanaSprintSectionIds.toTest:
return name.match(/test/i);
case asanaSprintSectionIds.ready:
return name.match(/^ready$/i);
}
});
console.log(`Matching section: ${JSON.stringify(matchingSection)}`);
if (!matchingSection) {
console.log(
`Could not find a matching section, skipping to next project`
);
continue;
}
const { gid: matchingSectionId } = matchingSection;
console.log(`Moving task to section ${matchingSectionId}`);
await moveTaskToProjectSection({
taskId,
projectId,
sectionId: matchingSectionId,
});
}
}
}
async function checkIfCanMergeWithoutAsanaTask({ repository, pullRequest }) {
const { assignees } = pullRequest;
const assigneeLogins = assignees.map(({ login }) => login);
if (!assigneeLogins.some((login) => login === "ms-testers")) {
return false;
}
// if mobsuccess.yml has the `accept_ms_testers_without_closed_task` flag set to true, we can merge
const mobsuccessyml = await getMobsuccessYMLFromRepo({
owner: repository.owner.login,
repo: repository.name,
branch: pullRequest.head ? pullRequest.head.ref : "master",
});
const asanaSettings = mobsuccessyml.asana || {};
if (asanaSettings.accept_ms_testers_without_closed_task) {
console.log(
"accept_ms_testers_without_closed_task is set to true, ok to merge"
);
return true;
}
return false;
}
function getAwsAmplifyLiveUrls({ id, labels, amplifyUri }) {
if (!amplifyUri) {
return [];
}
const result = [];
if (amplifyUri.match(/^{/)) {
const amplifyUris = JSON.parse(amplifyUri);
for (const label of labels) {
if (amplifyUris[label]) {
result.push(amplifyUris[label].replace("%", id));
}
}
} else {
result.push(amplifyUri.replace("%", id));
}
return result;
}
exports.action = async function action() {
try {
return await actionImpl();
} catch (error) {
console.error(
"Caught error while running action, is the Asana ticket in the Current Sprint board?"
);
console.error(error, JSON.stringify(error.value));
core.setFailed(error.message);
throw error;
}
};
async function actionImpl() {
// check if we run on a merge_group
const {
mergeGroup,
repository,
pullRequest,
action,
triggerPhrase,
amplifyUri,
storybookAmplifyUri,
} = exports.getActionParameters();
console.log("GitHub Context", github.context.payload);
if (mergeGroup) {
console.log("Running on a merge group - skipping Asana integration");
return;
}
const taskId = exports.findAsanaTaskId({ triggerPhrase, pullRequest });
const asanaPRStatus = await exports.getAsanaPRStatus({
pullRequest,
});
//console.log("pull", pullRequest);
console.log("asanaPRStatus", asanaPRStatus);
const labels = (pullRequest.labels || []).map(({ name }) => name);
console.info(`Calling action ${action}`);
switch (action) {
case "debug":
console.log(
"payload",
JSON.stringify(github.context.payload, undefined, 4)
);
break;
case "synchronize": {
if (!taskId) {
console.log("Cannot update Asana task: no taskId was found");
} else {
const pullRequestNumber =
pullRequest.number || pullRequest.html_url.split("/").pop();
const amplifyLiveUrls = getAwsAmplifyLiveUrls({
id: pullRequestNumber,
labels,
amplifyUri,
});
const updateOptions = {
custom_fields: {
...(amplifyLiveUrls.length
? {
[customFieldLive.gid]: amplifyLiveUrls.join("\n"),
}
: {}),
...(storybookAmplifyUri
? {
[customFieldStorybook.gid]: storybookAmplifyUri.replace(
"%",
pullRequestNumber
),
}
: {}),
[customFieldPR.gid]: pullRequest.html_url,
[customFieldPRStatus.gid]: asanaPRStatus,
},
};
const {
destination,
shouldRemoveAssignee,
shouldAssignToAsanaCreator = false,
} = (await getTaskDestination({ taskId, pullRequest })) || {};
console.log("Got destination", {
destination,
shouldRemoveAssignee,
shouldAssignToAsanaCreator,
});
if (shouldAssignToAsanaCreator) {
const taskForCreator = await getTask(taskId, {
opt_fields: "created_by",
});
updateOptions["assignee"] = taskForCreator.created_by.gid;
} else if (shouldRemoveAssignee) {
updateOptions["assignee"] = null;
}
if (destination) {
console.log(`Moving Asana task to section ${destination}`);
await moveTaskToSprintAndEpicSection({
taskId,
sectionId: destination,
});
}
console.log(`Updating Asana task: ${taskId}`, updateOptions);
await updateAsanaTask(taskId, updateOptions);
// fail the Action if the task is not draft and the task is not complete
const isDraft = await exports.getPullIsDraft({ pullRequest });
if (isDraft) {
console.log(
"Pull request in draft mode, not checking Asana task for completion"
);
} else {
const { completed } = await getTask(taskId, {
opt_fields: ["completed"],
});
console.log("Task is completed?", completed);
if (!completed) {
// check if can merge without a completed asana task
const canMergeWithoutAsanaTask = await checkIfCanMergeWithoutAsanaTask(
{ repository, pullRequest }
);
if (!canMergeWithoutAsanaTask) {
throw new Error(
"Asana task is not yet completed, blocking merge"
);
}
}
}
}
break;
}
}
}