forked from dawidd6/action-download-artifact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
284 lines (249 loc) · 9.75 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
const core = require('@actions/core')
const github = require('@actions/github')
const artifact = require('@actions/artifact')
const AdmZip = require('adm-zip')
const filesize = require('filesize')
const pathname = require('path')
const fs = require('fs')
async function downloadAction(name, path) {
const artifactClient = artifact.create()
const downloadOptions = {
createArtifactFolder: false
}
const downloadResponse = await artifactClient.downloadArtifact(
name,
path,
downloadOptions
)
core.setOutput("found_artifact", true)
}
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 skipUnpack = core.getBooleanInput("skip_unpack")
const ifNoArtifactFound = core.getInput("if_no_artifact_found")
let workflow = core.getInput("workflow")
let workflowConclusion = core.getInput("workflow_conclusion")
let pr = core.getInput("pr")
let commit = core.getInput("commit")
let branch = core.getInput("branch")
let event = core.getInput("event")
let runID = core.getInput("run_id")
let runNumber = core.getInput("run_number")
let checkArtifacts = core.getBooleanInput("check_artifacts")
let searchArtifacts = core.getBooleanInput("search_artifacts")
let dryRun = core.getInput("dry_run")
const client = github.getOctokit(token)
core.info(`==> Repository: ${owner}/${repo}`)
core.info(`==> Artifact name: ${name}`)
core.info(`==> Local path: ${path}`)
if (!workflow) {
const run = await client.rest.actions.getWorkflowRun({
owner: owner,
repo: repo,
run_id: runID || github.context.runId,
})
workflow = run.data.workflow_id
}
core.info(`==> Workflow name: ${workflow}`)
core.info(`==> Workflow conclusion: ${workflowConclusion}`)
const uniqueInputSets = [
{
"pr": pr,
"commit": commit,
"branch": branch,
"run_id": runID
}
]
uniqueInputSets.forEach((inputSet) => {
const inputs = Object.values(inputSet)
const providedInputs = inputs.filter(input => input !== '')
if (providedInputs.length > 1) {
throw new Error(`The following inputs cannot be used together: ${Object.keys(inputSet).join(", ")}`)
}
})
if (pr) {
core.info(`==> PR: ${pr}`)
const pull = await client.rest.pulls.get({
owner: owner,
repo: repo,
pull_number: pr,
})
commit = pull.data.head.sha
//branch = pull.data.head.ref
}
if (commit) {
core.info(`==> Commit: ${commit}`)
}
if (branch) {
branch = branch.replace(/^refs\/heads\//, "")
core.info(`==> Branch: ${branch}`)
}
if (event) {
core.info(`==> Event: ${event}`)
}
if (runNumber) {
core.info(`==> Run number: ${runNumber}`)
}
if (!runID) {
// Note that the runs are returned in most recent first order.
for await (const runs of client.paginate.iterator(client.rest.actions.listWorkflowRuns, {
owner: owner,
repo: repo,
workflow_id: workflow,
...(branch ? { branch } : {}),
...(event ? { event } : {}),
}
)) {
for (const run of runs.data) {
if (commit && run.head_sha != commit) {
continue
}
if (runNumber && run.run_number != runNumber) {
continue
}
if (workflowConclusion && (workflowConclusion != run.conclusion && workflowConclusion != run.status)) {
continue
}
if (checkArtifacts || searchArtifacts) {
let artifacts = await client.rest.actions.listWorkflowRunArtifacts({
owner: owner,
repo: repo,
run_id: run.id,
})
if (artifacts.data.artifacts.length == 0) {
continue
}
if (searchArtifacts) {
const artifact = artifacts.data.artifacts.find((artifact) => {
return artifact.name == name
})
if (!artifact) {
continue
}
}
}
runID = run.id
core.info(`==> (found) Run ID: ${runID}`)
core.info(`==> (found) Run date: ${run.created_at}`)
break
}
if (runID) {
break
}
}
}
if (!runID) {
if (workflowConclusion && (workflowConclusion != 'in_progress')) {
return setExitMessage(ifNoArtifactFound, "no matching workflow run found with any artifacts?")
}
try {
return await downloadAction(name, path)
} catch (error) {
return setExitMessage(ifNoArtifactFound, "no matching artifact in this workflow?")
}
}
let artifacts = await client.paginate(client.rest.actions.listWorkflowRunArtifacts, {
owner: owner,
repo: repo,
run_id: runID,
})
// One artifact or all if `name` input is not specified.
if (name) {
filtered = artifacts.filter((artifact) => {
return artifact.name == name
})
if (filtered.length == 0) {
core.info(`==> (not found) Artifact: ${name}`)
core.info('==> Found the following artifacts instead:')
for (const artifact of artifacts) {
core.info(`\t==> (found) Artifact: ${artifact.name}`)
}
}
artifacts = filtered
}
core.setOutput("artifacts", artifacts)
if (dryRun) {
if (artifacts.length == 0) {
core.setOutput("dry_run", false)
core.setOutput("found_artifact", false)
return
} else {
core.setOutput("dry_run", true)
core.setOutput("found_artifact", true)
core.info('==> (found) Artifacts')
for (const artifact of artifacts) {
const size = filesize(artifact.size_in_bytes, { base: 10 })
core.info(`\t==> Artifact:`)
core.info(`\t==> ID: ${artifact.id}`)
core.info(`\t==> Name: ${artifact.name}`)
core.info(`\t==> Size: ${size}`)
}
return
}
}
if (artifacts.length == 0) {
return setExitMessage(ifNoArtifactFound, "no artifacts found")
}
core.setOutput("found_artifact", true)
for (const artifact of artifacts) {
core.info(`==> Artifact: ${artifact.id}`)
const size = filesize(artifact.size_in_bytes, { base: 10 })
core.info(`==> Downloading: ${artifact.name}.zip (${size})`)
let zip
try {
zip = await client.rest.actions.downloadArtifact({
owner: owner,
repo: repo,
artifact_id: artifact.id,
archive_format: "zip",
})
} catch (error) {
if (error.message === "Artifact has expired") {
return setExitMessage(ifNoArtifactFound, "no downloadable artifacts found (expired)")
} else {
throw new Error(error.message)
}
}
if (skipUnpack) {
fs.mkdirSync(path, { recursive: true })
fs.writeFileSync(`${pathname.join(path, artifact.name)}.zip`, Buffer.from(zip.data), 'binary')
continue
}
const dir = name ? path : pathname.join(path, artifact.name)
fs.mkdirSync(dir, { recursive: true })
const adm = new AdmZip(Buffer.from(zip.data))
core.startGroup(`==> Extracting: ${artifact.name}.zip`)
adm.getEntries().forEach((entry) => {
const action = entry.isDirectory ? "creating" : "inflating"
const filepath = pathname.join(dir, entry.entryName)
core.info(` ${action}: ${filepath}`)
})
adm.extractAllTo(dir, true)
core.endGroup()
}
} catch (error) {
core.setOutput("found_artifact", false)
core.setOutput("error_message", error.message)
core.setFailed(error.message)
}
function setExitMessage(ifNoArtifactFound, message) {
core.setOutput("found_artifact", false)
switch (ifNoArtifactFound) {
case "fail":
core.setFailed(message)
break
case "warn":
core.warning(message)
break
case "ignore":
default:
core.info(message)
break
}
}
}
main()