-
Notifications
You must be signed in to change notification settings - Fork 4.3k
155 lines (153 loc) · 6.78 KB
/
self-assign.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
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
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Assign or close an issue
on:
issue_comment:
types: [created]
jobs:
assign:
permissions:
issues: write
name: Take or close an issue
if: ${{ !github.event.issue.pull_request }}
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
script: |
const body = context.payload.comment.body.replace( /\r\n/g, " " ).replace( /\n/g, " " ).split(' ');
console.log(body);
for (let i = 0; i < body.length; i++) {
const bodyString = body[i].toLowerCase();
if (bodyString == '.take-issue') {
console.log(`Assigining issue to ${context.payload.comment.user.login}`);
github.rest.issues.addAssignees({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
assignees: [context.payload.comment.user.login]
});
try {
github.rest.issues.removeLabel({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
name: 'awaiting triage'
});
} catch (error) {
console.log(`Failed to remove awaiting triage label. It may not exist on this issue. Error ${error}`);
}
} else if (bodyString == '.close-issue') {
console.log('Closing issue');
if (i + 1 < body.length && body[i+1].toLowerCase() == 'not_planned') {
github.rest.issues.update({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
state: 'closed',
state_reason: 'not_planned'
});
} else {
github.rest.issues.update({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
state: 'closed'
});
}
} else if (bodyString == '.reopen-issue') {
console.log('Reopening issue');
github.rest.issues.update({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open'
});
} else if (bodyString == '.set-labels' || bodyString == '.add-labels' || bodyString == '.remove-labels') {
console.log('Updating labels');
if (i + 1 >= body.length) {
console.log(`Invalid input ${body}`);
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '.<update>-labels command detected, but no labels provided.'
});
return;
} else {
const labelsInRepo = (await github.paginate(github.rest.issues.listLabelsForRepo, {
owner: context.repo.owner,
repo: context.repo.repo,
})).map(l => l.name);
let j = i+1;
let partsToConsider = body[j];
while ((partsToConsider.match(/'/g) || []).length % 2 == 1) {
j++;
if (j >= body.length) {
console.log(`Invalid input ${body}`);
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: '.<update>-labels command detected with mismatched set of \' quotes'
});
return;
}
partsToConsider += ' ' + body[j];
}
const labelsToActionOn = partsToConsider.split(',').map(l => l.replaceAll('\'', ''));
for (label of labelsToActionOn) {
if (labelsInRepo.indexOf(label) < 0) {
console.log(`Invalid label ${body}`);
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Label ${label} cannot be managed because it does not exist in the repo. Please check your spelling.`
});
return;
}
}
if (bodyString == '.set-labels') {
console.log(`Setting labels to ${labelsToActionOn}`);
github.rest.issues.setLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: labelsToActionOn
});
} else if (bodyString == '.add-labels') {
console.log(`Adding labels ${labelsToActionOn}`);
github.rest.issues.addLabels({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
labels: labelsToActionOn
});
} else if (bodyString == '.remove-labels') {
console.log(`Removing labels ${labelsToActionOn}`);
// There's no true removeLabels command, we'll remove them one by one
for (const label of labelsToActionOn) {
github.rest.issues.removeLabel({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
name: label
});
}
}
}
}
}