-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.ts
229 lines (201 loc) · 7.1 KB
/
main.ts
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import * as os from "os";
import * as path from "path";
import * as cache from "@actions/cache";
import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import { GitHub, getOctokitOptions} from "@actions/github/lib/utils";
import { throttling } from "@octokit/plugin-throttling";
const ThrottlingOctokit = GitHub.plugin(throttling);
interface ToolInfo {
owner: string;
project: string;
tag: string;
osPlatform: string;
osArch: string;
}
async function run() {
try {
// set up auth/environment
const token = process.env['GITHUB_TOKEN']
if (!token) {
throw new Error(
`No GitHub token found`
)
}
const octokit = new ThrottlingOctokit({
throttle: {
onRateLimit: (retryAfter, options) => {
core.warning(
`RateLimit detected for request ${options.method} ${options.url}.`
);
core.info(`Retrying after ${retryAfter} seconds.`);
return true;
},
onSecondaryRateLimit: (retryAfter, options) => {
core.warning(
`SecondaryRateLimit detected for request ${options.method} ${options.url}.`
);
core.info(`Retrying after ${retryAfter} seconds.`);
return true;
},
},
...getOctokitOptions(token),
})
const repo = core.getInput("repo");
if (!repo) {
throw new Error(
`Repo was not specified`
)
}
const tag = core.getInput("tag");
if (!tag) {
throw new Error(
`Tag not specified`
)
}
const cacheEnabled = (core.getInput("cache") === "enable")
&& tag !== "latest"
&& tag !== "";
const [owner, project] = repo.split("/")
let osMatch : string[] = []
// Determine Platform
let osPlatform = core.getInput("platform");
if (osPlatform === ""){
switch (os.platform()) {
case "linux":
osPlatform = "linux";
break;
case "darwin":
osPlatform = "darwin";
break;
case "win32":
osPlatform = "windows";
break;
default:
core.setFailed("Unsupported operating system - $this action is only released for Darwin, Linux and Windows");
return;
}
}
osMatch.push(osPlatform)
core.info(`==> System reported platform: ${os.platform()}`)
core.info(`==> Using platform: ${osPlatform}`)
// Determine Architecture
let osArch = core.getInput("arch");
if (osArch === "") {
osArch = os.arch()
switch (os.arch()) {
case "x64":
osMatch.push("x86_64", "x64", "amd64")
break;
default:
osMatch.push(os.arch())
return;
}
} else {
osMatch.push(osArch)
}
core.info(`==> System reported arch: ${os.arch()}`)
core.info(`==> Using arch: ${osArch}`)
let toolInfo: ToolInfo = {
owner: owner,
project: project,
tag: tag,
osArch: osArch,
osPlatform: osPlatform
};
let dest = toolPath(toolInfo);
// Look in the cache first.
let cacheKey = cachePrimaryKey(toolInfo);
if (cacheEnabled && cacheKey !== undefined) {
let ok = await cache.restoreCache([dest], cacheKey);
if (ok !== undefined) {
core.info(`Found ${project} in the cache: ${dest}`)
core.addPath(dest);
return;
}
}
let getReleaseUrl;
if (tag === "latest") {
getReleaseUrl = await octokit.rest.repos.getLatestRelease({
owner: owner,
repo: project,
})
} else {
getReleaseUrl = await octokit.rest.repos.getReleaseByTag({
owner: owner,
repo: project,
tag: tag,
})
}
let osMatchRegexForm = `(${osMatch.join('|')})`
let re = new RegExp(`${osMatchRegexForm}.*${osMatchRegexForm}.*\.(tar.gz|zip)`)
let asset = getReleaseUrl.data.assets.find(obj => {
core.info(`searching for ${obj.name} with ${re.source}`)
let normalized_obj_name = obj.name.toLowerCase()
return re.test(normalized_obj_name)
})
if (!asset ) {
const found = getReleaseUrl.data.assets.map(f => f.name)
throw new Error(
`Could not find a release for ${tag}. Found: ${found}`
)
}
const extractFn = getExtractFn(asset.name);
const url = asset.browser_download_url
core.info(`Downloading ${project} from ${url}`)
const binPath = await tc.downloadTool(url);
await extractFn(binPath, dest);
if (cacheEnabled && cacheKey !== undefined) {
try {
await cache.saveCache([dest], cacheKey);
} catch (error) {
const typedError = error as Error;
if (typedError.name === cache.ValidationError.name) {
throw error;
} else if (typedError.name === cache.ReserveCacheError.name) {
core.info(typedError.message);
} else {
core.warning(typedError.message);
}
}
}
core.addPath(dest);
core.info(`Successfully extracted ${project} to ${dest}`)
} catch (error) {
if (error instanceof Error) {
core.setFailed(error.message);
} else {
core.setFailed("catastrophic failure, please file an issue")
}
}
}
function cachePrimaryKey(info: ToolInfo): string|undefined {
// Currently not caching "latest" verisons of the tool.
if (info.tag === "latest") {
return undefined;
}
return "action-install-gh-release/" +
`${info.owner}/${info.project}/${info.tag}/${info.osPlatform}-${info.osArch}`;
}
function toolPath(info: ToolInfo): string {
return path.join(getCacheDirectory(),
info.owner, info.project, info.tag,
`${info.osPlatform}-${info.osArch}`);
}
function getCacheDirectory() {
const cacheDirectory = process.env['RUNNER_TOOL_CACHE'] || '';
if (cacheDirectory === '') {
core.warning('Expected RUNNER_TOOL_CACHE to be defined');
}
return cacheDirectory;
}
function getExtractFn(assetName: any) {
if (assetName.endsWith('.tar.gz')) {
return tc.extractTar;
} else if (assetName.endsWith('.zip')) {
return tc.extractZip;
} else {
throw new Error(`Unreachable error? File is neither .tar.gz nor .zip, got: ${assetName}`);
}
}
run();