-
-
Notifications
You must be signed in to change notification settings - Fork 38
202 lines (184 loc) 路 7.99 KB
/
Copy pathpr-stale-check.yml
File metadata and controls
202 lines (184 loc) 路 7.99 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
name: PR Stale Check
on:
schedule:
- cron: "0 7 * * *"
workflow_dispatch:
permissions:
pull-requests: write
issues: write
checks: read
statuses: read
jobs:
stale-check:
runs-on: ubuntu-latest
steps:
- name: Label stale PRs
uses: actions/github-script@v7
with:
script: |
const STALE_LABEL = 'stale';
const HIGH_LABEL = 'priority: high';
const HIGH_DAYS = 3;
const DEFAULT_DAYS = 7;
const CLOSE_AFTER_STALE_DAYS = 14;
const now = Date.now();
const msPerDay = 86400000;
const prs = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
});
for (const pr of prs.data) {
if (pr.draft) continue;
const labels = pr.labels.map(l => l.name);
const isHigh = labels.includes(HIGH_LABEL);
// Also check linked issue labels
if (!isHigh) {
const { data: events } = await github.rest.issues.listEventsForTimeline({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
per_page: 100,
});
for (const evt of events) {
if (evt.event === 'cross-referenced' && evt.source?.issue) {
const issue = evt.source.issue;
if (issue.labels?.some(l => l.name === HIGH_LABEL)) {
labels.push(HIGH_LABEL);
break;
}
}
}
}
const staleDays = labels.includes(HIGH_LABEL) ? HIGH_DAYS : DEFAULT_DAYS;
// Find the last event requiring author action:
// either a "changes requested" review or a failing CI check
const reviews = await github.rest.pulls.listReviews({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
});
let actionNeededSince = null;
for (const review of reviews.data) {
if (review.state === 'CHANGES_REQUESTED') {
const t = new Date(review.submitted_at).getTime();
if (!actionNeededSince || t > actionNeededSince) {
actionNeededSince = t;
}
}
}
// Check for failing CI on the head commit
const { data: checkRuns } = await github.rest.checks.listForRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: pr.head.sha,
});
for (const run of checkRuns.check_runs) {
if (run.conclusion === 'failure' && run.completed_at) {
const t = new Date(run.completed_at).getTime();
if (!actionNeededSince || t > actionNeededSince) {
actionNeededSince = t;
}
}
}
if (!actionNeededSince) {
if (labels.includes(STALE_LABEL)) {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
name: STALE_LABEL,
}).catch(() => {});
}
continue;
}
// Check if the author responded after the last review
const authorLogin = pr.user.login;
const comments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
});
let authorRespondedAfter = false;
for (const c of comments.data) {
if (c.user.login === authorLogin && new Date(c.created_at).getTime() > actionNeededSince) {
authorRespondedAfter = true;
break;
}
}
// Also check if author pushed after the review
if (!authorRespondedAfter) {
const commits = await github.rest.pulls.listCommits({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
});
for (const c of commits.data) {
const commitTime = new Date(c.commit.committer.date).getTime();
if (c.author?.login === authorLogin && commitTime > actionNeededSince) {
authorRespondedAfter = true;
break;
}
}
}
if (authorRespondedAfter) {
if (labels.includes(STALE_LABEL)) {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
name: STALE_LABEL,
}).catch(() => {});
core.info(`PR #${pr.number}: author responded, removed stale label`);
}
continue;
}
const daysSince = Math.floor((now - actionNeededSince) / msPerDay);
core.info(`PR #${pr.number}: ${daysSince}d since changes requested (SLA: ${staleDays}d)`);
if (daysSince >= staleDays && !labels.includes(STALE_LABEL)) {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
labels: [STALE_LABEL],
});
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body: `This PR has had no author follow-up for ${daysSince} days since changes were requested. Marking as **stale**. Please push updates or comment if you're still working on this.\n\nThis PR will be closed automatically if there is no activity within ${CLOSE_AFTER_STALE_DAYS} days.`,
});
core.info(`PR #${pr.number}: marked stale (${daysSince}d)`);
} else if (labels.includes(STALE_LABEL)) {
// Find when the stale label was added
const events = await github.rest.issues.listEvents({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
});
let staledAt = null;
for (const evt of events.data) {
if (evt.event === 'labeled' && evt.label?.name === STALE_LABEL) {
staledAt = new Date(evt.created_at).getTime();
}
}
if (staledAt) {
const staleDaysSince = Math.floor((now - staledAt) / msPerDay);
if (staleDaysSince >= CLOSE_AFTER_STALE_DAYS) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body: `Closing this PR after ${CLOSE_AFTER_STALE_DAYS} days with no activity since being marked stale. Feel free to reopen if you'd like to continue working on this.`,
});
await github.rest.pulls.update({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
state: 'closed',
});
core.info(`PR #${pr.number}: auto-closed (${staleDaysSince}d stale)`);
}
}
}
}