-
Notifications
You must be signed in to change notification settings - Fork 1
208 lines (187 loc) · 8.19 KB
/
Copy pathissue-triage.yml
File metadata and controls
208 lines (187 loc) · 8.19 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
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
name: Issue Triage
on:
issues:
types: [opened, reopened, labeled]
issue_comment:
types: [created]
permissions:
contents: read
issues: write
concurrency:
group: issue-triage-${{ github.event.issue.number || github.event.comment.id }}
cancel-in-progress: false
jobs:
normalize:
if: github.event.issue.pull_request == null
runs-on: ubuntu-latest
steps:
- name: Normalize issue status labels
uses: actions/github-script@v8
with:
script: |
// Repository-wide status labels controlled by this workflow.
const statusLabels = [
"status: blocked",
"status: declined",
"status: duplicate",
"status: waiting-for-feedback"
];
// Prefix used to detect issue type labels.
const typeLabelPrefix = "type:";
// Fallback status for newly opened or reopened issues.
const defaultStatus = "status: waiting-for-feedback";
// Hidden markers prevent duplicate bot comments.
const typeMissingMarker = "<!-- issue-triage:type-missing -->";
const statusDeniedMarker = "<!-- issue-triage:status-maintainer-only -->";
const issue = context.payload.issue;
const issueNumber = issue.number;
// Read the latest issue labels from GitHub API.
async function listCurrentLabels() {
const response = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber
});
return response.data.map((label) => label.name);
}
// Fetch all issue comments for duplicate-warning suppression.
async function listComments() {
return github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
per_page: 100
});
}
// Create a bot comment once per marker.
async function commentOnce(marker, body) {
const comments = await listComments();
const exists = comments.some((comment) => (comment.body || "").includes(marker));
if (!exists) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
body: `${marker}\n${body}`
});
}
}
// Attach a single label to the issue.
async function addLabel(name) {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
labels: [name]
});
}
// Remove a label if it exists; ignore already-removed labels.
async function removeLabel(name) {
try {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
name
});
} catch (error) {
if (error.status !== 404) {
throw error;
}
}
}
// Enforce exactly one status label while keeping the target status.
async function normalizeToSingleStatus(targetStatus) {
const labels = await listCurrentLabels();
const currentStatuses = labels.filter((name) => statusLabels.includes(name));
const statusesToRemove = currentStatuses.filter((name) => name !== targetStatus);
for (const status of statusesToRemove) {
await removeLabel(status);
}
if (!labels.includes(targetStatus)) {
await addLabel(targetStatus);
}
}
// Ensure exactly one status label exists after unauthorized edits are reverted.
async function ensureAtLeastOneStatusLabel() {
const labels = await listCurrentLabels();
const statusCount = labels.filter((name) => statusLabels.includes(name)).length;
if (statusCount === 0) {
await addLabel(defaultStatus);
}
}
// Check whether the current actor has maintainer-level repository permission.
async function isMaintainerActor() {
try {
const response = await github.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
username: context.actor
});
const permission = response.data.permission;
return permission === "admin" || permission === "maintain" || permission === "write";
} catch (error) {
if (error.status === 404) {
return false;
}
throw error;
}
}
// Warn when no type label is present on an issue.
async function warnIfTypeLabelMissing() {
const labels = await listCurrentLabels();
const hasType = labels.some((name) => name.startsWith(typeLabelPrefix));
if (!hasType) {
await commentOnce(
typeMissingMarker,
"⚠️ This issue has no `type:*` label. Please add one of the repository type labels (for example `type: bug`, `type: feature`, `type: refactor`)."
);
}
}
if (context.eventName === "issues") {
const action = context.payload.action;
if (action === "opened") {
// Ensure newly opened issues always start with a single valid status.
const current = (issue.labels || []).map((label) => typeof label === "string" ? label : label.name);
const statusSet = current.filter((name) => statusLabels.includes(name));
if (statusSet.length === 0) {
await addLabel(defaultStatus);
} else if (statusSet.length > 1) {
const keep = statusLabels.find((name) => statusSet.includes(name)) || statusSet[0];
await normalizeToSingleStatus(keep);
}
await warnIfTypeLabelMissing();
}
if (action === "reopened") {
// Reopened issues go back to waiting-for-feedback by default.
await normalizeToSingleStatus(defaultStatus);
await warnIfTypeLabelMissing();
}
if (action === "labeled") {
// When a status label is added manually, allow only maintainers to change status.
const labeled = context.payload.label && context.payload.label.name;
if (statusLabels.includes(labeled)) {
const maintainer = await isMaintainerActor();
if (!maintainer) {
await removeLabel(labeled);
await ensureAtLeastOneStatusLabel();
await commentOnce(
statusDeniedMarker,
`⚠️ \`status:*\` labels can only be changed by maintainers. The label \`${labeled}\` was removed.`
);
} else {
await normalizeToSingleStatus(labeled);
}
}
await warnIfTypeLabelMissing();
}
}
if (context.eventName === "issue_comment") {
const commenter = context.payload.comment.user && context.payload.comment.user.login;
const author = issue.user && issue.user.login;
if (commenter === author) {
// Reporter feedback clears waiting-for-feedback.
await removeLabel(defaultStatus);
}
await warnIfTypeLabelMissing();
}