-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (57 loc) · 2.02 KB
/
index.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
65
66
67
68
const core = require('@actions/core');
const github = require('@actions/github');
const bumpVersion = (changelog, oldVersion) => {
const version = oldVersion.split('.');
let versionBump;
const section_mapping = core.getInput('section_mapping').split(',');
core.info(`Section mapping: ${section_mapping}`);
section_mapping.some((mapping) => {
const [section, bump] = mapping.split('=>').map(e => e.trim());
if (changelog.search(section) >= 0) {
core.info(`${section} found`);
if (bump === 'minor') {
versionBump = bump;
return true;
}
versionBump = bump;
}
});
switch (versionBump) {
case 'minor':
version[1] = Number(version[1]) + 1;
version[2] = 0;
break;
case 'patch':
version[2] = Number(version[2]) + 1;
break;
}
return version.join('.');
}
const run = async () => {
try {
const githubToken = core.getInput('github_token', { required: true });
const octokit = github.getOctokit(githubToken);
const credentials = {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
};
const pullRequest = github.context.payload.pull_request;
const { data: { tag_name } } = await octokit.rest.repos.getLatestRelease(credentials);
core.info(`latest release: ${tag_name}`);
const searchWord = '```changelog';
const startIndex = pullRequest.body.search(searchWord) + searchWord.length;
let changelog = pullRequest.body.slice(startIndex);
const endIndex = changelog.search('```');
changelog = changelog.slice(0, endIndex);
if (!changelog.trim()) throw 'No changelog found';
const oldVersion = (tag_name || core.getInput('initial_version'));
const newVersion = bumpVersion(changelog, oldVersion);
core.info(`version: ${oldVersion} => ${newVersion}`);
if (oldVersion === newVersion) throw 'No version change';
core.setOutput('changelog', changelog);
core.setOutput('version', newVersion);
} catch (error) {
core.setFailed(error.message);
}
}
run();