Skip to content

Commit 39dc061

Browse files
Copilotrajbos
andcommitted
refactor: use existing sync script instead of inline code
Remove duplicate inline script from workflow and use the existing scripts/sync-changelog.js which has better features: - GitHub CLI and API token fallback support - Test mode for local development - Better error handling and user feedback - Comprehensive logging This eliminates code duplication and makes the workflow more maintainable. Co-authored-by: rajbos <6085745+rajbos@users.noreply.github.com>
1 parent f810ed6 commit 39dc061

1 file changed

Lines changed: 2 additions & 114 deletions

File tree

.github/workflows/sync-release-notes.yml

Lines changed: 2 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -31,120 +31,8 @@ jobs:
3131
env:
3232
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3333
run: |
34-
# Create a script to fetch GitHub releases and update CHANGELOG.md
35-
cat > sync_changelog.js << 'EOF'
36-
const { execSync } = require('child_process');
37-
const fs = require('fs');
38-
39-
async function syncReleaseNotes() {
40-
try {
41-
console.log('Fetching GitHub releases...');
42-
43-
// Fetch all releases using GitHub CLI
44-
const releasesJson = execSync('gh release list --json tagName,name,body,createdAt,isPrerelease --limit 50', { encoding: 'utf8' });
45-
const releases = JSON.parse(releasesJson);
46-
47-
console.log(`Found ${releases.length} releases`);
48-
49-
// Sort releases by creation date (newest first)
50-
releases.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
51-
52-
// Read current CHANGELOG.md
53-
let changelog = '';
54-
if (fs.existsSync('CHANGELOG.md')) {
55-
changelog = fs.readFileSync('CHANGELOG.md', 'utf8');
56-
}
57-
58-
// Extract the header and unreleased section
59-
const lines = changelog.split('\n');
60-
const headerEndIndex = lines.findIndex(line => line.startsWith('## [Unreleased]'));
61-
const unreleasedEndIndex = lines.findIndex((line, index) =>
62-
index > headerEndIndex && line.startsWith('## [') && !line.includes('Unreleased')
63-
);
64-
65-
let header = '';
66-
let unreleasedSection = '';
67-
68-
if (headerEndIndex >= 0) {
69-
header = lines.slice(0, headerEndIndex + 1).join('\n');
70-
if (unreleasedEndIndex >= 0) {
71-
unreleasedSection = lines.slice(headerEndIndex + 1, unreleasedEndIndex).join('\n');
72-
} else {
73-
// Take everything after unreleased header until we find a release or end
74-
const restOfFile = lines.slice(headerEndIndex + 1);
75-
const nextReleaseIndex = restOfFile.findIndex(line => line.startsWith('## [') && !line.includes('Unreleased'));
76-
if (nextReleaseIndex >= 0) {
77-
unreleasedSection = restOfFile.slice(0, nextReleaseIndex).join('\n');
78-
} else {
79-
unreleasedSection = restOfFile.join('\n');
80-
}
81-
}
82-
} else {
83-
// Create basic header if none exists
84-
header = '# Change Log\n\nAll notable changes to the "copilot-token-tracker" extension will be documented in this file.\n\nCheck [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file.\n\n## [Unreleased]';
85-
unreleasedSection = '\n';
86-
}
87-
88-
// Build new changelog content
89-
let newChangelog = header + unreleasedSection + '\n';
90-
91-
// Add releases
92-
for (const release of releases) {
93-
const version = release.tagName.startsWith('v') ? release.tagName.substring(1) : release.tagName;
94-
const releaseType = release.isPrerelease ? ' - Pre-release' : '';
95-
96-
newChangelog += `## [${version}]${releaseType}\n\n`;
97-
98-
if (release.body && release.body.trim()) {
99-
// Clean up the release body
100-
let body = release.body.trim();
101-
102-
// Remove any "Full Changelog" links at the end
103-
body = body.replace(/\*\*Full Changelog\*\*:.*$/gm, '').trim();
104-
105-
// Ensure bullet points are properly formatted
106-
const bodyLines = body.split('\n').map(line => {
107-
line = line.trim();
108-
if (line && !line.startsWith('-') && !line.startsWith('*') && !line.startsWith('#')) {
109-
return `- ${line}`;
110-
}
111-
return line;
112-
}).filter(line => line.length > 0);
113-
114-
newChangelog += bodyLines.join('\n') + '\n\n';
115-
} else {
116-
newChangelog += `- Release ${version}\n\n`;
117-
}
118-
}
119-
120-
// Write the new changelog
121-
fs.writeFileSync('CHANGELOG.md', newChangelog.trim() + '\n');
122-
console.log('CHANGELOG.md updated successfully!');
123-
124-
// Show what changed
125-
try {
126-
const diff = execSync('git diff CHANGELOG.md', { encoding: 'utf8' });
127-
if (diff.trim()) {
128-
console.log('\nChanges made to CHANGELOG.md:');
129-
console.log(diff);
130-
} else {
131-
console.log('No changes needed - CHANGELOG.md is already up to date');
132-
}
133-
} catch (error) {
134-
console.log('Could not show diff, but file was updated');
135-
}
136-
137-
} catch (error) {
138-
console.error('Error syncing release notes:', error);
139-
process.exit(1);
140-
}
141-
}
142-
143-
syncReleaseNotes();
144-
EOF
145-
146-
# Run the sync script
147-
node sync_changelog.js
34+
# Run the sync script from the scripts directory
35+
node scripts/sync-changelog.js
14836
14937
- name: Check for changes
15038
id: changes

0 commit comments

Comments
 (0)