From 07cd519c0d9ea2364c4d47a501297072913c1466 Mon Sep 17 00:00:00 2001 From: Ella Date: Fri, 28 Jun 2024 14:21:53 +0300 Subject: [PATCH 1/7] Sync backport changelog to issue --- .../workflows/check-backport-changelog.yml | 68 +++++++++++++++---- lib/init.php | 2 +- 2 files changed, 56 insertions(+), 14 deletions(-) diff --git a/.github/workflows/check-backport-changelog.yml b/.github/workflows/check-backport-changelog.yml index 355acb37bd14d..fd4501378f8be 100644 --- a/.github/workflows/check-backport-changelog.yml +++ b/.github/workflows/check-backport-changelog.yml @@ -1,4 +1,4 @@ -name: Verify Core Backport Changlog +name: Check Core Backport Changlog on: pull_request: @@ -17,23 +17,16 @@ on: - '!packages/block-library/**' - '!packages/e2e-tests/**' jobs: - check: - name: Check CHANGELOG diff + check-backport-changelog: + name: Check for a Core backport changelog entry runs-on: ubuntu-latest + if: ${{ !contains(github.event.pull_request.labels.*.name, 'No Core Sync Required') && !contains(github.event.pull_request.labels.*.name, 'Backport from WordPress Core') }} steps: - - name: 'Get PR commit count' - run: echo "PR_COMMIT_COUNT=$(( ${{ github.event.pull_request.commits }} + 1 ))" >> "${GITHUB_ENV}" - - name: Checkout code - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 with: ref: ${{ github.event.pull_request.head.ref }} repository: ${{ github.event.pull_request.head.repo.full_name }} - fetch-depth: ${{ env.PR_COMMIT_COUNT }} - show-progress: ${{ runner.debug == '1' && 'true' || 'false' }} - - name: 'Fetch relevant history from origin' - run: git fetch origin ${{ github.event.pull_request.base.ref }} - - name: Check CHANGELOG status - if: ${{ !contains(github.event.pull_request.labels.*.name, 'No Core Sync Required') && !contains(github.event.pull_request.labels.*.name, 'Backport from WordPress Core') }} + - name: Check the changelog folder env: PR_NUMBER: ${{ github.event.number }} run: | @@ -59,3 +52,52 @@ jobs: echo "Please update the content of ${changelog_file} to include the core backport PR URL, or update the file name to match the core backport PR number." exit 1 fi + sync-backport-changelog: + name: Sync Core backport changelogs to issues + runs-on: ubuntu-latest + if: ${{ !contains(github.event.pull_request.labels.*.name, 'No Core Sync Required') && !contains(github.event.pull_request.labels.*.name, 'Backport from WordPress Core') }} + steps: + - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + with: + ref: ${{ github.event.pull_request.head.ref }} + repository: ${{ github.event.pull_request.head.repo.full_name }} + - name: Sync Issues + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 + with: + script: | + const labelName = '🤖 Sync Backport Changelog'; + const issues = await github.paginate(github.rest.issues.listForRepo, { + owner: context.repo.owner, + repo: context.repo.repo, + labels: [labelName], + state: 'open', + per_page: 100, + }); + + if (issues.length === 0) { + console.log(`No issues found with the "${labelName}" label.`); + return; + } + + const [latestIssue] = issues; + const versionMatch = latestIssue.title.match(/(\d+\.\d+)/); + if (!versionMatch) { + console.log('Could not find version number in the latest issue title.'); + return; + } + + const version = versionMatch[1]; + console.log(`Latest version: ${version}`); + + const { execSync } = require('child_process'); + const processedChangelog = execSync(`awk '/./ {print ($0 ~ /^[-*]/ ? " " : "- ") $0}' backport-changelog/${version}/*.md`).toString(); + const prependChangelog = `This description is continuously generated by a script. Please do not edit, leave comments instead.\n\n`; + + console.log(processedChangelog); + + await github.rest.issues.update({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: latestIssue.number, + body: prependChangelog + processedChangelog + }); diff --git a/lib/init.php b/lib/init.php index 88dcba4525f6e..7fbf986c36d3e 100644 --- a/lib/init.php +++ b/lib/init.php @@ -6,7 +6,7 @@ */ /** - * Gutenberg's Menu. + * Gutenberg's Menu.. * * Adds a new wp-admin menu page for the Gutenberg editor. * From 1c890153e33ea2b045d72da9b79d456e9aed0721 Mon Sep 17 00:00:00 2001 From: Ella Date: Fri, 28 Jun 2024 14:46:27 +0300 Subject: [PATCH 2/7] Run on trunk merge --- .github/workflows/sync-backport-changelog.yml | 59 +++++++++++++++++++ lib/init.php | 2 +- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/sync-backport-changelog.yml diff --git a/.github/workflows/sync-backport-changelog.yml b/.github/workflows/sync-backport-changelog.yml new file mode 100644 index 0000000000000..544bb61ed8e91 --- /dev/null +++ b/.github/workflows/sync-backport-changelog.yml @@ -0,0 +1,59 @@ +name: Check Core Backport Changlog + +on: + push: + branches: + - try/sync-backport-changelog +jobs: + sync-backport-changelog: + name: Sync Core backport changelogs to issues + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + with: + ref: ${{ github.event.pull_request.head.ref }} + repository: ${{ github.event.pull_request.head.repo.full_name }} + - name: Sync Issues + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 + with: + script: | + const labelName = '🤖 Sync Backport Changelog'; + const issues = await github.paginate(github.rest.issues.listForRepo, { + owner: context.repo.owner, + repo: context.repo.repo, + labels: [labelName], + state: 'open', + per_page: 1, + }); + + if (issues.length === 0) { + console.log(`No issues found with the "${labelName}" label.`); + return; + } + + const [latestIssue] = issues; + const versionMatch = latestIssue.title.match(/(\d+\.\d+)/); + if (!versionMatch) { + console.log('Could not find a version number in the latest issue title.'); + return; + } + + const version = versionMatch[1]; + console.log(`Latest version: ${version}`); + + const { execSync } = require('child_process'); + const processedChangelog = execSync(`awk '/./ {print ($0 ~ /^[-*]/ ? " " : "- ") $0}' backport-changelog/${version}/*.md`).toString(); + const prependChangelog = `This description is continuously generated by a script. Please do not edit, leave comments instead.\n\n`; + const body = prependChangelog + processedChangelog; + + if (latestIssue.body.trim() !== body) { + console.log(processedChangelog); + await github.rest.issues.update({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: latestIssue.number, + body + }); + } else { + console.log('Issue description is already up to date.'); + } diff --git a/lib/init.php b/lib/init.php index 7fbf986c36d3e..88dcba4525f6e 100644 --- a/lib/init.php +++ b/lib/init.php @@ -6,7 +6,7 @@ */ /** - * Gutenberg's Menu.. + * Gutenberg's Menu. * * Adds a new wp-admin menu page for the Gutenberg editor. * From f93b0695b4a6006ba63fad93a58f43b9b0a978cc Mon Sep 17 00:00:00 2001 From: Ella Date: Fri, 28 Jun 2024 14:48:24 +0300 Subject: [PATCH 3/7] Remove previous job --- .../workflows/check-backport-changelog.yml | 49 ------------------- 1 file changed, 49 deletions(-) diff --git a/.github/workflows/check-backport-changelog.yml b/.github/workflows/check-backport-changelog.yml index fd4501378f8be..17699bf2ba87a 100644 --- a/.github/workflows/check-backport-changelog.yml +++ b/.github/workflows/check-backport-changelog.yml @@ -52,52 +52,3 @@ jobs: echo "Please update the content of ${changelog_file} to include the core backport PR URL, or update the file name to match the core backport PR number." exit 1 fi - sync-backport-changelog: - name: Sync Core backport changelogs to issues - runs-on: ubuntu-latest - if: ${{ !contains(github.event.pull_request.labels.*.name, 'No Core Sync Required') && !contains(github.event.pull_request.labels.*.name, 'Backport from WordPress Core') }} - steps: - - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - with: - ref: ${{ github.event.pull_request.head.ref }} - repository: ${{ github.event.pull_request.head.repo.full_name }} - - name: Sync Issues - uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 - with: - script: | - const labelName = '🤖 Sync Backport Changelog'; - const issues = await github.paginate(github.rest.issues.listForRepo, { - owner: context.repo.owner, - repo: context.repo.repo, - labels: [labelName], - state: 'open', - per_page: 100, - }); - - if (issues.length === 0) { - console.log(`No issues found with the "${labelName}" label.`); - return; - } - - const [latestIssue] = issues; - const versionMatch = latestIssue.title.match(/(\d+\.\d+)/); - if (!versionMatch) { - console.log('Could not find version number in the latest issue title.'); - return; - } - - const version = versionMatch[1]; - console.log(`Latest version: ${version}`); - - const { execSync } = require('child_process'); - const processedChangelog = execSync(`awk '/./ {print ($0 ~ /^[-*]/ ? " " : "- ") $0}' backport-changelog/${version}/*.md`).toString(); - const prependChangelog = `This description is continuously generated by a script. Please do not edit, leave comments instead.\n\n`; - - console.log(processedChangelog); - - await github.rest.issues.update({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: latestIssue.number, - body: prependChangelog + processedChangelog - }); From 8593f0492da0f0e7eef6708d6c99592d43344ae5 Mon Sep 17 00:00:00 2001 From: Ella Date: Fri, 28 Jun 2024 14:54:00 +0300 Subject: [PATCH 4/7] Remove PR checkout code --- .github/workflows/sync-backport-changelog.yml | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/.github/workflows/sync-backport-changelog.yml b/.github/workflows/sync-backport-changelog.yml index 544bb61ed8e91..d1fd7aa6cf339 100644 --- a/.github/workflows/sync-backport-changelog.yml +++ b/.github/workflows/sync-backport-changelog.yml @@ -1,19 +1,18 @@ -name: Check Core Backport Changlog +name: Sync Core Backport Changelog Issue on: push: branches: - try/sync-backport-changelog + jobs: sync-backport-changelog: - name: Sync Core backport changelogs to issues + name: Sync Core Backport Changelog Issue runs-on: ubuntu-latest steps: - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - with: - ref: ${{ github.event.pull_request.head.ref }} - repository: ${{ github.event.pull_request.head.repo.full_name }} - - name: Sync Issues + + - name: Sync Issue uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 with: script: | @@ -42,11 +41,12 @@ jobs: console.log(`Latest version: ${version}`); const { execSync } = require('child_process'); - const processedChangelog = execSync(`awk '/./ {print ($0 ~ /^[-*]/ ? " " : "- ") $0}' backport-changelog/${version}/*.md`).toString(); + const processedChangelog = execSync(`awk '/./ {print ($0 ~ /^[-*]/ ? " " : "- ") $0}' backport-changelog/${version}/*.md`).toString().trim(); + const prependChangelog = `This description is continuously generated by a script. Please do not edit, leave comments instead.\n\n`; const body = prependChangelog + processedChangelog; - if (latestIssue.body.trim() !== body) { + if (latestIssue.body.trim() !== body.trim()) { console.log(processedChangelog); await github.rest.issues.update({ owner: context.repo.owner, @@ -54,6 +54,7 @@ jobs: issue_number: latestIssue.number, body }); + console.log('Issue description updated successfully.'); } else { console.log('Issue description is already up to date.'); } From 1224442d9777cffc97a86cb6077de38dcdc8a5be Mon Sep 17 00:00:00 2001 From: Ella Date: Fri, 28 Jun 2024 15:18:35 +0300 Subject: [PATCH 5/7] Leave existing content intact --- .../workflows/check-backport-changelog.yml | 4 +-- .github/workflows/sync-backport-changelog.yml | 26 +++++++++++++------ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/.github/workflows/check-backport-changelog.yml b/.github/workflows/check-backport-changelog.yml index 17699bf2ba87a..606ca4c91683c 100644 --- a/.github/workflows/check-backport-changelog.yml +++ b/.github/workflows/check-backport-changelog.yml @@ -1,4 +1,4 @@ -name: Check Core Backport Changlog +name: Verify Core Backport Changlog on: pull_request: @@ -17,7 +17,7 @@ on: - '!packages/block-library/**' - '!packages/e2e-tests/**' jobs: - check-backport-changelog: + check: name: Check for a Core backport changelog entry runs-on: ubuntu-latest if: ${{ !contains(github.event.pull_request.labels.*.name, 'No Core Sync Required') && !contains(github.event.pull_request.labels.*.name, 'Backport from WordPress Core') }} diff --git a/.github/workflows/sync-backport-changelog.yml b/.github/workflows/sync-backport-changelog.yml index d1fd7aa6cf339..336a8b95b04b5 100644 --- a/.github/workflows/sync-backport-changelog.yml +++ b/.github/workflows/sync-backport-changelog.yml @@ -1,4 +1,4 @@ -name: Sync Core Backport Changelog Issue +name: Sync Core Backport Issue on: push: @@ -7,11 +7,10 @@ on: jobs: sync-backport-changelog: - name: Sync Core Backport Changelog Issue + name: Sync Core Backport Issue runs-on: ubuntu-latest steps: - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 - - name: Sync Issue uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 with: @@ -43,16 +42,27 @@ jobs: const { execSync } = require('child_process'); const processedChangelog = execSync(`awk '/./ {print ($0 ~ /^[-*]/ ? " " : "- ") $0}' backport-changelog/${version}/*.md`).toString().trim(); - const prependChangelog = `This description is continuously generated by a script. Please do not edit, leave comments instead.\n\n`; - const body = prependChangelog + processedChangelog; + const startDelimiter = ''; + const endDelimiter = ''; + const autoGeneratedContent = `${startDelimiter}\n${processedChangelog}\n${endDelimiter}`; + + const regex = new RegExp(`${startDelimiter}[\\s\\S]*${endDelimiter}`); + let newBody; + + if (regex.test(latestIssue.body)) { + // If delimiters exist, replace the content between them + newBody = latestIssue.body.replace(regex, autoGeneratedContent); + } else { + // If delimiters don't exist, append the new content at the end + newBody = `${latestIssue.body}\n\n${autoGeneratedContent}`; + } - if (latestIssue.body.trim() !== body.trim()) { - console.log(processedChangelog); + if (newBody.trim() !== latestIssue.body.trim()) { await github.rest.issues.update({ owner: context.repo.owner, repo: context.repo.repo, issue_number: latestIssue.number, - body + body: newBody }); console.log('Issue description updated successfully.'); } else { From d1070dfcf6736b50928f793fd93c4ee5aae62dbc Mon Sep 17 00:00:00 2001 From: Ella Date: Fri, 28 Jun 2024 15:22:30 +0300 Subject: [PATCH 6/7] Set branch to trunk --- .github/workflows/sync-backport-changelog.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/sync-backport-changelog.yml b/.github/workflows/sync-backport-changelog.yml index 336a8b95b04b5..b889a257bd5bb 100644 --- a/.github/workflows/sync-backport-changelog.yml +++ b/.github/workflows/sync-backport-changelog.yml @@ -3,7 +3,7 @@ name: Sync Core Backport Issue on: push: branches: - - try/sync-backport-changelog + - trunk jobs: sync-backport-changelog: From 8bb22a2b43fc439fcdb67b2151ffb80fb4a3ba02 Mon Sep 17 00:00:00 2001 From: Ella Date: Mon, 8 Jul 2024 15:50:25 +0200 Subject: [PATCH 7/7] Abort if no changes to backport changelog --- .github/workflows/sync-backport-changelog.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/sync-backport-changelog.yml b/.github/workflows/sync-backport-changelog.yml index b889a257bd5bb..46465cdaab58a 100644 --- a/.github/workflows/sync-backport-changelog.yml +++ b/.github/workflows/sync-backport-changelog.yml @@ -11,7 +11,13 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + with: + fetch-depth: 2 # Fetch the last two commits to compare changes + - name: Check for changes in backport-changelog + run: | + git diff --quiet HEAD^ HEAD -- backport-changelog || echo "changes=true" >> $GITHUB_OUTPUT - name: Sync Issue + if: env.changes == 'true' uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 with: script: |