From 17ad0b778b1d83c16f28f1ca07946b4962039dbf Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 12:27:46 +0900 Subject: [PATCH 01/25] fix: use artifact client for download --- src/client.ts | 18 +++++++++--------- src/service.ts | 7 ++++--- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/client.ts b/src/client.ts index 4c67e738..31cb8b70 100644 --- a/src/client.ts +++ b/src/client.ts @@ -2,9 +2,11 @@ import * as github from '@actions/github'; import { DefaultArtifactClient } from '@actions/artifact'; import { backOff } from 'exponential-backoff'; import { summary } from '@actions/core'; +import { readFile } from 'fs/promises'; import { Repository } from './repository'; import { workspace } from './path'; +import { log } from './logger'; export type Octokit = ReturnType; @@ -34,15 +36,13 @@ export const createClient = (repository: Repository, octokit: Octokit) => { return res; }, downloadArtifact: async (artifactId: number) => { - return backOff( - () => - octokit.rest.actions.downloadArtifact({ - ...repository, - artifact_id: artifactId, - archive_format: 'zip', - }), - { numOfAttempts: 5 }, - ); + const { downloadPath } = await backOff(() => artifactClient.downloadArtifact(artifactId, {}), { + numOfAttempts: 5, + }); + log.info('downloadPath:', downloadPath); + if (!downloadPath) throw new Error('Failed to download artifact.'); + const data = await readFile(downloadPath); + return { data }; }, postComment: async (issueNumber: number, comment: string) => { const _ = await backOff( diff --git a/src/service.ts b/src/service.ts index 3263952e..748134e3 100644 --- a/src/service.ts +++ b/src/service.ts @@ -17,16 +17,17 @@ import { pushImages } from './push'; import { targetDir } from './helper'; type DownloadClient = { - downloadArtifact: (id: number) => Promise<{ data: unknown }>; + downloadArtifact: (id: number) => Promise<{ data: Buffer }>; }; // Download expected images from target artifact. const downloadExpectedImages = async (client: DownloadClient, latestArtifactId: number) => { log.info(`Start to download expected images, artifact id = ${latestArtifactId}`); try { - const zip = await client.downloadArtifact(latestArtifactId); + const { data: buf } = await client.downloadArtifact(latestArtifactId); + log.info('download size: ', buf.byteLength); await Promise.all( - new Zip(Buffer.from(zip.data as any)) + new Zip(buf) .getEntries() .filter(f => { log.info('entryName:', f.entryName); From af1c61fc5969c591bb6aa063a1b152fd9bddf36c Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 12:33:48 +0900 Subject: [PATCH 02/25] fix --- src/client.ts | 19 +++++++++++++++---- src/service.ts | 13 +++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/client.ts b/src/client.ts index 31cb8b70..0b37ab17 100644 --- a/src/client.ts +++ b/src/client.ts @@ -35,10 +35,21 @@ export const createClient = (repository: Repository, octokit: Octokit) => { }); return res; }, - downloadArtifact: async (artifactId: number) => { - const { downloadPath } = await backOff(() => artifactClient.downloadArtifact(artifactId, {}), { - numOfAttempts: 5, - }); + downloadArtifact: async (token: string, artifactId: number, runId: number) => { + const { downloadPath } = await backOff( + () => + artifactClient.downloadArtifact(artifactId, { + findBy: { + token, + workflowRunId: runId, + repositoryName: repository.repo, + repositoryOwner: repository.owner, + }, + }), + { + numOfAttempts: 5, + }, + ); log.info('downloadPath:', downloadPath); if (!downloadPath) throw new Error('Failed to download artifact.'); const data = await readFile(downloadPath); diff --git a/src/service.ts b/src/service.ts index 748134e3..af6b3307 100644 --- a/src/service.ts +++ b/src/service.ts @@ -17,14 +17,19 @@ import { pushImages } from './push'; import { targetDir } from './helper'; type DownloadClient = { - downloadArtifact: (id: number) => Promise<{ data: Buffer }>; + downloadArtifact: (token: string, artifactId: number, runId: number) => Promise<{ data: Buffer }>; }; // 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 { data: buf } = await client.downloadArtifact(latestArtifactId); + const { data: buf } = await client.downloadArtifact(config.githubToken, latestArtifactId, runId); log.info('download size: ', buf.byteLength); await Promise.all( new Zip(buf) @@ -181,7 +186,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); From 0431fc375c9b444276493d6ff0898e9088b935db Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 13:30:48 +0900 Subject: [PATCH 03/25] fix --- src/client.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/client.ts b/src/client.ts index 0b37ab17..68fa479d 100644 --- a/src/client.ts +++ b/src/client.ts @@ -39,6 +39,7 @@ export const createClient = (repository: Repository, octokit: Octokit) => { const { downloadPath } = await backOff( () => artifactClient.downloadArtifact(artifactId, { + path: './', findBy: { token, workflowRunId: runId, From e1de4d89d8bdc61cf30e1847d9d80a99ac36d7ac Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 13:33:06 +0900 Subject: [PATCH 04/25] fix --- src/client.ts | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/client.ts b/src/client.ts index 68fa479d..8cf5650e 100644 --- a/src/client.ts +++ b/src/client.ts @@ -36,25 +36,23 @@ export const createClient = (repository: Repository, octokit: Octokit) => { return res; }, downloadArtifact: async (token: string, artifactId: number, runId: number) => { - const { downloadPath } = await backOff( - () => - artifactClient.downloadArtifact(artifactId, { - path: './', - findBy: { - token, - workflowRunId: runId, - repositoryName: repository.repo, - repositoryOwner: repository.owner, - }, - }), - { - numOfAttempts: 5, - }, - ); - log.info('downloadPath:', downloadPath); - if (!downloadPath) throw new Error('Failed to download artifact.'); - const data = await readFile(downloadPath); - return { data }; + try { + const { downloadPath } = await artifactClient.downloadArtifact(artifactId, { + path: './', + findBy: { + token, + workflowRunId: runId, + repositoryName: repository.repo, + repositoryOwner: repository.owner, + }, + }); + log.info('downloadPath:', downloadPath); + if (!downloadPath) throw new Error('Failed to download artifact.'); + const data = await readFile(downloadPath); + return { data }; + } catch (e) { + console.error(e); + } }, postComment: async (issueNumber: number, comment: string) => { const _ = await backOff( From 481d53d58479b93d694abad9b27d261590ac9d35 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 13:35:13 +0900 Subject: [PATCH 05/25] fix --- src/client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index 8cf5650e..e3a748d4 100644 --- a/src/client.ts +++ b/src/client.ts @@ -38,7 +38,7 @@ export const createClient = (repository: Repository, octokit: Octokit) => { downloadArtifact: async (token: string, artifactId: number, runId: number) => { try { const { downloadPath } = await artifactClient.downloadArtifact(artifactId, { - path: './', + path: './download', findBy: { token, workflowRunId: runId, From 221338f048b8dac4762fcb4468ee89f8cda8ae38 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 13:36:43 +0900 Subject: [PATCH 06/25] fix --- src/client.ts | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/client.ts b/src/client.ts index e3a748d4..d032680c 100644 --- a/src/client.ts +++ b/src/client.ts @@ -36,23 +36,25 @@ export const createClient = (repository: Repository, octokit: Octokit) => { return res; }, downloadArtifact: async (token: string, artifactId: number, runId: number) => { - try { - const { downloadPath } = await artifactClient.downloadArtifact(artifactId, { - path: './download', - findBy: { - token, - workflowRunId: runId, - repositoryName: repository.repo, - repositoryOwner: repository.owner, - }, - }); - log.info('downloadPath:', downloadPath); - if (!downloadPath) throw new Error('Failed to download artifact.'); - const data = await readFile(downloadPath); - return { data }; - } catch (e) { - console.error(e); - } + const { downloadPath } = await backOff( + () => + artifactClient.downloadArtifact(artifactId, { + path: '__download', + findBy: { + token, + workflowRunId: runId, + repositoryName: repository.repo, + repositoryOwner: repository.owner, + }, + }), + { + numOfAttempts: 5, + }, + ); + log.info('downloadPath:', downloadPath); + if (!downloadPath) throw new Error('Failed to download artifact.'); + const data = await readFile(downloadPath); + return { data }; }, postComment: async (issueNumber: number, comment: string) => { const _ = await backOff( From 7169fb3cf9d729534032bc6300bc87cfbaef7880 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 13:39:31 +0900 Subject: [PATCH 07/25] fix --- src/client.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index d032680c..eb5ea2bc 100644 --- a/src/client.ts +++ b/src/client.ts @@ -7,6 +7,7 @@ import { readFile } from 'fs/promises'; import { Repository } from './repository'; import { workspace } from './path'; import { log } from './logger'; +import { join } from 'path'; export type Octokit = ReturnType; @@ -39,7 +40,7 @@ export const createClient = (repository: Repository, octokit: Octokit) => { const { downloadPath } = await backOff( () => artifactClient.downloadArtifact(artifactId, { - path: '__download', + path: join(workspace(), '__reg_download'), findBy: { token, workflowRunId: runId, From 12484f800dc3c7917561f4e77a32d87132f47e09 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 13:43:20 +0900 Subject: [PATCH 08/25] fix --- src/client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index eb5ea2bc..16cd0e6f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -40,7 +40,7 @@ export const createClient = (repository: Repository, octokit: Octokit) => { const { downloadPath } = await backOff( () => artifactClient.downloadArtifact(artifactId, { - path: join(workspace(), '__reg_download'), + path: join(workspace(), '__reg_download/reg'), findBy: { token, workflowRunId: runId, From d1b7dccbc4b9e4d89485a7161eaa9a7a68e16353 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 13:44:48 +0900 Subject: [PATCH 09/25] fix --- src/client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index 16cd0e6f..3d94a3f7 100644 --- a/src/client.ts +++ b/src/client.ts @@ -40,7 +40,7 @@ export const createClient = (repository: Repository, octokit: Octokit) => { const { downloadPath } = await backOff( () => artifactClient.downloadArtifact(artifactId, { - path: join(workspace(), '__reg_download/reg'), + path: join(workspace(), '__reg_download/reg.zip'), findBy: { token, workflowRunId: runId, From 6b5ed4e175b2e269ae8322038add7de862a02005 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 13:47:11 +0900 Subject: [PATCH 10/25] fix --- src/client.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client.ts b/src/client.ts index 3d94a3f7..9b6f0437 100644 --- a/src/client.ts +++ b/src/client.ts @@ -40,7 +40,7 @@ export const createClient = (repository: Repository, octokit: Octokit) => { const { downloadPath } = await backOff( () => artifactClient.downloadArtifact(artifactId, { - path: join(workspace(), '__reg_download/reg.zip'), + path: join(workspace(), '__reg_download'), findBy: { token, workflowRunId: runId, @@ -54,7 +54,7 @@ export const createClient = (repository: Repository, octokit: Octokit) => { ); log.info('downloadPath:', downloadPath); if (!downloadPath) throw new Error('Failed to download artifact.'); - const data = await readFile(downloadPath); + const data = await readFile(join(downloadPath, 'reg.zip')); return { data }; }, postComment: async (issueNumber: number, comment: string) => { From 4df86be9a121ca6c278b7d2c2c3839af0abff681 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 13:48:35 +0900 Subject: [PATCH 11/25] fix --- src/client.ts | 4 ++-- src/service.ts | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/client.ts b/src/client.ts index 9b6f0437..5ebf6108 100644 --- a/src/client.ts +++ b/src/client.ts @@ -36,7 +36,7 @@ export const createClient = (repository: Repository, octokit: Octokit) => { }); return res; }, - downloadArtifact: async (token: string, artifactId: number, runId: number) => { + downloadArtifact: async (token: string, artifactId: number, runId: number, artifactName: string) => { const { downloadPath } = await backOff( () => artifactClient.downloadArtifact(artifactId, { @@ -54,7 +54,7 @@ export const createClient = (repository: Repository, octokit: Octokit) => { ); log.info('downloadPath:', downloadPath); if (!downloadPath) throw new Error('Failed to download artifact.'); - const data = await readFile(join(downloadPath, 'reg.zip')); + const data = await readFile(join(downloadPath, `${artifactName}.zip`)); return { data }; }, postComment: async (issueNumber: number, comment: string) => { diff --git a/src/service.ts b/src/service.ts index af6b3307..46f7fcd8 100644 --- a/src/service.ts +++ b/src/service.ts @@ -17,7 +17,12 @@ import { pushImages } from './push'; import { targetDir } from './helper'; type DownloadClient = { - downloadArtifact: (token: string, artifactId: number, runId: number) => Promise<{ data: Buffer }>; + downloadArtifact: ( + token: string, + artifactId: number, + runId: number, + artifactName: string, + ) => Promise<{ data: Buffer }>; }; // Download expected images from target artifact. @@ -29,7 +34,12 @@ const downloadExpectedImages = async ( ) => { log.info(`Start to download expected images, artifact id = ${latestArtifactId}`); try { - const { data: buf } = await client.downloadArtifact(config.githubToken, latestArtifactId, runId); + const { data: buf } = await client.downloadArtifact( + config.githubToken, + latestArtifactId, + runId, + config.artifactName, + ); log.info('download size: ', buf.byteLength); await Promise.all( new Zip(buf) From 2df6b890a91f10a1865a0cd6e7636776b52910be Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 13:51:52 +0900 Subject: [PATCH 12/25] fix --- src/client.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index 5ebf6108..2b8430e6 100644 --- a/src/client.ts +++ b/src/client.ts @@ -40,7 +40,6 @@ export const createClient = (repository: Repository, octokit: Octokit) => { const { downloadPath } = await backOff( () => artifactClient.downloadArtifact(artifactId, { - path: join(workspace(), '__reg_download'), findBy: { token, workflowRunId: runId, From 8e0481d5266ea8f1a3568c260084044fee9e5124 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 13:54:24 +0900 Subject: [PATCH 13/25] fix --- src/client.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/client.ts b/src/client.ts index 2b8430e6..5ebf6108 100644 --- a/src/client.ts +++ b/src/client.ts @@ -40,6 +40,7 @@ export const createClient = (repository: Repository, octokit: Octokit) => { const { downloadPath } = await backOff( () => artifactClient.downloadArtifact(artifactId, { + path: join(workspace(), '__reg_download'), findBy: { token, workflowRunId: runId, From 37eec198e959a566a1863d7e277b770f21abfe9e Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 13:55:59 +0900 Subject: [PATCH 14/25] fix --- src/client.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/client.ts b/src/client.ts index 5ebf6108..67a61acb 100644 --- a/src/client.ts +++ b/src/client.ts @@ -8,6 +8,7 @@ import { Repository } from './repository'; import { workspace } from './path'; import { log } from './logger'; import { join } from 'path'; +import { glob } from 'fast-glob'; export type Octokit = ReturnType; @@ -54,6 +55,7 @@ export const createClient = (repository: Repository, octokit: Octokit) => { ); log.info('downloadPath:', downloadPath); if (!downloadPath) throw new Error('Failed to download artifact.'); + console.log(glob.sync(join(downloadPath, '**/*'))) const data = await readFile(join(downloadPath, `${artifactName}.zip`)); return { data }; }, From ad485087fe1390c9d19dc7a539abdb91b0a473fa Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 14:05:14 +0900 Subject: [PATCH 15/25] fix --- src/client.ts | 10 +++------- src/constants.ts | 2 ++ src/service.ts | 35 +++++++++++------------------------ 3 files changed, 16 insertions(+), 31 deletions(-) diff --git a/src/client.ts b/src/client.ts index 67a61acb..c83db045 100644 --- a/src/client.ts +++ b/src/client.ts @@ -2,13 +2,12 @@ import * as github from '@actions/github'; import { DefaultArtifactClient } from '@actions/artifact'; import { backOff } from 'exponential-backoff'; import { summary } from '@actions/core'; -import { readFile } from 'fs/promises'; import { Repository } from './repository'; import { workspace } from './path'; import { log } from './logger'; import { join } from 'path'; -import { glob } from 'fast-glob'; +import { DOWNLOAD_PATH } from './constants'; export type Octokit = ReturnType; @@ -41,7 +40,7 @@ export const createClient = (repository: Repository, octokit: Octokit) => { const { downloadPath } = await backOff( () => artifactClient.downloadArtifact(artifactId, { - path: join(workspace(), '__reg_download'), + path: join(workspace(), DOWNLOAD_PATH), findBy: { token, workflowRunId: runId, @@ -54,10 +53,7 @@ export const createClient = (repository: Repository, octokit: Octokit) => { }, ); log.info('downloadPath:', downloadPath); - if (!downloadPath) throw new Error('Failed to download artifact.'); - console.log(glob.sync(join(downloadPath, '**/*'))) - const data = await readFile(join(downloadPath, `${artifactName}.zip`)); - return { data }; + 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 46f7fcd8..a6bc8de4 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'; @@ -34,33 +33,21 @@ const downloadExpectedImages = async ( ) => { log.info(`Start to download expected images, artifact id = ${latestArtifactId}`); try { - const { data: buf } = await client.downloadArtifact( - config.githubToken, - latestArtifactId, - runId, - config.artifactName, - ); - log.info('download size: ', buf.byteLength); + await client.downloadArtifact(config.githubToken, latestArtifactId, runId, config.artifactName); + const files = await glob(`${constants.DOWNLOAD_PATH}/**/*`); await Promise.all( - new Zip(buf) - .getEntries() + files .filter(f => { - log.info('entryName:', f.entryName); - return !f.isDirectory && f.entryName.startsWith(constants.ACTUAL_DIR_NAME); + log.info('fileName:', f); + return f.startsWith(constants.ACTUAL_DIR_NAME); }) .map(async file => { - const f = path.join( - workspace(), - file.entryName.replace(constants.ACTUAL_DIR_NAME, constants.EXPECTED_DIR_NAME), - ); + const f = path.join(workspace(), file.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()); + await fs.copyFile(file, f); }), - ).catch(e => { - log.error('Failed to extract images.', e); - throw e; - }); + ); } catch (e: any) { if (e.message === 'Artifact has expired') { log.error('Failed to download expected images. Because expected artifact has already expired.'); @@ -92,7 +79,7 @@ const compareAndUpload = async (client: UploadClient, config: Config): Promise Date: Tue, 30 Apr 2024 14:09:31 +0900 Subject: [PATCH 16/25] fix --- src/service.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/service.ts b/src/service.ts index a6bc8de4..38803fb3 100644 --- a/src/service.ts +++ b/src/service.ts @@ -16,12 +16,7 @@ import { pushImages } from './push'; import { targetDir } from './helper'; type DownloadClient = { - downloadArtifact: ( - token: string, - artifactId: number, - runId: number, - artifactName: string, - ) => Promise<{ data: Buffer }>; + downloadArtifact: (token: string, artifactId: number, runId: number, artifactName: string) => Promise; }; // Download expected images from target artifact. @@ -98,7 +93,7 @@ const init = async (config: Config) => { log.info(`start initialization with config.`, config); // Cleanup workspace - await fs.promises.rm(workspace(), { + await fs.rm(workspace(), { recursive: true, force: true, }); From 60faa08d85cc2ec89eaff1c264295b67fa18e7f5 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 14:09:54 +0900 Subject: [PATCH 17/25] fix --- src/client.ts | 2 +- src/service.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/client.ts b/src/client.ts index c83db045..2ea98b1e 100644 --- a/src/client.ts +++ b/src/client.ts @@ -36,7 +36,7 @@ export const createClient = (repository: Repository, octokit: Octokit) => { }); return res; }, - downloadArtifact: async (token: string, artifactId: number, runId: number, artifactName: string) => { + downloadArtifact: async (token: string, artifactId: number, runId: number) => { const { downloadPath } = await backOff( () => artifactClient.downloadArtifact(artifactId, { diff --git a/src/service.ts b/src/service.ts index 38803fb3..809fc5e3 100644 --- a/src/service.ts +++ b/src/service.ts @@ -16,7 +16,7 @@ import { pushImages } from './push'; import { targetDir } from './helper'; type DownloadClient = { - downloadArtifact: (token: string, artifactId: number, runId: number, artifactName: string) => Promise; + downloadArtifact: (token: string, artifactId: number, runId: number) => Promise; }; // Download expected images from target artifact. @@ -28,7 +28,7 @@ const downloadExpectedImages = async ( ) => { log.info(`Start to download expected images, artifact id = ${latestArtifactId}`); try { - await client.downloadArtifact(config.githubToken, latestArtifactId, runId, config.artifactName); + await client.downloadArtifact(config.githubToken, latestArtifactId, runId); const files = await glob(`${constants.DOWNLOAD_PATH}/**/*`); await Promise.all( files From bb415ef25b44913df93a36e278523671aed2a9e2 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 14:14:26 +0900 Subject: [PATCH 18/25] fix --- src/client.ts | 2 +- src/service.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/client.ts b/src/client.ts index 2ea98b1e..d75b8e27 100644 --- a/src/client.ts +++ b/src/client.ts @@ -40,7 +40,7 @@ export const createClient = (repository: Repository, octokit: Octokit) => { const { downloadPath } = await backOff( () => artifactClient.downloadArtifact(artifactId, { - path: join(workspace(), DOWNLOAD_PATH), + path: DOWNLOAD_PATH, findBy: { token, workflowRunId: runId, diff --git a/src/service.ts b/src/service.ts index 809fc5e3..79d00e59 100644 --- a/src/service.ts +++ b/src/service.ts @@ -30,6 +30,7 @@ const downloadExpectedImages = async ( try { await client.downloadArtifact(config.githubToken, latestArtifactId, runId); const files = await glob(`${constants.DOWNLOAD_PATH}/**/*`); + log.info('download files:', files); await Promise.all( files .filter(f => { @@ -40,7 +41,7 @@ const downloadExpectedImages = async ( const f = path.join(workspace(), file.replace(constants.ACTUAL_DIR_NAME, constants.EXPECTED_DIR_NAME)); await makeDir(path.dirname(f)); log.info('download to', f); - await fs.copyFile(file, f); + return fs.copyFile(file, f); }), ); } catch (e: any) { From d026505de3fccf737afcd5277fca1766f5da2a0f Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 14:18:32 +0900 Subject: [PATCH 19/25] fix --- src/service.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/service.ts b/src/service.ts index 79d00e59..36057da0 100644 --- a/src/service.ts +++ b/src/service.ts @@ -33,6 +33,7 @@ const downloadExpectedImages = async ( log.info('download files:', files); await Promise.all( files + .map(f => f.replace(`${constants.DOWNLOAD_PATH}/`, '')) .filter(f => { log.info('fileName:', f); return f.startsWith(constants.ACTUAL_DIR_NAME); From 5cecd2fe2cbbba39832c08f16fed056e8235a9d1 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 14:27:50 +0900 Subject: [PATCH 20/25] fix --- src/service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/service.ts b/src/service.ts index 36057da0..98487cd8 100644 --- a/src/service.ts +++ b/src/service.ts @@ -29,7 +29,7 @@ const downloadExpectedImages = async ( log.info(`Start to download expected images, artifact id = ${latestArtifactId}`); try { await client.downloadArtifact(config.githubToken, latestArtifactId, runId); - const files = await glob(`${constants.DOWNLOAD_PATH}/**/*`); + const files = await glob(`${constants.DOWNLOAD_PATH}/**/.{png,jpg,jpeg,tiff,bmp,gif}`); log.info('download files:', files); await Promise.all( files From 8893ed4b6d68f26caf18339521bb452e5f5bf33e Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 14:31:03 +0900 Subject: [PATCH 21/25] fix --- src/service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/service.ts b/src/service.ts index 98487cd8..759fe432 100644 --- a/src/service.ts +++ b/src/service.ts @@ -29,14 +29,14 @@ const downloadExpectedImages = async ( log.info(`Start to download expected images, artifact id = ${latestArtifactId}`); try { await client.downloadArtifact(config.githubToken, latestArtifactId, runId); - const files = await glob(`${constants.DOWNLOAD_PATH}/**/.{png,jpg,jpeg,tiff,bmp,gif}`); + const files = await glob(`${constants.DOWNLOAD_PATH}/**/*`); log.info('download files:', files); await Promise.all( files .map(f => f.replace(`${constants.DOWNLOAD_PATH}/`, '')) .filter(f => { log.info('fileName:', f); - return f.startsWith(constants.ACTUAL_DIR_NAME); + return f.startsWith(constants.ACTUAL_DIR_NAME) && /(png|jpg|jpeg|tiff|bmp|gif)$/.test(f); }) .map(async file => { const f = path.join(workspace(), file.replace(constants.ACTUAL_DIR_NAME, constants.EXPECTED_DIR_NAME)); From 86c0b61632ae0a161900624d85fe026f742ea4fd Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 14:35:33 +0900 Subject: [PATCH 22/25] fix --- src/service.ts | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/service.ts b/src/service.ts index 759fe432..cb3153ce 100644 --- a/src/service.ts +++ b/src/service.ts @@ -29,21 +29,9 @@ const downloadExpectedImages = async ( log.info(`Start to download expected images, artifact id = ${latestArtifactId}`); try { await client.downloadArtifact(config.githubToken, latestArtifactId, runId); - const files = await glob(`${constants.DOWNLOAD_PATH}/**/*`); - log.info('download files:', files); - await Promise.all( - files - .map(f => f.replace(`${constants.DOWNLOAD_PATH}/`, '')) - .filter(f => { - log.info('fileName:', f); - return f.startsWith(constants.ACTUAL_DIR_NAME) && /(png|jpg|jpeg|tiff|bmp|gif)$/.test(f); - }) - .map(async file => { - const f = path.join(workspace(), file.replace(constants.ACTUAL_DIR_NAME, constants.EXPECTED_DIR_NAME)); - await makeDir(path.dirname(f)); - log.info('download to', f); - return fs.copyFile(file, f); - }), + await cpy( + path.join(constants.DOWNLOAD_PATH, '**', constants.ACTUAL_DIR_NAME, `**/*.{png,jpg,jpeg,tiff,bmp,gif}`), + path.join(workspace(), constants.EXPECTED_DIR_NAME), ); } catch (e: any) { if (e.message === 'Artifact has expired') { From 9aeaafb630c454bc97f3f5cc1b5f31df37bc780e Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 14:41:09 +0900 Subject: [PATCH 23/25] fix --- src/service.ts | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/service.ts b/src/service.ts index cb3153ce..cc410714 100644 --- a/src/service.ts +++ b/src/service.ts @@ -16,7 +16,7 @@ import { pushImages } from './push'; import { targetDir } from './helper'; type DownloadClient = { - downloadArtifact: (token: string, artifactId: number, runId: number) => Promise; + downloadArtifact: (token: string, artifactId: number, runId: number, artifactName: string) => Promise; }; // Download expected images from target artifact. @@ -28,11 +28,27 @@ const downloadExpectedImages = async ( ) => { log.info(`Start to download expected images, artifact id = ${latestArtifactId}`); try { - await client.downloadArtifact(config.githubToken, latestArtifactId, runId); + await client.downloadArtifact(config.githubToken, latestArtifactId, runId, config.artifactName); + await cpy( - path.join(constants.DOWNLOAD_PATH, '**', constants.ACTUAL_DIR_NAME, `**/*.{png,jpg,jpeg,tiff,bmp,gif}`), + `${constants.DOWNLOAD_PATH}/**/${constants.ACTUAL_DIR_NAME}/**/*.{png,jpg,jpeg,tiff,bmp,gif}`, path.join(workspace(), constants.EXPECTED_DIR_NAME), ); + + const files = await glob(`${constants.DOWNLOAD_PATH}/**/*`); + await Promise.all( + files + .filter(f => { + log.info('fileName:', f); + return f.startsWith(constants.ACTUAL_DIR_NAME); + }) + .map(async file => { + const f = path.join(workspace(), file.replace(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.'); From 5d5309ced373f2db37508a90b32e4e3dbf376d6a Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 14:43:21 +0900 Subject: [PATCH 24/25] fix --- src/service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/service.ts b/src/service.ts index cc410714..af9a6e74 100644 --- a/src/service.ts +++ b/src/service.ts @@ -30,10 +30,10 @@ const downloadExpectedImages = async ( try { await client.downloadArtifact(config.githubToken, latestArtifactId, runId, config.artifactName); - await cpy( + console.log(await cpy( `${constants.DOWNLOAD_PATH}/**/${constants.ACTUAL_DIR_NAME}/**/*.{png,jpg,jpeg,tiff,bmp,gif}`, path.join(workspace(), constants.EXPECTED_DIR_NAME), - ); + )); const files = await glob(`${constants.DOWNLOAD_PATH}/**/*`); await Promise.all( From 886f4806cd57aebfd544c75cc95b68699946503e Mon Sep 17 00:00:00 2001 From: bokuweb Date: Tue, 30 Apr 2024 14:50:02 +0900 Subject: [PATCH 25/25] fix --- src/service.ts | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/service.ts b/src/service.ts index af9a6e74..c047c62b 100644 --- a/src/service.ts +++ b/src/service.ts @@ -30,24 +30,18 @@ const downloadExpectedImages = async ( try { await client.downloadArtifact(config.githubToken, latestArtifactId, runId, config.artifactName); - console.log(await cpy( - `${constants.DOWNLOAD_PATH}/**/${constants.ACTUAL_DIR_NAME}/**/*.{png,jpg,jpeg,tiff,bmp,gif}`, - path.join(workspace(), constants.EXPECTED_DIR_NAME), - )); - - const files = await glob(`${constants.DOWNLOAD_PATH}/**/*`); + const files = await glob(`${constants.DOWNLOAD_PATH}/**/${constants.ACTUAL_DIR_NAME}/**/*`); await Promise.all( - files - .filter(f => { - log.info('fileName:', f); - return f.startsWith(constants.ACTUAL_DIR_NAME); - }) - .map(async file => { - const f = path.join(workspace(), file.replace(constants.ACTUAL_DIR_NAME, constants.EXPECTED_DIR_NAME)); - await makeDir(path.dirname(f)); - log.info('download to', f); - await fs.copyFile(file, f); - }), + 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') {