Files
toolkit/packages/artifact/src/internal/upload/upload-artifact.ts
T

141 lines
3.8 KiB
TypeScript
Raw Normal View History

import * as core from '@actions/core'
import {UploadOptions} from './upload-options'
import {UploadResponse} from './upload-response'
import {validateArtifactName} from './path-and-artifact-name-validation'
2023-08-09 11:26:33 -07:00
import {createArtifactTwirpClient} from '../shared/artifact-twirp-client'
import {
UploadZipSpecification,
getUploadZipSpecification,
validateRootDirectory
} from './upload-zip-specification'
2023-08-09 11:26:33 -07:00
import {BackendIds, getBackendIdsFromToken, getExpiration} from '../shared/util'
import {
CreateArtifactRequest,
CreateArtifactResponse,
FinalizeArtifactResponse
} from 'src/generated'
export async function uploadArtifact(
name: string,
files: string[],
rootDirectory: string,
options?: UploadOptions | undefined // eslint-disable-line @typescript-eslint/no-unused-vars
): Promise<UploadResponse> {
2023-08-09 11:26:33 -07:00
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,
rootDirectory
)
if (zipSpecification.length === 0) {
core.warning(`No files were found to upload`)
return {
success: false
}
}
2023-08-09 11:26:33 -07:00
// get the IDs needed for the artifact creation
const backendIds = getBackendIds()
if (!backendIds.workflowRunBackendId || !backendIds.workflowJobRunBackendId) {
core.warning(`Failed to get backend ids`)
return {
success: false
}
}
// create the artifact client
const artifactClient = createArtifactTwirpClient('upload')
// create the artifact
const createArtifactReq: CreateArtifactRequest = {
workflowRunBackendId: backendIds.workflowRunBackendId,
workflowJobRunBackendId: backendIds.workflowJobRunBackendId,
2023-08-09 11:34:18 -07:00
name,
2023-08-09 11:26:33 -07:00
version: 4
}
// if there is a retention period, add it to the request
const expiresAt = getExpiration(options?.retentionDays)
if (expiresAt) {
createArtifactReq.expiresAt = expiresAt
}
2023-08-09 11:34:18 -07:00
const createArtifactResp = await createArtifact(async () =>
2023-08-09 11:26:33 -07:00
artifactClient.CreateArtifact(createArtifactReq)
)
if (!createArtifactResp || !createArtifactResp.ok) {
core.warning(`Failed to create artifact`)
return {
success: false
}
}
// TODO - Implement upload functionality
2023-08-09 11:26:33 -07:00
// finalize the artifact
2023-08-09 11:34:18 -07:00
const finalizeArtifactResp = await finalizeArtifact(async () =>
2023-08-09 11:26:33 -07:00
artifactClient.FinalizeArtifact({
workflowRunBackendId: backendIds.workflowRunBackendId,
workflowJobRunBackendId: backendIds.workflowJobRunBackendId,
2023-08-09 11:34:18 -07:00
name,
2023-08-09 11:26:33 -07:00
size: '0' // TODO - Add size
})
)
if (!finalizeArtifactResp || !finalizeArtifactResp.ok) {
core.warning(`Failed to finalize artifact`)
return {
success: false
}
}
const uploadResponse: UploadResponse = {
success: true,
size: 0,
2023-08-09 11:26:33 -07:00
id: parseInt(finalizeArtifactResp.artifactId) // TODO - will this be a problem due to the id being a bigint?
}
return uploadResponse
}
2023-08-09 11:26:33 -07:00
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: ''
}
}
}