From 0f21daf89b261c7e135889c2c65896adfcdf0d6e Mon Sep 17 00:00:00 2001 From: bokuweb Date: Sun, 26 May 2024 14:03:21 +0900 Subject: [PATCH 1/4] feat: Support retention days --- README.md | 7 +++++++ dist/action.yml | 3 +++ src/config.ts | 3 +++ src/push.ts | 26 +++++++++++++++++++++++++- src/service.ts | 1 + 5 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 58eb75fd..ba440d8a 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,13 @@ The option how to render changed file in comment. This action will change PR and The option to handle outdated comments in the PR. Available options are `none` and `minimize`. `none` do nothing. `minimize` will minimize outdated action comments. +### `retention-days` (Optional) + +- Type: number +- Default: `30` + +This option allows you to specify the duration for which images are stored in the branch. If not specified, the default is 30 days. + ## Limitation - If the `artifact` is deleted, the report will also be deleted, see [`Artifact and log retention policy`](https://docs.github.com/ja/actions/learn-github-actions/usage-limits-billing-and-administration#artifact-and-log-retention-policy) for the retention period of the `artifact`. diff --git a/dist/action.yml b/dist/action.yml index 8a8dd362..6ecd39c9 100644 --- a/dist/action.yml +++ b/dist/action.yml @@ -47,6 +47,9 @@ inputs: outdated-comment-action: description: "The option to handle outdated comments. `none` by default." required: false + retention-days: + description: "This option allows you to specify the duration for which images are stored in the branch. If not specified, the default is 30 days." + required: false runs: using: "node20" main: "lib/index.js" diff --git a/src/config.ts b/src/config.ts index 1106c4e7..0f92c4b2 100644 --- a/src/config.ts +++ b/src/config.ts @@ -18,6 +18,7 @@ export interface Config { reportFilePath: string | null; commentReportFormat: 'raw' | 'summarized'; outdatedCommentAction: 'none' | 'minimize'; + retentionDays: number; } const validateGitHubToken = (githubToken: string | undefined) => { @@ -116,6 +117,7 @@ export const getConfig = (): Config => { const matchingThreshold = getNumberInput('matching-threshold') ?? 0; const thresholdRate = getNumberInput('threshold-rate') ?? 0; const thresholdPixel = getNumberInput('threshold-pixel') ?? 0; + const retentionDays = getNumberInput('threshold-pixel') ?? 30; validateMatchingThreshold(matchingThreshold); validateThresholdRate(thresholdRate); const targetHash = core.getInput('target-hash') || null; @@ -146,5 +148,6 @@ export const getConfig = (): Config => { reportFilePath, commentReportFormat, outdatedCommentAction, + retentionDays, }; }; diff --git a/src/push.ts b/src/push.ts index dd68c3ac..878635e1 100644 --- a/src/push.ts +++ b/src/push.ts @@ -44,7 +44,6 @@ import { log } from './logger'; import { CompareOutput } from './compare'; import { workspace } from './path'; import * as constants from './constants'; -import { rejects } from 'assert'; import { backOff } from 'exponential-backoff'; export type PushImagesInput = { @@ -54,6 +53,7 @@ export type PushImagesInput = { branch: string; targetDir: string; env: EnvironmentVariables; + retentionDays: number; // commitName?: string; // commitEmail?: string; }; @@ -197,6 +197,30 @@ export const pushImages = async (input: PushImagesInput) => { } } + /* delete expired directories */ + try { + log.info(`retention days = ${input.retentionDays}`); + const retention = input.retentionDays * 24 * 60 * 60 * 1000; + const files = await fs.readdir(REPO_TEMP); + log.info(files); + for (const fileOrDir of files) { + const p = path.join(REPO_TEMP, fileOrDir); + if ((await fs.stat(p)).isDirectory()) { + const day = fileOrDir.split('_')[0]; + if (day) { + const now = new Date().getTime(); + const target = new Date(day).getTime(); + if (now - target > retention) { + log.info(`delete dir ${fileOrDir}`); + await fs.rmdir(p, { recursive: true }); + } + } + } + } + } catch (e) { + log.error('Failed to delete directories', e); + } + const destDir = input.targetDir; // Make sure the destination sourceDir exists diff --git a/src/service.ts b/src/service.ts index 023186d8..57c39858 100644 --- a/src/service.ts +++ b/src/service.ts @@ -212,6 +212,7 @@ export const run = async ({ env: process.env, // commitName: undefined, // commitEmail: undefined, + retentionDays: config.retentionDays, }); } } From 673d39bf12ba3b96e3d3d82b75225e6c5dc48458 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Sun, 26 May 2024 14:13:25 +0900 Subject: [PATCH 2/4] test --- src/push.ts | 2 +- src/service.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/push.ts b/src/push.ts index 878635e1..78c7f991 100644 --- a/src/push.ts +++ b/src/push.ts @@ -199,7 +199,7 @@ export const pushImages = async (input: PushImagesInput) => { /* delete expired directories */ try { - log.info(`retention days = ${input.retentionDays}`); + log.info(`Retention days = ${input.retentionDays}`); const retention = input.retentionDays * 24 * 60 * 60 * 1000; const files = await fs.readdir(REPO_TEMP); log.info(files); diff --git a/src/service.ts b/src/service.ts index 57c39858..978805db 100644 --- a/src/service.ts +++ b/src/service.ts @@ -202,7 +202,7 @@ export const run = async ({ // If changed, upload images to specified branch. if (!config.disableBranch) { - if (result.deletedItems.length !== 0 || result.failedItems.length !== 0 || result.newItems.length !== 0) { + // if (result.deletedItems.length !== 0 || result.failedItems.length !== 0 || result.newItems.length !== 0) { await pushImages({ githubToken: config.githubToken, runId, @@ -214,7 +214,7 @@ export const run = async ({ // commitEmail: undefined, retentionDays: config.retentionDays, }); - } + // } } const comment = createCommentWithTarget({ From 42ce48a81e3d4599a163286a4a7d603f05126fba Mon Sep 17 00:00:00 2001 From: bokuweb Date: Sun, 26 May 2024 14:15:37 +0900 Subject: [PATCH 3/4] test --- src/push.ts | 1 + src/service.ts | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/push.ts b/src/push.ts index 78c7f991..d74a226e 100644 --- a/src/push.ts +++ b/src/push.ts @@ -130,6 +130,7 @@ const copyImages = async (result: CompareOutput, temp: string, dest: string): Pr }; export const pushImages = async (input: PushImagesInput) => { + log.info('Staring Push images') const { env } = input; const config = genConfig(input); diff --git a/src/service.ts b/src/service.ts index 978805db..f959f86e 100644 --- a/src/service.ts +++ b/src/service.ts @@ -203,17 +203,17 @@ export const run = async ({ // If changed, upload images to specified branch. if (!config.disableBranch) { // if (result.deletedItems.length !== 0 || result.failedItems.length !== 0 || result.newItems.length !== 0) { - await pushImages({ - githubToken: config.githubToken, - runId, - result, - branch: config.branch, - targetDir: targetDir({ runId, artifactName: config.artifactName, date }), - env: process.env, - // commitName: undefined, - // commitEmail: undefined, - retentionDays: config.retentionDays, - }); + await pushImages({ + githubToken: config.githubToken, + runId, + result, + branch: config.branch, + targetDir: targetDir({ runId, artifactName: config.artifactName, date }), + env: process.env, + // commitName: undefined, + // commitEmail: undefined, + retentionDays: config.retentionDays, + }); // } } From a8d2744ff28b46801b58ea9061d7d109ddd86127 Mon Sep 17 00:00:00 2001 From: bokuweb Date: Sun, 26 May 2024 14:17:38 +0900 Subject: [PATCH 4/4] test --- src/service.ts | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/service.ts b/src/service.ts index f959f86e..5328c852 100644 --- a/src/service.ts +++ b/src/service.ts @@ -198,24 +198,24 @@ export const run = async ({ const result = await compareAndUpload(client, config); - log.info(result); + log.info('Result', result); // If changed, upload images to specified branch. - if (!config.disableBranch) { - // if (result.deletedItems.length !== 0 || result.failedItems.length !== 0 || result.newItems.length !== 0) { - await pushImages({ - githubToken: config.githubToken, - runId, - result, - branch: config.branch, - targetDir: targetDir({ runId, artifactName: config.artifactName, date }), - env: process.env, - // commitName: undefined, - // commitEmail: undefined, - retentionDays: config.retentionDays, - }); - // } - } + // if (!config.disableBranch) { + // if (result.deletedItems.length !== 0 || result.failedItems.length !== 0 || result.newItems.length !== 0) { + await pushImages({ + githubToken: config.githubToken, + runId, + result, + branch: config.branch, + targetDir: targetDir({ runId, artifactName: config.artifactName, date }), + env: process.env, + // commitName: undefined, + // commitEmail: undefined, + retentionDays: config.retentionDays, + }); + // } + // } const comment = createCommentWithTarget({ event,