From 2f5fb3f92b71b303a20d6b74fbc64dc64f3e32d8 Mon Sep 17 00:00:00 2001 From: Rob Herley Date: Wed, 17 Jan 2024 17:53:49 -0500 Subject: [PATCH] list for correct backend ids in internal delete --- .../__tests__/delete-artifacts.test.ts | 26 ++++++++++++- .../src/internal/delete/delete-artifact.ts | 39 ++++++++++++++++--- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/packages/artifact/__tests__/delete-artifacts.test.ts b/packages/artifact/__tests__/delete-artifacts.test.ts index 98727d62..70a39412 100644 --- a/packages/artifact/__tests__/delete-artifacts.test.ts +++ b/packages/artifact/__tests__/delete-artifacts.test.ts @@ -6,7 +6,7 @@ import { deleteArtifactPublic } from '../src/internal/delete/delete-artifact' 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 {noopLogs} from './common' @@ -145,7 +145,18 @@ describe('delete-artifact', () => { .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 .spyOn(ArtifactServiceClientJSON.prototype, 'DeleteArtifact') .mockResolvedValue({ @@ -159,6 +170,17 @@ describe('delete-artifact', () => { }) 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 .spyOn(ArtifactServiceClientJSON.prototype, 'DeleteArtifact') .mockRejectedValue(new Error('boom')) diff --git a/packages/artifact/src/internal/delete/delete-artifact.ts b/packages/artifact/src/internal/delete/delete-artifact.ts index 4183049b..785b4035 100644 --- a/packages/artifact/src/internal/delete/delete-artifact.ts +++ b/packages/artifact/src/internal/delete/delete-artifact.ts @@ -1,4 +1,4 @@ -import {info} from '@actions/core' +import {info, debug} from '@actions/core' import {getOctokit} from '@actions/github' import {DeleteArtifactResponse} from '../shared/interfaces' 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 {internalArtifactTwirpClient} from '../shared/artifact-twirp-client' import {getBackendIdsFromToken} from '../shared/util' -import {DeleteArtifactRequest} from '../../generated' +import { + DeleteArtifactRequest, + ListArtifactsRequest, + StringValue +} from '../../generated' import {getArtifactPublic} from '../find/get-artifact' -import {InvalidResponseError} from '../shared/errors' +import {ArtifactNotFoundError, InvalidResponseError} from '../shared/errors' export async function deleteArtifactPublic( artifactName: string, @@ -65,10 +69,35 @@ export async function deleteArtifactInternal( const {workflowRunBackendId, workflowJobRunBackendId} = getBackendIdsFromToken() - const req: DeleteArtifactRequest = { + const listReq: ListArtifactsRequest = { workflowRunBackendId, 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)