Skip to content

Commit 06a2ea2

Browse files
committed
Add workflow run query builder for specific workflow
1 parent 362faaf commit 06a2ea2

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ public PagedIterable<GHWorkflowRun> listRuns() {
156156
return new GHWorkflowRunsIterable(owner, root().createRequest().withUrlPath(getApiRoute(), "runs"));
157157
}
158158

159+
/**
160+
* Workflow run query builder for this workflow.
161+
*
162+
* @return the GHWorkflowRunQueryBuilder instance for querying runs
163+
*/
164+
public GHWorkflowRunQueryBuilder queryRuns() {
165+
return new GHWorkflowRunQueryBuilder(this);
166+
167+
}
168+
159169
private String getApiRoute() {
160170
if (owner == null) {
161171
// Workflow runs returned from search to do not have an owner. Attempt to use url.

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
* @see GHRepository#queryWorkflowRuns()
1212
*/
1313
public class GHWorkflowRunQueryBuilder extends GHQueryBuilder<GHWorkflowRun> {
14+
private final GHWorkflow ghWorkflow;
1415
private final GHRepository repo;
1516

1617
/**
@@ -22,6 +23,19 @@ public class GHWorkflowRunQueryBuilder extends GHQueryBuilder<GHWorkflowRun> {
2223
GHWorkflowRunQueryBuilder(GHRepository repo) {
2324
super(repo.root());
2425
this.repo = repo;
26+
this.ghWorkflow = null;
27+
}
28+
29+
/**
30+
* Instantiates a new GH workflow run query builder for a specific workflow.
31+
*
32+
* @param ghWorkflow
33+
* the ghWorkflow
34+
*/
35+
GHWorkflowRunQueryBuilder(GHWorkflow ghWorkflow) {
36+
super(ghWorkflow.getRepository().root());
37+
this.repo = ghWorkflow.getRepository();
38+
this.ghWorkflow = ghWorkflow;
2539
}
2640

2741
/**
@@ -133,7 +147,12 @@ public GHWorkflowRunQueryBuilder headSha(String headSha) {
133147
*/
134148
@Override
135149
public PagedIterable<GHWorkflowRun> list() {
136-
return new GHWorkflowRunsIterable(repo, req.withUrlPath(repo.getApiTailUrl("actions/runs")));
150+
if (ghWorkflow != null) {
151+
req.withUrlPath(repo.getApiTailUrl("actions/workflows"), String.valueOf(ghWorkflow.getId()), "runs");
152+
} else {
153+
req.withUrlPath(repo.getApiTailUrl("actions/runs"));
154+
}
155+
return new GHWorkflowRunsIterable(repo, req);
137156
}
138157

139158
/**

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,20 @@ private static Optional<GHWorkflowRun> getWorkflowRun(GHRepository repository,
148148
String workflowName,
149149
String branch,
150150
Conclusion conclusion) {
151-
List<GHWorkflowRun> workflowRuns = repository.queryWorkflowRuns()
152-
.branch(branch)
153-
.conclusion(conclusion)
154-
.event(GHEvent.PULL_REQUEST)
155-
.list()
156-
.withPageSize(20)
157-
.iterator()
158-
.nextPage();
151+
List<GHWorkflowRun> workflowRuns;
152+
try {
153+
workflowRuns = repository.getWorkflow(workflowName)
154+
.queryRuns()
155+
.branch(branch)
156+
.conclusion(conclusion)
157+
.event(GHEvent.PULL_REQUEST)
158+
.list()
159+
.withPageSize(20)
160+
.iterator()
161+
.nextPage();
162+
} catch (IOException e) {
163+
throw new IllegalStateException("Unable to get workflow run", e);
164+
}
159165

160166
for (GHWorkflowRun workflowRun : workflowRuns) {
161167
if (workflowRun.getName().equals(workflowName)) {

0 commit comments

Comments
 (0)