From 19d391836e55549dc6517cfd50788f3142b1df66 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 14:52:10 +0900 Subject: [PATCH] fix: use artifact client for download (#135) * fix: use artifact client for download * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix --- src/client.ts | 25 +++++++++++++++------- src/constants.ts | 2 ++ src/service.ts | 54 +++++++++++++++++++++++------------------------- 3 files changed, 46 insertions(+), 35 deletions(-) diff --git a/src/client.ts b/src/client.ts index 4c67e738..d75b8e27 100644 --- a/src/client.ts +++ b/src/client.ts @@ -5,6 +5,9 @@ import { summary } from '@actions/core'; import { Repository } from './repository'; import { workspace } from './path'; +import { log } from './logger'; +import { join } from 'path'; +import { DOWNLOAD_PATH } from './constants'; export type Octokit = ReturnType; @@ -33,16 +36,24 @@ export const createClient = (repository: Repository, octokit: Octokit) => { }); return res; }, - downloadArtifact: async (artifactId: number) => { - return backOff( + downloadArtifact: async (token: string, artifactId: number, runId: number) => { + const { downloadPath } = await backOff( () => - octokit.rest.actions.downloadArtifact({ - ...repository, - artifact_id: artifactId, - archive_format: 'zip', + artifactClient.downloadArtifact(artifactId, { + path: DOWNLOAD_PATH, + findBy: { + token, + workflowRunId: runId, + repositoryName: repository.repo, + repositoryOwner: repository.owner, + }, }), - { numOfAttempts: 5 }, + { + numOfAttempts: 5, + }, ); + log.info('downloadPath:', downloadPath); + return; }, postComment: async (issueNumber: number, comment: string) => { const _ = await backOff( diff --git a/src/constants.ts b/src/constants.ts index 978a85a9..db5a6b12 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -5,3 +5,5 @@ export const JSON_NAME = '0'; export const ARTIFACT_NAME = 'reg'; export const WORKSPACE_DIR_NAME = '__reg__'; + +export const DOWNLOAD_PATH = '__reg_download'; diff --git a/src/service.ts b/src/service.ts index 3263952e..c047c62b 100644 --- a/src/service.ts +++ b/src/service.ts @@ -1,9 +1,8 @@ -import * as fs from 'fs'; +import * as fs from 'fs/promises'; import * as path from 'path'; import cpy from 'cpy'; -import { sync as globSync } from 'glob'; +import { glob } from 'glob'; import makeDir from 'make-dir'; -import Zip from 'adm-zip'; import { log } from './logger'; import { Config } from './config'; @@ -17,34 +16,33 @@ import { pushImages } from './push'; import { targetDir } from './helper'; type DownloadClient = { - downloadArtifact: (id: number) => Promise<{ data: unknown }>; + downloadArtifact: (token: string, artifactId: number, runId: number, artifactName: string) => Promise; }; // Download expected images from target artifact. -const downloadExpectedImages = async (client: DownloadClient, latestArtifactId: number) => { +const downloadExpectedImages = async ( + client: DownloadClient, + latestArtifactId: number, + runId: number, + config: Config, +) => { log.info(`Start to download expected images, artifact id = ${latestArtifactId}`); try { - const zip = await client.downloadArtifact(latestArtifactId); + await client.downloadArtifact(config.githubToken, latestArtifactId, runId, config.artifactName); + + const files = await glob(`${constants.DOWNLOAD_PATH}/**/${constants.ACTUAL_DIR_NAME}/**/*`); await Promise.all( - new Zip(Buffer.from(zip.data as any)) - .getEntries() - .filter(f => { - log.info('entryName:', f.entryName); - return !f.isDirectory && f.entryName.startsWith(constants.ACTUAL_DIR_NAME); - }) - .map(async file => { - const f = path.join( - workspace(), - file.entryName.replace(constants.ACTUAL_DIR_NAME, constants.EXPECTED_DIR_NAME), - ); - await makeDir(path.dirname(f)); - log.info('download to', f); - await fs.promises.writeFile(f, file.getData()); - }), - ).catch(e => { - log.error('Failed to extract images.', e); - throw e; - }); + files.map(async file => { + if (!/(png|jpg|jpeg|tiff|bmp|gif)$/.test(file)) return; + const f = path.join( + workspace(), + file.replace(path.join(constants.DOWNLOAD_PATH, constants.ACTUAL_DIR_NAME), constants.EXPECTED_DIR_NAME), + ); + await makeDir(path.dirname(f)); + log.info('download to', f); + await fs.copyFile(file, f); + }), + ); } catch (e: any) { if (e.message === 'Artifact has expired') { log.error('Failed to download expected images. Because expected artifact has already expired.'); @@ -76,7 +74,7 @@ const compareAndUpload = async (client: UploadClient, config: Config): Promise { log.info(`start initialization with config.`, config); // Cleanup workspace - await fs.promises.rm(workspace(), { + await fs.rm(workspace(), { recursive: true, force: true, }); @@ -180,7 +178,7 @@ export const run = async ({ const { run: targetRun, artifact } = runAndArtifact; // Download and copy expected images to workspace. - await downloadExpectedImages(client, artifact.id); + await downloadExpectedImages(client, artifact.id, targetRun.id, config); const result = await compareAndUpload(client, config);