From b4a112b599fea85a0aa9ac242d14e98a8a5537d5 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Mon, 22 Jul 2024 14:25:54 +1000 Subject: [PATCH 1/5] Sync backport changelog action: Use outputs instead of env --- .github/workflows/sync-backport-changelog.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/sync-backport-changelog.yml b/.github/workflows/sync-backport-changelog.yml index 46465cdaab58ad..558ed796053511 100644 --- a/.github/workflows/sync-backport-changelog.yml +++ b/.github/workflows/sync-backport-changelog.yml @@ -14,10 +14,11 @@ jobs: with: fetch-depth: 2 # Fetch the last two commits to compare changes - name: Check for changes in backport-changelog + id: check-for-changes run: | - git diff --quiet HEAD^ HEAD -- backport-changelog || echo "changes=true" >> $GITHUB_OUTPUT + git diff --quiet HEAD^ HEAD -- backport-changelog || echo "HAS_CHANGES=1" >> "$GITHUB_OUTPUT" - name: Sync Issue - if: env.changes == 'true' + if: steps.check-for-changes.outputs.HAS_CHANGES uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 with: script: | From 4350f0fc218462048f84d860806559d26b8b0b13 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Mon, 22 Jul 2024 14:41:51 +1000 Subject: [PATCH 2/5] Default empty issue body to '' --- .github/workflows/sync-backport-changelog.yml | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/.github/workflows/sync-backport-changelog.yml b/.github/workflows/sync-backport-changelog.yml index 558ed796053511..76a87f89ba7030 100644 --- a/.github/workflows/sync-backport-changelog.yml +++ b/.github/workflows/sync-backport-changelog.yml @@ -53,18 +53,20 @@ jobs: const endDelimiter = ''; const autoGeneratedContent = `${startDelimiter}\n${processedChangelog}\n${endDelimiter}`; - const regex = new RegExp(`${startDelimiter}[\\s\\S]*${endDelimiter}`); + const existingBody = latestIssue.body ?? ''; + let newBody; - if (regex.test(latestIssue.body)) { + const regex = new RegExp(`${startDelimiter}[\\s\\S]*${endDelimiter}`); + if (regex.test(existingBody)) { // If delimiters exist, replace the content between them - newBody = latestIssue.body.replace(regex, autoGeneratedContent); + newBody = existingBody.replace(regex, autoGeneratedContent); } else { // If delimiters don't exist, append the new content at the end - newBody = `${latestIssue.body}\n\n${autoGeneratedContent}`; + newBody = `${existingBody}\n\n${autoGeneratedContent}`; } - if (newBody.trim() !== latestIssue.body.trim()) { + if (newBody.trim() !== existingBody.trim()) { await github.rest.issues.update({ owner: context.repo.owner, repo: context.repo.repo, From 6fd9e96ccbfb1d89753f6d54ba62c8466b907272 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Mon, 22 Jul 2024 15:10:00 +1000 Subject: [PATCH 3/5] Run sync when issue is labeled with Sync Backport Changelog --- .github/workflows/sync-backport-changelog.yml | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/.github/workflows/sync-backport-changelog.yml b/.github/workflows/sync-backport-changelog.yml index 76a87f89ba7030..38ed9543b401bc 100644 --- a/.github/workflows/sync-backport-changelog.yml +++ b/.github/workflows/sync-backport-changelog.yml @@ -4,11 +4,20 @@ on: push: branches: - trunk + issues: + types: [labeled] jobs: sync-backport-changelog: name: Sync Core Backport Issue runs-on: ubuntu-latest + if: > + github.event_name == 'push' || + ( + github.event_name == 'issues' && + github.event.action == 'labeled' && + github.event.label.name == '🤖 Sync Backport Changelog' + ) steps: - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 with: @@ -18,7 +27,13 @@ jobs: run: | git diff --quiet HEAD^ HEAD -- backport-changelog || echo "HAS_CHANGES=1" >> "$GITHUB_OUTPUT" - name: Sync Issue - if: steps.check-for-changes.outputs.HAS_CHANGES + if: > + steps.check-for-changes.outputs.HAS_CHANGES || + ( + github.event_name == 'issues' && + github.event.action == 'labeled' && + github.event.label.name == '🤖 Sync Backport Changelog' + ) uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 with: script: | From 24addd7468f5b0c52f269bfe2803fe39a99722df Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Tue, 23 Jul 2024 10:46:50 +1000 Subject: [PATCH 4/5] Only checkout code on push --- .github/workflows/sync-backport-changelog.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/sync-backport-changelog.yml b/.github/workflows/sync-backport-changelog.yml index 38ed9543b401bc..00d2f9513318f9 100644 --- a/.github/workflows/sync-backport-changelog.yml +++ b/.github/workflows/sync-backport-changelog.yml @@ -19,7 +19,9 @@ jobs: github.event.label.name == '🤖 Sync Backport Changelog' ) steps: - - uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7 + - name: Checkout + if: github.event_name == 'push' + 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 From fad4a176021b6445df09073815a1ed0ccde7b05e Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Tue, 23 Jul 2024 11:16:55 +1000 Subject: [PATCH 5/5] Use env again, only run 'Check for changes' on push --- .github/workflows/sync-backport-changelog.yml | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/.github/workflows/sync-backport-changelog.yml b/.github/workflows/sync-backport-changelog.yml index 00d2f9513318f9..bbc5663cf715be 100644 --- a/.github/workflows/sync-backport-changelog.yml +++ b/.github/workflows/sync-backport-changelog.yml @@ -20,22 +20,17 @@ jobs: ) steps: - name: Checkout - if: github.event_name == 'push' 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 - id: check-for-changes + if: github.event_name == 'push' run: | - git diff --quiet HEAD^ HEAD -- backport-changelog || echo "HAS_CHANGES=1" >> "$GITHUB_OUTPUT" + if git diff --quiet HEAD^ HEAD -- backport-changelog; then + echo "skip_sync=true" >> "$GITHUB_ENV" + fi - name: Sync Issue - if: > - steps.check-for-changes.outputs.HAS_CHANGES || - ( - github.event_name == 'issues' && - github.event.action == 'labeled' && - github.event.label.name == '🤖 Sync Backport Changelog' - ) + if: ${{ ! env.skip_sync }} uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 with: script: |