From 0555a5f45852d405b1093aebc39b6485ec6bc1c5 Mon Sep 17 00:00:00 2001 From: Bethany Date: Tue, 22 Aug 2023 09:17:43 -0700 Subject: [PATCH] add get-artifact logic --- packages/artifact/package-lock.json | 9 +++ packages/artifact/package.json | 1 + .../internal/download/download-artifact.ts | 2 +- .../src/internal/find/get-artifact.ts | 66 ++++++++++++++++++- 4 files changed, 75 insertions(+), 3 deletions(-) diff --git a/packages/artifact/package-lock.json b/packages/artifact/package-lock.json index 90338dff..d9e415c3 100644 --- a/packages/artifact/package-lock.json +++ b/packages/artifact/package-lock.json @@ -17,6 +17,7 @@ "@octokit/plugin-request-log": "^1.0.4", "@octokit/plugin-retry": "^3.0.9", "@protobuf-ts/plugin": "^2.2.3-alpha.1", + "@types/unzipper": "^0.10.6", "archiver": "^5.3.1", "crypto": "^1.0.1", "jwt-decode": "^3.1.2", @@ -441,6 +442,14 @@ "@types/node": "*" } }, + "node_modules/@types/unzipper": { + "version": "0.10.6", + "resolved": "https://registry.npmjs.org/@types/unzipper/-/unzipper-0.10.6.tgz", + "integrity": "sha512-zcBj329AHgKLQyz209N/S9R0GZqXSkUQO4tJSYE3x02qg4JuDFpgKMj50r82Erk1natCWQDIvSccDddt7jPzjA==", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/archiver": { "version": "5.3.1", "resolved": "https://registry.npmjs.org/archiver/-/archiver-5.3.1.tgz", diff --git a/packages/artifact/package.json b/packages/artifact/package.json index b6d0ae1c..e2ebc939 100644 --- a/packages/artifact/package.json +++ b/packages/artifact/package.json @@ -47,6 +47,7 @@ "@octokit/plugin-request-log": "^1.0.4", "@octokit/plugin-retry": "^3.0.9", "@protobuf-ts/plugin": "^2.2.3-alpha.1", + "@types/unzipper": "^0.10.6", "archiver": "^5.3.1", "crypto": "^1.0.1", "jwt-decode": "^3.1.2", diff --git a/packages/artifact/src/internal/download/download-artifact.ts b/packages/artifact/src/internal/download/download-artifact.ts index 17699dec..ecbac621 100644 --- a/packages/artifact/src/internal/download/download-artifact.ts +++ b/packages/artifact/src/internal/download/download-artifact.ts @@ -30,7 +30,7 @@ async function exists(path: string): Promise { } } -async function streamExtract(url: string, directory: PathLike): Promise { +async function streamExtract(url: string, directory: string): Promise { const client = new httpClient.HttpClient(getUserAgentString()) const response = await client.get(url) diff --git a/packages/artifact/src/internal/find/get-artifact.ts b/packages/artifact/src/internal/find/get-artifact.ts index 3b28ff91..9b40da32 100644 --- a/packages/artifact/src/internal/find/get-artifact.ts +++ b/packages/artifact/src/internal/find/get-artifact.ts @@ -1,4 +1,22 @@ -import {GetArtifactResponse} from '../shared/interfaces' +import {Artifact, GetArtifactResponse} from '../shared/interfaces' +import {getOctokit} from '@actions/github' +import {getUserAgentString} from '../shared/user-agent' +import {defaults as defaultGitHubOptions} from '@actions/github/lib/utils' +import { RetryOptions, getRetryOptions } from './retry-options' +import {RequestRequestOptions} from '@octokit/types' +import {requestLog} from '@octokit/plugin-request-log' +import {retry} from '@octokit/plugin-retry' + +type Options = { + log?: Console + userAgent?: string + previews?: string[] + retry?: RetryOptions + request?: RequestRequestOptions +} + +const maxRetryNumber = 5 +const exemptStatusCodes = [400, 401, 403, 404, 422] // https://github.com/octokit/plugin-retry.js/blob/9a2443746c350b3beedec35cf26e197ea318a261/src/index.ts#L14 export async function getArtifact( artifactName: string, @@ -7,5 +25,49 @@ export async function getArtifact( repositoryName: string, token: string ): Promise { - throw new Error('Not implemented') + + const [retryOpts, requestOpts] = getRetryOptions( + maxRetryNumber, + exemptStatusCodes, + defaultGitHubOptions + ) + + const opts: Options = { + log: undefined, + userAgent: getUserAgentString(), + previews: undefined, + retry: retryOpts, + request: requestOpts + } + + const github = getOctokit(token, opts, retry, requestLog) + + const getArtifactResp = await github.request('GET /repos/{owner}/{repo}/actions/runs/{run_id}/artifacts{?name}', { + owner: repositoryOwner, + repo: repositoryName, + run_id: workflowRunId, + name: artifactName, + }) + + if (getArtifactResp.status !== 200) { + return { + success: false, + } + } + + if (getArtifactResp.data.artifacts.length === 0) { + return { + success: false, + } + } + + return { + success: true, + artifact: { + name: getArtifactResp.data.artifacts[0].name, + id: getArtifactResp.data.artifacts[0].id, + url: getArtifactResp.data.artifacts[0].url, + size: getArtifactResp.data.artifacts[0].size_in_bytes, + }, + } }