2023-08-04 09:53:42 -04:00
|
|
|
import * as core from '@actions/core'
|
2023-08-03 13:34:41 -04:00
|
|
|
import {UploadOptions} from './upload-options'
|
|
|
|
|
import {UploadResponse} from './upload-response'
|
2023-08-04 09:53:42 -04:00
|
|
|
import {validateArtifactName} from './path-and-artifact-name-validation'
|
2023-08-09 11:26:33 -07:00
|
|
|
import {createArtifactTwirpClient} from '../shared/artifact-twirp-client'
|
2023-08-04 09:53:42 -04:00
|
|
|
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'
|
2023-08-03 13:34:41 -04:00
|
|
|
|
|
|
|
|
export async function uploadArtifact(
|
|
|
|
|
name: string,
|
2023-08-04 09:53:42 -04:00
|
|
|
files: string[],
|
|
|
|
|
rootDirectory: string,
|
2023-08-03 13:34:41 -04:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-04 09:53:42 -04:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 13:34:41 -04:00
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-03 13:34:41 -04:00
|
|
|
const uploadResponse: UploadResponse = {
|
2023-08-04 09:53:42 -04:00
|
|
|
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?
|
2023-08-03 13:34:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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: ''
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|