-
Notifications
You must be signed in to change notification settings - Fork 11
293 lines (251 loc) · 10.3 KB
/
Copy pathstale-assignment.yml
File metadata and controls
293 lines (251 loc) · 10.3 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
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
name: Stale Assignment Cleanup
on:
schedule:
- cron: "0 0 * * *" # every day at 00:00 UTC
workflow_dispatch:
permissions:
issues: write
contents: read
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Auto-handle stale assignments (promote or unassign)
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { github, context } = require("@actions/github");
const owner = context.repo.owner;
const repo = context.repo.repo;
const now = Date.now();
const warningLimitMs = 2 * 24 * 60 * 60 * 1000;
const unassignLimitMs = 3 * 24 * 60 * 60 * 1000;
const issues = await github.paginate(github.rest.issues.listForRepo, {
owner,
repo,
state: "open",
assignee: "*",
per_page: 100
});
const QUEUE_MARKER = "<!-- learnhub-queue-state";
const WARNING_MARKER = "<!-- stale-assignment-warning -->";
function parseQueueState(body) {
const state = {
primary: "",
primary_assigned_at: "",
backup: "",
backup_approach: "",
last_progress_at: ""
};
if (!body) return state;
const lines = String(body).split("\n");
for (const line of lines) {
const m = line.match(/^\s*([a-z_]+):\s*(.*)\s*$/i);
if (m) {
const key = m[1].trim();
const val = m[2].trim();
if (Object.prototype.hasOwnProperty.call(state, key)) {
state[key] = val;
}
}
}
return state;
}
function buildQueueBody(state) {
return (
`${QUEUE_MARKER}\n` +
`primary: ${state.primary || ""}\n` +
`primary_assigned_at: ${state.primary_assigned_at || ""}\n` +
`backup: ${state.backup || ""}\n` +
`backup_approach: ${state.backup_approach || ""}\n` +
`last_progress_at: ${state.last_progress_at || ""}\n` +
`-->`
);
}
for (const issue of issues) {
if (issue.pull_request) continue;
const issueNumber = issue.number;
const assignees = (issue.assignees || []).map(a => a.login);
if (assignees.length === 0) continue;
const labels = (issue.labels || []).map(l => String(l.name || "").toLowerCase());
if (labels.includes("pinned-assignment")) {
console.log(`Skipping #${issueNumber} because it has pinned-assignment.`);
continue;
}
const assignee = assignees[0];
const events = await github.paginate(github.rest.issues.listEvents, {
owner,
repo,
issue_number: issueNumber,
per_page: 100
});
const latestAssignedEvent = events
.filter(
event =>
event.event === "assigned" &&
event.assignee &&
event.assignee.login === assignee
)
.pop();
if (!latestAssignedEvent) {
console.log(
`Skipping #${issueNumber} because no matching assignment event was found.`
);
continue;
}
const assignedAtMs = new Date(latestAssignedEvent.created_at).getTime();
const elapsedMs = now - assignedAtMs;
const hasLinkedPR = events.some(
event =>
event.event === "cross-referenced" &&
event.source &&
event.source.issue &&
event.source.issue.pull_request
);
if (hasLinkedPR) {
console.log(`Skipping #${issueNumber} because it already has a linked PR.`);
continue;
}
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number: issueNumber,
per_page: 100
});
const hasWarning = comments.some(comment =>
String(comment.body || "").includes(WARNING_MARKER)
);
const queueComment = comments.find(c =>
String(c.body || "").includes(QUEUE_MARKER)
);
const queueState = queueComment
? parseQueueState(queueComment.body || "")
: {
primary: "",
primary_assigned_at: "",
backup: "",
backup_approach: "",
last_progress_at: ""
};
// ≥ 3 days: promote backup or unassign
if (elapsedMs >= unassignLimitMs) {
const primary = queueState.primary || assignee;
const backup = queueState.backup;
if (backup) {
const currentAssignees = (issue.assignees || []).map(a => a.login);
if (currentAssignees.length > 0) {
await github.rest.issues.removeAssignees({
owner,
repo,
issue_number: issueNumber,
assignees: currentAssignees
});
}
await github.rest.issues.addAssignees({
owner,
repo,
issue_number: issueNumber,
assignees: [backup]
});
const promotionTimeIso = new Date().toISOString();
queueState.primary = backup;
queueState.primary_assigned_at = promotionTimeIso;
queueState.backup = "";
queueState.backup_approach = "";
queueState.last_progress_at = promotionTimeIso;
const queueBody = buildQueueBody(queueState);
if (queueComment) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: queueComment.id,
body: queueBody
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: queueBody
});
}
try {
await github.rest.issues.addLabels({
owner,
repo,
issue_number: issueNumber,
labels: ["auto-promoted"]
});
} catch (e) {}
const promotionBody =
`⏳ The primary claimant (@${primary}) did not show enough progress in 3 days. ` +
`@${backup} is now the new priority assignee for this issue.\n\n` +
`Please:\n` +
`- Star the repository\n` +
`- Fork the repository\n` +
`- Understand the issue clearly\n` +
`- Share meaningful progress as soon as you can.\n`;
await github.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: promotionBody
});
console.log(
`Promoted @${backup} on issue #${issueNumber} because primary @${primary} was stale.`
);
} else {
await github.rest.issues.removeAssignees({
owner,
repo,
issue_number: issueNumber,
assignees: [assignee]
});
try {
await github.rest.issues.removeLabel({
owner,
repo,
issue_number: issueNumber,
name: "assigned"
});
} catch (e) {}
try {
await github.rest.issues.addLabels({
owner,
repo,
issue_number: issueNumber,
labels: ["available"]
});
} catch (e) {}
const unassignBody =
`@${assignee} this issue has been unassigned automatically after 3 days of inactivity ` +
`with no linked PR and no backup claimant. It is now open for other contributors.\n\n` +
`If you still want to work on it, comment again with \`/assign\` and share your approach.\n`;
await github.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: unassignBody
});
console.log(
`Unassigned @${assignee} from issue #${issueNumber} (no backup claimant).`
);
}
continue;
}
// ≥ 2 days, no warning yet: send reminder
if (elapsedMs >= warningLimitMs && !hasWarning) {
const warningBody =
`${WARNING_MARKER}\n` +
`@${assignee} friendly reminder: this issue has been assigned for 2 days and no linked PR was detected yet. ` +
`If there is no meaningful progress within 3 days, it may be promoted to a backup claimant or unassigned automatically.\n`;
await github.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: warningBody
});
console.log(`Warning sent to @${assignee} on issue #${issueNumber}`);
}
}