|
| 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