This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (88 loc) · 2.74 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const core = require('@actions/core');
const github = require('@actions/github');
const { Octokit } = require("@octokit/action");
const fs = require("fs");
const path = require("path");
let cur_ver;
let new_ver;
let was_bumped;
async function main(){
try {
const pathToVersionFile = core.getInput('path-to-version-file', { required: true });
const mode = core.getInput('mode');
const newVersion= core.getInput('new-verison');
cur_ver = "";
new_ver = "";
was_bumped = false;
if(!process.env.GITHUB_TOKEN){
throw new Error("GITHUB_TOKEN env not set");
}
if(mode == "GET_VERSION"){
cur_ver = getVersion(pathToVersionFile);
}
else if(mode == "BUMP_VERSION"){
cur_ver = getVersion(pathToVersionFile);
new_ver = await updateVersion(cur_ver);
updateVersionFile(pathToVersionFile, new_ver);
was_bumped = cur_ver != new_ver;
}
else if(mode == "SET_VERSION"){
cur_ver = getVersion(pathToVersionFile);
new_ver = newVersion;
updateVersionFile(pathToVersionFile, new_ver);
was_bumped = cur_ver != new_ver;
}
else{
throw new Error("Mode not recognised");
}
} catch (error) {
core.setFailed(error.message);
}
finally{
core.setOutput("cur_version", cur_ver);
core.setOutput("new_version", new_ver);
core.setOutput("was_bumped", was_bumped);
}
}
function getVersion(pathToFile){
return fs.readFileSync(path.resolve(pathToFile), {encoding: 'utf8'});
}
async function updateVersion(version){
const labels = await getLabels();
const versionNumbers = version.match(/\d+/g);
for(let i = 0; i < versionNumbers.length; i++){
versionNumbers[i] = Number.parseInt(versionNumbers[i]);
}
for(const label of labels){
core.debug(label);
if(label == "release:major"){
versionNumbers[0]++;
versionNumbers[1] = 0;
versionNumbers[2] = 0;
}
else if(label == "release:minor"){
versionNumbers[1]++;
versionNumbers[2] = 0;
}
else if(label == "release:patch"){
versionNumbers[2]++;
}
}
const versionString = `${versionNumbers[0]}.${versionNumbers[1]}.${versionNumbers[2]}`;
return versionString;
}
async function getLabels(){
const octokit = new Octokit();
const context = github.context;
const PRS = await octokit.request('GET /repos/{owner}/{repo}/pulls?state=all', {
owner: context.payload.repository.owner.name,
repo: context.payload.repository.name
});
const prToMaster = PRS.data.find(pr => pr.merge_commit_sha == context.payload.after);
let labels = prToMaster.labels;
return labels ? labels.map(label => label.name) : [];
}
function updateVersionFile(pathToFile, newVersion){
fs.writeFileSync(path.resolve(pathToFile), newVersion);
}
main();