Query runs for a workflow
Describe the bug
This is a follow-up to issue #1244. v1.135 added listRuns to GHWorkflow, but that doesn’t have support for adding any query parameters list GHRepository’s queryWorkflowRuns does.
I checked out the source code and tried adding support for this myself, but ran into two issues:
- On importing the project into Eclipse, I get a “Maven Project Build Lifecycle Mapping Problem” that I have no idea how to fix:
Plugin execution not covered by lifecycle configuration: com.infradna.tool:bridge-method-injector:1.18:process (execution: default, phase: process-classes)
- I don’t know how test data is generated
With these limitations in mind, I have the following suggested changes. I’ve added an optional workflow field to GHWorkflowRunQueryBuilder which is set if created by GHWorkflow.queryWorkflowRuns. If this is set, the query’s list method uses the list workflow runs endpoint instead of list workflow runs for a repository.
Also, I added support in the query builder to set status to a Conclusion or an arbitrary string, since the GitHub API supports any status or conclusion values for this parameter.
diff --git a/src/main/java/org/kohsuke/github/GHWorkflow.java b/src/main/java/org/kohsuke/github/GHWorkflow.java
index ea195e5b1..569abb582 100644
--- a/src/main/java/org/kohsuke/github/GHWorkflow.java
+++ b/src/main/java/org/kohsuke/github/GHWorkflow.java
@@ -145,6 +145,15 @@ public class GHWorkflow extends GHObject {
return new GHWorkflowRunsIterable(owner, root().createRequest().withUrlPath(getApiRoute(), "runs"));
}
+ /**
+ * Retrieves workflow runs.
+ *
+ * @return the workflow run query builder
+ */
+ public GHWorkflowRunQueryBuilder queryWorkflowRuns() {
+ 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.
diff --git a/src/main/java/org/kohsuke/github/GHWorkflowRunQueryBuilder.java b/src/main/java/org/kohsuke/github/GHWorkflowRunQueryBuilder.ja
va
index 57a482d1b..0093ccb33 100644
--- a/src/main/java/org/kohsuke/github/GHWorkflowRunQueryBuilder.java
+++ b/src/main/java/org/kohsuke/github/GHWorkflowRunQueryBuilder.java
@@ -1,5 +1,6 @@
package org.kohsuke.github;
+import org.kohsuke.github.GHWorkflowRun.Conclusion;
import org.kohsuke.github.GHWorkflowRun.Status;
/**
@@ -10,10 +11,18 @@ import org.kohsuke.github.GHWorkflowRun.Status;
*/
public class GHWorkflowRunQueryBuilder extends GHQueryBuilder<GHWorkflowRun> {
private final GHRepository repo;
+ private final GHWorkflow workflow;
GHWorkflowRunQueryBuilder(GHRepository repo) {
super(repo.root());
this.repo = repo;
+ workflow = null;
+ }
+
+ GHWorkflowRunQueryBuilder(GHWorkflow workflow) {
+ super(workflow.getRepository().root());
+ this.repo = workflow.getRepository();
+ this.workflow = workflow;
}
/**
@@ -88,8 +97,36 @@ public class GHWorkflowRunQueryBuilder extends GHQueryBuilder<GHWorkflowRun> {
return this;
}
+ /**
+ * Status workflow run query builder.
+ *
+ * @param conclusion
+ * the conclusion
+ * @return the gh workflow run query builder
+ */
+ public GHWorkflowRunQueryBuilder status(Conclusion conclusion) {
+ req.with("status", conclusion.toString());
+ return this;
+ }
+
+ /**
+ * Status workflow run query builder.
+ *
+ * @param status
+ * the status
+ * @return the gh workflow run query builder
+ */
+ public GHWorkflowRunQueryBuilder status(String status) {
+ req.with("status", status);
+ return this;
+ }
+
@Override
public PagedIterable<GHWorkflowRun> list() {
- return new GHWorkflowRunsIterable(repo, req.withUrlPath(repo.getApiTailUrl("actions/runs")));
+ if (workflow == null)
+ return new GHWorkflowRunsIterable(repo, req.withUrlPath(repo.getApiTailUrl("actions/runs")));
+ else
+ return new GHWorkflowRunsIterable(repo, req.withUrlPath(
+ repo.getApiTailUrl(String.format("actions/workflows/%d/runs", workflow.getId()))));
}
}
diff --git a/src/test/java/org/kohsuke/github/GHWorkflowTest.java b/src/test/java/org/kohsuke/github/GHWorkflowTest.java
index a91f9f4ca..86e653787 100644
--- a/src/test/java/org/kohsuke/github/GHWorkflowTest.java
+++ b/src/test/java/org/kohsuke/github/GHWorkflowTest.java
@@ -121,6 +121,17 @@ public class GHWorkflowTest extends AbstractGitHubWireMockTest {
checkWorkflowRunProperties(workflowRuns.get(1), workflow.getId());
}
+ //Test
+ public void testQueryWorkflowRuns() throws IOException {
+ GHWorkflow workflow = repo.getWorkflow("test-workflow.yml");
+
+ List<GHWorkflowRun> workflowRuns = workflow.queryWorkflowRuns()
+ .list().toList();
+ assertThat(workflowRuns.size(), is(1));
+
+ checkWorkflowRunProperties(workflowRuns.get(0), workflow.getId());
+ }
+
private static void checkWorkflowRunProperties(GHWorkflowRun workflowRun, long workflowId) throws IOException {
assertThat(workflowRun.getWorkflowId(), equalTo(workflowId));
assertThat(workflowRun.getId(), notNullValue());
I was able to build the library and test this change in my application, and it appears to work as expected. These are the changes I made in my application to test it: the original code was using both GHWorkflow.listRuns and GHRepository.queryWorkflowRuns, and had to filter the results; now both parts of the code use GHWorkflow.queryWorkflowRuns with minimal or no filtering.
@@ -1199,21 +1240,15 @@ public class GitHubRestAPIClient {
String.format("%s/%s", organization, repository));
GHWorkflow wf = repo.getWorkflow(workflowId);
- // FIXME: GHWorkflow doesn't support query parameters for listing runs
- // See: https://github.com/hub4j/github-api/issues/1244
- PagedIterable<GHWorkflowRun> workflowRuns = wf.listRuns();
- // repo.queryWorkflowRuns().branch(branch).list();
+ GHWorkflowRunQueryBuilder query = wf.queryWorkflowRuns();
+ if (StringUtils.isNotBlank(branch))
+ query = query.branch(branch);
+ PagedIterable<GHWorkflowRun> workflowRuns = query.list();
- // Until we can select a single branch, we need a large enough
- // page size to have a good chance of seeing the desired workflow.
- PagedIterator<GHWorkflowRun> iter = workflowRuns/* .withPageSize(1) */.iterator();
+ PagedIterator<GHWorkflowRun> iter = workflowRuns.withPageSize(1).iterator();
while (iter.hasNext()) {
GHWorkflowRun run = iter.next();
- if (StringUtils.isNotBlank(branch)) {
- if (!run.getHeadBranch().equals(branch))
- continue;
- }
// HDBE-1868: If this run was skipped, ignore it.
if (run.getConclusion() == GHWorkflowRun.Conclusion.SKIPPED)
@@ -1285,22 +1320,16 @@ public class GitHubRestAPIClient {
String.format("%s/%s", organization, repository));
GHWorkflow wf = repo.getWorkflow(workflowId);
- PagedIterable<GHWorkflowRun> workflowRuns =
- repo.queryWorkflowRuns()
- .branch(Optional.ofNullable(branch).orElse(repo.getDefaultBranch()))
- .list();
+ GHWorkflowRunQueryBuilder query = wf.queryWorkflowRuns();
+ if (StringUtils.isNotBlank(branch))
+ query = query.branch(branch);
+ PagedIterable<GHWorkflowRun> workflowRuns = query.list();
PagedIterator<GHWorkflowRun> iter = workflowRuns.iterator();
while (iter.hasNext()) {
GHWorkflowRun run = iter.next();
- if (run.getWorkflowId() != wf.getId())
- continue;
-
- if ((run.getStatus() != GHWorkflowRun.Status.COMPLETED) ||
- (run.getConclusion() != GHWorkflowRun.Conclusion.SUCCESS))
- continue;
-
return new WorkflowRun(run, findTargetCommit(run, commitArtifact));
}
@Typraeurion
I've invited you to join the hub4j-test-org to be able to generate test data. After you've accepted, look at https://github.com/hub4j/github-api/blob/main/CONTRIBUTING.md#running-wiremock-tests to see how you can set your environment to record new data.
- On importing the project into Eclipse, I get a “Maven Project Build Lifecycle Mapping Problem” that I have no idea how to fix:
Plugin execution not covered by lifecycle configuration: com.infradna.tool:bridge-method-injector:1.18:process (execution: default, phase: process-classes)
Hmm, try rebasing on the latest main branch. That may help. If it doesn't then we'll need to debug a bit.