From 6ee005d6b75ac7940c60a89c0f837f31753245ec Mon Sep 17 00:00:00 2001 From: Rob Herley Date: Thu, 30 Nov 2023 21:37:06 -0500 Subject: [PATCH] adopt new internal artifact api behavior --- action.yml | 12 +++++++----- src/download-artifact.ts | 41 +++++++++++++++++++++------------------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/action.yml b/action.yml index 898a840..7b37ab7 100644 --- a/action.yml +++ b/action.yml @@ -9,21 +9,23 @@ inputs: description: 'Destination path' required: false github-token: - description: The GitHub token used to download the artifact - default: ${{ github.token }} + description: 'The GitHub token used to authenticate with the GitHub API. + This is required when downloading artifacts from a different repository or from a different workflow run. + If this is not specified, the action will attempt to download artifacts from the current repository and the current workflow run.' required: false repository: description: 'The repository owner and the repository name joined together by "/". - This specifies the repository that artifacts will be downloaded from. If downloading artifacts from external workflow runs or repositories then the above download-token must be permissions to this repository.' + If github-token is specified, this is the repository that artifacts will be downloaded from.' required: false default: ${{ github.repository }} run-id: - description: 'The id of the workflow run where the desired download artifact was uploaded from. If downloading artifacts from anything other than the current workflow run then this needs to be overwritten.' + description: 'The id of the workflow run where the desired download artifact was uploaded from. + If github-token is specified, this is the run that artifacts will be downloaded from.' required: false default: ${{ github.run_id }} outputs: download-path: description: 'Path of artifact download' runs: - using: 'node16' + using: 'node20' main: 'dist/index.js' diff --git a/src/download-artifact.ts b/src/download-artifact.ts index 2cdb916..4217393 100644 --- a/src/download-artifact.ts +++ b/src/download-artifact.ts @@ -17,9 +17,9 @@ async function run(): Promise { const inputs = { name: core.getInput(Inputs.Name, {required: false}), path: core.getInput(Inputs.Path, {required: false}), - token: core.getInput(Inputs.GitHubToken, {required: true}), - repository: core.getInput(Inputs.Repository, {required: true}), - runID: parseInt(core.getInput(Inputs.RunID, {required: true})) + token: core.getInput(Inputs.GitHubToken, {required: false}), + repository: core.getInput(Inputs.Repository, {required: false}), + runID: parseInt(core.getInput(Inputs.RunID, {required: false})) } if (!inputs.path) { @@ -33,11 +33,21 @@ async function run(): Promise { const resolvedPath = path.resolve(inputs.path) core.debug(`Resolved path is ${resolvedPath}`) - const [owner, repo] = inputs.repository.split('/') - if (!owner || !repo) { - throw new Error( - `Invalid repository: '${inputs.repository}'. Must be in format owner/repo` - ) + const options: artifact.FindOptions = {} + if (inputs.token) { + const [repositoryOwner, repositoryName] = inputs.repository.split('/') + if (!repositoryOwner || !repositoryName) { + throw new Error( + `Invalid repository: '${inputs.repository}'. Must be in format owner/repo` + ) + } + + options.findBy = { + token: inputs.token, + workflowRunId: inputs.runID, + repositoryName, + repositoryOwner + } } const artifactClient = artifact.create() @@ -46,10 +56,7 @@ async function run(): Promise { if (inputs.name) { const {artifact: targetArtifact} = await artifactClient.getArtifact( inputs.name, - inputs.runID, - owner, - repo, - inputs.token + options ) if (!targetArtifact) { @@ -62,12 +69,7 @@ async function run(): Promise { artifacts = [targetArtifact] } else { - const listArtifactResponse = await artifactClient.listArtifacts( - inputs.runID, - owner, - repo, - inputs.token - ) + const listArtifactResponse = await artifactClient.listArtifacts(options) if (listArtifactResponse.artifacts.length === 0) { throw new Error( @@ -80,7 +82,8 @@ async function run(): Promise { } const downloadPromises = artifacts.map(artifact => - artifactClient.downloadArtifact(artifact.id, owner, repo, inputs.token, { + artifactClient.downloadArtifact(artifact.id, { + ...options, path: path.join(resolvedPath, artifact.name) }) )