Skip to content

Commit

Permalink
Merge 912dcff into 60a8105
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb authored Jun 18, 2024
2 parents 60a8105 + 912dcff commit 029d8dc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 36 deletions.
19 changes: 7 additions & 12 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as github from '@actions/github';
import { DefaultArtifactClient } from '@actions/artifact';
import { backOff } from 'exponential-backoff';
import { summary } from '@actions/core';
import { DOWNLOAD_PATH } from './constants';

import { Repository } from './repository';
import { workspace } from './path';

Expand Down Expand Up @@ -33,21 +33,16 @@ export const createClient = (repository: Repository, octokit: Octokit) => {
});
return res;
},
downloadArtifact: async (token: string, artifactId: number, runId: number) => {
await backOff(
downloadArtifact: async (artifactId: number) => {
return backOff(
() =>
artifactClient.downloadArtifact(artifactId, {
path: DOWNLOAD_PATH,
findBy: {
token,
workflowRunId: runId,
repositoryName: repository.repo,
repositoryOwner: repository.owner,
},
octokit.rest.actions.downloadArtifact({
...repository,
artifact_id: artifactId,
archive_format: 'zip',
}),
{ numOfAttempts: 5 },
);
return;
},
listComments: async (issueNumber: number): Promise<{ node_id: string; body?: string | undefined }[]> => {
return backOff(
Expand Down
1 change: 0 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ export const JSON_NAME = '0';

export const ARTIFACT_NAME = 'reg';
export const WORKSPACE_DIR_NAME = '__reg__';
export const DOWNLOAD_PATH = '__reg_download';
44 changes: 21 additions & 23 deletions src/service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as fs from 'fs/promises';
import * as fs from 'fs';
import * as path from 'path';
import cpy from 'cpy';
import { sync as globSync } from 'glob';
import makeDir from 'make-dir';
import { glob } from 'glob';
import Zip from 'adm-zip';

import { log } from './logger';
import { Config } from './config';
import { Event } from './event';
Expand All @@ -15,31 +17,27 @@ import { pushImages } from './push';
import { targetDir } from './helper';

type DownloadClient = {
downloadArtifact: (token: string, artifactId: number, runId: number, artifactName: string) => Promise<void>;
downloadArtifact: (id: number) => Promise<{ data: unknown }>;
};

// Download expected images from target artifact.
const downloadExpectedImages = async (
client: DownloadClient,
latestArtifactId: number,
runId: number,
config: Config,
) => {
const downloadExpectedImages = async (client: DownloadClient, latestArtifactId: number) => {
log.info(`Start to download expected images, artifact id = ${latestArtifactId}`);
try {
await client.downloadArtifact(config.githubToken, latestArtifactId, runId, config.artifactName);
const files = await glob(`${constants.DOWNLOAD_PATH}/**/${constants.ACTUAL_DIR_NAME}/**/*`);
log.info(`file size ${files.length}`);
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),
);
const zip = await client.downloadArtifact(latestArtifactId);
const buf = Buffer.from(zip.data as any);
log.info(`Downloaded zip size = ${buf.byteLength}`);
const entries = new Zip(buf).getEntries();
log.info(`entry size = ${entries.length}`);
for (const entry of entries) {
if (entry.isDirectory || !entry.entryName.startsWith(constants.ACTUAL_DIR_NAME)) continue;
// https://github.com/reg-viz/reg-actions/security/code-scanning/2
if (entry.entryName.includes('..')) continue;
const f = path.join(workspace(), entry.entryName.replace(constants.ACTUAL_DIR_NAME, constants.EXPECTED_DIR_NAME));
await makeDir(path.dirname(f));
log.info('download to', f);
await fs.copyFile(file, f);
});
await fs.promises.writeFile(f, entry.getData());
}
} catch (e: any) {
if (e.message === 'Artifact has expired') {
log.error('Failed to download expected images. Because expected artifact has already expired.');
Expand Down Expand Up @@ -71,7 +69,7 @@ const compareAndUpload = async (client: UploadClient, config: Config): Promise<C
const result = await compare(config);
log.info('compare result', result);

const files = await glob.glob(path.join(workspace(), '**/*'));
const files = globSync(path.join(workspace(), '**/*'));

log.info('Start upload artifact');

Expand All @@ -90,7 +88,7 @@ const init = async (config: Config) => {
log.info(`start initialization with config.`, config);

// Cleanup workspace
await fs.rm(workspace(), {
await fs.promises.rm(workspace(), {
recursive: true,
force: true,
});
Expand Down Expand Up @@ -189,7 +187,7 @@ export const run = async ({
const { run: targetRun, artifact } = runAndArtifact;

// Download and copy expected images to workspace.
await downloadExpectedImages(client, artifact.id, targetRun.id, config);
await downloadExpectedImages(client, artifact.id);

const result = await compareAndUpload(client, config);

Expand Down

0 comments on commit 029d8dc

Please sign in to comment.