1
0
Fork 0

list for correct backend ids in internal delete

pull/1626/head
Rob Herley 2024-01-17 17:53:49 -05:00
parent 7fd71a5e13
commit 2f5fb3f92b
No known key found for this signature in database
GPG Key ID: D1602042C3543B06
2 changed files with 58 additions and 7 deletions

View File

@ -6,7 +6,7 @@ import {
deleteArtifactPublic deleteArtifactPublic
} from '../src/internal/delete/delete-artifact' } from '../src/internal/delete/delete-artifact'
import * as config from '../src/internal/shared/config' import * as config from '../src/internal/shared/config'
import {ArtifactServiceClientJSON} from '../src/generated' import {ArtifactServiceClientJSON, Timestamp} from '../src/generated'
import * as util from '../src/internal/shared/util' import * as util from '../src/internal/shared/util'
import {noopLogs} from './common' import {noopLogs} from './common'
@ -145,7 +145,18 @@ describe('delete-artifact', () => {
.mockReturnValue('https://results.local') .mockReturnValue('https://results.local')
}) })
it('should return a list of artifacts', async () => { it('should delete an artifact', async () => {
jest
.spyOn(ArtifactServiceClientJSON.prototype, 'ListArtifacts')
.mockResolvedValue({
artifacts: fixtures.artifacts.map(artifact => ({
...fixtures.backendIds,
databaseId: artifact.id.toString(),
name: artifact.name,
size: artifact.size.toString(),
createdAt: Timestamp.fromDate(artifact.createdAt)
}))
})
jest jest
.spyOn(ArtifactServiceClientJSON.prototype, 'DeleteArtifact') .spyOn(ArtifactServiceClientJSON.prototype, 'DeleteArtifact')
.mockResolvedValue({ .mockResolvedValue({
@ -159,6 +170,17 @@ describe('delete-artifact', () => {
}) })
it('should fail if non-200 response', async () => { it('should fail if non-200 response', async () => {
jest
.spyOn(ArtifactServiceClientJSON.prototype, 'ListArtifacts')
.mockResolvedValue({
artifacts: fixtures.artifacts.map(artifact => ({
...fixtures.backendIds,
databaseId: artifact.id.toString(),
name: artifact.name,
size: artifact.size.toString(),
createdAt: Timestamp.fromDate(artifact.createdAt)
}))
})
jest jest
.spyOn(ArtifactServiceClientJSON.prototype, 'DeleteArtifact') .spyOn(ArtifactServiceClientJSON.prototype, 'DeleteArtifact')
.mockRejectedValue(new Error('boom')) .mockRejectedValue(new Error('boom'))

View File

@ -1,4 +1,4 @@
import {info} from '@actions/core' import {info, debug} from '@actions/core'
import {getOctokit} from '@actions/github' import {getOctokit} from '@actions/github'
import {DeleteArtifactResponse} from '../shared/interfaces' import {DeleteArtifactResponse} from '../shared/interfaces'
import {getUserAgentString} from '../shared/user-agent' import {getUserAgentString} from '../shared/user-agent'
@ -9,9 +9,13 @@ import {retry} from '@octokit/plugin-retry'
import {OctokitOptions} from '@octokit/core/dist-types/types' import {OctokitOptions} from '@octokit/core/dist-types/types'
import {internalArtifactTwirpClient} from '../shared/artifact-twirp-client' import {internalArtifactTwirpClient} from '../shared/artifact-twirp-client'
import {getBackendIdsFromToken} from '../shared/util' import {getBackendIdsFromToken} from '../shared/util'
import {DeleteArtifactRequest} from '../../generated' import {
DeleteArtifactRequest,
ListArtifactsRequest,
StringValue
} from '../../generated'
import {getArtifactPublic} from '../find/get-artifact' import {getArtifactPublic} from '../find/get-artifact'
import {InvalidResponseError} from '../shared/errors' import {ArtifactNotFoundError, InvalidResponseError} from '../shared/errors'
export async function deleteArtifactPublic( export async function deleteArtifactPublic(
artifactName: string, artifactName: string,
@ -65,10 +69,35 @@ export async function deleteArtifactInternal(
const {workflowRunBackendId, workflowJobRunBackendId} = const {workflowRunBackendId, workflowJobRunBackendId} =
getBackendIdsFromToken() getBackendIdsFromToken()
const req: DeleteArtifactRequest = { const listReq: ListArtifactsRequest = {
workflowRunBackendId, workflowRunBackendId,
workflowJobRunBackendId, workflowJobRunBackendId,
name: artifactName nameFilter: StringValue.create({value: artifactName})
}
const listRes = await artifactClient.ListArtifacts(listReq)
if (listRes.artifacts.length === 0) {
throw new ArtifactNotFoundError(
`Artifact not found for name: ${artifactName}`
)
}
let artifact = listRes.artifacts[0]
if (listRes.artifacts.length > 1) {
artifact = listRes.artifacts.sort(
(a, b) => Number(b.databaseId) - Number(a.databaseId)
)[0]
debug(
`More than one artifact found for a single name, returning newest (id: ${artifact.databaseId})`
)
}
const req: DeleteArtifactRequest = {
workflowRunBackendId: artifact.workflowRunBackendId,
workflowJobRunBackendId: artifact.workflowJobRunBackendId,
name: artifact.name
} }
const res = await artifactClient.DeleteArtifact(req) const res = await artifactClient.DeleteArtifact(req)