-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
57 lines (49 loc) · 1.65 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
const core = require('@actions/core');
const exec = require('@actions/exec');
const messages = {};
messages.exceptList = 'Source branch is in except list. Check was skipped';
messages.squash = 'Only 1 commit is possible in pull request. Please squash your commits';
try {
const sourceBranch = core.getInput('source-branch');
const targetBranch = core.getInput('target-branch');
const exceptBranches = core.getInput('except-branches').split(';');
const commitsCount = Number.parseInt(core.getInput('commits-count'));
if (exceptBranches.includes(sourceBranch)) {
core.info(messages.exceptList);
} else {
getCommitsCount(sourceBranch, targetBranch)
.then(currentCommitsCount => {
if (currentCommitsCount > commitsCount) {
core.setFailed(messages.squash);
}
});
}
} catch (error) {
core.setFailed(error.message);
}
async function getCommitsCount(sourceBranch, targetBranch) {
try {
let out = '';
let err = '';
const options = {
listeners: {
stdout: (data) => {
out += data.toString();
},
stderr: (data) => {
err += data.toString();
}
},
};
await exec.exec(`${__dirname}/commits-count.sh`, [sourceBranch, targetBranch], options);
if (err) {
core.setFailed(err);
process.exit(0);
} else {
return Number.parseInt(out.trim());
}
} catch (error) {
core.setFailed(`Error: ${error.message}`);
process.exit(0);
}
}