-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
85 lines (75 loc) · 1.92 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
const core = require("@actions/core");
const github = require("@actions/github");
const env = process.env;
function setBuildVersion(buildVersion) {
core.setOutput("NEXT_BUILD_VERSION", buildVersion);
}
async function listAllTags(octokit, owner, repo) {
var page = 1;
var tags = [];
while (true) {
const response = await octokit.repos.listTags({
owner,
repo,
per_page: 100,
page: page,
});
const newTags = response["data"].map((obj) => obj["name"]);
if (!newTags.length) {
break;
}
tags.push(...newTags);
page++;
}
return tags;
}
async function run() {
const token = core.getInput("GH_TOKEN");
var tagPrefix = core.getInput("TAG_PREFIX");
if (!tagPrefix) {
tagPrefix = "v";
}
const octokit = new github.GitHub(token);
const owner = env.GITHUB_REPOSITORY.split("/")[0];
const repo = env.GITHUB_REPOSITORY.split("/")[1];
const versionTagRegex = new RegExp(`^${tagPrefix}(\\d+)\\.(\\d+)\\.(\\d+)$`);
const allTags = await listAllTags(octokit, owner, repo);
const tags = allTags.filter((el) => el.match(versionTagRegex));
if (tags.length < 1) {
setBuildVersion("0.0.1");
return;
}
tags.sort((l, r) => {
const lx = parseInt(l.split(".")[0]);
const rx = parseInt(r.split(".")[0]);
if (lx < rx) {
return 1;
}
if (rx < lx) {
return -1;
}
const ly = parseInt(l.split(".")[1]);
const ry = parseInt(r.split(".")[1]);
if (ly < ry) {
return 1;
}
if (ry < ly) {
return -1;
}
const lz = parseInt(l.split(".")[2]);
const rz = parseInt(r.split(".")[2]);
if (lz < rz) {
return 1;
}
if (rz < lz) {
return -1;
}
return 0;
});
const split = tags[0].substring(tagPrefix.length).split(".");
const nextX = parseInt(split[0]);
const nextY = parseInt(split[1]);
const nextZ = parseInt(split[2]) + 1;
setBuildVersion(`${nextX}.${nextY}.${nextZ}`);
}
run();