Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bokuweb committed Oct 22, 2023
1 parent b56064f commit 7b9ba04
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type CreateCommentWithTargetInput = {
artifactName: string;
targetRun: Run;
result: CompareOutput;
date: string;
};

export type CreateCommentWithoutTargetInput = {
Expand Down Expand Up @@ -40,15 +41,17 @@ const createBaseUrl = ({
branch,
runId,
artifactName,
date,
}: {
owner: string;
repoName: string;
branch: string;
runId: number;
artifactName: string;
date;
}): string => {
//raw.githubusercontent.com/bokuweb/reg-actions/reg/6602221723_reg/actual/sample.png
return `https://raw.githubusercontent.com/${owner}/${repoName}/${branch}/${runId}_${artifactName}/`;
return `https://raw.githubusercontent.com/${owner}/${repoName}/${branch}/${date}_${runId}_${artifactName}/`;
};

const differences = ({ result, baseUrl }: { result: CompareOutput; baseUrl: string }): string => {
Expand Down Expand Up @@ -122,12 +125,13 @@ export const createCommentWithTarget = ({
sha: currentHash,
targetRun,
result,
date,
}: CreateCommentWithTargetInput): string => {
const [owner, repoName] = event.repository.full_name.split('/');
const targetHash = targetRun.head_sha;
const currentHashShort = currentHash.slice(0, 7);
const targetHashShort = targetHash.slice(0, 7);
const baseUrl = createBaseUrl({ owner, repoName, branch: regBranch, runId, artifactName });
const baseUrl = createBaseUrl({ owner, repoName, branch: regBranch, runId, artifactName, date });
const successOrFailMessage = isSuccess(result)
? `${badge(result)}
Expand Down Expand Up @@ -159,7 +163,7 @@ ${deletedItems({ result, baseUrl })}
return body;
};

export const createCommentWithoutTarget = ({ event, runId, result }: CreateCommentWithoutTargetInput): string => {
export const createCommentWithoutTarget = ({ result }: CreateCommentWithoutTargetInput): string => {
const body = `Failed to find a target artifact.
All items will be treated as new items and will be used as expected data for the next time.
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Config {
thresholdPixel: number;
targetHash: string | null;
artifactName: string;
branch: string;
}

const validateGitHubToken = (githubToken: string | undefined) => {
Expand Down Expand Up @@ -82,6 +83,7 @@ export const getConfig = (): Config => {
const targetHash = core.getInput('target-hash') || null;
validateTargetHash(targetHash);
const artifactName = core.getInput('artifact-name') || ARTIFACT_NAME;
const branch = core.getInput('branch') || 'reg_actions';

return {
githubToken,
Expand All @@ -92,5 +94,6 @@ export const getConfig = (): Config => {
thresholdPixel,
targetHash,
artifactName,
branch,
};
};
2 changes: 2 additions & 0 deletions src/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const targetDir = ({ runId, artifactName, date }: { runId: number; artifactName: string; date: string }) =>
`${date}_${runId}_${artifactName}`;
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const main = async () => {

const { repo, runId, sha } = github.context;

const date = new Date().toISOString().split('T')[0];

log.info(`runid = ${runId}, sha = ${sha}`);

const event = getEvent();
Expand All @@ -24,7 +26,7 @@ const main = async () => {

log.info(`start`);

await run(event, runId, sha, client, config);
await run({ event, runId, sha, client, date, config });
};

main().catch(e => core.setFailed(e.message));
2 changes: 1 addition & 1 deletion src/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export const pushImages = async (input: PushImagesInput) => {
await mkdirP(path.resolve(REPO_TEMP, destDir));

await copyImages(input.result, REPO_TEMP, destDir);

await add(execOptions);

const message = `Update ${input.branch} to output generated at runId:${input.runId}`;
Expand Down
24 changes: 20 additions & 4 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { createCommentWithTarget, createCommentWithoutTarget } from './comment';
import * as constants from './constants';
import { workspace } from './path';
import { pushImages } from './push';
import { targetDir } from './helper';

type DownloadClient = {
downloadArtifact: (id: number) => Promise<{ data: unknown }>;
Expand Down Expand Up @@ -110,7 +111,21 @@ type SummaryClient = {

type Client = CommentClient & DownloadClient & UploadClient & RunClient & SummaryClient;

export const run = async (event: Event, runId: number, sha: string, client: Client, config: Config) => {
export const run = async ({
event,
runId,
sha,
client,
date,
config,
}: {
event: Event;
runId: number;
sha: string;
client: Client;
date: string;
config: Config;
}) => {
// Setup directory for artifact and copy images.
await init(config);

Expand Down Expand Up @@ -159,8 +174,8 @@ export const run = async (event: Event, runId: number, sha: string, client: Clie
githubToken: config.githubToken,
runId,
result,
branch: 'reg',
targetDir: `${runId}_${config.artifactName}`,
branch: config.branch,
targetDir: targetDir({ runId, artifactName: config.artifactName, date }),
env: process.env,
// commitName: undefined,
// commitEmail: undefined,
Expand All @@ -172,9 +187,10 @@ export const run = async (event: Event, runId: number, sha: string, client: Clie
runId,
sha,
targetRun,
date,
result,
artifactName: config.artifactName,
regBranch: 'reg',
regBranch: config.branch,
});

await client.postComment(event.number, comment);
Expand Down

0 comments on commit 7b9ba04

Please sign in to comment.