Skip to content

Commit

Permalink
Merge fa650a0 into 19d3918
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb committed Apr 30, 2024
2 parents 19d3918 + fa650a0 commit dc763ec
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 46 deletions.
25 changes: 7 additions & 18 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ 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 @@ -36,24 +33,16 @@ export const createClient = (repository: Repository, octokit: Octokit) => {
});
return res;
},
downloadArtifact: async (token: string, artifactId: number, runId: number) => {
const { downloadPath } = 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,
},
{ numOfAttempts: 5 },
);
log.info('downloadPath:', downloadPath);
return;
},
postComment: async (issueNumber: number, comment: string) => {
const _ = await backOff(
Expand Down
2 changes: 0 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@ export const JSON_NAME = '0';

export const ARTIFACT_NAME = 'reg';
export const WORKSPACE_DIR_NAME = '__reg__';

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

import { log } from './logger';
import { Config } from './config';
Expand All @@ -16,33 +17,34 @@ 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}/**/*`);
const zip = await client.downloadArtifact(latestArtifactId);
await Promise.all(
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);
}),
);
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;
});
} 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 @@ -74,7 +76,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 @@ -93,7 +95,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 @@ -178,7 +180,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 dc763ec

Please sign in to comment.