5 changed files with 176 additions and 159 deletions
@ -0,0 +1,134 @@ |
|||||||
|
const github = require('@actions/github') |
||||||
|
const AdmZip = require('adm-zip') |
||||||
|
const filesize = require('filesize') |
||||||
|
const pathname = require('path') |
||||||
|
const _ = require('underscore') |
||||||
|
const fs = require('fs') |
||||||
|
|
||||||
|
const artifactInputs = require('./artifactInput.js') |
||||||
|
|
||||||
|
|
||||||
|
const getRunId = async (client, inputs) => { |
||||||
|
if (inputs.runID) { |
||||||
|
console.log("==> RunID:", inputs.runID) |
||||||
|
} else { |
||||||
|
const restInputs = { |
||||||
|
owner: inputs.owner, |
||||||
|
repo: inputs.repo, |
||||||
|
workflow_id: inputs.workflow, |
||||||
|
branch: inputs.branch, |
||||||
|
event: inputs.event, |
||||||
|
} |
||||||
|
|
||||||
|
const workflowRun = _.chain(client.paginate(client.rest.listWorkflowRuns, restInputs)) |
||||||
|
.filter(run => { |
||||||
|
return ( |
||||||
|
(inputs.commit && run.head_sha != inputs.commit) |
||||||
|
&& (inputs.runNumber && run.run_number != inputs.runNumber) |
||||||
|
&& (inputs.workflowConclusion && (inputs.workflowConclusion != run.conclusion && inputs.workflowConclusion != run.status)) |
||||||
|
) |
||||||
|
}) |
||||||
|
.value() |
||||||
|
|
||||||
|
console.log(`runs: ${workflowRun}`) |
||||||
|
|
||||||
|
//.filter(run => {
|
||||||
|
// const artifacts = clients.rest.actions.listWorkflowRunArtifacts({
|
||||||
|
// owner: inputs.owner,
|
||||||
|
// repo: inputs.repo,
|
||||||
|
// run_id: run.id
|
||||||
|
// })
|
||||||
|
// return artifacts.data.artifacts.length > 0 ? true : false
|
||||||
|
//})
|
||||||
|
//.first()
|
||||||
|
//.value()
|
||||||
|
|
||||||
|
if (!workflowRun) |
||||||
|
throw new Error("cannot find the workflow") |
||||||
|
|
||||||
|
return workflowRun.id
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
const run = async inputs => { |
||||||
|
try { |
||||||
|
const client = github.getOctokit(token) |
||||||
|
|
||||||
|
_.chain(inputs) |
||||||
|
.keys() |
||||||
|
.each(key => { |
||||||
|
if (key) |
||||||
|
console.log(`==> ${key}: ${inputs[key]}`) |
||||||
|
}) |
||||||
|
.value() |
||||||
|
|
||||||
|
if (inputs.pr) { |
||||||
|
console.log("==> PR:", pr) |
||||||
|
|
||||||
|
const pull = await client.pulls.get({ |
||||||
|
owner: owner, |
||||||
|
repo: repo, |
||||||
|
pull_number: pr, |
||||||
|
}) |
||||||
|
commit = pull.data.head.sha |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
const runId = getRunId(client, inputs) |
||||||
|
|
||||||
|
const allArtifacts = await client.paginate(client.rest.actions.listWorkflowRunArtifacts, { |
||||||
|
owner: owner, |
||||||
|
repo: repo, |
||||||
|
run_id: runID, |
||||||
|
}) |
||||||
|
|
||||||
|
const { artifactsToDownload, renameCheckErrors } = artifactInputs.getListOfArtifactsToDownload(allArtifacts, artifactNamesFromAction) |
||||||
|
|
||||||
|
if (artifactsToDownload.length == 0) |
||||||
|
throw new Error("no artifacts found") |
||||||
|
|
||||||
|
if (renameCheckErrors.length > 0) { |
||||||
|
_.each(renameCheckErrors, element => { |
||||||
|
console.log(`==> Multiple matches on "${element.namePattern}". Cannot use the artifact rename functionality`) |
||||||
|
}) |
||||||
|
|
||||||
|
throw new Error("!!! trying to use the artifact rename functionality on a glob pattern with multiple matches") |
||||||
|
} |
||||||
|
|
||||||
|
for (const artifact of artifactsToDownload) { |
||||||
|
console.log("==> Artifact:", artifact.id) |
||||||
|
|
||||||
|
const size = filesize(artifact.size_in_bytes, { base: 10 }) |
||||||
|
|
||||||
|
console.log(`==> Downloading: ${artifact.name}.zip (${size})`) |
||||||
|
|
||||||
|
const zip = await client.actions.downloadArtifact({ |
||||||
|
owner: owner, |
||||||
|
repo: repo, |
||||||
|
artifact_id: artifact.id, |
||||||
|
archive_format: "zip", |
||||||
|
}) |
||||||
|
|
||||||
|
//const dir = artifacts.length == 1 ? pathFromAction : pathname.join(path, artifact.name)
|
||||||
|
|
||||||
|
fs.mkdirSync(pathFromAction, { recursive: true }) |
||||||
|
|
||||||
|
const adm = new AdmZip(Buffer.from(zip.data)) |
||||||
|
|
||||||
|
adm.getEntries().forEach((entry) => { |
||||||
|
const action = entry.isDirectory ? "creating" : "inflating" |
||||||
|
const filepath = pathname.join(pathFromAction, entry.entryName) |
||||||
|
|
||||||
|
console.log(` ${action}: ${filepath}`) |
||||||
|
}) |
||||||
|
|
||||||
|
adm.extractAllTo(pathFromAction, true) |
||||||
|
} |
||||||
|
} catch (error) { |
||||||
|
core.setFailed(error.message) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
module.exports = { |
||||||
|
getRunId: getRunId |
||||||
|
} |
||||||
@ -1,160 +1,26 @@ |
|||||||
const core = require('@actions/core') |
const core = require('@actions/core') |
||||||
const github = require('@actions/github') |
|
||||||
const AdmZip = require('adm-zip') |
|
||||||
const filesize = require('filesize') |
|
||||||
const pathname = require('path') |
|
||||||
const _ = require('underscore') |
|
||||||
const fs = require('fs') |
|
||||||
|
|
||||||
const handleArtifacts = require('./handleArtifacts.js') |
const action = require('./action.js') |
||||||
|
|
||||||
async function main() { |
const main = async () => { |
||||||
try { |
const inputs = { |
||||||
const token = core.getInput("github_token", { required: true }) |
token: core.getInput("github_token", { required: true }), |
||||||
const workflow = core.getInput("workflow", { required: true }) |
workflow: core.getInput("workflow", { required: true }), |
||||||
const [owner, repo] = core.getInput("repo", { required: true }).split("/") |
owner: core.getInput("repo", { required: true }).split("/")[0], |
||||||
const pathFromAction = core.getInput("path", { required: true }) |
repo: core.getInput("repo", { required: true }).split("/")[1], |
||||||
const artifactNamesFromAction = core.getInput("artifacts") ? core.getInput("artifacts") : "*" |
pathFromAction: core.getInput("path", { required: true }), |
||||||
let workflowConclusion = core.getInput("workflow_conclusion") |
artifactNamesFromAction: core.getInput("artifacts") ? core.getInput("artifacts") : "*", |
||||||
let pr = core.getInput("pr") |
workflowConclusion: core.getInput("workflow_conclusion"), |
||||||
let commit = core.getInput("commit") |
pr: core.getInput("pr"), |
||||||
let branch = core.getInput("branch") |
commit: core.getInput("commit"), |
||||||
let event = core.getInput("event") |
branch: core.getInput("branch"), |
||||||
let runID = core.getInput("run_id") |
event: core.getInput("event"), |
||||||
let runNumber = core.getInput("run_number") |
runID: core.getInput("run_id"), |
||||||
let checkArtifacts = core.getInput("check_artifacts") |
runNumber: core.getInput("run_number"), |
||||||
|
checkArtifacts: core.getInput("check_artifacts") |
||||||
const client = github.getOctokit(token) |
|
||||||
|
|
||||||
console.log("==> Workflow:", workflow) |
|
||||||
|
|
||||||
console.log("==> Repo:", owner + "/" + repo) |
|
||||||
|
|
||||||
console.log("==> Conclusion:", workflowConclusion) |
|
||||||
|
|
||||||
if (pr) { |
|
||||||
console.log("==> PR:", pr) |
|
||||||
|
|
||||||
const pull = await client.pulls.get({ |
|
||||||
owner: owner, |
|
||||||
repo: repo, |
|
||||||
pull_number: pr, |
|
||||||
}) |
|
||||||
commit = pull.data.head.sha |
|
||||||
} |
|
||||||
|
|
||||||
if (commit) { |
|
||||||
console.log("==> Commit:", commit) |
|
||||||
} |
|
||||||
|
|
||||||
if (branch) { |
|
||||||
branch = branch.replace(/^refs\/heads\//, "") |
|
||||||
console.log("==> Branch:", branch) |
|
||||||
} |
|
||||||
|
|
||||||
if (event) { |
|
||||||
console.log("==> Event:", event) |
|
||||||
} |
|
||||||
|
|
||||||
if (runNumber) { |
|
||||||
console.log("==> RunNumber:", runNumber) |
|
||||||
} |
|
||||||
|
|
||||||
if (!runID) { |
|
||||||
for await (const runs of client.paginate.iterator(client.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) { |
|
||||||
let artifacts = await client.actions.listWorkflowRunArtifacts({ |
|
||||||
owner: owner, |
|
||||||
repo: repo, |
|
||||||
run_id: run.id, |
|
||||||
}) |
|
||||||
if (artifacts.data.artifacts.length == 0) { |
|
||||||
continue |
|
||||||
} |
|
||||||
} |
|
||||||
runID = run.id |
|
||||||
break |
|
||||||
} |
|
||||||
if (runID) { |
|
||||||
break |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
if (runID) { |
|
||||||
console.log("==> RunID:", runID) |
|
||||||
} else { |
|
||||||
throw new Error("no matching workflow run found") |
|
||||||
} |
|
||||||
|
|
||||||
const allArtifacts = await client.paginate(client.actions.listWorkflowRunArtifacts, { |
|
||||||
owner: owner, |
|
||||||
repo: repo, |
|
||||||
run_id: runID, |
|
||||||
}) |
|
||||||
|
|
||||||
const { artifactsToDownload, renameCheckErrors } = handleArtifacts.getListOfArtifactsToDownload(allArtifacts, artifactNamesFromAction) |
|
||||||
|
|
||||||
if (artifactsToDownload.length == 0) |
|
||||||
throw new Error("no artifacts found") |
|
||||||
|
|
||||||
if (renameCheckErrors.length > 0) { |
|
||||||
_.each(renameCheckErrors, element => { |
|
||||||
console.log(`==> Multiple matches on "${element.namePattern}". Cannot use the artifact rename functionality`) |
|
||||||
}) |
|
||||||
|
|
||||||
throw new Error("!!! trying to use the artifact rename functionality on a glob pattern with multiple matches") |
|
||||||
} |
|
||||||
|
|
||||||
for (const artifact of artifactsToDownload) { |
|
||||||
console.log("==> Artifact:", artifact.id) |
|
||||||
|
|
||||||
const size = filesize(artifact.size_in_bytes, { base: 10 }) |
|
||||||
|
|
||||||
console.log(`==> Downloading: ${artifact.name}.zip (${size})`) |
|
||||||
|
|
||||||
const zip = await client.actions.downloadArtifact({ |
|
||||||
owner: owner, |
|
||||||
repo: repo, |
|
||||||
artifact_id: artifact.id, |
|
||||||
archive_format: "zip", |
|
||||||
}) |
|
||||||
|
|
||||||
//const dir = artifacts.length == 1 ? pathFromAction : pathname.join(path, artifact.name)
|
|
||||||
|
|
||||||
fs.mkdirSync(pathFromAction, { recursive: true }) |
|
||||||
|
|
||||||
const adm = new AdmZip(Buffer.from(zip.data)) |
|
||||||
|
|
||||||
adm.getEntries().forEach((entry) => { |
|
||||||
const action = entry.isDirectory ? "creating" : "inflating" |
|
||||||
const filepath = pathname.join(pathFromAction, entry.entryName) |
|
||||||
|
|
||||||
console.log(` ${action}: ${filepath}`) |
|
||||||
}) |
|
||||||
|
|
||||||
adm.extractAllTo(pathFromAction, true) |
|
||||||
} |
|
||||||
} catch (error) { |
|
||||||
core.setFailed(error.message) |
|
||||||
} |
} |
||||||
|
|
||||||
|
action.run(inputs) |
||||||
} |
} |
||||||
|
|
||||||
main() |
main() |
||||||
|
|||||||
@ -0,0 +1,17 @@ |
|||||||
|
const action = require('../action.js') |
||||||
|
const github = require('@actions/github') |
||||||
|
|
||||||
|
|
||||||
|
const GITHUB_TOKEN = process.env.GITHUB_TOKEN |
||||||
|
|
||||||
|
test('single artifact filtering', async () => { |
||||||
|
const client = github.getOctokit(GITHUB_TOKEN) |
||||||
|
const inputs = { |
||||||
|
owner: "bitwarden", |
||||||
|
repo: "gh-actions", |
||||||
|
workflow_id: "upload-test-artifacts.yml" |
||||||
|
} |
||||||
|
|
||||||
|
//expect(await action.getRunId(client, inputs)).toStrictEqual(expect.any(Number))
|
||||||
|
expect(await action.getRunId(client, inputs)).toStrictEqual(1) |
||||||
|
}) |
||||||
Loading…
Reference in new issue