-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from glefloch/fix/80
Cancel workflow on pull request close action
- Loading branch information
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/main/java/io/quarkus/bot/CancelWorkflowOnClosedPullRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package io.quarkus.bot; | ||
|
||
import io.quarkiverse.githubapp.event.PullRequest; | ||
import io.quarkus.bot.config.QuarkusBotConfig; | ||
import io.quarkus.bot.workflow.WorkflowConstants; | ||
import org.jboss.logging.Logger; | ||
import org.kohsuke.github.GHEventPayload; | ||
import org.kohsuke.github.GHPullRequest; | ||
import org.kohsuke.github.GHWorkflowRun; | ||
|
||
import javax.inject.Inject; | ||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
class CancelWorkflowOnClosedPullRequest { | ||
private static final Logger LOG = Logger.getLogger(CancelWorkflowOnClosedPullRequest.class); | ||
|
||
@Inject | ||
QuarkusBotConfig quarkusBotConfig; | ||
|
||
public void onClose(@PullRequest.Closed GHEventPayload.PullRequest pullRequestPayload) throws IOException { | ||
|
||
GHPullRequest pullRequest = pullRequestPayload.getPullRequest(); | ||
|
||
List<GHWorkflowRun> ghWorkflowRuns = pullRequest.getRepository() | ||
.queryWorkflowRuns() | ||
.branch(pullRequest.getHead().getRef()) | ||
.list() | ||
.toList() | ||
.stream() | ||
.filter(workflowRun -> workflowRun.getHeadRepository().getOwnerName() | ||
.equals(pullRequest.getHead().getRepository().getOwnerName())) | ||
.filter(workflowRun -> WorkflowConstants.QUARKUS_CI_WORKFLOW_NAME.equals(workflowRun.getName()) || | ||
WorkflowConstants.QUARKUS_DOCUMENTATION_CI_WORKFLOW_NAME.equals(workflowRun.getName())) | ||
.filter(workflowRun -> workflowRun.getStatus() == GHWorkflowRun.Status.QUEUED | ||
|| workflowRun.getStatus() == GHWorkflowRun.Status.IN_PROGRESS) | ||
.collect(Collectors.toList()); | ||
|
||
for (GHWorkflowRun workflowRun : ghWorkflowRuns) { | ||
if (!quarkusBotConfig.isDryRun()) { | ||
workflowRun.cancel(); | ||
} else { | ||
LOG.info("Workflow run #" + workflowRun.getId() + " - Cancelling as pull request #" + pullRequest.getNumber() | ||
+ " is now closed"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters