From 73a7c368bf1e165e3387cd64b4b66b08bf34cc01 Mon Sep 17 00:00:00 2001 From: twsl <45483159+twsl@users.noreply.github.com> Date: Wed, 13 Aug 2025 11:41:17 +0000 Subject: [PATCH] Download artifacts to dedicated subfolder when no name or id specified --- __tests__/download.test.ts | 118 +++++++++++++++++++++++++++++++++++++ src/download-artifact.ts | 2 +- 2 files changed, 119 insertions(+), 1 deletion(-) diff --git a/__tests__/download.test.ts b/__tests__/download.test.ts index 0709fbe..fd606ae 100644 --- a/__tests__/download.test.ts +++ b/__tests__/download.test.ts @@ -406,4 +406,122 @@ describe('download', () => { }) ) }) + + test('downloads single artifact to dedicated folders named after the artifact within provided path', async () => { + jest.clearAllMocks() + + const testPath = '/test/path' + const mockArtifacts = [ + {id: 101, name: 'frontend-build', size: 2048, digest: 'abc123'} + ] + + mockInputs({ + [Inputs.Name]: '', + [Inputs.Pattern]: '', + [Inputs.ArtifactIds]: '', + [Inputs.Path]: testPath, + [Inputs.MergeMultiple]: false + }) + + jest + .spyOn(artifact, 'listArtifacts') + .mockImplementation(() => Promise.resolve({artifacts: mockArtifacts})) + + jest + .spyOn(artifact, 'downloadArtifact') + .mockImplementation(() => Promise.resolve({digestMismatch: false})) + + await run() + + expect(artifact.downloadArtifact).toHaveBeenCalledTimes(1) + + // Verify the artifact is downloaded to its own folder within the provided path + expect(artifact.downloadArtifact).toHaveBeenCalledWith( + 101, + expect.objectContaining({ + path: path.join(path.resolve(testPath), 'frontend-build'), + expectedHash: 'abc123' + }) + ) + + expect(core.info).toHaveBeenCalledWith( + 'No input name, artifact-ids or pattern filtered specified, downloading all artifacts' + ) + expect(core.info).toHaveBeenCalledWith( + 'An extra directory with the artifact name will be created for each download' + ) + expect(core.info).toHaveBeenCalledWith('Total of 1 artifact(s) downloaded') + expect(core.setOutput).toHaveBeenCalledWith( + 'download-path', + path.resolve(testPath) + ) + }) + + test('downloads multiple artifacts to separate folders named after each artifact within provided path', async () => { + jest.clearAllMocks() + + const testPath = '/test/path' + const mockArtifacts = [ + {id: 101, name: 'frontend-build', size: 2048, digest: 'abc123'}, + {id: 102, name: 'backend-build', size: 4096, digest: 'def456'}, + {id: 103, name: 'test-results', size: 1024, digest: 'ghi789'} + ] + + mockInputs({ + [Inputs.Name]: '', + [Inputs.Pattern]: '', + [Inputs.ArtifactIds]: '', + [Inputs.Path]: testPath, + [Inputs.MergeMultiple]: false + }) + + jest + .spyOn(artifact, 'listArtifacts') + .mockImplementation(() => Promise.resolve({artifacts: mockArtifacts})) + + jest + .spyOn(artifact, 'downloadArtifact') + .mockImplementation(() => Promise.resolve({digestMismatch: false})) + + await run() + + expect(artifact.downloadArtifact).toHaveBeenCalledTimes(3) + + // Verify each artifact is downloaded to its own folder within the provided path + expect(artifact.downloadArtifact).toHaveBeenCalledWith( + 101, + expect.objectContaining({ + path: path.join(path.resolve(testPath), 'frontend-build'), + expectedHash: 'abc123' + }) + ) + + expect(artifact.downloadArtifact).toHaveBeenCalledWith( + 102, + expect.objectContaining({ + path: path.join(path.resolve(testPath), 'backend-build'), + expectedHash: 'def456' + }) + ) + + expect(artifact.downloadArtifact).toHaveBeenCalledWith( + 103, + expect.objectContaining({ + path: path.join(path.resolve(testPath), 'test-results'), + expectedHash: 'ghi789' + }) + ) + + expect(core.info).toHaveBeenCalledWith( + 'No input name, artifact-ids or pattern filtered specified, downloading all artifacts' + ) + expect(core.info).toHaveBeenCalledWith( + 'An extra directory with the artifact name will be created for each download' + ) + expect(core.info).toHaveBeenCalledWith('Total of 3 artifact(s) downloaded') + expect(core.setOutput).toHaveBeenCalledWith( + 'download-path', + path.resolve(testPath) + ) + }) }) diff --git a/src/download-artifact.ts b/src/download-artifact.ts index 6f2d782..1e39403 100644 --- a/src/download-artifact.ts +++ b/src/download-artifact.ts @@ -176,7 +176,7 @@ export async function run(): Promise { path: isSingleArtifactDownload || inputs.mergeMultiple || - artifacts.length === 1 + (artifacts.length === 1 && isDownloadByIds) ? resolvedPath : path.join(resolvedPath, artifact.name), expectedHash: artifact.digest