diff --git a/main.js b/main.js index 8e2daf8..4db532b 100644 --- a/main.js +++ b/main.js @@ -3,70 +3,65 @@ const github = require('@actions/github') 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 name = core.getInput("name") - let workflowConclusion = core.getInput("workflow_conclusion") - let runID = core.getInput("run_id") - const client = github.getOctokit(token) - + const token = core.getInput("github_token") || process.env.GITHUB_TOKEN; + const workflow = core.getInput("workflow", { required: true }); + const repo = core.getInput("repo") || process.env.GITHUB_REPOSITORY; + const [owner, repository] = repo.split("/"); + const name = core.getInput("name"); + let workflowConclusion = core.getInput("workflow_conclusion") || 'success'; + let runID = core.getInput("run_id"); + const client = github.getOctokit(token); if (!runID) { for await (const runs of client.paginate.iterator(client.rest.actions.listWorkflowRuns, { owner: owner, - repo: repo, + repo: repository, workflow_id: workflow - } - )) { + })) { for (const run of runs.data) { if (workflowConclusion && (workflowConclusion != run.conclusion && workflowConclusion != run.status)) { - continue + continue; } - runID = run.id - break + runID = run.id; + break; } if (runID) { - break + break; } } } if (!runID) { - throw new Error("no matching workflow run found") + throw new Error("no matching workflow run found"); } let artifacts = await client.paginate(client.rest.actions.listWorkflowRunArtifacts, { owner: owner, - repo: repo, + repo: repository, run_id: runID, - }) + }); - // One artifact or all if `name` input is not specified. if (name) { - artifacts = artifacts.filter((artifact) => { - return artifact.name == name - }) + artifacts = artifacts.filter((artifact) => artifact.name === name); } - if (artifacts.length == 0) - throw new Error("no artifacts found") - + if (artifacts.length === 0) { + throw new Error("no artifacts found"); + } + for (const artifact of artifacts) { - console.log("==> Artifact:", artifact.id) - + console.log("==> Deleting artifact:", artifact.id); await client.rest.actions.deleteArtifact({ owner: owner, - repo: repo, + repo: repository, artifact_id: artifact.id, - }) - + }); } + } catch (error) { - core.setFailed(error.message) + core.setFailed(error.message); } } -main() - +main();