From 0555a5f45852d405b1093aebc39b6485ec6bc1c5 Mon Sep 17 00:00:00 2001 From: Bethany Date: Tue, 22 Aug 2023 09:17:43 -0700 Subject: [PATCH 01/13] 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, + }, + } } From 4214a1ff24ee8178826063c6b3d27fe16bb5d719 Mon Sep 17 00:00:00 2001 From: Bethany Date: Tue, 22 Aug 2023 09:57:14 -0700 Subject: [PATCH 02/13] update dependencies and prettier --- packages/artifact/package-lock.json | 27 +++++++++++++++++++ packages/artifact/package.json | 1 + .../internal/download/download-artifact.ts | 4 ++- .../src/internal/find/get-artifact.ts | 26 +++++++++--------- .../src/internal/find/list-artifacts.ts | 4 ++- 5 files changed, 48 insertions(+), 14 deletions(-) diff --git a/packages/artifact/package-lock.json b/packages/artifact/package-lock.json index d9e415c3..83971346 100644 --- a/packages/artifact/package-lock.json +++ b/packages/artifact/package-lock.json @@ -16,6 +16,7 @@ "@octokit/core": "^3.5.1", "@octokit/plugin-request-log": "^1.0.4", "@octokit/plugin-retry": "^3.0.9", + "@octokit/request-error": "^5.0.0", "@protobuf-ts/plugin": "^2.2.3-alpha.1", "@types/unzipper": "^0.10.6", "archiver": "^5.3.1", @@ -293,6 +294,32 @@ "universal-user-agent": "^6.0.0" } }, + "node_modules/@octokit/request-error": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-5.0.0.tgz", + "integrity": "sha512-1ue0DH0Lif5iEqT52+Rf/hf0RmGO9NWFjrzmrkArpG9trFfDM/efx00BJHdLGuro4BR/gECxCU2Twf5OKrRFsQ==", + "dependencies": { + "@octokit/types": "^11.0.0", + "deprecation": "^2.0.0", + "once": "^1.4.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/@octokit/request-error/node_modules/@octokit/openapi-types": { + "version": "18.0.0", + "resolved": "https://registry.npmjs.org/@octokit/openapi-types/-/openapi-types-18.0.0.tgz", + "integrity": "sha512-V8GImKs3TeQRxRtXFpG2wl19V7444NIOTDF24AWuIbmNaNYOQMWRbjcGDXV5B+0n887fgDcuMNOmlul+k+oJtw==" + }, + "node_modules/@octokit/request-error/node_modules/@octokit/types": { + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/@octokit/types/-/types-11.1.0.tgz", + "integrity": "sha512-Fz0+7GyLm/bHt8fwEqgvRBWwIV1S6wRRyq+V6exRKLVWaKGsuy6H9QFYeBVDV7rK6fO3XwHgQOPxv+cLj2zpXQ==", + "dependencies": { + "@octokit/openapi-types": "^18.0.0" + } + }, "node_modules/@octokit/request/node_modules/@octokit/request-error": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-2.1.0.tgz", diff --git a/packages/artifact/package.json b/packages/artifact/package.json index e2ebc939..dc776716 100644 --- a/packages/artifact/package.json +++ b/packages/artifact/package.json @@ -46,6 +46,7 @@ "@octokit/core": "^3.5.1", "@octokit/plugin-request-log": "^1.0.4", "@octokit/plugin-retry": "^3.0.9", + "@octokit/request-error": "^5.0.0", "@protobuf-ts/plugin": "^2.2.3-alpha.1", "@types/unzipper": "^0.10.6", "archiver": "^5.3.1", diff --git a/packages/artifact/src/internal/download/download-artifact.ts b/packages/artifact/src/internal/download/download-artifact.ts index ecbac621..6a90364d 100644 --- a/packages/artifact/src/internal/download/download-artifact.ts +++ b/packages/artifact/src/internal/download/download-artifact.ts @@ -61,7 +61,9 @@ export async function downloadArtifact( } if (!(await exists(downloadPath))) { - core.debug(`Artifact destination folder does not exist, creating: ${downloadPath}`) + core.debug( + `Artifact destination folder does not exist, creating: ${downloadPath}` + ) await fs.mkdir(downloadPath, {recursive: true}) } else { core.debug(`Artifact destination folder already exists: ${downloadPath}`) diff --git a/packages/artifact/src/internal/find/get-artifact.ts b/packages/artifact/src/internal/find/get-artifact.ts index 9b40da32..b911e77a 100644 --- a/packages/artifact/src/internal/find/get-artifact.ts +++ b/packages/artifact/src/internal/find/get-artifact.ts @@ -2,7 +2,7 @@ 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 {RetryOptions, getRetryOptions} from './retry-options' import {RequestRequestOptions} from '@octokit/types' import {requestLog} from '@octokit/plugin-request-log' import {retry} from '@octokit/plugin-retry' @@ -25,7 +25,6 @@ export async function getArtifact( repositoryName: string, token: string ): Promise { - const [retryOpts, requestOpts] = getRetryOptions( maxRetryNumber, exemptStatusCodes, @@ -42,22 +41,25 @@ export async function getArtifact( 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, - }) + 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, + success: false } } if (getArtifactResp.data.artifacts.length === 0) { return { - success: false, + success: false } } @@ -67,7 +69,7 @@ export async function getArtifact( 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, - }, + size: getArtifactResp.data.artifacts[0].size_in_bytes + } } } diff --git a/packages/artifact/src/internal/find/list-artifacts.ts b/packages/artifact/src/internal/find/list-artifacts.ts index 7fd96838..2b357ac8 100644 --- a/packages/artifact/src/internal/find/list-artifacts.ts +++ b/packages/artifact/src/internal/find/list-artifacts.ts @@ -29,7 +29,9 @@ export async function listArtifacts( repositoryName: string, token: string ): Promise { - info(`Fetching artifact list for workflow run ${workflowRunId} in repository ${repositoryOwner}/${repositoryName}`) + info( + `Fetching artifact list for workflow run ${workflowRunId} in repository ${repositoryOwner}/${repositoryName}` + ) const artifacts: Artifact[] = [] const [retryOpts, requestOpts] = getRetryOptions( From 81a802e7e0235cfaa92616d9f2fa66b7a282cafb Mon Sep 17 00:00:00 2001 From: Bethany Date: Tue, 22 Aug 2023 10:06:40 -0700 Subject: [PATCH 03/13] lint --- .../src/internal/download/download-artifact.ts | 1 - packages/artifact/src/internal/find/get-artifact.ts | 2 +- packages/artifact/src/internal/find/list-artifacts.ts | 10 +++++----- packages/artifact/src/internal/shared/user-agent.ts | 2 +- packages/artifact/src/internal/upload/blob-upload.ts | 2 +- .../artifact/src/internal/upload/upload-artifact.ts | 6 +++--- packages/artifact/tsconfig.json | 3 ++- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/artifact/src/internal/download/download-artifact.ts b/packages/artifact/src/internal/download/download-artifact.ts index 6a90364d..afd80d9b 100644 --- a/packages/artifact/src/internal/download/download-artifact.ts +++ b/packages/artifact/src/internal/download/download-artifact.ts @@ -1,6 +1,5 @@ import path from 'path' import fs from 'fs/promises' -import {PathLike} from 'fs' import * as github from '@actions/github' import * as core from '@actions/core' import * as httpClient from '@actions/http-client' diff --git a/packages/artifact/src/internal/find/get-artifact.ts b/packages/artifact/src/internal/find/get-artifact.ts index b911e77a..60f11319 100644 --- a/packages/artifact/src/internal/find/get-artifact.ts +++ b/packages/artifact/src/internal/find/get-artifact.ts @@ -1,4 +1,4 @@ -import {Artifact, GetArtifactResponse} from '../shared/interfaces' +import {GetArtifactResponse} from '../shared/interfaces' import {getOctokit} from '@actions/github' import {getUserAgentString} from '../shared/user-agent' import {defaults as defaultGitHubOptions} from '@actions/github/lib/utils' diff --git a/packages/artifact/src/internal/find/list-artifacts.ts b/packages/artifact/src/internal/find/list-artifacts.ts index 2b357ac8..40f5738b 100644 --- a/packages/artifact/src/internal/find/list-artifacts.ts +++ b/packages/artifact/src/internal/find/list-artifacts.ts @@ -72,14 +72,14 @@ export async function listArtifacts( } // Iterate over the first page - listArtifactResponse.artifacts.forEach(artifact => { + for (const artifact of listArtifactResponse.artifacts) { artifacts.push({ name: artifact.name, id: artifact.id, url: artifact.url, size: artifact.size_in_bytes }) - }) + } // Iterate over any remaining pages for ( @@ -99,19 +99,19 @@ export async function listArtifacts( page: currentPageNumber }) - listArtifactResponse.artifacts.forEach(artifact => { + for (const artifact of listArtifactResponse.artifacts) { artifacts.push({ name: artifact.name, id: artifact.id, url: artifact.url, size: artifact.size_in_bytes }) - }) + } } info(`Finished fetching artifact list`) return { - artifacts: artifacts + artifacts } } diff --git a/packages/artifact/src/internal/shared/user-agent.ts b/packages/artifact/src/internal/shared/user-agent.ts index eee01446..ee47a96c 100644 --- a/packages/artifact/src/internal/shared/user-agent.ts +++ b/packages/artifact/src/internal/shared/user-agent.ts @@ -1,4 +1,4 @@ -var packageJson = require('../../../package.json') +import * as packageJson from '../../../package.json' /** * Ensure that this User Agent String is used in all HTTP calls so that we can monitor telemetry between different versions of this package diff --git a/packages/artifact/src/internal/upload/blob-upload.ts b/packages/artifact/src/internal/upload/blob-upload.ts index 82af468a..42a3fbd5 100644 --- a/packages/artifact/src/internal/upload/blob-upload.ts +++ b/packages/artifact/src/internal/upload/blob-upload.ts @@ -91,6 +91,6 @@ export async function uploadZipToBlobStorage( return { isSuccess: true, uploadSize: uploadByteCount, - md5Hash: md5Hash + md5Hash } } diff --git a/packages/artifact/src/internal/upload/upload-artifact.ts b/packages/artifact/src/internal/upload/upload-artifact.ts index 35cd9ac3..01b37626 100644 --- a/packages/artifact/src/internal/upload/upload-artifact.ts +++ b/packages/artifact/src/internal/upload/upload-artifact.ts @@ -61,7 +61,7 @@ export async function uploadArtifact( const createArtifactReq: CreateArtifactRequest = { workflowRunBackendId: backendIds.workflowRunBackendId, workflowJobRunBackendId: backendIds.workflowJobRunBackendId, - name: name, + name, version: 4 } @@ -96,13 +96,13 @@ export async function uploadArtifact( const finalizeArtifactReq: FinalizeArtifactRequest = { workflowRunBackendId: backendIds.workflowRunBackendId, workflowJobRunBackendId: backendIds.workflowJobRunBackendId, - name: name, + name, size: uploadResult.uploadSize!.toString() } if (uploadResult.md5Hash) { finalizeArtifactReq.hash = StringValue.create({ - value: `md5:${uploadResult.md5Hash!}` + value: `md5:${uploadResult.md5Hash}` }) } diff --git a/packages/artifact/tsconfig.json b/packages/artifact/tsconfig.json index cee05147..048d5b76 100644 --- a/packages/artifact/tsconfig.json +++ b/packages/artifact/tsconfig.json @@ -16,5 +16,6 @@ }, "include": [ "./src" - ] + ], + "resolveJsonModule": true } From dd26bb1149d1db07fab41a3074e0592be610180d Mon Sep 17 00:00:00 2001 From: Bethany Date: Tue, 22 Aug 2023 11:33:00 -0700 Subject: [PATCH 04/13] use require --- .eslintignore | 1 + packages/artifact/src/internal/find/get-artifact.ts | 7 +++++++ packages/artifact/src/internal/shared/user-agent.ts | 2 +- packages/artifact/tsconfig.json | 3 +-- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.eslintignore b/.eslintignore index 3ac58072..a0478cef 100644 --- a/.eslintignore +++ b/.eslintignore @@ -3,3 +3,4 @@ packages/*/node_modules/ packages/*/lib/ packages/glob/__tests__/_temp packages/*/src/generated/*/ +packages/artifact/src/internal/shared/user-agent.ts diff --git a/packages/artifact/src/internal/find/get-artifact.ts b/packages/artifact/src/internal/find/get-artifact.ts index 60f11319..16cd163b 100644 --- a/packages/artifact/src/internal/find/get-artifact.ts +++ b/packages/artifact/src/internal/find/get-artifact.ts @@ -6,6 +6,7 @@ import {RetryOptions, getRetryOptions} from './retry-options' import {RequestRequestOptions} from '@octokit/types' import {requestLog} from '@octokit/plugin-request-log' import {retry} from '@octokit/plugin-retry' +import * as core from '@actions/core' type Options = { log?: Console @@ -52,17 +53,23 @@ export async function getArtifact( ) if (getArtifactResp.status !== 200) { + core.warning('non-200 response from GitHub API: ${getArtifactResp.status}') return { success: false } } if (getArtifactResp.data.artifacts.length === 0) { + core.warning('no artifacts found') return { success: false } } + if (getArtifactResp.data.artifacts.length > 1) { + core.warning('more than one artifact found, returning first') + } + return { success: true, artifact: { diff --git a/packages/artifact/src/internal/shared/user-agent.ts b/packages/artifact/src/internal/shared/user-agent.ts index ee47a96c..ec8ab1c7 100644 --- a/packages/artifact/src/internal/shared/user-agent.ts +++ b/packages/artifact/src/internal/shared/user-agent.ts @@ -1,4 +1,4 @@ -import * as packageJson from '../../../package.json' +const packageJson = require('../../../package.json') /** * Ensure that this User Agent String is used in all HTTP calls so that we can monitor telemetry between different versions of this package diff --git a/packages/artifact/tsconfig.json b/packages/artifact/tsconfig.json index 048d5b76..cee05147 100644 --- a/packages/artifact/tsconfig.json +++ b/packages/artifact/tsconfig.json @@ -16,6 +16,5 @@ }, "include": [ "./src" - ], - "resolveJsonModule": true + ] } From 671bf1ebd55da086e8dd1d842d2f75142a24df24 Mon Sep 17 00:00:00 2001 From: Bethany Date: Tue, 22 Aug 2023 11:44:38 -0700 Subject: [PATCH 05/13] use GITHUB_WORKSPACE as default download dir --- .../artifact/src/internal/download/download-artifact.ts | 3 ++- packages/artifact/src/internal/shared/config.ts | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/artifact/src/internal/download/download-artifact.ts b/packages/artifact/src/internal/download/download-artifact.ts index afd80d9b..9f9b47a6 100644 --- a/packages/artifact/src/internal/download/download-artifact.ts +++ b/packages/artifact/src/internal/download/download-artifact.ts @@ -9,6 +9,7 @@ import { DownloadArtifactResponse } from '../shared/interfaces' import {getUserAgentString} from '../shared/user-agent' +import { getGitHubWorkspaceDir } from '../shared/config' const scrubQueryParameters = (url: string): string => { const parsed = new URL(url) @@ -54,7 +55,7 @@ export async function downloadArtifact( token: string, options?: DownloadArtifactOptions ): Promise { - let downloadPath = options?.path || process.cwd() // TODO: make this align with GITHUB_WORKSPACE + let downloadPath = options?.path || getGitHubWorkspaceDir() if (options?.createArtifactFolder) { downloadPath = path.join(downloadPath, 'my-artifact') // TODO: need to pass artifact name } diff --git a/packages/artifact/src/internal/shared/config.ts b/packages/artifact/src/internal/shared/config.ts index dabb853b..8d8a1668 100644 --- a/packages/artifact/src/internal/shared/config.ts +++ b/packages/artifact/src/internal/shared/config.ts @@ -26,3 +26,11 @@ export function isGhes(): boolean { ) return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM' } + +export function getGitHubWorkspaceDir(): string { + const ghWorkspaceDir = process.env['GITHUB_WORKSPACE'] + if (!ghWorkspaceDir) { + throw new Error('Unable to get the GITHUB_WORKSPACE env variable') + } + return ghWorkspaceDir +} From 6adf053d36890f4d0afc88a11fb634e40a6737c0 Mon Sep 17 00:00:00 2001 From: Bethany Date: Tue, 22 Aug 2023 11:47:14 -0700 Subject: [PATCH 06/13] prettier --- packages/artifact/src/internal/download/download-artifact.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/artifact/src/internal/download/download-artifact.ts b/packages/artifact/src/internal/download/download-artifact.ts index 9f9b47a6..a799539e 100644 --- a/packages/artifact/src/internal/download/download-artifact.ts +++ b/packages/artifact/src/internal/download/download-artifact.ts @@ -9,7 +9,7 @@ import { DownloadArtifactResponse } from '../shared/interfaces' import {getUserAgentString} from '../shared/user-agent' -import { getGitHubWorkspaceDir } from '../shared/config' +import {getGitHubWorkspaceDir} from '../shared/config' const scrubQueryParameters = (url: string): string => { const parsed = new URL(url) From ced07aa89c5da3cec1848b9424dcce8c85607ba0 Mon Sep 17 00:00:00 2001 From: Bethany Date: Wed, 23 Aug 2023 06:47:51 -0700 Subject: [PATCH 07/13] Use options to specify download folder --- .../artifact/src/internal/download/download-artifact.ts | 4 ++-- packages/artifact/src/internal/shared/interfaces.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/artifact/src/internal/download/download-artifact.ts b/packages/artifact/src/internal/download/download-artifact.ts index a799539e..c8bf2cd3 100644 --- a/packages/artifact/src/internal/download/download-artifact.ts +++ b/packages/artifact/src/internal/download/download-artifact.ts @@ -56,8 +56,8 @@ export async function downloadArtifact( options?: DownloadArtifactOptions ): Promise { let downloadPath = options?.path || getGitHubWorkspaceDir() - if (options?.createArtifactFolder) { - downloadPath = path.join(downloadPath, 'my-artifact') // TODO: need to pass artifact name + if (options?.createArtifactFolderName) { + downloadPath = path.join(downloadPath, options?.createArtifactFolderName) } if (!(await exists(downloadPath))) { diff --git a/packages/artifact/src/internal/shared/interfaces.ts b/packages/artifact/src/internal/shared/interfaces.ts index 4f9a5c43..d8132d7b 100644 --- a/packages/artifact/src/internal/shared/interfaces.ts +++ b/packages/artifact/src/internal/shared/interfaces.ts @@ -93,10 +93,10 @@ export interface DownloadArtifactOptions { path?: string /** - * Specifies if a root folder with the artifact name is created for the artifact that is downloaded - * Zip contents are expanded into this folder. Defaults to false if not specified + * Specifies if a root folder with the given name is created for the artifact that is downloaded + * Zip contents are expanded into this folder. A folder will not be created if not specified. * */ - createArtifactFolder?: boolean + createArtifactFolderName?: string } /***************************************************************************** From b4f8e602b2aee7ed1d141afda970e07225317d05 Mon Sep 17 00:00:00 2001 From: Bethany Date: Wed, 23 Aug 2023 07:21:01 -0700 Subject: [PATCH 08/13] remove folder option in favor of path --- .../artifact/src/internal/download/download-artifact.ts | 3 --- packages/artifact/src/internal/shared/interfaces.ts | 6 ------ 2 files changed, 9 deletions(-) diff --git a/packages/artifact/src/internal/download/download-artifact.ts b/packages/artifact/src/internal/download/download-artifact.ts index c8bf2cd3..64abe289 100644 --- a/packages/artifact/src/internal/download/download-artifact.ts +++ b/packages/artifact/src/internal/download/download-artifact.ts @@ -56,9 +56,6 @@ export async function downloadArtifact( options?: DownloadArtifactOptions ): Promise { let downloadPath = options?.path || getGitHubWorkspaceDir() - if (options?.createArtifactFolderName) { - downloadPath = path.join(downloadPath, options?.createArtifactFolderName) - } if (!(await exists(downloadPath))) { core.debug( diff --git a/packages/artifact/src/internal/shared/interfaces.ts b/packages/artifact/src/internal/shared/interfaces.ts index d8132d7b..7e3e862f 100644 --- a/packages/artifact/src/internal/shared/interfaces.ts +++ b/packages/artifact/src/internal/shared/interfaces.ts @@ -91,12 +91,6 @@ export interface DownloadArtifactOptions { * Denotes where the artifact will be downloaded to. If not specified then the artifact is download to GITHUB_WORKSPACE */ path?: string - - /** - * Specifies if a root folder with the given name is created for the artifact that is downloaded - * Zip contents are expanded into this folder. A folder will not be created if not specified. - * */ - createArtifactFolderName?: string } /***************************************************************************** From 88f749f68653a31ae33525e39fe25165b511d6ae Mon Sep 17 00:00:00 2001 From: Bethany Date: Wed, 23 Aug 2023 07:28:17 -0700 Subject: [PATCH 09/13] lint --- packages/artifact/src/internal/download/download-artifact.ts | 3 +-- packages/artifact/src/internal/upload/upload-artifact.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/artifact/src/internal/download/download-artifact.ts b/packages/artifact/src/internal/download/download-artifact.ts index 64abe289..32ac56ad 100644 --- a/packages/artifact/src/internal/download/download-artifact.ts +++ b/packages/artifact/src/internal/download/download-artifact.ts @@ -1,4 +1,3 @@ -import path from 'path' import fs from 'fs/promises' import * as github from '@actions/github' import * as core from '@actions/core' @@ -55,7 +54,7 @@ export async function downloadArtifact( token: string, options?: DownloadArtifactOptions ): Promise { - let downloadPath = options?.path || getGitHubWorkspaceDir() + const downloadPath = options?.path || getGitHubWorkspaceDir() if (!(await exists(downloadPath))) { core.debug( diff --git a/packages/artifact/src/internal/upload/upload-artifact.ts b/packages/artifact/src/internal/upload/upload-artifact.ts index 01b37626..55911476 100644 --- a/packages/artifact/src/internal/upload/upload-artifact.ts +++ b/packages/artifact/src/internal/upload/upload-artifact.ts @@ -97,7 +97,7 @@ export async function uploadArtifact( workflowRunBackendId: backendIds.workflowRunBackendId, workflowJobRunBackendId: backendIds.workflowJobRunBackendId, name, - size: uploadResult.uploadSize!.toString() + size: uploadResult.uploadSize ? uploadResult.uploadSize.toString() : '0' } if (uploadResult.md5Hash) { From b2da9aa12c1cea5fccc411c1bc0723a3cbd43957 Mon Sep 17 00:00:00 2001 From: Bethany Date: Wed, 23 Aug 2023 07:35:23 -0700 Subject: [PATCH 10/13] use string interpolation --- packages/artifact/src/internal/find/get-artifact.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/artifact/src/internal/find/get-artifact.ts b/packages/artifact/src/internal/find/get-artifact.ts index 16cd163b..87f65773 100644 --- a/packages/artifact/src/internal/find/get-artifact.ts +++ b/packages/artifact/src/internal/find/get-artifact.ts @@ -53,7 +53,7 @@ export async function getArtifact( ) if (getArtifactResp.status !== 200) { - core.warning('non-200 response from GitHub API: ${getArtifactResp.status}') + core.warning(`non-200 response from GitHub API: ${getArtifactResp.status}`) return { success: false } @@ -67,7 +67,9 @@ export async function getArtifact( } if (getArtifactResp.data.artifacts.length > 1) { - core.warning('more than one artifact found, returning first') + core.warning( + 'more than one artifact found for a single name, returning first' + ) } return { From 4b6a4d80e13a01ecbf41e39098883d69cb54c0d3 Mon Sep 17 00:00:00 2001 From: Bethany Date: Wed, 23 Aug 2023 10:12:06 -0700 Subject: [PATCH 11/13] use inline eslint disable --- .eslintignore | 1 - packages/artifact/src/internal/shared/user-agent.ts | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/.eslintignore b/.eslintignore index a0478cef..3ac58072 100644 --- a/.eslintignore +++ b/.eslintignore @@ -3,4 +3,3 @@ packages/*/node_modules/ packages/*/lib/ packages/glob/__tests__/_temp packages/*/src/generated/*/ -packages/artifact/src/internal/shared/user-agent.ts diff --git a/packages/artifact/src/internal/shared/user-agent.ts b/packages/artifact/src/internal/shared/user-agent.ts index ec8ab1c7..94c07e89 100644 --- a/packages/artifact/src/internal/shared/user-agent.ts +++ b/packages/artifact/src/internal/shared/user-agent.ts @@ -1,3 +1,4 @@ +// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports const packageJson = require('../../../package.json') /** From 06e751600e2c016997ae8b1393bff0c4c79fc52f Mon Sep 17 00:00:00 2001 From: Bethany Date: Wed, 23 Aug 2023 10:36:33 -0700 Subject: [PATCH 12/13] move constants to retry-options --- .../src/internal/find/get-artifact.ts | 14 +++--------- .../src/internal/find/list-artifacts.ts | 22 ++++--------------- .../src/internal/find/retry-options.ts | 10 ++++++--- 3 files changed, 14 insertions(+), 32 deletions(-) diff --git a/packages/artifact/src/internal/find/get-artifact.ts b/packages/artifact/src/internal/find/get-artifact.ts index 87f65773..3e585b33 100644 --- a/packages/artifact/src/internal/find/get-artifact.ts +++ b/packages/artifact/src/internal/find/get-artifact.ts @@ -2,19 +2,11 @@ import {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 {getRetryOptions} from './retry-options' import {requestLog} from '@octokit/plugin-request-log' import {retry} from '@octokit/plugin-retry' import * as core from '@actions/core' - -type Options = { - log?: Console - userAgent?: string - previews?: string[] - retry?: RetryOptions - request?: RequestRequestOptions -} +import {OctokitOptions} from '@octokit/core/dist-types/types' const maxRetryNumber = 5 const exemptStatusCodes = [400, 401, 403, 404, 422] // https://github.com/octokit/plugin-retry.js/blob/9a2443746c350b3beedec35cf26e197ea318a261/src/index.ts#L14 @@ -32,7 +24,7 @@ export async function getArtifact( defaultGitHubOptions ) - const opts: Options = { + const opts: OctokitOptions = { log: undefined, userAgent: getUserAgentString(), previews: undefined, diff --git a/packages/artifact/src/internal/find/list-artifacts.ts b/packages/artifact/src/internal/find/list-artifacts.ts index 40f5738b..ea15fb36 100644 --- a/packages/artifact/src/internal/find/list-artifacts.ts +++ b/packages/artifact/src/internal/find/list-artifacts.ts @@ -2,26 +2,16 @@ import {info, warning, debug} from '@actions/core' import {getOctokit} from '@actions/github' import {ListArtifactsResponse, Artifact} from '../shared/interfaces' import {getUserAgentString} from '../shared/user-agent' -import {RetryOptions, getRetryOptions} from './retry-options' +import {getRetryOptions} from './retry-options' import {defaults as defaultGitHubOptions} from '@actions/github/lib/utils' import {requestLog} from '@octokit/plugin-request-log' import {retry} from '@octokit/plugin-retry' -import {RequestRequestOptions} from '@octokit/types' - -type Options = { - log?: Console - userAgent?: string - previews?: string[] - retry?: RetryOptions - request?: RequestRequestOptions -} +import {OctokitOptions} from '@octokit/core/dist-types/types' // Limiting to 1000 for perf reasons const maximumArtifactCount = 1000 const paginationCount = 100 const maxNumberOfPages = maximumArtifactCount / paginationCount -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 listArtifacts( workflowRunId: number, @@ -34,13 +24,9 @@ export async function listArtifacts( ) const artifacts: Artifact[] = [] - const [retryOpts, requestOpts] = getRetryOptions( - maxRetryNumber, - exemptStatusCodes, - defaultGitHubOptions - ) + const [retryOpts, requestOpts] = getRetryOptions(defaultGitHubOptions) - const opts: Options = { + const opts: OctokitOptions = { log: undefined, userAgent: getUserAgentString(), previews: undefined, diff --git a/packages/artifact/src/internal/find/retry-options.ts b/packages/artifact/src/internal/find/retry-options.ts index 3fe51b5a..ab10309b 100644 --- a/packages/artifact/src/internal/find/retry-options.ts +++ b/packages/artifact/src/internal/find/retry-options.ts @@ -7,10 +7,14 @@ export type RetryOptions = { enabled?: boolean } +// Defaults for fetching artifacts +const defaultMaxRetryNumber = 5 +const defaultExemptStatusCodes = [400, 401, 403, 404, 422] // https://github.com/octokit/plugin-retry.js/blob/9a2443746c350b3beedec35cf26e197ea318a261/src/index.ts#L14 + export function getRetryOptions( - retries: number, - exemptStatusCodes: number[], - defaultOptions: OctokitOptions + defaultOptions: OctokitOptions, + retries: number = defaultMaxRetryNumber, + exemptStatusCodes: number[] = defaultExemptStatusCodes ): [RetryOptions, RequestRequestOptions | undefined] { if (retries <= 0) { return [{enabled: false}, defaultOptions.request] From 291200d54fe224ac22ceff54211a0a083f5c4495 Mon Sep 17 00:00:00 2001 From: Bethany Date: Wed, 23 Aug 2023 10:40:25 -0700 Subject: [PATCH 13/13] include get artifact changes --- packages/artifact/src/internal/find/get-artifact.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/artifact/src/internal/find/get-artifact.ts b/packages/artifact/src/internal/find/get-artifact.ts index 3e585b33..24c38a99 100644 --- a/packages/artifact/src/internal/find/get-artifact.ts +++ b/packages/artifact/src/internal/find/get-artifact.ts @@ -8,9 +8,6 @@ import {retry} from '@octokit/plugin-retry' import * as core from '@actions/core' import {OctokitOptions} from '@octokit/core/dist-types/types' -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, workflowRunId: number, @@ -18,11 +15,7 @@ export async function getArtifact( repositoryName: string, token: string ): Promise { - const [retryOpts, requestOpts] = getRetryOptions( - maxRetryNumber, - exemptStatusCodes, - defaultGitHubOptions - ) + const [retryOpts, requestOpts] = getRetryOptions(defaultGitHubOptions) const opts: OctokitOptions = { log: undefined,