-
Notifications
You must be signed in to change notification settings - Fork 17
133 lines (114 loc) · 4.29 KB
/
prepare-release.yml
File metadata and controls
133 lines (114 loc) · 4.29 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
name: Create Release PR
on:
push:
branches: [dev]
permissions: {}
jobs:
prepare-release:
if: "!contains(github.event.head_commit.message, 'chore: prepare release')"
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- name: Generate bot token
id: generate-token
uses: actions/create-github-app-token@f8d387b68d61c58ab83c6c016672934102569859
with:
app-id: ${{ secrets.APP_ID }}
private-key: ${{ secrets.APP_PRIVATE_KEY }}
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
fetch-depth: 0
persist-credentials: true
token: ${{ steps.generate-token.outputs.token }}
- uses: fregante/setup-git-user@024bc0b8e177d7e77203b48dab6fb45666854b35 # v2.0.2
- uses: knope-dev/action@19617851f9f13ab2f27a05989c55efb18aca3675 # v2.1.2
with:
version: 0.22.3
- name: Prepare Release
run: knope prepare-release --verbose
env:
GITHUB_TOKEN: ${{ steps.generate-token.outputs.token }}
- name: Enrich changelog and update release PR
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
with:
github-token: ${{ steps.generate-token.outputs.token }}
script: |
const fs = require("fs");
const { owner, repo } = context.repo;
async function resolveHash(hash) {
try {
const prs = await github.rest.repos.listPullRequestsAssociatedWithCommit({
owner, repo, commit_sha: hash,
});
const pr = prs.data[0];
return pr
? `([#${pr.number}](${pr.html_url}) by @${pr.user.login})`
: `(\`${hash}\`)`;
} catch {
return `(\`${hash}\`)`;
}
}
async function enrichText(text) {
const marker = /<!-- commit:([0-9a-f]+) -->/g;
const hashes = [...new Set([...text.matchAll(marker)].map(m => m[1]))];
for (const hash of hashes) {
text = text.replaceAll(`<!-- commit:${hash} -->`, await resolveHash(hash));
}
return text;
}
const changelog = "CHANGELOG.md";
if (fs.existsSync(changelog)) {
const enriched = await enrichText(fs.readFileSync(changelog, "utf8"));
fs.writeFileSync(changelog, enriched);
}
const prs = await github.rest.pulls.list({
owner, repo,
head: `${owner}:release`,
state: "open",
});
if (prs.data.length === 0) {
core.warning("No open PR found for the release branch.");
return;
}
const pr = prs.data[0];
const label = "internal";
try {
await github.rest.issues.getLabel({ owner, repo, name: label });
} catch (e) {
if (e.status === 404) {
await github.rest.issues.createLabel({ owner, repo, name: label, color: "e4e669" });
} else {
throw e;
}
}
await Promise.all([
github.rest.pulls.update({
owner, repo,
pull_number: pr.number,
body: await enrichText(pr.body ?? ""),
}),
github.rest.issues.addLabels({
owner, repo,
issue_number: pr.number,
labels: [label],
}),
]);
- name: Amend release commit with enriched changelog
shell: bash
run: |
if [[ "$(git rev-parse --abbrev-ref HEAD)" != "release" ]]; then
echo "Not on release branch — skipping amend."
exit 1
fi
if git diff --cached --name-only | grep -q "CHANGELOG.md"; then
git add CHANGELOG.md
git commit --amend --no-edit
elif git diff --name-only HEAD | grep -q "CHANGELOG.md"; then
git add CHANGELOG.md
git commit --amend --no-edit
fi
- name: Push release branch
shell: bash
run: git push --force --set-upstream origin release