1818 with :
1919 fetch-depth : 0
2020 persist-credentials : true
21- token : ${{ secrets.RELEASE_PAT }}
2221
2322 - uses : fregante/setup-git-user@024bc0b8e177d7e77203b48dab6fb45666854b35 # v2.0.2
2423
@@ -28,62 +27,91 @@ jobs:
2827
2928 - name : Prepare Release
3029 run : knope prepare-release --verbose
31- continue-on-error : true
3230 env :
33- GITHUB_TOKEN : ${{ secrets.RELEASE_PAT }}
31+ GITHUB_TOKEN : ${{ secrets.GITHUB_TOKEN }}
3432
35- - name : Enrich changelog with PR attribution
33+ - name : Enrich changelog and update release PR
3634 uses : actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
3735 with :
3836 github-token : ${{ secrets.GITHUB_TOKEN }}
3937 script : |
4038 const fs = require("fs");
41- const changelog = "CHANGELOG.md";
42-
43- if (!fs.existsSync(changelog)) return;
44-
45- let content = fs.readFileSync(changelog, "utf8");
46- const marker = /<!-- commit:([0-9a-f]+) -->/g;
47- const hashes = [...new Set([...content.matchAll(marker)].map(m => m[1]))];
48-
49- if (hashes.length === 0) return;
50-
5139 const { owner, repo } = context.repo;
5240
53- for (const hash of hashes) {
54- let replacement = "";
41+ async function resolveHash(hash) {
5542 try {
56- // Find PRs associated with this commit. Knope uses the commit that
57- // first introduced the changeset file, so this reliably points to
58- // the original PR regardless of later edits on the branch.
5943 const prs = await github.rest.repos.listPullRequestsAssociatedWithCommit({
6044 owner, repo, commit_sha: hash,
6145 });
6246 const pr = prs.data[0];
63- if (pr) {
64- const isInternal = pr.labels.some(label => label.name.toLowerCase() === 'internal');
65- if (isInternal) {
66- const lineRegex = new RegExp(`^.*.*\\r?\\n?`, 'gm');
67- content = content.replace(lineRegex, '');
68- continue;
69- }
70- replacement = `([#${pr.number}](${pr.html_url}) by @${pr.user.login})`;
71- } else {
72- // Commit exists but has no associated PR (e.g. direct push to dev)
73- replacement = `(\`${hash}\`)`;
74- }
47+ return pr
48+ ? `([#${pr.number}](${pr.html_url}) by @${pr.user.login})`
49+ : `(\`${hash}\`)`;
7550 } catch {
76- replacement = `(\`${hash}\`)`;
51+ return `(\`${hash}\`)`;
52+ }
53+ }
54+
55+ async function enrichText(text) {
56+ const marker = /<!-- commit:([0-9a-f]+) -->/g;
57+ const hashes = [...new Set([...text.matchAll(marker)].map(m => m[1]))];
58+ for (const hash of hashes) {
59+ text = text.replaceAll(`<!-- commit:${hash} -->`, await resolveHash(hash));
60+ }
61+ return text;
62+ }
63+
64+ const changelog = "CHANGELOG.md";
65+ if (fs.existsSync(changelog)) {
66+ const enriched = await enrichText(fs.readFileSync(changelog, "utf8"));
67+ fs.writeFileSync(changelog, enriched);
68+ }
69+
70+ const prs = await github.rest.pulls.list({
71+ owner, repo,
72+ head: `${owner}:release`,
73+ state: "open",
74+ });
75+
76+ if (prs.data.length === 0) {
77+ core.warning("No open PR found for the release branch.");
78+ return;
79+ }
80+
81+ const pr = prs.data[0];
82+
83+ const label = "internal";
84+ try {
85+ await github.rest.issues.getLabel({ owner, repo, name: label });
86+ } catch (e) {
87+ if (e.status === 404) {
88+ await github.rest.issues.createLabel({ owner, repo, name: label, color: "e4e669" });
89+ } else {
90+ throw e;
7791 }
78- content = content.replaceAll(`<!-- commit:${hash} -->`, replacement);
7992 }
8093
81- fs.writeFileSync(changelog, content);
94+ await Promise.all([
95+ github.rest.pulls.update({
96+ owner, repo,
97+ pull_number: pr.number,
98+ body: await enrichText(pr.body ?? ""),
99+ }),
100+ github.rest.issues.addLabels({
101+ owner, repo,
102+ issue_number: pr.number,
103+ labels: [label],
104+ }),
105+ ]);
82106
83107 - name : Amend release commit with enriched changelog
84108 shell : bash
85109 run : |
86- # Only amend if the changelog was actually staged by knope
110+ if [[ "$(git rev-parse --abbrev-ref HEAD)" != "release" ]]; then
111+ echo "Not on release branch — skipping amend."
112+ exit 1
113+ fi
114+
87115 if git diff --cached --name-only | grep -q "CHANGELOG.md"; then
88116 git add CHANGELOG.md
89117 git commit --amend --no-edit
0 commit comments