From 3176d5c225edbdc47edb37f48c30f56c9e2ecee2 Mon Sep 17 00:00:00 2001 From: Ella <4710635+ellatrix@users.noreply.github.com> Date: Mon, 8 Jul 2024 20:45:31 +0200 Subject: [PATCH] Automatically sync backport changelog to issue (#62973) --- .../workflows/check-backport-changelog.yml | 15 +--- .github/workflows/sync-backport-changelog.yml | 76 +++++++++++++++++++ 2 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 .github/workflows/sync-backport-changelog.yml diff --git a/.github/workflows/check-backport-changelog.yml b/.github/workflows/check-backport-changelog.yml index 355acb37bd14d4..606ca4c91683ce 100644 --- a/.github/workflows/check-backport-changelog.yml +++ b/.github/workflows/check-backport-changelog.yml @@ -18,22 +18,15 @@ on: - '!packages/e2e-tests/**' jobs: check: - name: Check CHANGELOG diff + 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: | diff --git a/.github/workflows/sync-backport-changelog.yml b/.github/workflows/sync-backport-changelog.yml new file mode 100644 index 00000000000000..46465cdaab58ad --- /dev/null +++ b/.github/workflows/sync-backport-changelog.yml @@ -0,0 +1,76 @@ +name: Sync Core Backport Issue + +on: + push: + branches: + - trunk + +jobs: + sync-backport-changelog: + name: Sync Core Backport Issue + 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: | + 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().trim(); + + 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 (newBody.trim() !== latestIssue.body.trim()) { + await github.rest.issues.update({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: latestIssue.number, + body: newBody + }); + console.log('Issue description updated successfully.'); + } else { + console.log('Issue description is already up to date.'); + }