Skip to content

Add workflow run query builder for specific workflow #2101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/org/kohsuke/github/GHWorkflow.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ public PagedIterable<GHWorkflowRun> listRuns() {
return new GHWorkflowRunsIterable(owner, root().createRequest().withUrlPath(getApiRoute(), "runs"));
}

/**
* Workflow run query builder for this workflow.
*
* @return the GHWorkflowRunQueryBuilder instance for querying runs
*/
public GHWorkflowRunQueryBuilder queryRuns() {
return new GHWorkflowRunQueryBuilder(this);

}

private String getApiRoute() {
if (owner == null) {
// Workflow runs returned from search to do not have an owner. Attempt to use url.
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/org/kohsuke/github/GHWorkflowRunQueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @see GHRepository#queryWorkflowRuns()
*/
public class GHWorkflowRunQueryBuilder extends GHQueryBuilder<GHWorkflowRun> {
private final GHWorkflow ghWorkflow;
private final GHRepository repo;

/**
Expand All @@ -22,6 +23,19 @@ public class GHWorkflowRunQueryBuilder extends GHQueryBuilder<GHWorkflowRun> {
GHWorkflowRunQueryBuilder(GHRepository repo) {
super(repo.root());
this.repo = repo;
this.ghWorkflow = null;
}

/**
* Instantiates a new GH workflow run query builder for a specific workflow.
*
* @param ghWorkflow
* the ghWorkflow
*/
GHWorkflowRunQueryBuilder(GHWorkflow ghWorkflow) {
super(ghWorkflow.getRepository().root());
this.repo = ghWorkflow.getRepository();
this.ghWorkflow = ghWorkflow;
}

/**
Expand Down Expand Up @@ -133,7 +147,12 @@ public GHWorkflowRunQueryBuilder headSha(String headSha) {
*/
@Override
public PagedIterable<GHWorkflowRun> list() {
return new GHWorkflowRunsIterable(repo, req.withUrlPath(repo.getApiTailUrl("actions/runs")));
if (ghWorkflow != null) {
req.withUrlPath(repo.getApiTailUrl("actions/workflows"), String.valueOf(ghWorkflow.getId()), "runs");
} else {
req.withUrlPath(repo.getApiTailUrl("actions/runs"));
}
return new GHWorkflowRunsIterable(repo, req);
}

/**
Expand Down
22 changes: 14 additions & 8 deletions src/test/java/org/kohsuke/github/GHWorkflowRunTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,20 @@ private static Optional<GHWorkflowRun> getWorkflowRun(GHRepository repository,
String workflowName,
String branch,
Conclusion conclusion) {
List<GHWorkflowRun> workflowRuns = repository.queryWorkflowRuns()
.branch(branch)
.conclusion(conclusion)
.event(GHEvent.PULL_REQUEST)
.list()
.withPageSize(20)
.iterator()
.nextPage();
List<GHWorkflowRun> workflowRuns;
try {
workflowRuns = repository.getWorkflow(workflowName)
.queryRuns()
.branch(branch)
.conclusion(conclusion)
.event(GHEvent.PULL_REQUEST)
.list()
.withPageSize(20)
.iterator()
.nextPage();
} catch (IOException e) {
throw new IllegalStateException("Unable to get workflow run", e);
}

for (GHWorkflowRun workflowRun : workflowRuns) {
if (workflowRun.getName().equals(workflowName)) {
Expand Down
Loading