-
Notifications
You must be signed in to change notification settings - Fork 5
/
validateChangelog.js
32 lines (29 loc) · 1.09 KB
/
validateChangelog.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
const fs = require('fs');
const version = require('./package.json').version;
const devmoji = require('./devmoji.config');
const ACCEPTABLE_ENTRIES = devmoji.devmoji.map(i => i.code);
const CHANGELOG_DIR = './changelog';
const parsePullRequestId = githubRef => {
const result = /refs\/pull\/(\d+)\/merge/g.exec(githubRef);
if (!result) throw new Error('Reference not found.');
const [, pullRequestId] = result;
return pullRequestId;
};
function main() {
let files = fs.readdirSync(CHANGELOG_DIR);
files = files.filter(f => f.charAt(0) !== '.');
if (!files.length) throw new Error('No changelog found');
const pullId = parsePullRequestId(process.env.GITHUB_REF);
let idFound = false;
files.forEach(item => {
const type = item.split('-')[0];
if (!ACCEPTABLE_ENTRIES.includes(type))
throw new Error(
'Invalid changelog type, valid types:' + ACCEPTABLE_ENTRIES.join(',')
);
const prNumber = item.split('-')[1].replace('.md', '');
if (prNumber === pullId) idFound = true;
});
if (!idFound) throw new Error('Pull request ID doesnt match changelog ids');
}
main();