-
Notifications
You must be signed in to change notification settings - Fork 277
/
main.js
128 lines (111 loc) · 4.7 KB
/
main.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const core = require('@actions/core');
const exec = require('@actions/exec');
const github = require('@actions/github');
const AdmZip = require('adm-zip');
const filesize = require('filesize');
const pathname = require('path');
const fs = require('fs');
const { throttling } = require('@octokit/plugin-throttling');
const { GitHub } = require('@actions/github/lib/utils');
const Util = require('util');
const Stream = require('stream');
async function getGoodArtifacts(client, owner, repo, releaseId, name) {
console.log(`==> GET /repos/${owner}/${repo}/releases/${releaseId}/assets`);
const goodRepoArtifacts = await client.paginate(
"GET /repos/{owner}/{repo}/releases/{release_id}/assets",
{
owner: owner,
repo: repo,
release_id: releaseId,
per_page: 100,
},
(releaseAssets, done) => {
console.log(" ==> releaseAssets", releaseAssets);
const goodAssets = releaseAssets.data.filter((a) => {
console.log("==> Asset check", a);
return a.name == name
});
if (goodAssets.length > 0) {
done();
}
return goodAssets;
}
);
console.log("==> maybe goodRepoArtifacts:", goodRepoArtifacts);
return goodRepoArtifacts;
}
async function main() {
try {
const token = core.getInput("github_token", { required: true });
const [owner, repo] = core.getInput("repo", { required: true }).split("/");
const path = core.getInput("path", { required: true });
const name = core.getInput("name");
const download = core.getInput("download");
const releaseTag = core.getInput("release-tag");
const OctokitWithThrottling = GitHub.plugin(throttling);
const client = new OctokitWithThrottling({
auth: token,
throttle: {
onRateLimit: (retryAfter, options) => {
console.log(
`Request quota exhausted for request ${options.method} ${options.url}`
);
// Retry twice after hitting a rate limit error, then give up
if (options.request.retryCount <= 2) {
console.log(`Retrying after ${retryAfter} seconds!`);
return true;
} else {
console.log("Exhausted 2 retries");
core.setFailed("Exhausted 2 retries");
}
},
onAbuseLimit: (retryAfter, options) => {
// does not retry, only logs a warning
console.log(
`Abuse detected for request ${options.method} ${options.url}`
);
core.setFailed(`GitHub REST API Abuse detected for request ${options.method} ${options.url}`)
},
},
});
console.log("==> Repo:", owner + "/" + repo);
const releaseInfo = await client.repos.getReleaseByTag({
owner,
repo,
tag: releaseTag,
});
console.log(`==> Release info for tag ${releaseTag} = ${JSON.stringify(releaseInfo.data, null, 2)}`);
const releaseId = releaseInfo.data.id;
const goodArtifacts = await getGoodArtifacts(client, owner, repo, releaseId, name);
console.log("==> goodArtifacts:", goodArtifacts);
const artifactStatus = goodArtifacts.length === 0 ? "missing" : "found";
console.log("==> Artifact", name, artifactStatus);
console.log("==> download", download);
core.setOutput("status", artifactStatus);
if (artifactStatus === "found" && download == "true") {
console.log("==> # artifacts:", goodArtifacts.length);
const artifact = goodArtifacts[0];
console.log("==> Artifact:", artifact.id)
const size = filesize(artifact.size, { base: 10 })
console.log(`==> Downloading: ${artifact.name} (${size}) to path: ${path}`)
const dir = pathname.dirname(path)
console.log(`==> Creating containing dir if needed: ${dir}`)
fs.mkdirSync(dir, { recursive: true })
await exec.exec('curl', [
'-L',
'-o', path,
'-H', 'Accept: application/octet-stream',
'-H', `Authorization: token ${token}`,
artifact.url
])
}
if (artifactStatus === "missing" && download == "true") {
core.setFailed("Required", name, "that is missing");
}
return;
} catch (err) {
console.error(err.stack);
core.setFailed(err.message);
}
}
main();