Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "fix: replace client" #146

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading