forked from jessicalostinspace/commit-difference-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
57 lines (47 loc) · 1.67 KB
/
index.js
File metadata and controls
57 lines (47 loc) · 1.67 KB
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
const core = require('@actions/core');
const exec = require('@actions/exec');
const src = __dirname;
try {
const baseBranch = core.getInput('base-branch');
const secondaryBranch = core.getInput('secondary-branch');
const regexp = /^[\.A-Za-z0-9_-]*$/;
console.log(`base-branch: ${baseBranch}`);
console.log(`secondary-branch: ${secondaryBranch}`);
if (!!baseBranch && !!secondaryBranch && regexp.test(baseBranch) && regexp.test(secondaryBranch)) {
getCommitDifference(baseBranch, secondaryBranch);
} else {
const regexError = "Branch names must contain only numbers, strings, underscores, periods, and dashes.";
core.setFailed(regexError);
}
} catch (error) {
core.setFailed(error.message);
}
async function getCommitDifference(baseBranch, secondaryBranch) {
try {
let output = '';
let err = '';
// These are option configurations for the @actions/exec lib`
const options = {};
options.listeners = {
stdout: (data) => {
output += data.toString();
},
stderr: (data) => {
err += data.toString();
}
};
options.cwd = './';
await exec.exec(`${src}/commit-diff-count.sh`, [baseBranch, secondaryBranch], options);
const { commitDiffCount } = JSON.parse(output);
if (commitDiffCount) {
console.log('\x1b[32m%s\x1b[0m', `Difference in commits between ${secondaryBranch} and ${baseBranch}: ${commitDiffCount}`);
core.setOutput("commit-difference-count", commitDiffCount);
} else {
core.setFailed(err);
process.exit(1);
}
} catch (err) {
core.setFailed(`Could not get commit difference between branches because: ${err.message}`);
process.exit(0);
}
}