Skip to content

Commit

Permalink
refactor: extraction of cancel workflow runs method
Browse files Browse the repository at this point in the history
this is intended to be completely non-functional, nothing should be different
here in how this works from before, it is a pure method extraction

testing this change:
the cancel_self workflow action was updated so it may be run manually, and
the no-longer-required access_token was removed from it
  • Loading branch information
mikehardy committed Apr 1, 2021
1 parent 41ce15d commit 66e9135
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/cancel-self.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: Cancel Self

on: [push]
on:
push:
workflow_dispatch:

jobs:
task:
Expand All @@ -9,9 +11,7 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: actions/setup-node@v2
- name: Test Step
uses: ./ # Uses an action in the root directory
with:
access_token: ${{ github.token }}
- uses: actions/setup-node@v1
- run: echo 'Sleeping...'; sleep 120; echo 'Done.';
22 changes: 13 additions & 9 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5897,15 +5897,7 @@ async function main() {
run.status !== 'completed' &&
new Date(run.created_at) < new Date(current_run.created_at));
console.log(`with ${runningWorkflows.length} runs to cancel.`);
for (const { id, head_sha, status, html_url } of runningWorkflows) {
console.log('Canceling run: ', { id, head_sha, status, html_url });
const res = await octokit.actions.cancelWorkflowRun({
owner,
repo,
run_id: id
});
console.log(`Cancel run ${id} responded with status ${res.status}`);
}
await cancelWorkflowRuns(runningWorkflows, owner, repo, token);
}
catch (e) {
const msg = e.message || e;
Expand All @@ -5914,6 +5906,18 @@ async function main() {
console.log('');
}));
}
async function cancelWorkflowRuns(runningWorkflows, owner, repo, token) {
const octokit = github.getOctokit(token);
for (const { id, head_sha, status, html_url } of runningWorkflows) {
console.log('Canceling run: ', { id, head_sha, status, html_url });
const res = await octokit.actions.cancelWorkflowRun({
owner,
repo,
run_id: id
});
console.log(`Cancel run ${id} responded with status ${res.status}`);
}
}
main()
.then(() => core.info('Cancel Complete.'))
.catch(e => core.setFailed(e.message));
Expand Down
30 changes: 20 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,7 @@ async function main(): Promise<void> {
new Date(run.created_at) < new Date(current_run.created_at)
);
console.log(`with ${runningWorkflows.length} runs to cancel.`);

for (const { id, head_sha, status, html_url } of runningWorkflows) {
console.log('Canceling run: ', { id, head_sha, status, html_url });
const res = await octokit.actions.cancelWorkflowRun({
owner,
repo,
run_id: id
});
console.log(`Cancel run ${id} responded with status ${res.status}`);
}
await cancelWorkflowRuns(runningWorkflows, owner, repo, token);
} catch (e) {
const msg = e.message || e;
console.log(`Error while canceling workflow_id ${workflow_id}: ${msg}`);
Expand All @@ -98,6 +89,25 @@ async function main(): Promise<void> {
);
}

async function cancelWorkflowRuns(
runningWorkflows: { id: number; head_sha: string; status: string; html_url: string }[],
owner: string,
repo: string,
token: string
): Promise<void> {
const octokit = github.getOctokit(token);

for (const { id, head_sha, status, html_url } of runningWorkflows) {
console.log('Canceling run: ', { id, head_sha, status, html_url });
const res = await octokit.actions.cancelWorkflowRun({
owner,
repo,
run_id: id
});
console.log(`Cancel run ${id} responded with status ${res.status}`);
}
}

main()
.then(() => core.info('Cancel Complete.'))
.catch(e => core.setFailed(e.message));

0 comments on commit 66e9135

Please sign in to comment.