generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
71 lines (55 loc) · 2.46 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
69
70
71
const core = require('@actions/core');
const { exec } = require('@actions/exec');
const github = require('@actions/github');
const { Toolkit } = require('actions-toolkit');
const STRATEGIES = ['#patch', '#minor', '#major'];
Toolkit.run(async (tools) => {
{
try {
// get context
const { payload } = github.context;
const args = { message: '', email: '', username: '' };
if (payload.head_commit) {
args.email = payload.head_commit.committer.email;
args.username = payload.head_commit.committer.username;
args.message = payload.head_commit.message;
}
// get input credentials
const inputUser = core.getInput('user');
const inputEmail = core.getInput('email');
const inputBranch = core.getInput('branch');
const unrelated = core.getInput('unrelated');
const { message, email, username } = args;
const userName = inputUser || username;
const userEmail = inputEmail || email;
if (!userName || !userEmail) {
const errorMessage = `failed to extract username or email from github context, please provide 'username' and 'email' through the workflow file.`;
return core.setFailed(errorMessage);
}
const defaultStrategy = STRATEGIES.filter((strat) => message && message.includes(strat))[0] || STRATEGIES[0];
const strategy = defaultStrategy.replace('#', '');
const commitMessage = message && message.replace(defaultStrategy, '');
tools.log(`Latest commit message: ${commitMessage}`);
tools.log(`Running with ${userName} ${userEmail} and bumping strategy ${strategy}`);
tools.log(`Branch is ${inputBranch}`);
// git login and pull
const pullArgs = ['pull', 'origin', inputBranch, '--tags'];
if (unrelated) {
pullArgs.push('--allow-unrelated-histories');
}
await exec('git', ['config', '--local', 'user.name', userName]);
await exec('git', ['config', '--local', 'user.email', userEmail]);
await exec('git', pullArgs);
// version by strategy
await exec('npm', ['version', strategy, '--no-commit-hooks', '-m', `${commitMessage} %s`]);
const version = tools.getPackageJSON().version;
core.info(`version is ${version}`);
// push new version and tag
await exec('git', ['push', 'origin', `HEAD:${inputBranch}`, '--tags']);
// set output version
core.setOutput('version', version);
} catch (error) {
core.setFailed(error.message);
}
}
});