Repository navigation
316 lines (275 loc) · 10.2 KB
/
Copy pathcleanup.yml
File metadata and controls
316 lines (275 loc) · 10.2 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
name: cleanup
on:
schedule:
# https://crontab.guru/every-night-at-midnight
- cron: '0 0 * * *'
workflow_dispatch:
jobs:
environments:
if: ${{ github.repository == 'primer/react' }}
runs-on: ubuntu-latest
permissions:
deployments: write
steps:
- name: Generate token
id: generate_token
uses: tibdex/github-app-token@3beb63f4bd073e61482598c45c71c1019b59b73a
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.PRIVATE_KEY }}
- uses: actions/github-script@5c56fde4671bc2d3592fb0f2c5b5bab9ddae03b1
with:
github-token: ${{ steps.generate_token.outputs.token }}
script: |
const environments = [];
for await (const response of github.paginate.iterator(
github.rest.repos.getAllEnvironments,
{
owner: 'primer',
repo: 'react',
per_page: 100,
}
)) {
environments.push(...response.data);
}
core.info(`${environments.length} environments found`);
const previews = environments.filter(environment => {
return environment.name.startsWith('storybook-preview');
});
core.info(`${previews.length} preview environments found`);
core.startGroup('Status');
const changes = [];
for (const preview of previews) {
// Assumes that environments are named `storybook-preview-123`
const segments = preview.name.split('-');
const number = segments[2];
const { data: pull } = await github.rest.pulls.get({
owner: 'primer',
repo: 'react',
pull_number: number,
});
if (pull.state === 'open') {
core.info(`${preview.name} is still being used by: ${pull.html_url}`);
changes.push({
preview,
pull,
type: 'none',
});
continue;
}
core.info(`Removing ${preview.name} since ${pull.html_url} is ${pull.state}`);
await github.rest.repos.deleteAnEnvironment({
owner: 'primer',
repo: 'react',
environment_name: preview.name,
});
changes.push({
preview,
pull,
type: 'removed',
});
}
core.endGroup();
await core.summary
.addHeading('Environments')
.addTable([
[
{
data: 'Environment',
header: true,
},
{
data: 'Status',
header: true,
},
{
data: 'Pull Request',
header: true,
},
],
...changes.map((change) => {
return [
change.preview.name,
change.type === 'removed' ? 'Removed' : '',
change.pull.html_url,
];
}),
])
.write();
branches:
if: ${{ github.repository == 'primer/react' }}
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Generate token
id: generate_token
uses: tibdex/github-app-token@3beb63f4bd073e61482598c45c71c1019b59b73a
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.PRIVATE_KEY }}
- uses: actions/github-script@5c56fde4671bc2d3592fb0f2c5b5bab9ddae03b1
with:
github-token: ${{ steps.generate_token.outputs.token }}
script: |
const owner = 'primer';
const repo = 'react';
const branches = [];
// Clamp the day so subtracting calendar months stays within the target month
// (for example, August 31 should become February 28/29 rather than March 2/3).
const subtractMonths = (date, months) => {
const result = new Date(date);
const day = result.getDate();
result.setDate(1);
result.setMonth(result.getMonth() - months);
result.setDate(Math.min(day, new Date(result.getFullYear(), result.getMonth() + 1, 0).getDate()));
return result;
};
const sixMonthsAgo = subtractMonths(new Date(), 6);
const getErrorMessage = (error) => {
return error instanceof Error ? error.message : String(error);
};
let defaultBranch = context.payload.repository?.default_branch;
if (!defaultBranch) {
const { data: repository } = await github.rest.repos.get({
owner,
repo,
});
defaultBranch = repository.default_branch;
}
if (!defaultBranch) {
core.warning('Default branch could not be determined, falling back to main');
}
defaultBranch ||= 'main';
for await (const response of github.paginate.iterator(
github.rest.repos.listBranches,
{
owner,
repo,
per_page: 100,
}
)) {
branches.push(...response.data);
}
core.info(`${branches.length} branches found`);
core.info(`Keeping default branch ${defaultBranch}`);
core.startGroup('Status');
const changes = [];
for (const branch of branches) {
if (branch.name === defaultBranch) {
core.info(`Keeping ${branch.name} because it is the default branch`);
continue;
}
if (branch.protected) {
core.info(`Keeping ${branch.name} because it is protected`);
continue;
}
let pull;
let reason = 'recent-activity';
let type = 'none';
const { data: pulls } = await github.rest.pulls.list({
owner,
repo,
head: `${owner}:${branch.name}`,
state: 'all',
per_page: 1,
});
pull = pulls[0];
if (pull && pull.state !== 'open') {
reason = `pr-${pull.state}`;
core.info(`Removing ${branch.name} since ${pull.html_url} is ${pull.state}`);
try {
await github.rest.git.deleteRef({
owner,
repo,
ref: `heads/${branch.name}`,
});
type = 'removed';
} catch (error) {
reason = `${reason}-delete-failed`;
core.warning(`Failed to remove ${branch.name}: ${getErrorMessage(error)}`);
}
changes.push({
branch,
pull,
reason,
type,
});
continue;
}
const { data: commit } = await github.rest.repos.getCommit({
owner,
repo,
ref: branch.commit.sha,
});
// Prefer the committer date because it best reflects the latest activity on the branch,
// and fall back to the author date when a committer date is unavailable.
const commitDate = commit.commit.committer?.date ?? commit.commit.author?.date;
if (!commitDate) {
reason = 'unknown-commit-date';
core.warning(`Keeping ${branch.name} because its latest commit date could not be determined`);
} else {
const parsedCommitDate = new Date(commitDate);
if (Number.isNaN(parsedCommitDate.getTime())) {
reason = 'unknown-commit-date';
core.warning(`Keeping ${branch.name} because its latest commit date could not be parsed`);
} else if (parsedCommitDate < sixMonthsAgo) {
reason = 'stale-6-months';
core.info(`Removing ${branch.name} because its latest commit is older than 6 months`);
try {
await github.rest.git.deleteRef({
owner,
repo,
ref: `heads/${branch.name}`,
});
type = 'removed';
} catch (error) {
reason = `${reason}-delete-failed`;
core.warning(`Failed to remove ${branch.name}: ${getErrorMessage(error)}`);
}
} else if (pull) {
reason = 'pr-open';
core.info(`${branch.name} is still being used by: ${pull.html_url}`);
} else {
core.info(`Keeping ${branch.name} because it has recent activity`);
}
}
changes.push({
branch,
pull,
reason,
type,
});
}
core.endGroup();
await core.summary
.addHeading('Branches')
.addTable([
[
{
data: 'Branch',
header: true,
},
{
data: 'Status',
header: true,
},
{
data: 'Reason',
header: true,
},
{
data: 'Pull Request',
header: true,
},
],
...changes.map((change) => {
return [
change.branch.name,
change.type === 'removed' ? 'Removed' : 'Kept',
change.reason,
change.pull?.html_url ?? '',
];
}),
])
.write();