Browse Source

trying to get the script back to a usable state with the octokit api changes

feature/add-renaming-download-action
Joseph Flinn 4 years ago
parent
commit
e03f022998
  1. 134
      download-artifacts/action.js
  2. 0
      download-artifacts/artifactInput.js
  3. 174
      download-artifacts/index.js
  4. 17
      download-artifacts/tests/action.test.js
  5. 10
      download-artifacts/tests/artifactInput.test.js

134
download-artifacts/action.js

@ -0,0 +1,134 @@ @@ -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
}

0
download-artifacts/handleArtifacts.js → download-artifacts/artifactInput.js

174
download-artifacts/index.js

@ -1,160 +1,26 @@ @@ -1,160 +1,26 @@
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')
async function main() {
try {
const token = core.getInput("github_token", { required: true })
const workflow = core.getInput("workflow", { required: true })
const [owner, repo] = core.getInput("repo", { required: true }).split("/")
const pathFromAction = core.getInput("path", { required: true })
const artifactNamesFromAction = core.getInput("artifacts") ? core.getInput("artifacts") : "*"
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.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)
const action = require('./action.js')
const main = async () => {
const inputs = {
token: core.getInput("github_token", { required: true }),
workflow: core.getInput("workflow", { required: true }),
owner: core.getInput("repo", { required: true }).split("/")[0],
repo: core.getInput("repo", { required: true }).split("/")[1],
pathFromAction: core.getInput("path", { required: true }),
artifactNamesFromAction: core.getInput("artifacts") ? core.getInput("artifacts") : "*",
workflowConclusion: core.getInput("workflow_conclusion"),
pr: core.getInput("pr"),
commit: core.getInput("commit"),
branch: core.getInput("branch"),
event: core.getInput("event"),
runID: core.getInput("run_id"),
runNumber: core.getInput("run_number"),
checkArtifacts: core.getInput("check_artifacts")
}
action.run(inputs)
}
main()

17
download-artifacts/tests/action.test.js

@ -0,0 +1,17 @@ @@ -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)
})

10
download-artifacts/test.js → download-artifacts/tests/artifactInput.test.js

@ -1,4 +1,4 @@ @@ -1,4 +1,4 @@
const handleArtifacts = require('./handleArtifacts.js')
const artifactInput = require('../artifactInput.js')
const allArtifacts = [
@ -12,7 +12,7 @@ const allArtifacts = [ @@ -12,7 +12,7 @@ const allArtifacts = [
test('single artifact filtering', () => {
const artifactFilters = `artifact1.txt`
expect(handleArtifacts.getListOfArtifactsToDownload(allArtifacts, artifactFilters)).toStrictEqual({
expect(artifactInput.getListOfArtifactsToDownload(allArtifacts, artifactFilters)).toStrictEqual({
artifactsToDownload: [{ "id": 1234, "name": "artifact1.txt", "size_in_bytes": 42, "downloadName": "artifact1.txt" }],
errors: []
})
@ -23,7 +23,7 @@ test('multiple artifact filtering', () => { @@ -23,7 +23,7 @@ test('multiple artifact filtering', () => {
artifact2.txt,
dump.zip`
expect(handleArtifacts.getListOfArtifactsToDownload(allArtifacts, artifactFilters)).toStrictEqual({
expect(artifactInput.getListOfArtifactsToDownload(allArtifacts, artifactFilters)).toStrictEqual({
artifactsToDownload: [
{ "id": 1234, "name": "artifact1.txt", "size_in_bytes": 42, downloadName: "artifact1.txt" },
{ "id": 1235, "name": "artifact2.txt", "size_in_bytes": 42, downloadName: "artifact2.txt" },
@ -36,7 +36,7 @@ test('multiple artifact filtering', () => { @@ -36,7 +36,7 @@ test('multiple artifact filtering', () => {
test('single artifact filtering with rename', () => {
const artifactFilters = `artifact1.txt|renamed_artifact1.txt`
expect(handleArtifacts.getListOfArtifactsToDownload(allArtifacts, artifactFilters)).toStrictEqual({
expect(artifactInput.getListOfArtifactsToDownload(allArtifacts, artifactFilters)).toStrictEqual({
artifactsToDownload: [{ "id": 1234, "name": "artifact1.txt", "size_in_bytes": 42, downloadName: "renamed_artifact1.txt" }],
errors: []
})
@ -47,7 +47,7 @@ test('multiple artifact filtering with rename', () => { @@ -47,7 +47,7 @@ test('multiple artifact filtering with rename', () => {
artifact2.txt|renamed_artifact2.txt,
dump.zip|renamed_dump.zip`
expect(handleArtifacts.getListOfArtifactsToDownload(allArtifacts, artifactFilters)).toStrictEqual({
expect(artifactInput.getListOfArtifactsToDownload(allArtifacts, artifactFilters)).toStrictEqual({
artifactsToDownload: [
{ "id": 1234, "name": "artifact1.txt", "size_in_bytes": 42, downloadName: "renamed_artifact1.txt" },
{ "id": 1235, "name": "artifact2.txt", "size_in_bytes": 42, downloadName: "renamed_artifact2.txt" },
Loading…
Cancel
Save