From c1f9d37323afac4dced1431052013461238d8493 Mon Sep 17 00:00:00 2001 From: Rob Herley Date: Sat, 2 Dec 2023 21:18:22 -0500 Subject: [PATCH] updates to get/list artifacts --- .../src/generated/results/api/v1/artifact.ts | 15 ++++++- .../src/internal/find/get-artifact.ts | 42 +++++++++++-------- .../src/internal/find/list-artifacts.ts | 15 +++++-- .../src/internal/shared/interfaces.ts | 5 +++ 4 files changed, 55 insertions(+), 22 deletions(-) diff --git a/packages/artifact/src/generated/results/api/v1/artifact.ts b/packages/artifact/src/generated/results/api/v1/artifact.ts index adabae5e..acce3037 100644 --- a/packages/artifact/src/generated/results/api/v1/artifact.ts +++ b/packages/artifact/src/generated/results/api/v1/artifact.ts @@ -163,6 +163,12 @@ export interface ListArtifactsResponse_MonolithArtifact { * @generated from protobuf field: int64 size = 5; */ size: string; + /** + * When the artifact was created in the monolith + * + * @generated from protobuf field: google.protobuf.Timestamp created_at = 6; + */ + createdAt?: Timestamp; } /** * @generated from protobuf message github.actions.results.api.v1.GetSignedArtifactURLRequest @@ -571,7 +577,8 @@ class ListArtifactsResponse_MonolithArtifact$Type extends MessageType Timestamp } ]); } create(value?: PartialMessage): ListArtifactsResponse_MonolithArtifact { @@ -601,6 +608,9 @@ class ListArtifactsResponse_MonolithArtifact$Type extends MessageType 1) { - core.warning( - 'more than one artifact found for a single name, returning first' + artifact = getArtifactResp.data.artifacts.reduce((prev, current) => { + new Date(prev.created_at) > new Date(current.created_at) ? prev : current + }) + + core.debug( + `more than one artifact found for a single name, returning newest (id: ${artifact.id})` ) } return { success: true, artifact: { - name: getArtifactResp.data.artifacts[0].name, - id: getArtifactResp.data.artifacts[0].id, - size: getArtifactResp.data.artifacts[0].size_in_bytes + name: artifact.name, + id: artifact.id, + size: artifact.size_in_bytes, + createdAt: artifact.created_at ? new Date(artifact.created_at) : undefined } } } @@ -93,26 +99,28 @@ export async function getArtifactInternal( } } + let artifact = res.artifacts[0] if (res.artifacts.length > 1) { - core.warning( - 'more than one artifact found for a single name, returning first' + artifact = res.artifacts.reduce((prev, current) => { + const prevDate = Timestamp.toDate(prev.createdAt || Timestamp.now()) + const currentDate = Timestamp.toDate(current.createdAt || Timestamp.now()) + return prevDate > currentDate ? prev : current + }) + + core.debug( + `more than one artifact found for a single name, returning newest (id: ${artifact.databaseId})` ) } - // In the case of reruns, we may have artifacts with the same name scoped under the same workflow run. - // Let's prefer the artifact closest scoped to this run. - // If it doesn't exist (e.g. partial rerun) we'll use the first match. - const artifact = - res.artifacts.find( - artifact => artifact.workflowRunBackendId === workflowRunBackendId - ) || res.artifacts[0] - return { success: true, artifact: { name: artifact.name, id: Number(artifact.databaseId), - size: Number(artifact.size) + size: Number(artifact.size), + createdAt: artifact.createdAt + ? Timestamp.toDate(artifact.createdAt) + : undefined } } } diff --git a/packages/artifact/src/internal/find/list-artifacts.ts b/packages/artifact/src/internal/find/list-artifacts.ts index c25a58bd..a951fed1 100644 --- a/packages/artifact/src/internal/find/list-artifacts.ts +++ b/packages/artifact/src/internal/find/list-artifacts.ts @@ -9,7 +9,7 @@ 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 {ListArtifactsRequest} from 'src/generated' +import {ListArtifactsRequest, Timestamp} from 'src/generated' // Limiting to 1000 for perf reasons const maximumArtifactCount = 1000 @@ -65,7 +65,8 @@ export async function listArtifactsPublic( artifacts.push({ name: artifact.name, id: artifact.id, - size: artifact.size_in_bytes + size: artifact.size_in_bytes, + createdAt: artifact.created_at ? new Date(artifact.created_at) : undefined }) } @@ -91,7 +92,10 @@ export async function listArtifactsPublic( artifacts.push({ name: artifact.name, id: artifact.id, - size: artifact.size_in_bytes + size: artifact.size_in_bytes, + createdAt: artifact.created_at + ? new Date(artifact.created_at) + : undefined }) } } @@ -118,7 +122,10 @@ export async function listArtifactsInternal(): Promise { const artifacts = res.artifacts.map(artifact => ({ name: artifact.name, id: Number(artifact.databaseId), - size: Number(artifact.size) + size: Number(artifact.size), + createdAt: artifact.createdAt + ? Timestamp.toDate(artifact.createdAt) + : undefined })) info(`Found ${artifacts.length} artifact(s)`) diff --git a/packages/artifact/src/internal/shared/interfaces.ts b/packages/artifact/src/internal/shared/interfaces.ts index bd764412..0b0e3768 100644 --- a/packages/artifact/src/internal/shared/interfaces.ts +++ b/packages/artifact/src/internal/shared/interfaces.ts @@ -124,6 +124,11 @@ export interface Artifact { * The size of the artifact in bytes */ size: number + + /** + * The time when the artifact was created + */ + createdAt?: Date } // FindOptions are for fetching Artifact(s) out of the scope of the current run.