forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 2
91 lines (80 loc) · 3.92 KB
/
sync-backport-changelog.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
name: Sync Core Backport Issue
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:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
fetch-depth: 2 # Fetch the last two commits to compare changes
- name: Check for changes in backport-changelog
if: github.event_name == 'push'
run: |
if git diff --quiet HEAD^ HEAD -- backport-changelog; then
echo "skip_sync=true" >> "$GITHUB_ENV"
fi
- name: Sync Issue
if: ${{ ! env.skip_sync }}
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 = '<!-- START TRUNK BACKPORT CHANGELOG -->';
const endDelimiter = '<!-- END TRUNK BACKPORT CHANGELOG -->';
const autoGeneratedContent = `${startDelimiter}\n${processedChangelog}\n${endDelimiter}`;
const existingBody = latestIssue.body ?? '';
let newBody;
const regex = new RegExp(`${startDelimiter}[\\s\\S]*${endDelimiter}`);
if (regex.test(existingBody)) {
// If delimiters exist, replace the content between them
newBody = existingBody.replace(regex, autoGeneratedContent);
} else {
// If delimiters don't exist, append the new content at the end
newBody = `${existingBody}\n\n${autoGeneratedContent}`;
}
if (newBody.trim() !== existingBody.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.');
}