-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_test_tag.js
61 lines (51 loc) · 1.52 KB
/
create_test_tag.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
const { exec } = require('child_process');
const getTypeParameter = () => {
const flagIndex = process.argv.indexOf('--type');
return flagIndex > -1 ? process.argv[flagIndex + 1] : 'test';
};
const createAndPushTag = (tagName) => {
exec(`git tag ${tagName}`, (error, sdtout) => {
if (!error) {
console.log(`Created the tag ${tagName}`);
console.log('Pushing...');
exec(`git push origin ${tagName}`, (error, stdout) => {
if (!error) {
console.log(`Pushed the tag ${tagName}`);
}
});
}
});
};
const findMaxTagNumber = (branchTags, type) => {
return branchTags.reduce((maxTagNumber, tagName) => {
let tagNumber = -1;
const splitTagName = tagName.split('_');
try {
if (splitTagName[0] === type) {
tagNumber = Number.parseInt(splitTagName.pop());
}
} catch (e) {
tagNumber = -1;
}
return !Number.isNaN(tagNumber)
? Math.max(tagNumber, maxTagNumber)
: maxTagNumber;
}, 0);
};
const grepTagNames = (currentBranch) => {
exec(`git tag | grep ${currentBranch}`, (error, sdtout) => {
const type = getTypeParameter();
if (!error) {
const branchTags = sdtout.split('\n').slice(0, -1);
const maxTagNumber = findMaxTagNumber(branchTags, type);
createAndPushTag(`${type}_${currentBranch}_${maxTagNumber + 1}`);
} else {
createAndPushTag(`${type}_${currentBranch}_1`);
}
});
};
exec('git branch --show-current', (error, sdtout) => {
if (!error) {
grepTagNames(sdtout.trim());
}
});