diff --git a/packages/artifact/__tests__/download-artifact.test.ts b/packages/artifact/__tests__/download-artifact.test.ts new file mode 100644 index 00000000..e86cb3a7 --- /dev/null +++ b/packages/artifact/__tests__/download-artifact.test.ts @@ -0,0 +1,279 @@ +import fs from 'fs' +import * as http from 'http' +import * as net from 'net' +import * as path from 'path' +import * as core from '@actions/core' +import * as github from '@actions/github' +import {HttpClient} from '@actions/http-client' +import type {RestEndpointMethods} from '@octokit/plugin-rest-endpoint-methods/dist-types/generated/method-types' +import archiver from 'archiver' + +import {downloadArtifact} from '../src/internal/download/download-artifact' +import {getUserAgentString} from '../src/internal/shared/user-agent' + +type MockedDownloadArtifact = jest.MockedFunction< + RestEndpointMethods['actions']['downloadArtifact'] +> + +const testDir = path.join(__dirname, '_temp', 'download-artifact') +const fixtures = { + workspaceDir: path.join(testDir, 'workspace'), + exampleArtifact: { + path: path.join(testDir, 'artifact.zip'), + files: [ + { + path: 'hello.txt', + content: 'Hello World!' + }, + { + path: 'goodbye.txt', + content: 'Goodbye World!' + } + ] + }, + artifactID: 1234, + repositoryOwner: 'actions', + repositoryName: 'toolkit', + token: 'ghp_1234567890', + blobStorageUrl: 'https://blob-storage.local?signed=true' +} + +jest.mock('@actions/github', () => ({ + getOctokit: jest.fn().mockReturnValue({ + rest: { + actions: { + downloadArtifact: jest.fn() + } + } + }) +})) + +jest.mock('@actions/http-client') + +// Create a zip archive with the contents of the example artifact +const createTestArchive = async (): Promise => { + const archive = archiver('zip', { + zlib: {level: 9} + }) + for (const file of fixtures.exampleArtifact.files) { + archive.append(file.content, {name: file.path}) + } + archive.finalize() + + return new Promise((resolve, reject) => { + archive.pipe(fs.createWriteStream(fixtures.exampleArtifact.path)) + archive.on('error', reject) + archive.on('finish', resolve) + }) +} + +const expectExtractedArchive = async (dir: string): Promise => { + for (const file of fixtures.exampleArtifact.files) { + const filePath = path.join(dir, file.path) + expect(fs.readFileSync(filePath, 'utf8')).toEqual(file.content) + } +} + +describe('download-artifact', () => { + beforeEach(async () => { + jest.spyOn(core, 'debug').mockImplementation(() => {}) + jest.spyOn(core, 'info').mockImplementation(() => {}) + jest.spyOn(core, 'warning').mockImplementation(() => {}) + + await fs.promises.mkdir(testDir, {recursive: true}) + await createTestArchive() + + process.env['GITHUB_WORKSPACE'] = fixtures.workspaceDir + }) + + afterEach(async () => { + jest.restoreAllMocks() + await fs.promises.rm(testDir, {recursive: true}) + delete process.env['GITHUB_WORKSPACE'] + }) + + it('should successfully download an artifact to $GITHUB_WORKSPACE', async () => { + const downloadArtifactMock = github.getOctokit(fixtures.token).rest.actions + .downloadArtifact as MockedDownloadArtifact + downloadArtifactMock.mockResolvedValueOnce({ + headers: { + location: fixtures.blobStorageUrl + }, + status: 302, + url: '', + data: Buffer.from('') + }) + + const getMock = jest.fn(() => { + const message = new http.IncomingMessage(new net.Socket()) + message.statusCode = 200 + message.push(fs.readFileSync(fixtures.exampleArtifact.path)) + return { + message + } + }) + const httpClientMock = (HttpClient as jest.Mock).mockImplementation(() => { + return { + get: getMock + } + }) + + const response = await downloadArtifact( + fixtures.artifactID, + fixtures.repositoryOwner, + fixtures.repositoryName, + fixtures.token + ) + + expect(downloadArtifactMock).toHaveBeenCalledWith({ + owner: fixtures.repositoryOwner, + repo: fixtures.repositoryName, + artifact_id: fixtures.artifactID, + archive_format: 'zip', + request: { + redirect: 'manual' + } + }) + expect(httpClientMock).toHaveBeenCalledWith(getUserAgentString()) + expect(getMock).toHaveBeenCalledWith(fixtures.blobStorageUrl) + + expectExtractedArchive(fixtures.workspaceDir) + + expect(response.success).toBe(true) + expect(response.downloadPath).toBe(fixtures.workspaceDir) + }) + + it('should successfully download an artifact to user defined path', async () => { + const customPath = path.join(testDir, 'custom') + + const downloadArtifactMock = github.getOctokit(fixtures.token).rest.actions + .downloadArtifact as MockedDownloadArtifact + downloadArtifactMock.mockResolvedValueOnce({ + headers: { + location: fixtures.blobStorageUrl + }, + status: 302, + url: '', + data: Buffer.from('') + }) + + const getMock = jest.fn(() => { + const message = new http.IncomingMessage(new net.Socket()) + message.statusCode = 200 + message.push(fs.readFileSync(fixtures.exampleArtifact.path)) + return { + message + } + }) + const httpClientMock = (HttpClient as jest.Mock).mockImplementation(() => { + return { + get: getMock + } + }) + + const response = await downloadArtifact( + fixtures.artifactID, + fixtures.repositoryOwner, + fixtures.repositoryName, + fixtures.token, + { + path: customPath + } + ) + + expect(downloadArtifactMock).toHaveBeenCalledWith({ + owner: fixtures.repositoryOwner, + repo: fixtures.repositoryName, + artifact_id: fixtures.artifactID, + archive_format: 'zip', + request: { + redirect: 'manual' + } + }) + expect(httpClientMock).toHaveBeenCalledWith(getUserAgentString()) + expect(getMock).toHaveBeenCalledWith(fixtures.blobStorageUrl) + + expectExtractedArchive(customPath) + + expect(response.success).toBe(true) + expect(response.downloadPath).toBe(customPath) + }) + + it('should fail if download artifact API does not respond with location', async () => { + const downloadArtifactMock = github.getOctokit(fixtures.token).rest.actions + .downloadArtifact as MockedDownloadArtifact + downloadArtifactMock.mockResolvedValueOnce({ + headers: {}, + status: 302, + url: '', + data: Buffer.from('') + }) + + await expect( + downloadArtifact( + fixtures.artifactID, + fixtures.repositoryOwner, + fixtures.repositoryName, + fixtures.token + ) + ).rejects.toBeInstanceOf(Error) + + expect(downloadArtifactMock).toHaveBeenCalledWith({ + owner: fixtures.repositoryOwner, + repo: fixtures.repositoryName, + artifact_id: fixtures.artifactID, + archive_format: 'zip', + request: { + redirect: 'manual' + } + }) + }) + + it('should fail if blob storage response is non-200', async () => { + const downloadArtifactMock = github.getOctokit(fixtures.token).rest.actions + .downloadArtifact as MockedDownloadArtifact + downloadArtifactMock.mockResolvedValueOnce({ + headers: { + location: fixtures.blobStorageUrl + }, + status: 302, + url: '', + data: Buffer.from('') + }) + + const getMock = jest.fn(() => { + const message = new http.IncomingMessage(new net.Socket()) + message.statusCode = 500 + message.push('Internal Server Error') + return { + message + } + }) + const httpClientMock = (HttpClient as jest.Mock).mockImplementation(() => { + return { + get: getMock + } + }) + + await expect( + downloadArtifact( + fixtures.artifactID, + fixtures.repositoryOwner, + fixtures.repositoryName, + fixtures.token + ) + ).rejects.toBeInstanceOf(Error) + + expect(downloadArtifactMock).toHaveBeenCalledWith({ + owner: fixtures.repositoryOwner, + repo: fixtures.repositoryName, + artifact_id: fixtures.artifactID, + archive_format: 'zip', + request: { + redirect: 'manual' + } + }) + expect(httpClientMock).toHaveBeenCalledWith(getUserAgentString()) + expect(getMock).toHaveBeenCalledWith(fixtures.blobStorageUrl) + }) +}) diff --git a/packages/artifact/package-lock.json b/packages/artifact/package-lock.json index 90c4bd6f..83971346 100644 --- a/packages/artifact/package-lock.json +++ b/packages/artifact/package-lock.json @@ -16,11 +16,14 @@ "@octokit/core": "^3.5.1", "@octokit/plugin-request-log": "^1.0.4", "@octokit/plugin-retry": "^3.0.9", + "@octokit/request-error": "^5.0.0", "@protobuf-ts/plugin": "^2.2.3-alpha.1", + "@types/unzipper": "^0.10.6", "archiver": "^5.3.1", "crypto": "^1.0.1", "jwt-decode": "^3.1.2", - "twirp-ts": "^2.5.0" + "twirp-ts": "^2.5.0", + "unzipper": "^0.10.14" }, "devDependencies": { "@types/archiver": "^5.3.2", @@ -291,6 +294,32 @@ "universal-user-agent": "^6.0.0" } }, + "node_modules/@octokit/request-error": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-5.0.0.tgz", + "integrity": "sha512-1ue0DH0Lif5iEqT52+Rf/hf0RmGO9NWFjrzmrkArpG9trFfDM/efx00BJHdLGuro4BR/gECxCU2Twf5OKrRFsQ==", + "dependencies": { + "@octokit/types": "^11.0.0", + "deprecation": "^2.0.0", + "once": "^1.4.0" + }, + "engines": { + "node": ">= 18" + } + }, + "node_modules/@octokit/request-error/node_modules/@octokit/openapi-types": { + "version": "18.0.0", + "resolved": "https://registry.npmjs.org/@octokit/openapi-types/-/openapi-types-18.0.0.tgz", + "integrity": "sha512-V8GImKs3TeQRxRtXFpG2wl19V7444NIOTDF24AWuIbmNaNYOQMWRbjcGDXV5B+0n887fgDcuMNOmlul+k+oJtw==" + }, + "node_modules/@octokit/request-error/node_modules/@octokit/types": { + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/@octokit/types/-/types-11.1.0.tgz", + "integrity": "sha512-Fz0+7GyLm/bHt8fwEqgvRBWwIV1S6wRRyq+V6exRKLVWaKGsuy6H9QFYeBVDV7rK6fO3XwHgQOPxv+cLj2zpXQ==", + "dependencies": { + "@octokit/openapi-types": "^18.0.0" + } + }, "node_modules/@octokit/request/node_modules/@octokit/request-error": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/@octokit/request-error/-/request-error-2.1.0.tgz", @@ -440,6 +469,14 @@ "@types/node": "*" } }, + "node_modules/@types/unzipper": { + "version": "0.10.6", + "resolved": "https://registry.npmjs.org/@types/unzipper/-/unzipper-0.10.6.tgz", + "integrity": "sha512-zcBj329AHgKLQyz209N/S9R0GZqXSkUQO4tJSYE3x02qg4JuDFpgKMj50r82Erk1natCWQDIvSccDddt7jPzjA==", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/archiver": { "version": "5.3.1", "resolved": "https://registry.npmjs.org/archiver/-/archiver-5.3.1.tgz", @@ -543,6 +580,26 @@ "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-2.2.3.tgz", "integrity": "sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ==" }, + "node_modules/big-integer": { + "version": "1.6.51", + "resolved": "https://registry.npmjs.org/big-integer/-/big-integer-1.6.51.tgz", + "integrity": "sha512-GPEid2Y9QU1Exl1rpO9B2IPJGHPSupF5GnVIP0blYvNOMer2bTvSWs1jGOUg04hTmu67nmLsQ9TBo1puaotBHg==", + "engines": { + "node": ">=0.6" + } + }, + "node_modules/binary": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/binary/-/binary-0.3.0.tgz", + "integrity": "sha512-D4H1y5KYwpJgK8wk1Cue5LLPgmwHKYSChkbspQg5JtVuR5ulGckxfR62H3AE9UDkdMC8yyXlqYihuz3Aqg2XZg==", + "dependencies": { + "buffers": "~0.1.1", + "chainsaw": "~0.1.0" + }, + "engines": { + "node": "*" + } + }, "node_modules/bl": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", @@ -553,6 +610,11 @@ "readable-stream": "^3.4.0" } }, + "node_modules/bluebird": { + "version": "3.4.7", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.4.7.tgz", + "integrity": "sha512-iD3898SR7sWVRHbiQv+sHUtHnMvC1o3nW5rAcqnq3uOn07DSAppZYUkIGslDz6gXC7HfunPe7YVBgoEJASPcHA==" + }, "node_modules/bottleneck": { "version": "2.19.5", "resolved": "https://registry.npmjs.org/bottleneck/-/bottleneck-2.19.5.tgz", @@ -598,6 +660,22 @@ "node": "*" } }, + "node_modules/buffer-indexof-polyfill": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/buffer-indexof-polyfill/-/buffer-indexof-polyfill-1.0.2.tgz", + "integrity": "sha512-I7wzHwA3t1/lwXQh+A5PbNvJxgfo5r3xulgpYDB5zckTu/Z9oUK9biouBKQUjEqzaz3HnAT6TYoovmE+GqSf7A==", + "engines": { + "node": ">=0.10" + } + }, + "node_modules/buffers": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/buffers/-/buffers-0.1.1.tgz", + "integrity": "sha512-9q/rDEGSb/Qsvv2qvzIzdluL5k7AaJOTrw23z9reQthrbF7is4CtlT0DXyO1oei2DCp4uojjzQ7igaSHp1kAEQ==", + "engines": { + "node": ">=0.2.0" + } + }, "node_modules/camel-case": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/camel-case/-/camel-case-4.1.2.tgz", @@ -607,6 +685,17 @@ "tslib": "^2.0.3" } }, + "node_modules/chainsaw": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/chainsaw/-/chainsaw-0.1.0.tgz", + "integrity": "sha512-75kWfWt6MEKNC8xYXIdRpDehRYY/tNSgwKaJq+dbbDcxORuVrrQ+SEHoWsniVn9XPYfP4gmdWIeDk/4YNp1rNQ==", + "dependencies": { + "traverse": ">=0.3.0 <0.4" + }, + "engines": { + "node": "*" + } + }, "node_modules/combined-stream": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", @@ -704,6 +793,41 @@ "dot-object": "bin/dot-object" } }, + "node_modules/duplexer2": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/duplexer2/-/duplexer2-0.1.4.tgz", + "integrity": "sha512-asLFVfWWtJ90ZyOUHMqk7/S2w2guQKxUI2itj3d92ADHhxUSbCMGi1f1cBcJ7xM1To+pE/Khbwo1yuNbMEPKeA==", + "dependencies": { + "readable-stream": "^2.0.2" + } + }, + "node_modules/duplexer2/node_modules/readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/duplexer2/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "node_modules/duplexer2/node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, "node_modules/end-of-stream": { "version": "1.4.4", "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", @@ -743,6 +867,20 @@ "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==" }, + "node_modules/fstream": { + "version": "1.0.12", + "resolved": "https://registry.npmjs.org/fstream/-/fstream-1.0.12.tgz", + "integrity": "sha512-WvJ193OHa0GHPEL+AycEJgxvBEwyfRkN1vhjca23OaPVMCaLCXTd5qAu82AjTcgP1UJmytkOKb63Ypde7raDIg==", + "dependencies": { + "graceful-fs": "^4.1.2", + "inherits": "~2.0.0", + "mkdirp": ">=0.5 0", + "rimraf": "2" + }, + "engines": { + "node": ">=0.6" + } + }, "node_modules/glob": { "version": "7.2.3", "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", @@ -856,6 +994,11 @@ "safe-buffer": "~5.1.0" } }, + "node_modules/listenercount": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/listenercount/-/listenercount-1.0.1.tgz", + "integrity": "sha512-3mk/Zag0+IJxeDrxSgaDPy4zZ3w05PRZeJNnlWhzFz5OkX49J4krc+A8X2d2M69vGMBEX0uyl8M+W+8gH+kBqQ==" + }, "node_modules/lodash": { "version": "4.17.21", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", @@ -924,6 +1067,25 @@ "node": "*" } }, + "node_modules/minimist": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", + "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/mkdirp": { + "version": "0.5.6", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz", + "integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==", + "dependencies": { + "minimist": "^1.2.6" + }, + "bin": { + "mkdirp": "bin/cmd.js" + } + }, "node_modules/no-case": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/no-case/-/no-case-3.0.4.tgz", @@ -1057,6 +1219,17 @@ "node": ">=10" } }, + "node_modules/rimraf": { + "version": "2.7.1", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz", + "integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==", + "dependencies": { + "glob": "^7.1.3" + }, + "bin": { + "rimraf": "bin.js" + } + }, "node_modules/safe-buffer": { "version": "5.2.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", @@ -1081,6 +1254,11 @@ "resolved": "https://registry.npmjs.org/sax/-/sax-1.2.4.tgz", "integrity": "sha512-NqVDv9TpANUjFm0N8uM5GxL36UgKi9/atZw+x7YFnQ8ckwFGKrl4xX4yWtrey3UJm5nP1kUbnYgLopqWNSRhWw==" }, + "node_modules/setimmediate": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz", + "integrity": "sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA==" + }, "node_modules/string_decoder": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", @@ -1109,6 +1287,14 @@ "resolved": "https://registry.npmjs.org/tr46/-/tr46-0.0.3.tgz", "integrity": "sha512-N3WMsuqV66lT30CrXNbEjx4GEwlow3v6rr4mCcv6prnfwhS01rkgyFdjPNBYd9br7LpXV1+Emh01fHnq2Gdgrw==" }, + "node_modules/traverse": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/traverse/-/traverse-0.3.9.tgz", + "integrity": "sha512-iawgk0hLP3SxGKDfnDJf8wTz4p2qImnyihM5Hh/sGvQ3K37dPi/w8sRhdNIxYA1TwFwc5mDhIJq+O0RsvXBKdQ==", + "engines": { + "node": "*" + } + }, "node_modules/ts-poet": { "version": "4.15.0", "resolved": "https://registry.npmjs.org/ts-poet/-/ts-poet-4.15.0.tgz", @@ -1177,6 +1363,50 @@ "resolved": "https://registry.npmjs.org/universal-user-agent/-/universal-user-agent-6.0.0.tgz", "integrity": "sha512-isyNax3wXoKaulPDZWHQqbmIx1k2tb9fb3GGDBRxCscfYV2Ch7WxPArBsFEG8s/safwXTT7H4QGhaIkTp9447w==" }, + "node_modules/unzipper": { + "version": "0.10.14", + "resolved": "https://registry.npmjs.org/unzipper/-/unzipper-0.10.14.tgz", + "integrity": "sha512-ti4wZj+0bQTiX2KmKWuwj7lhV+2n//uXEotUmGuQqrbVZSEGFMbI68+c6JCQ8aAmUWYvtHEz2A8K6wXvueR/6g==", + "dependencies": { + "big-integer": "^1.6.17", + "binary": "~0.3.0", + "bluebird": "~3.4.1", + "buffer-indexof-polyfill": "~1.0.0", + "duplexer2": "~0.1.4", + "fstream": "^1.0.12", + "graceful-fs": "^4.2.2", + "listenercount": "~1.0.1", + "readable-stream": "~2.3.6", + "setimmediate": "~1.0.4" + } + }, + "node_modules/unzipper/node_modules/readable-stream": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.8.tgz", + "integrity": "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA==", + "dependencies": { + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" + } + }, + "node_modules/unzipper/node_modules/safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "node_modules/unzipper/node_modules/string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "dependencies": { + "safe-buffer": "~5.1.0" + } + }, "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", diff --git a/packages/artifact/package.json b/packages/artifact/package.json index b9e86639..dc776716 100644 --- a/packages/artifact/package.json +++ b/packages/artifact/package.json @@ -46,11 +46,14 @@ "@octokit/core": "^3.5.1", "@octokit/plugin-request-log": "^1.0.4", "@octokit/plugin-retry": "^3.0.9", + "@octokit/request-error": "^5.0.0", "@protobuf-ts/plugin": "^2.2.3-alpha.1", + "@types/unzipper": "^0.10.6", "archiver": "^5.3.1", "crypto": "^1.0.1", "jwt-decode": "^3.1.2", - "twirp-ts": "^2.5.0" + "twirp-ts": "^2.5.0", + "unzipper": "^0.10.14" }, "devDependencies": { "@types/archiver": "^5.3.2", diff --git a/packages/artifact/src/artifact.ts b/packages/artifact/src/artifact.ts index b566df8e..5742b32d 100644 --- a/packages/artifact/src/artifact.ts +++ b/packages/artifact/src/artifact.ts @@ -1,10 +1,10 @@ import {ArtifactClient, Client} from './internal/client' -import {UploadOptions, UploadResponse} from './internal/shared/interfaces' /** * Exported functionality that we want to expose for any users of @actions/artifact */ -export {ArtifactClient, UploadOptions, UploadResponse} +export * from './internal/shared/interfaces' +export {ArtifactClient} export function create(): ArtifactClient { return Client.create() diff --git a/packages/artifact/src/internal/download/download-artifact.ts b/packages/artifact/src/internal/download/download-artifact.ts index 7e9dfbfd..6301bfb5 100644 --- a/packages/artifact/src/internal/download/download-artifact.ts +++ b/packages/artifact/src/internal/download/download-artifact.ts @@ -1,7 +1,46 @@ +import fs from 'fs/promises' +import * as github from '@actions/github' +import * as core from '@actions/core' +import * as httpClient from '@actions/http-client' +import unzipper from 'unzipper' import { DownloadArtifactOptions, DownloadArtifactResponse } from '../shared/interfaces' +import {getUserAgentString} from '../shared/user-agent' +import {getGitHubWorkspaceDir} from '../shared/config' + +const scrubQueryParameters = (url: string): string => { + const parsed = new URL(url) + parsed.search = '' + return parsed.toString() +} + +async function exists(path: string): Promise { + try { + await fs.access(path) + return true + } catch (error) { + if (error.code === 'ENOENT') { + return false + } else { + throw error + } + } +} + +async function streamExtract(url: string, directory: string): Promise { + const client = new httpClient.HttpClient(getUserAgentString()) + const response = await client.get(url) + + if (response.message.statusCode !== 200) { + throw new Error( + `Unexpected HTTP response from blob storage: ${response.message.statusCode} ${response.message.statusMessage}` + ) + } + + return response.message.pipe(unzipper.Extract({path: directory})).promise() +} export async function downloadArtifact( artifactId: number, @@ -10,5 +49,53 @@ export async function downloadArtifact( token: string, options?: DownloadArtifactOptions ): Promise { - throw new Error('Not implemented') + const downloadPath = options?.path || getGitHubWorkspaceDir() + + if (!(await exists(downloadPath))) { + core.debug( + `Artifact destination folder does not exist, creating: ${downloadPath}` + ) + await fs.mkdir(downloadPath, {recursive: true}) + } else { + core.debug(`Artifact destination folder already exists: ${downloadPath}`) + } + + const api = github.getOctokit(token) + + core.info( + `Downloading artifact '${artifactId}' from '${repositoryOwner}/${repositoryName}'` + ) + + const {headers, status} = await api.rest.actions.downloadArtifact({ + owner: repositoryOwner, + repo: repositoryName, + artifact_id: artifactId, + archive_format: 'zip', + request: { + redirect: 'manual' + } + }) + + if (status !== 302) { + throw new Error(`Unable to download artifact. Unexpected status: ${status}`) + } + + const {location} = headers + if (!location) { + throw new Error(`Unable to redirect to artifact download url`) + } + + core.info( + `Redirecting to blob download url: ${scrubQueryParameters(location)}` + ) + + try { + core.info(`Starting download of artifact to: ${downloadPath}`) + await streamExtract(location, downloadPath) + core.info(`Artifact download completed successfully.`) + } catch (error) { + throw new Error(`Unable to download and extract artifact: ${error.message}`) + } + + return {success: true, downloadPath} } diff --git a/packages/artifact/src/internal/find/get-artifact.ts b/packages/artifact/src/internal/find/get-artifact.ts index 3b28ff91..24c38a99 100644 --- a/packages/artifact/src/internal/find/get-artifact.ts +++ b/packages/artifact/src/internal/find/get-artifact.ts @@ -1,4 +1,12 @@ import {GetArtifactResponse} from '../shared/interfaces' +import {getOctokit} from '@actions/github' +import {getUserAgentString} from '../shared/user-agent' +import {defaults as defaultGitHubOptions} from '@actions/github/lib/utils' +import {getRetryOptions} from './retry-options' +import {requestLog} from '@octokit/plugin-request-log' +import {retry} from '@octokit/plugin-retry' +import * as core from '@actions/core' +import {OctokitOptions} from '@octokit/core/dist-types/types' export async function getArtifact( artifactName: string, @@ -7,5 +15,55 @@ export async function getArtifact( repositoryName: string, token: string ): Promise { - throw new Error('Not implemented') + const [retryOpts, requestOpts] = getRetryOptions(defaultGitHubOptions) + + const opts: OctokitOptions = { + log: undefined, + userAgent: getUserAgentString(), + previews: undefined, + retry: retryOpts, + request: requestOpts + } + + const github = getOctokit(token, opts, retry, requestLog) + + const getArtifactResp = await github.request( + 'GET /repos/{owner}/{repo}/actions/runs/{run_id}/artifacts{?name}', + { + owner: repositoryOwner, + repo: repositoryName, + run_id: workflowRunId, + name: artifactName + } + ) + + if (getArtifactResp.status !== 200) { + core.warning(`non-200 response from GitHub API: ${getArtifactResp.status}`) + return { + success: false + } + } + + if (getArtifactResp.data.artifacts.length === 0) { + core.warning('no artifacts found') + return { + success: false + } + } + + if (getArtifactResp.data.artifacts.length > 1) { + core.warning( + 'more than one artifact found for a single name, returning first' + ) + } + + return { + success: true, + artifact: { + name: getArtifactResp.data.artifacts[0].name, + id: getArtifactResp.data.artifacts[0].id, + url: getArtifactResp.data.artifacts[0].url, + size: getArtifactResp.data.artifacts[0].size_in_bytes + } + } } diff --git a/packages/artifact/src/internal/find/list-artifacts.ts b/packages/artifact/src/internal/find/list-artifacts.ts index 7fd96838..ea15fb36 100644 --- a/packages/artifact/src/internal/find/list-artifacts.ts +++ b/packages/artifact/src/internal/find/list-artifacts.ts @@ -2,26 +2,16 @@ import {info, warning, debug} from '@actions/core' import {getOctokit} from '@actions/github' import {ListArtifactsResponse, Artifact} from '../shared/interfaces' import {getUserAgentString} from '../shared/user-agent' -import {RetryOptions, getRetryOptions} from './retry-options' +import {getRetryOptions} from './retry-options' import {defaults as defaultGitHubOptions} from '@actions/github/lib/utils' import {requestLog} from '@octokit/plugin-request-log' import {retry} from '@octokit/plugin-retry' -import {RequestRequestOptions} from '@octokit/types' - -type Options = { - log?: Console - userAgent?: string - previews?: string[] - retry?: RetryOptions - request?: RequestRequestOptions -} +import {OctokitOptions} from '@octokit/core/dist-types/types' // Limiting to 1000 for perf reasons const maximumArtifactCount = 1000 const paginationCount = 100 const maxNumberOfPages = maximumArtifactCount / paginationCount -const maxRetryNumber = 5 -const exemptStatusCodes = [400, 401, 403, 404, 422] // https://github.com/octokit/plugin-retry.js/blob/9a2443746c350b3beedec35cf26e197ea318a261/src/index.ts#L14 export async function listArtifacts( workflowRunId: number, @@ -29,16 +19,14 @@ export async function listArtifacts( repositoryName: string, token: string ): Promise { - info(`Fetching artifact list for workflow run ${workflowRunId} in repository ${repositoryOwner}/${repositoryName}`) - - const artifacts: Artifact[] = [] - const [retryOpts, requestOpts] = getRetryOptions( - maxRetryNumber, - exemptStatusCodes, - defaultGitHubOptions + info( + `Fetching artifact list for workflow run ${workflowRunId} in repository ${repositoryOwner}/${repositoryName}` ) - const opts: Options = { + const artifacts: Artifact[] = [] + const [retryOpts, requestOpts] = getRetryOptions(defaultGitHubOptions) + + const opts: OctokitOptions = { log: undefined, userAgent: getUserAgentString(), previews: undefined, @@ -70,14 +58,14 @@ export async function listArtifacts( } // Iterate over the first page - listArtifactResponse.artifacts.forEach(artifact => { + for (const artifact of listArtifactResponse.artifacts) { artifacts.push({ name: artifact.name, id: artifact.id, url: artifact.url, size: artifact.size_in_bytes }) - }) + } // Iterate over any remaining pages for ( @@ -97,19 +85,19 @@ export async function listArtifacts( page: currentPageNumber }) - listArtifactResponse.artifacts.forEach(artifact => { + for (const artifact of listArtifactResponse.artifacts) { artifacts.push({ name: artifact.name, id: artifact.id, url: artifact.url, size: artifact.size_in_bytes }) - }) + } } info(`Finished fetching artifact list`) return { - artifacts: artifacts + artifacts } } diff --git a/packages/artifact/src/internal/find/retry-options.ts b/packages/artifact/src/internal/find/retry-options.ts index 3fe51b5a..ab10309b 100644 --- a/packages/artifact/src/internal/find/retry-options.ts +++ b/packages/artifact/src/internal/find/retry-options.ts @@ -7,10 +7,14 @@ export type RetryOptions = { enabled?: boolean } +// Defaults for fetching artifacts +const defaultMaxRetryNumber = 5 +const defaultExemptStatusCodes = [400, 401, 403, 404, 422] // https://github.com/octokit/plugin-retry.js/blob/9a2443746c350b3beedec35cf26e197ea318a261/src/index.ts#L14 + export function getRetryOptions( - retries: number, - exemptStatusCodes: number[], - defaultOptions: OctokitOptions + defaultOptions: OctokitOptions, + retries: number = defaultMaxRetryNumber, + exemptStatusCodes: number[] = defaultExemptStatusCodes ): [RetryOptions, RequestRequestOptions | undefined] { if (retries <= 0) { return [{enabled: false}, defaultOptions.request] diff --git a/packages/artifact/src/internal/shared/config.ts b/packages/artifact/src/internal/shared/config.ts index dabb853b..8d8a1668 100644 --- a/packages/artifact/src/internal/shared/config.ts +++ b/packages/artifact/src/internal/shared/config.ts @@ -26,3 +26,11 @@ export function isGhes(): boolean { ) return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM' } + +export function getGitHubWorkspaceDir(): string { + const ghWorkspaceDir = process.env['GITHUB_WORKSPACE'] + if (!ghWorkspaceDir) { + throw new Error('Unable to get the GITHUB_WORKSPACE env variable') + } + return ghWorkspaceDir +} diff --git a/packages/artifact/src/internal/shared/interfaces.ts b/packages/artifact/src/internal/shared/interfaces.ts index d1f7a7aa..7e3e862f 100644 --- a/packages/artifact/src/internal/shared/interfaces.ts +++ b/packages/artifact/src/internal/shared/interfaces.ts @@ -80,6 +80,10 @@ export interface DownloadArtifactResponse { * If the artifact download was successful */ success: boolean + /** + * The path where the artifact was downloaded to + */ + downloadPath?: string } export interface DownloadArtifactOptions { @@ -87,12 +91,6 @@ export interface DownloadArtifactOptions { * Denotes where the artifact will be downloaded to. If not specified then the artifact is download to GITHUB_WORKSPACE */ path?: string - - /** - * Specifies if a root folder with the artifact name is created for the artifact that is downloaded - * Zip contents are expanded into this folder. Defaults to false if not specified - * */ - createArtifactFolder?: boolean } /***************************************************************************** diff --git a/packages/artifact/src/internal/shared/user-agent.ts b/packages/artifact/src/internal/shared/user-agent.ts index eee01446..94c07e89 100644 --- a/packages/artifact/src/internal/shared/user-agent.ts +++ b/packages/artifact/src/internal/shared/user-agent.ts @@ -1,4 +1,5 @@ -var packageJson = require('../../../package.json') +// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports +const packageJson = require('../../../package.json') /** * Ensure that this User Agent String is used in all HTTP calls so that we can monitor telemetry between different versions of this package diff --git a/packages/artifact/src/internal/upload/blob-upload.ts b/packages/artifact/src/internal/upload/blob-upload.ts index 82af468a..42a3fbd5 100644 --- a/packages/artifact/src/internal/upload/blob-upload.ts +++ b/packages/artifact/src/internal/upload/blob-upload.ts @@ -91,6 +91,6 @@ export async function uploadZipToBlobStorage( return { isSuccess: true, uploadSize: uploadByteCount, - md5Hash: md5Hash + md5Hash } } diff --git a/packages/artifact/src/internal/upload/upload-artifact.ts b/packages/artifact/src/internal/upload/upload-artifact.ts index 35cd9ac3..55911476 100644 --- a/packages/artifact/src/internal/upload/upload-artifact.ts +++ b/packages/artifact/src/internal/upload/upload-artifact.ts @@ -61,7 +61,7 @@ export async function uploadArtifact( const createArtifactReq: CreateArtifactRequest = { workflowRunBackendId: backendIds.workflowRunBackendId, workflowJobRunBackendId: backendIds.workflowJobRunBackendId, - name: name, + name, version: 4 } @@ -96,13 +96,13 @@ export async function uploadArtifact( const finalizeArtifactReq: FinalizeArtifactRequest = { workflowRunBackendId: backendIds.workflowRunBackendId, workflowJobRunBackendId: backendIds.workflowJobRunBackendId, - name: name, - size: uploadResult.uploadSize!.toString() + name, + size: uploadResult.uploadSize ? uploadResult.uploadSize.toString() : '0' } if (uploadResult.md5Hash) { finalizeArtifactReq.hash = StringValue.create({ - value: `md5:${uploadResult.md5Hash!}` + value: `md5:${uploadResult.md5Hash}` }) }