This repository has been archived by the owner on Apr 15, 2023. It is now read-only.
generated from actions/javascript-action
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
generate-release-notes.js
64 lines (56 loc) · 1.79 KB
/
generate-release-notes.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
54
55
56
57
58
59
60
61
62
63
64
const util = require('util');
const execFile = util.promisify(require('child_process').execFile);
const repoURL = process.env.GITHUB_SERVER_URL + '/' + process.env.GITHUB_REPOSITORY;
const excludePreset = /^bump |^meta|^document|^lint|^refactor|readme|dependencies|^v?\d+\.\d+\.\d+/i;
async function generateReleaseNotes({
range,
exclude = '',
commitTemplate = '- {hash} {title}',
releaseTemplate = '{commits}\n\n{range}',
dateFormat = 'short',
sort = 'desc',
skipOnEmpty = false
}) {
dateFormat = dateFormat.includes('%') ? 'format:' + dateFormat : dateFormat;
// Get commits between computed range
let {stdout: commits} = await execFile('git', [
'log',
'--format=%H¬%ad¬%s',
'--date=' + dateFormat,
sort === 'asc' && '--reverse',
range
].filter(Boolean));
commits = commits.split('\n').filter(Boolean).map(line => {
const [hash, date, title] = line.split('¬');
return {
hash: hash.slice(0, 8),
date,
title
};
});
if (exclude) {
// Booleans aren't currently supported: https://github.com/actions/toolkit/issues/361
const regex = exclude === 'true' || exclude === true ? excludePreset : new RegExp(exclude);
commits = commits.filter(({title}) => !regex.test(title));
}
const commitEntries = [];
if (commits.length === 0) {
if (skipOnEmpty) {
return;
}
commitEntries.push('_Maintenance release_');
} else {
for (const {hash, date, title} of commits) {
const line = commitTemplate
.replace('{hash}', hash)
.replace('{url}', repoURL + '/commit/' + hash)
.replace('{date}', date)
.replace('{title}', title);
commitEntries.push(line);
}
}
return releaseTemplate
.replace('{commits}', commitEntries.join('\n'))
.replace('{range}', `[\`${range}\`](${repoURL}/compare/${range})`);
}
exports.generateReleaseNotes = generateReleaseNotes;