Skip to content

Commit

Permalink
Merge 886f480 into 95ecd3b
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb authored Apr 30, 2024
2 parents 95ecd3b + 886f480 commit 3763a3b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 35 deletions.
25 changes: 18 additions & 7 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof github.getOctokit>;

Expand Down Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
54 changes: 26 additions & 28 deletions src/service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<void>;
};

// 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.');
Expand Down Expand Up @@ -76,7 +74,7 @@ const compareAndUpload = async (client: UploadClient, config: Config): Promise<C
const result = await compare(config);
log.info('compare result', result);

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

log.info('Start upload artifact');

Expand All @@ -95,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,
});
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 3763a3b

Please sign in to comment.