Skip to content

Commit

Permalink
Add github-release.js
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgervang authored May 22, 2024
1 parent 4dc3f23 commit 03765f9
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions scripts/github-release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {execSync} from 'child_process';
import {readFileSync} from 'fs';

// Get the latest tag
const tag = getGitTag();
if (!tag) {
console.error('TAG NOT FOUND');
process.exit(1);
}

// Parse changelog
const changelog = getReleaseNotes(tag);
if (!changelog) {
console.error('CHANGELOG NOT FOUND');
process.exit(1);
}

// Publish release notes to GitHub
// https://docs.github.com/en/rest/reference/repos#create-a-release
const requestBody = {
tag_name: tag,
name: tag,
body: changelog,
prerelease: tag.search(/alpha|beta|rc/) > 0
};

console.log(JSON.stringify(requestBody));

function getGitTag() {
try {
return execSync('git describe --tags --exact-match HEAD', {
stdio: [null, 'pipe', null],
encoding: 'utf-8'
}).trim();
} catch (err) {
// not tagged
return null;
}
}

function getReleaseNotes(version) {
let changelog = readFileSync('CHANGELOG.md', 'utf-8');
const header = changelog.match(new RegExp(`^###.*\\b${version.replace('v', '')}\\b.*$`, 'm'));
if (!header) {
return null;
}
changelog = changelog.slice(header.index + header[0].length);
const endIndex = changelog.search(/^#/m);
if (endIndex > 0) {
changelog = changelog.slice(0, endIndex);
}
return changelog.trim();
}

0 comments on commit 03765f9

Please sign in to comment.