Skip to content

Commit

Permalink
feat: added flags dry-run and max-runs-limit
Browse files Browse the repository at this point in the history
  • Loading branch information
philprime committed Feb 22, 2023
1 parent 1181a62 commit 3b06263
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 31 deletions.
10 changes: 8 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ inputs:
repository:
description: Repository name with owner. E.g. actions/checkout
required: true
dry_run:
description: Only find stale workflow runs but do not delete anything
default: 'false'
max_runs_limit:
description: Limits the number of fetched workflow runs, e.g. 500. Defaults to 0, meaning no limit set.
default: '0'
outputs: {}
runs:
using: "node16"
main: "dist/index.js"
using: 'node16'
main: 'dist/index.js'
1 change: 0 additions & 1 deletion dist/index.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion dist/index.spec.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions dist/jest.config.d.ts

This file was deleted.

5 changes: 0 additions & 5 deletions dist/removeStaleWorkflowRuns.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion dist/src/index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion dist/src/index.spec.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"prepare": "ncc build src/index.ts -o dist --source-map --license licenses.txt --minify",
"test": "jest",
"all": "yarn run lint && yarn run prepare && yarn run test",
"watch": "ncc build src/index.ts --watch -o dist --source-map --license licenses.txt"
"watch": "ncc build src/index.ts --watch -o dist --source-map"
},
"engines": {
"node": "^16"
Expand Down
1 change: 1 addition & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as process from 'process';

test('test runs', () => {
process.env['INPUT_REPOSITORY'] = 'kula-app/remove-stale-workflow-runs';
process.env['INPUT_MAX_RUNS_LIMIT'] = '5000';
const ip = path.join(__dirname, '../', 'dist', 'index.js');
const result = cp.execSync(`node ${ip}`, { env: process.env }).toString();
console.log(result);
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ async function run() {
throw `Invalid repository "${repository}" (needs to have one slash, i.e. 'owner/repo')`;
}
const [owner, repo] = repoParts;
const authToken = core.getInput('token');
const authToken = core.getInput('token', {
required: true,
});
const dryRun = core.getBooleanInput('dry_run');
const maxRunsLimit = parseInt(core.getInput('max_runs_limit'));

// -- Perform Tasks --
await removeStaleWorkflowRuns({
authToken,
owner,
repo,
dryRun,
maxRunsLimit,
});
} catch (error: any) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
Expand Down
54 changes: 42 additions & 12 deletions src/removeStaleWorkflowRuns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ export async function removeStaleWorkflowRuns({
owner,
repo,
authToken,
dryRun,
maxRunsLimit,
}: {
authToken: string;
owner: string;
repo: string;
dryRun: boolean;
maxRunsLimit: number;
}) {
const octokit = new Octokit({
auth: authToken,
Expand Down Expand Up @@ -45,11 +49,29 @@ export async function removeStaleWorkflowRuns({

// Fetch all workflow runs
core.info(`Fetching all workflow runs of repo '${owner}/${repo}' ...`);
const runs = await octokit.paginate(octokit.actions.listWorkflowRunsForRepo, {
owner: owner,
repo: repo,
per_page: 100,
});
if (maxRunsLimit > 0) {
core.warning(`Number of workflows is limited to ${maxRunsLimit}`);
} else {
core.debug(`No limit for workflow runs given`);
}
let fetchedRunsCounter = 0;
const runs = await octokit.paginate(
octokit.actions.listWorkflowRunsForRepo,
{
owner: owner,
repo: repo,
per_page: 100,
},
(response, done) => {
fetchedRunsCounter += response.data.length;
core.debug(`Fetched ${fetchedRunsCounter}/${response.data.total_count} runs...`);
if (maxRunsLimit > 0 && fetchedRunsCounter >= maxRunsLimit) {
core.warning(`Workflow run limit of ${maxRunsLimit} reached, not fetching further pages!`);
done();
}
return response.data;
},
);
if (runs.length === 0) {
core.info(`No workflow runs found, nothing left to do!`);
return;
Expand Down Expand Up @@ -79,17 +101,25 @@ export async function removeStaleWorkflowRuns({
for (const [idx, run] of runs.entries()) {
if (staleBranches.has(run.head_branch)) {
core.info(
`(${idx}/${runs.length}) Workflow run #${run.run_number} used stale branch '${run.head_branch}', deleting it...`,
`(${idx + 1}/${runs.length}) Workflow run #${run.run_number} used stale branch '${
run.head_branch
}', deleting it...`,
);
await octokit.actions.deleteWorkflowRun({
owner: owner,
repo: repo,
run_id: run.id,
});
if (!dryRun) {
await octokit.actions.deleteWorkflowRun({
owner: owner,
repo: repo,
run_id: run.id,
});
} else {
core.warning('Dry run is enabled, not deleting!');
}
counter += 1;
} else {
core.info(
`(${idx}/${runs.length}) Workflow run #${run.run_number} used active branch '${run.head_branch}', skipping it...`,
`(${idx + 1}/${runs.length}) Workflow run #${run.run_number} used active branch '${
run.head_branch
}', skipping it...`,
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"target": "es2017",
"moduleResolution": "node",
"module": "commonjs",
"declaration": true,
"declaration": false,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
Expand Down

0 comments on commit 3b06263

Please sign in to comment.