-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
113 lines (100 loc) · 4.2 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
106
107
108
109
110
111
112
113
const core = require('@actions/core');
const { Octokit } = require("@octokit/rest");
const fs = require("fs")
const { execSync } = require("child_process")
async function downloadRelease(octokit, os, org, repo, release, token) {
const postfix = `_${os}_amd64.${os === "windows" ? "zip" : "tar.gz"}`;
const tempdir = os === "windows" ? process.env.TEMP + "\\" : "/tmp/";
const extract = os === "windows" ? "tar -xvf" : "tar -xvzf";
const archive = `${tempdir}gotestfmt${postfix}`;
const releaseAssets = await octokit.rest.repos.listReleaseAssets({
owner: org,
repo: repo,
release_id: release.id,
})
for (let asset of releaseAssets.data) {
console.log("Examining release asset " + asset.name + " at " + asset.browser_download_url + " ...")
if (asset.name.endsWith(postfix)) {
console.log("Found binary named " + asset.name + " at " + asset.browser_download_url + " , attempting download...")
if (token) {
execSync(`curl -L -o ${archive} -H "Authorization: Bearer ${token}" ${asset.browser_download_url}`)
} else {
execSync(`curl -L -o ${archive} ${asset.browser_download_url}`)
}
console.log("Unpacking archive file...")
core.addPath(process.env.GITHUB_WORKSPACE)
process.chdir(process.env.GITHUB_WORKSPACE)
execSync(`${extract} "${archive}"`)
console.log("Removing asset archive...")
fs.unlinkSync(archive)
console.log("Successfully set up gotestfmt.")
return
}
}
throw `No release asset matched postfix '${postfix}'.`
}
async function downloadGofmt(octokit, version, versionPrefix, os, org, repo, token) {
if (version !== "") {
if (!version.startsWith(versionPrefix)) {
throw "Specified version " + version + " does not start with required version prefix " + versionPrefix + "."
}
console.log("Downloading gotestfmt version " + version + " from " + org + "/" + repo + "...")
} else {
console.log("Downloading latest stable gotestfmt version starting with " + versionPrefix + " from " + org + "/" + repo + "...")
}
const releases = await octokit.rest.repos.listReleases({
owner: org,
repo: repo
// No pagination added, we are optimistic that there is a stable release within the first 100
// releases.
})
let tries = 0
for (let release of releases.data) {
if ((version !== "" && release.name === version) || (!release.prerelease && release.name.startsWith(versionPrefix))) {
console.log("Found release " + release.name + " matching criteria, attempting to download binary...")
try {
await downloadRelease(octokit, os, org, repo, release, token)
return
} catch (e) {
tries++
if (tries > 3) {
console.log("Binary download failed, tried " + tries + " times, giving up. (" + e + ")")
throw e
}
console.log("Binary download failed, trying next release. (" + e + ")")
}
}
}
console.log("Failed to find a release matching the criteria.")
throw "Failed to find a release matching the criteria."
}
async function determineOS() {
const uname = execSync("uname")
let os = uname.toString().trim().toLowerCase()
if (os.indexOf("msys_nt") === 0)
{
os = "windows";
}
console.log(`Running on OS '${os}'`)
return os
}
async function main() {
try {
// versionPrefix is the prefix of the version gotestfmt-action supports.
const versionPrefix = "v2."
const token = core.getInput('token');
const version = core.getInput('version');
const org = core.getInput("org")
const repo = core.getInput("repo")
const octokit = new Octokit({
auth: token,
})
const os = await determineOS()
await downloadGofmt(octokit, version, versionPrefix, os, org, repo, token)
console.log("Setup complete.")
} catch (error) {
console.log("Setup failed.")
core.setFailed(error);
}
}
main()