-
Notifications
You must be signed in to change notification settings - Fork 11
189 lines (155 loc) · 5.55 KB
/
maintain-prod-release-pr.yaml
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
on:
# run manually from the actions panels
workflow_dispatch:
# since we can't currently create a PR and have the jobs run
# successfully, run when one is manually created.
pull_request:
types: [opened]
branches: [prod]
# update the PR text when new commits are merged to the master branch
push:
branches:
- master
# and re-run early in the morning, mainly so the title gets updated with today's date
schedule:
- cron: "42 1 * * *"
permissions:
contents: read
pull-requests: write
jobs:
main:
name: Maintain Prod PR
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
function renderCommit(commitMsg) {
const prRegex = /\(#(\d+)\)$/m;
const allRefsRegex = /#(\d+)\s/g;
const prRef = commitMsg.match(prRegex);
if (!prRef) {
return `- ${"???" + commitMsg}`;
}
const prNumber = prRef[1];
const otherReferences = [...commitMsg.matchAll(allRefsRegex)]
.map((m) => m[1])
.filter((r) => r !== prNumber);
const messages = [
`- #${prNumber}`,
...otherReferences.map((r) => ` - re #${r}`),
];
return messages.join("\n");
}
function buildSection(title, commitMsgs) {
if (commitMsgs.length === 0) return "";
return `\n## ${title}\n\n${commitMsgs.map(renderCommit).join("\n")}`;
}
function buildBody(messages) {
const groups = [
{
type: "feat",
title: "Features",
},
{
type: "fix",
title: "Fixes",
},
{
type: "chore",
title: "Chores",
},
{
type: "refactor",
title: "Refactors",
},
];
const knownSections = groups.map((group) =>
buildSection(
group.title,
messages.filter((m) => m.startsWith(group.type))
)
);
const allKnownSections = groups.map((g) => g.type);
const unknownSections = buildSection(
"Other",
messages.filter((m) => !allKnownSections.some((s) => m.startsWith(s)))
);
return [...knownSections, unknownSections].join("\n");
}
async function findReleasePR() {
const { data: prs } = await github.rest.pulls.list({
owner,
repo,
base: "prod",
state: "open",
});
if (prs.length === 0) {
console.log("no release PR found");
return undefined;
}
if (prs.length !== 1) {
throw new Error(`${prs.length} production PRs found...`);
}
const pr = prs[0];
console.log(
`Found PR#${pr.number} ${pr.title} ${
pr.draft ? "(draft)" : "(not draft)"
} - ${pr.html_url}`
);
return pr;
}
const pr = await findReleasePR();
if (!pr) {
console.log("checking commits...");
const commits = await github.rest.repos.compareCommitsWithBasehead({
owner,
repo,
basehead: "prod...master",
});
if (commits.data.commits.length === 0) {
console.log("no changes to release!");
return;
}
const commitMessages = commits.data.commits.map((c) => c.commit.message);
const result = buildBody(commitMessages);
console.log("need to create a draft release PR!", result);
// but don't actually do it yet: if you create one using the github
// token, it doesn't run the security jobs. Create an empty one by hand,
// for now.
/*
const newPr = await github.rest.pulls.create({
owner,
repo,
title: "Production Release (next)",
head: "master",
base: "prod",
draft: true,
body: result,
});
console.log("created: " + newPr.data.html_url);
*/
} else {
const { data: commits } = await github.rest.pulls.listCommits({
owner,
repo,
pull_number: pr.number,
});
const commitMessages = commits.map((c) => c.commit.message);
const body = buildBody(commitMessages);
const title = `Production Release ${new Date().toISOString().slice(0, 10)}`;
if (body === pr.body && title === pr.title) {
console.log("up to date!");
} else {
console.log(`Updating PR!`, { title, body });
await github.rest.pulls.update({
owner,
repo,
pull_number: pr.number,
body,
title,
});
}
}