forked from philips-labs/action-delete-artifacts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
74 lines (62 loc) · 2.18 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
const core = require('@actions/core')
const github = require('@actions/github')
async function main() {
try {
const token = core.getInput("github_token", { required: true })
const workflow = core.getInput("workflow")
const [owner, repo] = core.getInput("repo", { required: true }).split("/")
const name = core.getInput("name")
let workflowConclusion = core.getInput("workflow_conclusion")
let runID = core.getInput("run_id")
const client = github.getOctokit(token)
if(!workflow && !runID) {
throw new Error("neither workflow nor runID was specified")
}
if (!runID) {
for await (const runs of client.paginate.iterator(client.rest.actions.listWorkflowRuns, {
owner: owner,
repo: repo,
workflow_id: workflow
}
)) {
for (const run of runs.data) {
if (workflowConclusion && (workflowConclusion != run.conclusion && workflowConclusion != run.status)) {
continue
}
runID = run.id
break
}
if (runID) {
break
}
}
}
if (!runID) {
throw new Error("no matching workflow run found")
}
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) {
artifacts = artifacts.filter((artifact) => {
return artifact.name == name
})
}
if (artifacts.length == 0)
throw new Error("no artifacts found")
for (const artifact of artifacts) {
console.log("==> Artifact:", artifact.id)
await client.rest.actions.deleteArtifact({
owner: owner,
repo: repo,
artifact_id: artifact.id,
})
}
} catch (error) {
core.setFailed(error.message)
}
}
main()