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

feat: Support retention days #139

Merged
merged 5 commits into from
May 26, 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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
3 changes: 3 additions & 0 deletions dist/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface Config {
reportFilePath: string | null;
commentReportFormat: 'raw' | 'summarized';
outdatedCommentAction: 'none' | 'minimize';
retentionDays: number;
}

const validateGitHubToken = (githubToken: string | undefined) => {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -146,5 +148,6 @@ export const getConfig = (): Config => {
reportFilePath,
commentReportFormat,
outdatedCommentAction,
retentionDays,
};
};
26 changes: 25 additions & 1 deletion src/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -54,6 +53,7 @@ export type PushImagesInput = {
branch: string;
targetDir: string;
env: EnvironmentVariables;
retentionDays: number;
// commitName?: string;
// commitEmail?: string;
};
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -197,6 +198,29 @@ 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);
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
Expand Down
3 changes: 2 additions & 1 deletion src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ 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) {
Expand All @@ -212,6 +212,7 @@ export const run = async ({
env: process.env,
// commitName: undefined,
// commitEmail: undefined,
retentionDays: config.retentionDays,
});
}
}
Expand Down
Loading