From 1fb743dadfdac94d80d54053d25be9ea47e25368 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 --- package-lock.json | 9 +++ package.json | 1 + src/internal/download/download-artifact.ts | 2 +- src/internal/find/get-artifact.ts | 66 +++++++++++++++++++++- 4 files changed, 75 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 90338df..d9e415c 100644 --- a/package-lock.json +++ b/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/package.json b/package.json index b6d0ae1..e2ebc93 100644 --- a/package.json +++ b/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/src/internal/download/download-artifact.ts b/src/internal/download/download-artifact.ts index 17699de..ecbac62 100644 --- a/src/internal/download/download-artifact.ts +++ b/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/src/internal/find/get-artifact.ts b/src/internal/find/get-artifact.ts index 3b28ff9..9b40da3 100644 --- a/src/internal/find/get-artifact.ts +++ b/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 af762e2836312c8d1308baf6dc4ba96ce54e070e 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 --- package-lock.json | 27 ++++++++++++++++++++++ package.json | 1 + src/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/package-lock.json b/package-lock.json index d9e415c..8397134 100644 --- a/package-lock.json +++ b/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/package.json b/package.json index e2ebc93..dc77671 100644 --- a/package.json +++ b/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/src/internal/download/download-artifact.ts b/src/internal/download/download-artifact.ts index ecbac62..6a90364 100644 --- a/src/internal/download/download-artifact.ts +++ b/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/src/internal/find/get-artifact.ts b/src/internal/find/get-artifact.ts index 9b40da3..b911e77 100644 --- a/src/internal/find/get-artifact.ts +++ b/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/src/internal/find/list-artifacts.ts b/src/internal/find/list-artifacts.ts index 7fd9683..2b357ac 100644 --- a/src/internal/find/list-artifacts.ts +++ b/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 cbe3f44b0173179e2025c4928e3704232e9c5f47 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 - src/internal/find/get-artifact.ts | 2 +- src/internal/find/list-artifacts.ts | 10 +++++----- src/internal/shared/user-agent.ts | 2 +- src/internal/upload/blob-upload.ts | 2 +- src/internal/upload/upload-artifact.ts | 6 +++--- tsconfig.json | 3 ++- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/internal/download/download-artifact.ts b/src/internal/download/download-artifact.ts index 6a90364..afd80d9 100644 --- a/src/internal/download/download-artifact.ts +++ b/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/src/internal/find/get-artifact.ts b/src/internal/find/get-artifact.ts index b911e77..60f1131 100644 --- a/src/internal/find/get-artifact.ts +++ b/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/src/internal/find/list-artifacts.ts b/src/internal/find/list-artifacts.ts index 2b357ac..40f5738 100644 --- a/src/internal/find/list-artifacts.ts +++ b/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/src/internal/shared/user-agent.ts b/src/internal/shared/user-agent.ts index eee0144..ee47a96 100644 --- a/src/internal/shared/user-agent.ts +++ b/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/src/internal/upload/blob-upload.ts b/src/internal/upload/blob-upload.ts index 82af468..42a3fbd 100644 --- a/src/internal/upload/blob-upload.ts +++ b/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/src/internal/upload/upload-artifact.ts b/src/internal/upload/upload-artifact.ts index 35cd9ac..01b3762 100644 --- a/src/internal/upload/upload-artifact.ts +++ b/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/tsconfig.json b/tsconfig.json index cee0514..048d5b7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,5 +16,6 @@ }, "include": [ "./src" - ] + ], + "resolveJsonModule": true } From 7262d472fe5ef2ee0e4b61787e48c08108de693d Mon Sep 17 00:00:00 2001 From: Bethany Date: Tue, 22 Aug 2023 11:33:00 -0700 Subject: [PATCH 04/13] use require --- src/internal/find/get-artifact.ts | 7 +++++++ src/internal/shared/user-agent.ts | 2 +- tsconfig.json | 3 +-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/internal/find/get-artifact.ts b/src/internal/find/get-artifact.ts index 60f1131..16cd163 100644 --- a/src/internal/find/get-artifact.ts +++ b/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/src/internal/shared/user-agent.ts b/src/internal/shared/user-agent.ts index ee47a96..ec8ab1c 100644 --- a/src/internal/shared/user-agent.ts +++ b/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/tsconfig.json b/tsconfig.json index 048d5b7..cee0514 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,6 +16,5 @@ }, "include": [ "./src" - ], - "resolveJsonModule": true + ] } From e7ddf2aea87cea237e0442124eb54ae0acd4799b 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 --- src/internal/download/download-artifact.ts | 3 ++- src/internal/shared/config.ts | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/internal/download/download-artifact.ts b/src/internal/download/download-artifact.ts index afd80d9..9f9b47a 100644 --- a/src/internal/download/download-artifact.ts +++ b/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/src/internal/shared/config.ts b/src/internal/shared/config.ts index dabb853..8d8a166 100644 --- a/src/internal/shared/config.ts +++ b/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 69e1f6f7c08c7e0d1e4410253907657a2b7b944d Mon Sep 17 00:00:00 2001 From: Bethany Date: Tue, 22 Aug 2023 11:47:14 -0700 Subject: [PATCH 06/13] prettier --- src/internal/download/download-artifact.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal/download/download-artifact.ts b/src/internal/download/download-artifact.ts index 9f9b47a..a799539 100644 --- a/src/internal/download/download-artifact.ts +++ b/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 d0cf96f319c8061b4aa0777c29641c95dc5aa202 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 --- src/internal/download/download-artifact.ts | 4 ++-- src/internal/shared/interfaces.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/internal/download/download-artifact.ts b/src/internal/download/download-artifact.ts index a799539..c8bf2cd 100644 --- a/src/internal/download/download-artifact.ts +++ b/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/src/internal/shared/interfaces.ts b/src/internal/shared/interfaces.ts index 4f9a5c4..d8132d7 100644 --- a/src/internal/shared/interfaces.ts +++ b/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 68fb6c17d57c198ba8d4101dad355147db8b2680 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 --- src/internal/download/download-artifact.ts | 3 --- src/internal/shared/interfaces.ts | 6 ------ 2 files changed, 9 deletions(-) diff --git a/src/internal/download/download-artifact.ts b/src/internal/download/download-artifact.ts index c8bf2cd..64abe28 100644 --- a/src/internal/download/download-artifact.ts +++ b/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/src/internal/shared/interfaces.ts b/src/internal/shared/interfaces.ts index d8132d7..7e3e862 100644 --- a/src/internal/shared/interfaces.ts +++ b/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 7188d5f703d99ca4b28e964bba4be64623be9ec6 Mon Sep 17 00:00:00 2001 From: Bethany Date: Wed, 23 Aug 2023 07:28:17 -0700 Subject: [PATCH 09/13] lint --- src/internal/download/download-artifact.ts | 3 +-- src/internal/upload/upload-artifact.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/internal/download/download-artifact.ts b/src/internal/download/download-artifact.ts index 64abe28..32ac56a 100644 --- a/src/internal/download/download-artifact.ts +++ b/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/src/internal/upload/upload-artifact.ts b/src/internal/upload/upload-artifact.ts index 01b3762..5591147 100644 --- a/src/internal/upload/upload-artifact.ts +++ b/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 99502be3f44a2170815f5fb51749786f481260f4 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 --- src/internal/find/get-artifact.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/internal/find/get-artifact.ts b/src/internal/find/get-artifact.ts index 16cd163..87f6577 100644 --- a/src/internal/find/get-artifact.ts +++ b/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 51b7d6a651868e0d864fb6d6331dd6ba2ce3639b 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 --- src/internal/shared/user-agent.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/internal/shared/user-agent.ts b/src/internal/shared/user-agent.ts index ec8ab1c..94c07e8 100644 --- a/src/internal/shared/user-agent.ts +++ b/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 c59b53ff169749ca2a4da4776184dae9550f43b3 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/src/internal/find/get-artifact.ts b/src/internal/find/get-artifact.ts index 87f6577..3e585b3 100644 --- a/src/internal/find/get-artifact.ts +++ b/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/src/internal/find/list-artifacts.ts b/src/internal/find/list-artifacts.ts index 40f5738..ea15fb3 100644 --- a/src/internal/find/list-artifacts.ts +++ b/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/src/internal/find/retry-options.ts b/src/internal/find/retry-options.ts index 3fe51b5..ab10309 100644 --- a/src/internal/find/retry-options.ts +++ b/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 bf39487f1551161427b19ec64722128ae04617f2 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 --- src/internal/find/get-artifact.ts | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/internal/find/get-artifact.ts b/src/internal/find/get-artifact.ts index 3e585b3..24c38a9 100644 --- a/src/internal/find/get-artifact.ts +++ b/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,