Merge pull request #242 from actions/v4-beta-internal-client

Use internal client when `github-token` is not specified
This commit is contained in:
Rob Herley 2023-12-04 13:20:07 -05:00 committed by GitHub
commit 8b83831f82
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 830 additions and 133 deletions

View file

@ -9,16 +9,18 @@ inputs:
description: 'Destination path' description: 'Destination path'
required: false required: false
github-token: github-token:
description: The GitHub token used to download the artifact description: 'The GitHub token used to authenticate with the GitHub API.
default: ${{ github.token }} 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 required: false
repository: repository:
description: 'The repository owner and the repository name joined together by "/". 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 required: false
default: ${{ github.repository }} default: ${{ github.repository }}
run-id: 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 required: false
default: ${{ github.run_id }} default: ${{ github.run_id }}
outputs: outputs:

899
dist/index.js vendored

File diff suppressed because it is too large Load diff

View file

@ -17,9 +17,9 @@ async function run(): Promise<void> {
const inputs = { const inputs = {
name: core.getInput(Inputs.Name, {required: false}), name: core.getInput(Inputs.Name, {required: false}),
path: core.getInput(Inputs.Path, {required: false}), path: core.getInput(Inputs.Path, {required: false}),
token: core.getInput(Inputs.GitHubToken, {required: true}), token: core.getInput(Inputs.GitHubToken, {required: false}),
repository: core.getInput(Inputs.Repository, {required: true}), repository: core.getInput(Inputs.Repository, {required: false}),
runID: parseInt(core.getInput(Inputs.RunID, {required: true})) runID: parseInt(core.getInput(Inputs.RunID, {required: false}))
} }
if (!inputs.path) { if (!inputs.path) {
@ -30,15 +30,25 @@ async function run(): Promise<void> {
inputs.path = inputs.path.replace('~', os.homedir()) inputs.path = inputs.path.replace('~', os.homedir())
} }
const isSingleArtifactDownload: boolean = !!inputs.name const isSingleArtifactDownload = !!inputs.name
const resolvedPath = path.resolve(inputs.path) const resolvedPath = path.resolve(inputs.path)
core.debug(`Resolved path is ${resolvedPath}`) core.debug(`Resolved path is ${resolvedPath}`)
const [owner, repo] = inputs.repository.split('/') const options: artifact.FindOptions = {}
if (!owner || !repo) { if (inputs.token) {
throw new Error( const [repositoryOwner, repositoryName] = inputs.repository.split('/')
`Invalid repository: '${inputs.repository}'. Must be in format owner/repo` 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() const artifactClient = artifact.create()
@ -49,10 +59,7 @@ async function run(): Promise<void> {
const {artifact: targetArtifact} = await artifactClient.getArtifact( const {artifact: targetArtifact} = await artifactClient.getArtifact(
inputs.name, inputs.name,
inputs.runID, options
owner,
repo,
inputs.token
) )
if (!targetArtifact) { if (!targetArtifact) {
@ -65,15 +72,15 @@ async function run(): Promise<void> {
artifacts = [targetArtifact] artifacts = [targetArtifact]
} else { } else {
core.info(`No input name specified, downloading all artifacts. Extra directory with the artifact name will be created for each download`) core.info(
`No input name specified, downloading all artifacts. Extra directory with the artifact name will be created for each download`
const listArtifactResponse = await artifactClient.listArtifacts(
inputs.runID,
owner,
repo,
inputs.token
) )
const listArtifactResponse = await artifactClient.listArtifacts({
latest: true,
...options
})
if (listArtifactResponse.artifacts.length === 0) { if (listArtifactResponse.artifacts.length === 0) {
throw new Error( throw new Error(
`No artifacts found for run '${inputs.runID}' in '${inputs.repository}'` `No artifacts found for run '${inputs.runID}' in '${inputs.repository}'`
@ -85,8 +92,11 @@ async function run(): Promise<void> {
} }
const downloadPromises = artifacts.map(artifact => const downloadPromises = artifacts.map(artifact =>
artifactClient.downloadArtifact(artifact.id, owner, repo, inputs.token, { artifactClient.downloadArtifact(artifact.id, {
path: isSingleArtifactDownload ? resolvedPath : path.join(resolvedPath, artifact.name) ...options,
path: isSingleArtifactDownload
? resolvedPath
: path.join(resolvedPath, artifact.name)
}) })
) )