/
github-release.js
53 lines (47 loc) · 1.29 KB
/
github-release.js
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
const {execSync} = require('child_process');
const {readFileSync} = require('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 --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();
}