Skip to content

Commit 11d34ae

Browse files
committed
add force-cancel workflow run
1 parent 3745013 commit 11d34ae

4 files changed

Lines changed: 64 additions & 0 deletions

File tree

src/main/java/org/kohsuke/github/GHWorkflowRun.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ public String toString() {
209209
}
210210
private GHUser actor;
211211
private String artifactsUrl;
212+
private String forceCancelUrl;
212213
private String cancelUrl;
213214
private String checkSuiteUrl;
214215

@@ -267,6 +268,16 @@ public void cancel() throws IOException {
267268
root().createRequest().method("POST").withUrlPath(getApiRoute(), "cancel").send();
268269
}
269270

271+
/**
272+
* Force-cancel the workflow run.
273+
*
274+
* @throws IOException
275+
* the io exception
276+
*/
277+
public void forceCancel() throws IOException {
278+
root().createRequest().method("POST").withUrlPath(getApiRoute(), "force-cancel").send();
279+
}
280+
270281
/**
271282
* Delete the workflow run.
272283
*
@@ -336,6 +347,15 @@ public URL getCancelUrl() {
336347
return GitHubClient.parseURL(cancelUrl);
337348
}
338349

350+
/**
351+
* The cancel URL, like https://api.github.com/repos/octo-org/octo-repo/actions/runs/30433642/force-cancel
352+
*
353+
* @return the cancel url
354+
*/
355+
public URL getForceCancelUrl() {
356+
return GitHubClient.parseURL(forceCancelUrl);
357+
}
358+
339359
/**
340360
* The check suite URL, like https://api.github.com/repos/octo-org/octo-repo/check-suites/414944374
341361
*

src/test/java/org/kohsuke/github/GHEventPayloadTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,8 @@ public void workflow_run() throws Exception {
17951795
is("https://api.github.com/repos/gsmet/quarkus-bot-java-playground/check-suites/2327154397"));
17961796
assertThat(workflowRun.getArtifactsUrl().toString(),
17971797
is("https://api.github.com/repos/gsmet/quarkus-bot-java-playground/actions/runs/680604745/artifacts"));
1798+
assertThat(workflowRun.getForceCancelUrl().toString(),
1799+
is("https://api.github.com/repos/gsmet/quarkus-bot-java-playground/actions/runs/680604745/force-cancel"));
17981800
assertThat(workflowRun.getCancelUrl().toString(),
17991801
is("https://api.github.com/repos/gsmet/quarkus-bot-java-playground/actions/runs/680604745/cancel"));
18001802
assertThat(workflowRun.getRerunUrl().toString(),

src/test/java/org/kohsuke/github/GHWorkflowRunTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,46 @@ public void testCancelAndRerun() throws IOException {
420420
workflowRun.cancel();
421421
}
422422

423+
/**
424+
* Test force cancel a run.
425+
*
426+
* @throws IOException
427+
* Signals that an I/O exception has occurred.
428+
*/
429+
@Test
430+
public void testForceCancel() throws IOException {
431+
GHWorkflow workflow = repo.getWorkflow(SLOW_WORKFLOW_PATH);
432+
433+
long latestPreexistingWorkflowRunId = getLatestPreexistingWorkflowRunId();
434+
435+
workflow.dispatch(MAIN_BRANCH);
436+
437+
// now that we have triggered the workflow run, we will wait until it's in progress and then force cancel it
438+
await((nonRecordingRepo) -> getWorkflowRun(nonRecordingRepo,
439+
SLOW_WORKFLOW_NAME,
440+
MAIN_BRANCH,
441+
Status.IN_PROGRESS,
442+
latestPreexistingWorkflowRunId).isPresent());
443+
444+
GHWorkflowRun workflowRun = getWorkflowRun(SLOW_WORKFLOW_NAME,
445+
MAIN_BRANCH,
446+
Status.IN_PROGRESS,
447+
latestPreexistingWorkflowRunId)
448+
.orElseThrow(() -> new IllegalStateException("We must have a valid workflow run starting from here"));
449+
450+
assertThat(workflowRun.getId(), notNullValue());
451+
452+
workflowRun.forceCancel();
453+
long cancelledWorkflowRunId = workflowRun.getId();
454+
455+
// let's wait until it's completed
456+
await((nonRecordingRepo) -> getWorkflowRunStatus(nonRecordingRepo, cancelledWorkflowRunId) == Status.COMPLETED);
457+
458+
// let's check that it has been properly cancelled
459+
workflowRun = repo.getWorkflowRun(cancelledWorkflowRunId);
460+
assertThat(workflowRun.getConclusion(), equalTo(Conclusion.CANCELLED));
461+
}
462+
423463
/**
424464
* Test delete.
425465
*
@@ -591,6 +631,7 @@ public void testManualRunAndBasicInformation() throws IOException {
591631
assertThat(workflowRun.getCheckSuiteUrl().getPath(), containsString("/check-suites/"));
592632
assertThat(workflowRun.getArtifactsUrl().getPath(), endsWith("/artifacts"));
593633
assertThat(workflowRun.getCancelUrl().getPath(), endsWith("/cancel"));
634+
assertThat(workflowRun.getForceCancelUrl().getPath(), endsWith("/force-cancel"));
594635
assertThat(workflowRun.getRerunUrl().getPath(), endsWith("/rerun"));
595636
assertThat(workflowRun.getWorkflowUrl().getPath(), containsString("/actions/workflows/"));
596637
assertThat(workflowRun.getHeadBranch(), equalTo(MAIN_BRANCH));

src/test/java/org/kohsuke/github/GHWorkflowTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ private static void checkWorkflowRunProperties(GHWorkflowRun workflowRun, long w
3535
assertThat(workflowRun.getCheckSuiteUrl().getPath(), containsString("/check-suites/"));
3636
assertThat(workflowRun.getArtifactsUrl().getPath(), endsWith("/artifacts"));
3737
assertThat(workflowRun.getCancelUrl().getPath(), endsWith("/cancel"));
38+
assertThat(workflowRun.getForceCancelUrl().getPath(), endsWith("/force-cancel"));
3839
assertThat(workflowRun.getRerunUrl().getPath(), endsWith("/rerun"));
3940
assertThat(workflowRun.getWorkflowUrl().getPath(), containsString("/actions/workflows/"));
4041
assertThat(workflowRun.getHeadBranch(), equalTo("main"));

0 commit comments

Comments
 (0)