-
Notifications
You must be signed in to change notification settings - Fork 5
125 lines (113 loc) · 4.37 KB
/
reconcile-issue-comment.yaml
File metadata and controls
125 lines (113 loc) · 4.37 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
name: Reusable Reconcile Issue Comment
on:
workflow_call:
jobs:
event_type:
name: Check event type
runs-on: ubuntu-latest
if: >
github.event_name == 'issue_comment' &&
(
github.event.action == 'created' ||
github.event.action == 'edited'
)
steps:
- name: ok
run: /bin/true
addLabels:
needs: event_type
name: Add Labels
runs-on: ubuntu-latest
steps:
- name: Get Token
id: get_workflow_token
uses: peter-murray/workflow-application-token-action@v4
with:
application_id: ${{ vars.TRUSTIFICATION_BOT_ID }}
application_private_key: ${{ secrets.TRUSTIFICATION_BOT_KEY }}
- name: Handle commands
uses: actions/github-script@v8
with:
github-token: ${{ steps.get_workflow_token.outputs.token }}
script: |
const commentRegex = /(<!--.*?-->)|(<!--[\S\s]+?-->)|(<!--[\S\s]*?$)/g;
const labelRegex = /^(\/(area|kind|priority|sig|triage|wg))\s*(.*?)\s*$/gm;
const removeLabelRegex = /^\/remove-(area|committee|kind|language|priority|sig|triage|wg)\s*(.*?)\s*$/gm;
const labelsToAdd = [];
const labelsToRemove = [];
let match;
const { data: comment } = await github.rest.issues.getComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
});
const bodyWithoutComments = comment.body.replaceAll(commentRegex, '');
while ((match = labelRegex.exec(bodyWithoutComments)) !== null) {
const keyword = match[2];
const text = match[3];
const labelToAdd = `${keyword}/${text}`;
const labelExists = await github.rest.issues.getLabel({
owner: context.repo.owner,
repo: context.repo.repo,
name: labelToAdd,
}).catch(() => false);
if (labelExists) {
labelsToAdd.push(labelToAdd);
} else {
console.log(`label ${labelToAdd} does not exist on this repo`);
}
}
console.log(labelsToAdd);
while ((match = removeLabelRegex.exec(bodyWithoutComments)) !== null) {
const keyword = match[1];
const text = match[2];
const labelToRemove = `${keyword}/${text}`;
labelsToRemove.push(labelToRemove);
}
console.log(labelsToRemove);
if (labelsToAdd.length == 0 && labelsToRemove.length == 0) {
console.log("Nothing to do!");
return;
}
// If we make it this far, we should verify the commenter is at
// least a collaborator.
// TODO(djzager): Is this enough?
const commenterLogin = comment.user.login;
try {
await github.rest.repos.checkCollaborator({
owner: context.repo.owner,
repo: context.repo.repo,
username: commenterLogin,
});
console.log(`Commenter ${commenterLogin} is a collaborator.`);
} catch (error) {
console.log(error);
console.log(`Commenter ${commenterLogin} is not a collaborator.`);
await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: "Only collaborators can add/remove labels",
});
return;
}
// Add the labels
if (labelsToAdd.length > 0) {
await github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: labelsToAdd
});
}
// Remove the labels
if (labelsToRemove.length > 0) {
for (const labelToRemove of labelsToRemove) {
await github.rest.issues.removeLabel({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
name: labelToRemove
});
}
}