|
| 1 | +import * as core from "@actions/core"; |
| 2 | +import * as github from "@actions/github"; |
| 3 | +import { DefaultArtifactClient } from "@actions/artifact"; |
| 4 | +import * as filepath from "node:path"; |
| 5 | + |
| 6 | +async function main() { |
| 7 | + const ghToken = core.getInput("github-token", { required: true }); |
| 8 | + const workflowId = core.getInput("workflow-id", { required: true }); |
| 9 | + const [repoOwner, repoName] = core |
| 10 | + .getInput("repository", { required: true }) |
| 11 | + .split("/"); |
| 12 | + const prNumber = parseInt(core.getInput("pr-number", { required: true }), 10); |
| 13 | + const artifactName = core.getInput("artifact-name", { required: true }); |
| 14 | + let path = core.getInput("path", { required: false }); |
| 15 | + |
| 16 | + if (!path) { |
| 17 | + const workspace = process.env.GITHUB_WORKSPACE || process.cwd(); |
| 18 | + if (!workspace) { |
| 19 | + throw new Error("GITHUB_WORKSPACE is not set"); |
| 20 | + } |
| 21 | + path = workspace; |
| 22 | + } |
| 23 | + |
| 24 | + path = filepath.join(path, artifactName); |
| 25 | + |
| 26 | + const client = github.getOctokit(ghToken); |
| 27 | + |
| 28 | + const { data } = await client.rest.pulls.get({ |
| 29 | + owner: repoOwner, |
| 30 | + repo: repoName, |
| 31 | + pull_number: prNumber, |
| 32 | + }); |
| 33 | + |
| 34 | + const headSha = data.head.sha; |
| 35 | + // Now let's get all workflows associated with this sha. |
| 36 | + const { data: runs } = await client.rest.actions.listWorkflowRuns({ |
| 37 | + repo: repoName, |
| 38 | + owner: repoOwner, |
| 39 | + head_sha: headSha, |
| 40 | + workflow_id: workflowId, |
| 41 | + status: "completed", |
| 42 | + }); |
| 43 | + |
| 44 | + if (runs.workflow_runs.length <= 0) { |
| 45 | + throw new Error(`No workflow runs found for sha ${headSha}`); |
| 46 | + } |
| 47 | + |
| 48 | + const latestRun = runs.workflow_runs[0]; |
| 49 | + core.info(`Latest run: ${latestRun.id} <${latestRun.html_url}>`); |
| 50 | + |
| 51 | + // Now that we have the run we can get a list of all artifacts there: |
| 52 | + const { data: artifacts } = |
| 53 | + await client.rest.actions.listWorkflowRunArtifacts({ |
| 54 | + owner: repoOwner, |
| 55 | + repo: repoName, |
| 56 | + run_id: latestRun.id, |
| 57 | + }); |
| 58 | + |
| 59 | + const artifact = new DefaultArtifactClient(); |
| 60 | + const candidates = artifacts.artifacts.filter( |
| 61 | + (artifact) => artifact.name == artifactName, |
| 62 | + ); |
| 63 | + if (candidates.length <= 0) { |
| 64 | + throw new Error(`No artifacts found with name ${artifactName}`); |
| 65 | + } |
| 66 | + |
| 67 | + const response = await artifact.downloadArtifact(candidates[0].id, { |
| 68 | + path: path, |
| 69 | + findBy: { |
| 70 | + repositoryName: repoName, |
| 71 | + repositoryOwner: repoOwner, |
| 72 | + workflowRunId: latestRun.id, |
| 73 | + token: ghToken, |
| 74 | + }, |
| 75 | + }); |
| 76 | + core.setOutput("artifact-download-path", response.downloadPath); |
| 77 | + core.setOutput("artifact-id", candidates[0].id); |
| 78 | + core.setOutput("workflow-run-id", latestRun.id); |
| 79 | +} |
| 80 | + |
| 81 | +main(); |
0 commit comments