-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
73 lines (63 loc) · 2.41 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
const core = require("@actions/core");
const {Octokit} = require("@octokit/rest");
const {cmpTags} = require("tag-cmp");
async function run() {
try {
const repository = core.getInput("repository", {required: true});
const repoParts = repository.split("/");
if (repoParts.length !== 2) {
throw `Invalid repository "${repository}" (needs to have one slash)`;
}
const [owner, repo] = repoParts;
const prefix = core.getInput("prefix") || "";
const regex = core.getInput("regex") || null;
const releasesOnly = (core.getInput("releases-only") || "false").toLowerCase() === "true";
// It's somewhat safe to assume that the most recenly created release is actually latest.
const sortTagsDefault = (releasesOnly ? "false" : "true");
const sortTags = (core.getInput("sort-tags") || sortTagsDefault).toLowerCase() === "true";
core.setOutput("tag", await getLatestTag(owner, repo, releasesOnly, prefix, regex, sortTags));
} catch (error) {
core.setFailed(error);
}
}
const octokit = new Octokit({auth: core.getInput("token") || null});
async function getLatestTag(owner, repo, releasesOnly, prefix, regex, sortTags) {
const endpoint = (releasesOnly ? octokit.repos.listReleases : octokit.repos.listTags);
const pages = endpoint.endpoint.merge({"owner": owner, "repo": repo, "per_page": 100});
const tags = [];
for await (const item of getItemsFromPages(pages)) {
const tag = (releasesOnly ? item["tag_name"] : item["name"]);
if (!tag.startsWith(prefix)) {
continue;
}
if (regex && !new RegExp(regex).test(tag)) {
continue;
}
if (!sortTags) {
// Assume that the API returns the most recent tag(s) first.
return tag;
}
tags.push(tag);
}
if (tags.length === 0) {
let error = `The repository "${owner}/${repo}" has no `;
error += releasesOnly ? "releases" : "tags";
if (prefix) {
error += ` matching "${prefix}*"`;
}
throw error;
}
tags.sort(cmpTags);
const [latestTag] = tags.slice(-1);
return latestTag;
}
async function* getItemsFromPages(pages) {
for await (const page of octokit.paginate.iterator(pages)) {
for (const item of page.data) {
yield item;
}
}
}
if (require.main === module) {
run();
}