From b851b70474a8e76bef7a43d3e5f10c73bf933605 Mon Sep 17 00:00:00 2001 From: Bethany Date: Wed, 9 Aug 2023 12:08:43 -0700 Subject: [PATCH] catch errors at the root, remove unneccessary disabled rule --- packages/artifact/src/internal/client.ts | 10 ++- .../internal/shared/artifact-twirp-client.ts | 4 +- packages/artifact/src/internal/shared/util.ts | 8 +- .../src/internal/upload/upload-artifact.ts | 75 ++++--------------- 4 files changed, 29 insertions(+), 68 deletions(-) diff --git a/packages/artifact/src/internal/client.ts b/packages/artifact/src/internal/client.ts index bf33120f..26546430 100644 --- a/packages/artifact/src/internal/client.ts +++ b/packages/artifact/src/internal/client.ts @@ -1,6 +1,7 @@ import {UploadOptions} from './upload/upload-options' import {UploadResponse} from './upload/upload-response' import {uploadArtifact} from './upload/upload-artifact' +import {warning} from '@actions/core' export interface ArtifactClient { /** @@ -39,6 +40,13 @@ export class Client implements ArtifactClient { rootDirectory: string, options?: UploadOptions | undefined ): Promise { - return uploadArtifact(name, files, rootDirectory, options) + try { + return uploadArtifact(name, files, rootDirectory, options) + } catch (error) { + warning(`Failed to upload artifact: ${error}`) + return { + success: false + } + } } } diff --git a/packages/artifact/src/internal/shared/artifact-twirp-client.ts b/packages/artifact/src/internal/shared/artifact-twirp-client.ts index d2450183..a77d02a4 100644 --- a/packages/artifact/src/internal/shared/artifact-twirp-client.ts +++ b/packages/artifact/src/internal/shared/artifact-twirp-client.ts @@ -56,8 +56,6 @@ class ArtifactHttpClient implements Rpc { const headers = { 'Content-Type': contentType } - info(`Making request to ${url} with data: ${JSON.stringify(data)}`) - try { const response = await this.retryableRequest(async () => this.httpClient.post(url, JSON.stringify(data), headers) @@ -65,7 +63,7 @@ class ArtifactHttpClient implements Rpc { const body = await response.readBody() return JSON.parse(body) } catch (error) { - throw new Error(error.message) + throw new Error(`Failed to ${method}: ${error.message}`) } } diff --git a/packages/artifact/src/internal/shared/util.ts b/packages/artifact/src/internal/shared/util.ts index 1b7ca9e1..62849eb5 100644 --- a/packages/artifact/src/internal/shared/util.ts +++ b/packages/artifact/src/internal/shared/util.ts @@ -11,13 +11,15 @@ interface ActionsToken { scp: string } +const InvalidJwtError = new Error('Failed to get backend IDs: The provided JWT token is invalid') + // uses the JWT token claims to get the // workflow run and workflow job run backend ids export function getBackendIdsFromToken(): BackendIds { const token = getRuntimeToken() const decoded = jwt_decode(token) if (!decoded.scp) { - throw new Error('No scp claim in JWT token') + throw InvalidJwtError } /* @@ -29,7 +31,7 @@ export function getBackendIdsFromToken(): BackendIds { const scpParts = decoded.scp.split(' ') if (scpParts.length === 0) { - throw new Error('No scp parts in JWT token') + throw InvalidJwtError } /* * example scpParts: @@ -58,7 +60,7 @@ export function getBackendIdsFromToken(): BackendIds { } } - throw new Error('No valid Actions.Results scope in JWT token') + throw InvalidJwtError } export function getExpiration(retentionDays?: number): Timestamp | undefined { diff --git a/packages/artifact/src/internal/upload/upload-artifact.ts b/packages/artifact/src/internal/upload/upload-artifact.ts index a793ce17..2d3d3333 100644 --- a/packages/artifact/src/internal/upload/upload-artifact.ts +++ b/packages/artifact/src/internal/upload/upload-artifact.ts @@ -8,30 +8,17 @@ import { getUploadZipSpecification, validateRootDirectory } from './upload-zip-specification' -import {BackendIds, getBackendIdsFromToken, getExpiration} from '../shared/util' -import { - CreateArtifactRequest, - CreateArtifactResponse, - FinalizeArtifactResponse -} from 'src/generated' +import {getBackendIdsFromToken, getExpiration} from '../shared/util' +import {CreateArtifactRequest} from 'src/generated' export async function uploadArtifact( name: string, files: string[], rootDirectory: string, - options?: UploadOptions | undefined // eslint-disable-line @typescript-eslint/no-unused-vars + options?: UploadOptions | undefined ): Promise { - try { - validateArtifactName(name) - validateRootDirectory(rootDirectory) - } catch (error) { - core.warning( - `Received error trying to validate artifact name or root directory: ${error}` - ) - return { - success: false - } - } + validateArtifactName(name) + validateRootDirectory(rootDirectory) const zipSpecification: UploadZipSpecification[] = getUploadZipSpecification( files, @@ -45,7 +32,7 @@ export async function uploadArtifact( } // get the IDs needed for the artifact creation - const backendIds = getBackendIds() + const backendIds = getBackendIdsFromToken() if (!backendIds.workflowRunBackendId || !backendIds.workflowJobRunBackendId) { core.warning(`Failed to get backend ids`) return { @@ -69,10 +56,9 @@ export async function uploadArtifact( if (expiresAt) { createArtifactReq.expiresAt = expiresAt } - const createArtifactResp = await createArtifact(async () => - artifactClient.CreateArtifact(createArtifactReq) - ) - if (!createArtifactResp || !createArtifactResp.ok) { + + const createArtifactResp = await artifactClient.CreateArtifact(createArtifactReq) + if (!createArtifactResp.ok) { core.warning(`Failed to create artifact`) return { success: false @@ -82,20 +68,21 @@ export async function uploadArtifact( // TODO - Implement upload functionality // finalize the artifact - const finalizeArtifactResp = await finalizeArtifact(async () => - artifactClient.FinalizeArtifact({ + const finalizeArtifactResp = await artifactClient.FinalizeArtifact( + { workflowRunBackendId: backendIds.workflowRunBackendId, workflowJobRunBackendId: backendIds.workflowJobRunBackendId, name, size: '0' // TODO - Add size - }) + } ) - if (!finalizeArtifactResp || !finalizeArtifactResp.ok) { + if (!finalizeArtifactResp.ok) { core.warning(`Failed to finalize artifact`) return { success: false } } + const uploadResponse: UploadResponse = { success: true, size: 0, @@ -104,37 +91,3 @@ export async function uploadArtifact( return uploadResponse } - -async function createArtifact( - operation: () => Promise -): Promise { - try { - return await operation() - } catch (error) { - core.warning(`Received error trying to create artifact: ${error}`) - return - } -} - -async function finalizeArtifact( - operation: () => Promise -): Promise { - try { - return await operation() - } catch (error) { - core.warning(`Received error trying to create artifact: ${error}`) - return - } -} - -function getBackendIds(): BackendIds { - try { - return getBackendIdsFromToken() - } catch (error) { - core.warning(`Received error trying to get backend ids: ${error}`) - return { - workflowRunBackendId: '', - workflowJobRunBackendId: '' - } - } -}