-
Notifications
You must be signed in to change notification settings - Fork 133
83 lines (79 loc) · 4.16 KB
/
pr-comment-trigger.yml
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
name: PR Comment Trigger
on:
issue_comment:
types:
- created
permissions:
issues: write
jobs:
pr-comment:
runs-on: ubuntu-latest
if: ${{ github.event.issue.pull_request && github.event.comment.user.login == github.event.issue.user.login }}
steps:
- name: Execute script if PR author commented
uses: actions/github-script@v6
with:
github-token: ${{ secrets.ZOWE_ROBOT_TOKEN }}
script: |
console.log("PR URL:", context.payload.issue.pull_request.url);
console.log("PR author:", context.payload.issue.user.login);
console.log("Comment author:", context.payload.comment.user.login);
const body = context.payload.comment.body;
if (body.startsWith("/labels ")) {
console.log("Comment body:", body);
const repoLabels = (await github.rest.issues.listLabelsForRepo(context.repo)).data.map((label) => label.name);
const labelMapping = {};
for (const label of repoLabels) {
if (label.includes(": ")) {
console.log("Found repo label:", label);
labelMapping[label.replace(" ", "")] = label;
labelMapping[label.slice(label.indexOf(":") + 2)] = label;
}
}
const labelsCurrent = (await github.rest.issues.listLabelsOnIssue({ ...context.repo, issue_number: context.payload.issue.number })).data.map((label) => label.name);
console.log("PR labels:", labelsCurrent.join(", "));
const cmdArgs = body.split(" ").slice(1);
const labelsToAdd = cmdArgs.filter((arg) => !arg.startsWith("-")).map((arg) => arg.startsWith("+") ? arg.slice(1) : arg);
console.log("Labels to add:", labelsToAdd.join(", "));
const labelsToRemove = cmdArgs.filter((arg) => arg.startsWith("-")).map((arg) => arg.slice(1));
console.log("Labels to remove:", labelsToRemove.join(", "));
const labelsInvalid = [];
for (const label of labelsToAdd) {
if (labelMapping[label] == null) {
console.log("Invalid label:", label);
labelsInvalid.push(label);
} else if (labelsCurrent.includes(labelMapping[label])) {
console.log("Skipping label:", labelMapping[label]);
} else {
console.log("Adding label:", labelMapping[label]);
try {
await github.rest.issues.addLabels({ ...context.repo, issue_number: context.payload.issue.number, labels: [labelMapping[label]] });
} catch (err) {
console.log("Failed to add label:", err);
labelsInvalid.push(label);
}
}
}
for (const label of labelsToRemove) {
if (labelMapping[label] == null) {
console.log("Invalid label:", label);
labelsInvalid.push(label);
} else if (!labelsCurrent.includes(labelMapping[label])) {
console.log("Skipping label:", labelMapping[label]);
} else {
console.log("Removing label:", labelMapping[label]);
try {
await github.rest.issues.removeLabel({ ...context.repo, issue_number: context.payload.issue.number, name: labelMapping[label] });
} catch (err) {
console.log("Failed to remove label:", err);
labelsInvalid.push(label);
}
}
}
await github.rest.issues.deleteComment({ ...context.repo, comment_id: context.payload.comment.id });
if (labelsInvalid.length > 0) {
console.log("Invalid labels:", labelsInvalid.join(", "));
const bodyQuoted = body.split("\n").map((line) => "> " + line).join("\n");
await github.rest.issues.createComment({ ...context.repo, issue_number: context.payload.issue.number, body: bodyQuoted + "\n\nThe following labels are invalid: " + labelsInvalid.join(", ") });
}
}