Skip to content

Commit

Permalink
feat: changed max runs limit to limit deletions
Browse files Browse the repository at this point in the history
  • Loading branch information
philprime committed Apr 27, 2023
1 parent 866c8bf commit 9366028
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 65 deletions.
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.

127 changes: 64 additions & 63 deletions src/removeStaleWorkflowRuns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,78 +50,79 @@ export async function removeStaleWorkflowRuns({
// Fetch all workflow runs
core.info(`Fetching all workflow runs of repo '${owner}/${repo}' ...`);
if (maxRunsLimit > 0) {
core.warning(`Number of workflows to fetch is limited to ${maxRunsLimit}`);
core.warning(`Number of workflows to delete is limited to ${maxRunsLimit}`);
} else {
core.debug(`No limit for workflow runs given`);
}
let counter = 0;
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;
}
core.info(`Found ${runs.length} workflow runs`);
let isAborted = false;
for await (const response of octokit.paginate.iterator(octokit.actions.listWorkflowRunsForRepo, {
owner: owner,
repo: repo,
per_page: 100,
})) {
if (isAborted) {
break;
}
const runs = response.data;
fetchedRunsCounter += runs.length;
core.debug(`Fetched ${fetchedRunsCounter}/${runs.total_count} runs...`);
if (runs.length === 0) {
core.info(`No more workflow runs found, nothing left to do!`);
break;
}
core.info(`Found ${runs.length} workflow runs`);

// Build list of branch names
core.info(`Building list of branches in workflow runs...`);
const runBranches = new Set(runs.map((run) => run.head_branch));
core.info(`Found ${[...runBranches].length} unique branch names in workflow runs`);
// Build list of branch names
core.info(`Building list of branches in workflow runs...`);
const runBranches = new Set(runs.map((run) => run.head_branch));
core.info(`Found ${[...runBranches].length} unique branch names in workflow runs`);

// Filter out branches which still exist
const staleBranches = new Set([...runBranches].filter((name) => name !== null && !branchNames.has(name)));
const sortedStaleBranchNames = [...staleBranches].sort().flatMap((branch) => branch as string);
if (sortedStaleBranchNames.length === 0) {
core.info('No stale branch names found, nothing left to do!');
return;
}
core.info(`Found ${sortedStaleBranchNames.length} stale workflow branch names:`);
for (const branch of sortedStaleBranchNames) {
core.info(`- ${branch}`);
}
// Filter out branches which still exist
const staleBranches = new Set([...runBranches].filter((name) => name !== null && !branchNames.has(name)));
const sortedStaleBranchNames = [...staleBranches].sort().flatMap((branch) => branch as string);
if (sortedStaleBranchNames.length === 0) {
core.info('No stale branch names found, continuing with next batch of runs...');
continue;
}
core.info(`Found ${sortedStaleBranchNames.length} stale workflow branch names:`);
for (const branch of sortedStaleBranchNames) {
core.info(`- ${branch}`);
}

// Iterate all runs and delete the ones of stale workflows
let counter = 0;
core.info(`Deleting workflow runs of stale branches...`);
for (const [idx, run] of runs.entries()) {
if (staleBranches.has(run.head_branch)) {
core.info(
`(${idx + 1}/${runs.length}) Workflow run #${run.run_number} used stale branch '${
run.head_branch
}', deleting it...`,
);
if (!dryRun) {
await octokit.actions.deleteWorkflowRun({
owner: owner,
repo: repo,
run_id: run.id,
});
// Iterate all runs and delete the ones of stale workflows
core.info(`Deleting workflow runs of stale branches...`);
for (const [idx, run] of runs.entries()) {
if (staleBranches.has(run.head_branch)) {
core.info(
`(${idx + 1}/${runs.length}) Workflow run #${run.run_number} used stale branch '${
run.head_branch
}', deleting it...`,
);
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.warning('Dry run is enabled, not deleting!');
core.info(
`(${idx + 1}/${runs.length}) Workflow run #${run.run_number} used active branch '${
run.head_branch
}', skipping it...`,
);
}
if (maxRunsLimit > 0 && counter >= maxRunsLimit) {
core.warning(`Workflow deletion limit of ${maxRunsLimit} reached, not deleting any further runs!`);
isAborted = true;
break;
}
counter += 1;
} else {
core.info(
`(${idx + 1}/${runs.length}) Workflow run #${run.run_number} used active branch '${
run.head_branch
}', skipping it...`,
);
}
}
core.info(`Finished deleting workflow runs, deleted ${counter} of ${runs.length} runs`);
core.info(`Finished deleting workflow runs, deleted ${counter} of ${fetchedRunsCounter} fetched runs`);
}

0 comments on commit 9366028

Please sign in to comment.