Skip to content

Commit

Permalink
refactor: break workflow run filter into multiple parts
Browse files Browse the repository at this point in the history
this will allow the cancel all but latest feature to be easier to review
  • Loading branch information
mikehardy committed Apr 1, 2021
1 parent a5cd3aa commit 12fef67
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
6 changes: 3 additions & 3 deletions dist/index.js
Expand Up @@ -5894,9 +5894,9 @@ async function main() {
const branchWorkflows = data.workflow_runs.filter(run => run.head_branch === branch);
console.log(`Found ${branchWorkflows.length} runs for workflow ${workflow_id} on branch ${branch}`);
console.log(branchWorkflows.map(run => `- ${run.html_url}`).join('\n'));
const runningWorkflows = branchWorkflows.filter(run => (ignore_sha || run.head_sha !== headSha) &&
run.status !== 'completed' &&
new Date(run.created_at) < new Date(current_run.created_at));
let runningWorkflows = branchWorkflows.filter(run => run.status !== 'completed');
runningWorkflows = runningWorkflows.filter(run => ignore_sha || run.head_sha !== headSha);
runningWorkflows = runningWorkflows.filter(run => new Date(run.created_at) < new Date(current_run.created_at));
console.log(`with ${runningWorkflows.length} runs to cancel.`);
await cancelWorkflowRuns(runningWorkflows, owner, repo, token);
}
Expand Down
15 changes: 10 additions & 5 deletions src/index.ts
Expand Up @@ -73,12 +73,17 @@ async function main(): Promise<void> {
);
console.log(branchWorkflows.map(run => `- ${run.html_url}`).join('\n'));

const runningWorkflows = branchWorkflows.filter(
run =>
(ignore_sha || run.head_sha !== headSha) &&
run.status !== 'completed' &&
new Date(run.created_at) < new Date(current_run.created_at)
// Filter for only uncompleted workflow runs
let runningWorkflows = branchWorkflows.filter(run => run.status !== 'completed');

// Filter out for only our headSha unless ignoring it
runningWorkflows = runningWorkflows.filter(run => ignore_sha || run.head_sha !== headSha);

// Filter all workflow runs newer than ours
runningWorkflows = runningWorkflows.filter(
run => new Date(run.created_at) < new Date(current_run.created_at)
);

console.log(`with ${runningWorkflows.length} runs to cancel.`);
await cancelWorkflowRuns(runningWorkflows, owner, repo, token);
} catch (e) {
Expand Down

0 comments on commit 12fef67

Please sign in to comment.