mirror of https://github.com/actions/toolkit
catch errors at the root, remove unneccessary disabled rule
parent
e8fb71c4bb
commit
b851b70474
|
@ -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<UploadResponse> {
|
||||
try {
|
||||
return uploadArtifact(name, files, rootDirectory, options)
|
||||
} catch (error) {
|
||||
warning(`Failed to upload artifact: ${error}`)
|
||||
return {
|
||||
success: false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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}`)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<ActionsToken>(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 {
|
||||
|
|
|
@ -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<UploadResponse> {
|
||||
try {
|
||||
validateArtifactName(name)
|
||||
validateRootDirectory(rootDirectory)
|
||||
} catch (error) {
|
||||
core.warning(
|
||||
`Received error trying to validate artifact name or root directory: ${error}`
|
||||
)
|
||||
return {
|
||||
success: false
|
||||
}
|
||||
}
|
||||
|
||||
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<CreateArtifactResponse>
|
||||
): Promise<CreateArtifactResponse | undefined> {
|
||||
try {
|
||||
return await operation()
|
||||
} catch (error) {
|
||||
core.warning(`Received error trying to create artifact: ${error}`)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
async function finalizeArtifact(
|
||||
operation: () => Promise<FinalizeArtifactResponse>
|
||||
): Promise<FinalizeArtifactResponse | undefined> {
|
||||
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: ''
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue