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 {UploadOptions} from './upload/upload-options'
|
||||||
import {UploadResponse} from './upload/upload-response'
|
import {UploadResponse} from './upload/upload-response'
|
||||||
import {uploadArtifact} from './upload/upload-artifact'
|
import {uploadArtifact} from './upload/upload-artifact'
|
||||||
|
import {warning} from '@actions/core'
|
||||||
|
|
||||||
export interface ArtifactClient {
|
export interface ArtifactClient {
|
||||||
/**
|
/**
|
||||||
|
@ -39,6 +40,13 @@ export class Client implements ArtifactClient {
|
||||||
rootDirectory: string,
|
rootDirectory: string,
|
||||||
options?: UploadOptions | undefined
|
options?: UploadOptions | undefined
|
||||||
): Promise<UploadResponse> {
|
): Promise<UploadResponse> {
|
||||||
return uploadArtifact(name, files, rootDirectory, options)
|
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 = {
|
const headers = {
|
||||||
'Content-Type': contentType
|
'Content-Type': contentType
|
||||||
}
|
}
|
||||||
info(`Making request to ${url} with data: ${JSON.stringify(data)}`)
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await this.retryableRequest(async () =>
|
const response = await this.retryableRequest(async () =>
|
||||||
this.httpClient.post(url, JSON.stringify(data), headers)
|
this.httpClient.post(url, JSON.stringify(data), headers)
|
||||||
|
@ -65,7 +63,7 @@ class ArtifactHttpClient implements Rpc {
|
||||||
const body = await response.readBody()
|
const body = await response.readBody()
|
||||||
return JSON.parse(body)
|
return JSON.parse(body)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw new Error(error.message)
|
throw new Error(`Failed to ${method}: ${error.message}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,13 +11,15 @@ interface ActionsToken {
|
||||||
scp: string
|
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
|
// uses the JWT token claims to get the
|
||||||
// workflow run and workflow job run backend ids
|
// workflow run and workflow job run backend ids
|
||||||
export function getBackendIdsFromToken(): BackendIds {
|
export function getBackendIdsFromToken(): BackendIds {
|
||||||
const token = getRuntimeToken()
|
const token = getRuntimeToken()
|
||||||
const decoded = jwt_decode<ActionsToken>(token)
|
const decoded = jwt_decode<ActionsToken>(token)
|
||||||
if (!decoded.scp) {
|
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(' ')
|
const scpParts = decoded.scp.split(' ')
|
||||||
if (scpParts.length === 0) {
|
if (scpParts.length === 0) {
|
||||||
throw new Error('No scp parts in JWT token')
|
throw InvalidJwtError
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* example scpParts:
|
* 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 {
|
export function getExpiration(retentionDays?: number): Timestamp | undefined {
|
||||||
|
|
|
@ -8,30 +8,17 @@ import {
|
||||||
getUploadZipSpecification,
|
getUploadZipSpecification,
|
||||||
validateRootDirectory
|
validateRootDirectory
|
||||||
} from './upload-zip-specification'
|
} from './upload-zip-specification'
|
||||||
import {BackendIds, getBackendIdsFromToken, getExpiration} from '../shared/util'
|
import {getBackendIdsFromToken, getExpiration} from '../shared/util'
|
||||||
import {
|
import {CreateArtifactRequest} from 'src/generated'
|
||||||
CreateArtifactRequest,
|
|
||||||
CreateArtifactResponse,
|
|
||||||
FinalizeArtifactResponse
|
|
||||||
} from 'src/generated'
|
|
||||||
|
|
||||||
export async function uploadArtifact(
|
export async function uploadArtifact(
|
||||||
name: string,
|
name: string,
|
||||||
files: string[],
|
files: string[],
|
||||||
rootDirectory: string,
|
rootDirectory: string,
|
||||||
options?: UploadOptions | undefined // eslint-disable-line @typescript-eslint/no-unused-vars
|
options?: UploadOptions | undefined
|
||||||
): Promise<UploadResponse> {
|
): Promise<UploadResponse> {
|
||||||
try {
|
validateArtifactName(name)
|
||||||
validateArtifactName(name)
|
validateRootDirectory(rootDirectory)
|
||||||
validateRootDirectory(rootDirectory)
|
|
||||||
} catch (error) {
|
|
||||||
core.warning(
|
|
||||||
`Received error trying to validate artifact name or root directory: ${error}`
|
|
||||||
)
|
|
||||||
return {
|
|
||||||
success: false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const zipSpecification: UploadZipSpecification[] = getUploadZipSpecification(
|
const zipSpecification: UploadZipSpecification[] = getUploadZipSpecification(
|
||||||
files,
|
files,
|
||||||
|
@ -45,7 +32,7 @@ export async function uploadArtifact(
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the IDs needed for the artifact creation
|
// get the IDs needed for the artifact creation
|
||||||
const backendIds = getBackendIds()
|
const backendIds = getBackendIdsFromToken()
|
||||||
if (!backendIds.workflowRunBackendId || !backendIds.workflowJobRunBackendId) {
|
if (!backendIds.workflowRunBackendId || !backendIds.workflowJobRunBackendId) {
|
||||||
core.warning(`Failed to get backend ids`)
|
core.warning(`Failed to get backend ids`)
|
||||||
return {
|
return {
|
||||||
|
@ -69,10 +56,9 @@ export async function uploadArtifact(
|
||||||
if (expiresAt) {
|
if (expiresAt) {
|
||||||
createArtifactReq.expiresAt = expiresAt
|
createArtifactReq.expiresAt = expiresAt
|
||||||
}
|
}
|
||||||
const createArtifactResp = await createArtifact(async () =>
|
|
||||||
artifactClient.CreateArtifact(createArtifactReq)
|
const createArtifactResp = await artifactClient.CreateArtifact(createArtifactReq)
|
||||||
)
|
if (!createArtifactResp.ok) {
|
||||||
if (!createArtifactResp || !createArtifactResp.ok) {
|
|
||||||
core.warning(`Failed to create artifact`)
|
core.warning(`Failed to create artifact`)
|
||||||
return {
|
return {
|
||||||
success: false
|
success: false
|
||||||
|
@ -82,20 +68,21 @@ export async function uploadArtifact(
|
||||||
// TODO - Implement upload functionality
|
// TODO - Implement upload functionality
|
||||||
|
|
||||||
// finalize the artifact
|
// finalize the artifact
|
||||||
const finalizeArtifactResp = await finalizeArtifact(async () =>
|
const finalizeArtifactResp = await artifactClient.FinalizeArtifact(
|
||||||
artifactClient.FinalizeArtifact({
|
{
|
||||||
workflowRunBackendId: backendIds.workflowRunBackendId,
|
workflowRunBackendId: backendIds.workflowRunBackendId,
|
||||||
workflowJobRunBackendId: backendIds.workflowJobRunBackendId,
|
workflowJobRunBackendId: backendIds.workflowJobRunBackendId,
|
||||||
name,
|
name,
|
||||||
size: '0' // TODO - Add size
|
size: '0' // TODO - Add size
|
||||||
})
|
}
|
||||||
)
|
)
|
||||||
if (!finalizeArtifactResp || !finalizeArtifactResp.ok) {
|
if (!finalizeArtifactResp.ok) {
|
||||||
core.warning(`Failed to finalize artifact`)
|
core.warning(`Failed to finalize artifact`)
|
||||||
return {
|
return {
|
||||||
success: false
|
success: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const uploadResponse: UploadResponse = {
|
const uploadResponse: UploadResponse = {
|
||||||
success: true,
|
success: true,
|
||||||
size: 0,
|
size: 0,
|
||||||
|
@ -104,37 +91,3 @@ export async function uploadArtifact(
|
||||||
|
|
||||||
return uploadResponse
|
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