Skip to content

Commit bd1580b

Browse files
ellatrixcarstingaxion
authored andcommitted
Automatically sync backport changelog to issue (WordPress#62973)
1 parent dbf7e0a commit bd1580b

File tree

2 files changed

+80
-11
lines changed

2 files changed

+80
-11
lines changed

.github/workflows/check-backport-changelog.yml

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,15 @@ on:
1818
- '!packages/e2e-tests/**'
1919
jobs:
2020
check:
21-
name: Check CHANGELOG diff
21+
name: Check for a Core backport changelog entry
2222
runs-on: ubuntu-latest
23+
if: ${{ !contains(github.event.pull_request.labels.*.name, 'No Core Sync Required') && !contains(github.event.pull_request.labels.*.name, 'Backport from WordPress Core') }}
2324
steps:
24-
- name: 'Get PR commit count'
25-
run: echo "PR_COMMIT_COUNT=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> "${GITHUB_ENV}"
26-
- name: Checkout code
27-
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
25+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
2826
with:
2927
ref: ${{ github.event.pull_request.head.ref }}
3028
repository: ${{ github.event.pull_request.head.repo.full_name }}
31-
fetch-depth: ${{ env.PR_COMMIT_COUNT }}
32-
show-progress: ${{ runner.debug == '1' && 'true' || 'false' }}
33-
- name: 'Fetch relevant history from origin'
34-
run: git fetch origin ${{ github.event.pull_request.base.ref }}
35-
- name: Check CHANGELOG status
36-
if: ${{ !contains(github.event.pull_request.labels.*.name, 'No Core Sync Required') && !contains(github.event.pull_request.labels.*.name, 'Backport from WordPress Core') }}
29+
- name: Check the changelog folder
3730
env:
3831
PR_NUMBER: ${{ github.event.number }}
3932
run: |
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
name: Sync Core Backport Issue
2+
3+
on:
4+
push:
5+
branches:
6+
- trunk
7+
8+
jobs:
9+
sync-backport-changelog:
10+
name: Sync Core Backport Issue
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
14+
with:
15+
fetch-depth: 2 # Fetch the last two commits to compare changes
16+
- name: Check for changes in backport-changelog
17+
run: |
18+
git diff --quiet HEAD^ HEAD -- backport-changelog || echo "changes=true" >> $GITHUB_OUTPUT
19+
- name: Sync Issue
20+
if: env.changes == 'true'
21+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
22+
with:
23+
script: |
24+
const labelName = '🤖 Sync Backport Changelog';
25+
const issues = await github.paginate(github.rest.issues.listForRepo, {
26+
owner: context.repo.owner,
27+
repo: context.repo.repo,
28+
labels: [labelName],
29+
state: 'open',
30+
per_page: 1,
31+
});
32+
33+
if (issues.length === 0) {
34+
console.log(`No issues found with the "${labelName}" label.`);
35+
return;
36+
}
37+
38+
const [latestIssue] = issues;
39+
const versionMatch = latestIssue.title.match(/(\d+\.\d+)/);
40+
if (!versionMatch) {
41+
console.log('Could not find a version number in the latest issue title.');
42+
return;
43+
}
44+
45+
const version = versionMatch[1];
46+
console.log(`Latest version: ${version}`);
47+
48+
const { execSync } = require('child_process');
49+
const processedChangelog = execSync(`awk '/./ {print ($0 ~ /^[-*]/ ? " " : "- ") $0}' backport-changelog/${version}/*.md`).toString().trim();
50+
51+
const startDelimiter = '<!-- START TRUNK BACKPORT CHANGELOG -->';
52+
const endDelimiter = '<!-- END TRUNK BACKPORT CHANGELOG -->';
53+
const autoGeneratedContent = `${startDelimiter}\n${processedChangelog}\n${endDelimiter}`;
54+
55+
const regex = new RegExp(`${startDelimiter}[\\s\\S]*${endDelimiter}`);
56+
let newBody;
57+
58+
if (regex.test(latestIssue.body)) {
59+
// If delimiters exist, replace the content between them
60+
newBody = latestIssue.body.replace(regex, autoGeneratedContent);
61+
} else {
62+
// If delimiters don't exist, append the new content at the end
63+
newBody = `${latestIssue.body}\n\n${autoGeneratedContent}`;
64+
}
65+
66+
if (newBody.trim() !== latestIssue.body.trim()) {
67+
await github.rest.issues.update({
68+
owner: context.repo.owner,
69+
repo: context.repo.repo,
70+
issue_number: latestIssue.number,
71+
body: newBody
72+
});
73+
console.log('Issue description updated successfully.');
74+
} else {
75+
console.log('Issue description is already up to date.');
76+
}

0 commit comments

Comments
 (0)