Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically sync backport changelog to issue #62973

Merged
merged 7 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions .github/workflows/check-backport-changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need commit history for this test, so cleaning up a bit.

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: |
Expand Down
76 changes: 76 additions & 0 deletions .github/workflows/sync-backport-changelog.yml
Original file line number Diff line number Diff line change
@@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we check the last commit for changes in this directory before doing anything else? Seems like it might save us some API requests.
Another alternative could be conditioning this workflow on a check similar to the one check-backport-changelog uses.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What API requests would it save? I'm already checking if the content is different.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The very first one

https://github.com/WordPress/gutenberg/pull/62973/files#diff-b61aaec5beeb30ae49637bf34830218b191db2c3fb71db321153b7cf6a93226dR18-R25

What I'm getting at is that I think that not starting the workflow if it's not necessary is a good idea

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an early abort 8bb22a2


const startDelimiter = '<!-- START TRUNK BACKPORT CHANGELOG -->';
const endDelimiter = '<!-- END TRUNK BACKPORT CHANGELOG -->';
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.');
}
Loading