Skip to content

Commit

Permalink
Added list-workflow-runs API
Browse files Browse the repository at this point in the history
  • Loading branch information
Serkan ÖZAL committed Sep 25, 2021
1 parent dae92fc commit 4d14405
Show file tree
Hide file tree
Showing 15 changed files with 830 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/main/java/org/kohsuke/github/GHOrganization.java
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,9 @@ public GHHook getHook(int id) throws IOException {
* Deletes hook.
*
* @param id
* the id
* @throws IOException
* the io exception
*/
public void deleteHook(int id) throws IOException {
GHHooks.orgContext(this).deleteHook(id);
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/kohsuke/github/GHWorkflow.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ public void dispatch(String ref, Map<String, Object> inputs) throws IOException
requester.fetchHttpStatusCode();
}

/**
* Lists the workflow runs belong to this workflow.
*
* @return the paged iterable
*/
public PagedIterable<GHWorkflowRun> listRuns() {
return new GHWorkflowRunsIterable(owner, root().createRequest().withUrlPath(getApiRoute(), "runs"));
}

private String getApiRoute() {
if (owner == null) {
// Workflow runs returned from search to do not have an owner. Attempt to use url.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.kohsuke.github.GHWorkflowRun.Status;

import java.net.MalformedURLException;

/**
* Lists up workflow runs with some filtering and sorting.
*
Expand Down Expand Up @@ -92,10 +90,6 @@ public GHWorkflowRunQueryBuilder status(Status status) {

@Override
public PagedIterable<GHWorkflowRun> list() {
try {
return new GHWorkflowRunsIterable(repo, req.withUrlPath(repo.getApiTailUrl("actions/runs")).build());
} catch (MalformedURLException e) {
throw new GHException(e.getMessage(), e);
}
return new GHWorkflowRunsIterable(repo, req.withUrlPath(repo.getApiTailUrl("actions/runs")));
}
}
9 changes: 7 additions & 2 deletions src/main/java/org/kohsuke/github/GHWorkflowRunsIterable.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.kohsuke.github;

import java.net.MalformedURLException;
import java.util.Iterator;

import javax.annotation.Nonnull;
Expand All @@ -13,9 +14,13 @@ class GHWorkflowRunsIterable extends PagedIterable<GHWorkflowRun> {

private GHWorkflowRunsPage result;

public GHWorkflowRunsIterable(GHRepository owner, GitHubRequest request) {
public GHWorkflowRunsIterable(GHRepository owner, GitHubRequest.Builder<?> requestBuilder) {
this.owner = owner;
this.request = request;
try {
this.request = requestBuilder.build();
} catch (MalformedURLException e) {
throw new GHException("Malformed URL", e);
}
}

@Nonnull
Expand Down
42 changes: 41 additions & 1 deletion src/test/java/org/kohsuke/github/GHWorkflowTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.notNullValue;

public class GHWorkflowTest extends AbstractGitHubWireMockTest {

Expand Down Expand Up @@ -108,4 +109,43 @@ public void testListWorkflows() throws IOException {
assertThat(workflow.getBadgeUrl().getPath(),
equalTo("/hub4j-test-org/GHWorkflowTest/workflows/test-workflow/badge.svg"));
}

@Test
public void testListWorkflowRuns() throws IOException {
GHWorkflow workflow = repo.getWorkflow("test-workflow.yml");

List<GHWorkflowRun> workflowRuns = workflow.listRuns().toList();
assertThat(workflowRuns.size(), is(2));

checkWorkflowRunProperties(workflowRuns.get(0), workflow.getId());
checkWorkflowRunProperties(workflowRuns.get(1), workflow.getId());
}

private static void checkWorkflowRunProperties(GHWorkflowRun workflowRun, long workflowId) throws IOException {
assertThat(workflowRun.getWorkflowId(), equalTo(workflowId));
assertThat(workflowRun.getId(), notNullValue());
assertThat(workflowRun.getNodeId(), notNullValue());
assertThat(workflowRun.getRepository().getFullName(), equalTo(REPO_NAME));
assertThat(workflowRun.getUrl().getPath(), containsString("/actions/runs/"));
assertThat(workflowRun.getHtmlUrl().getPath(), containsString("/actions/runs/"));
assertThat(workflowRun.getJobsUrl().getPath(), endsWith("/jobs"));
assertThat(workflowRun.getLogsUrl().getPath(), endsWith("/logs"));
assertThat(workflowRun.getCheckSuiteUrl().getPath(), containsString("/check-suites/"));
assertThat(workflowRun.getArtifactsUrl().getPath(), endsWith("/artifacts"));
assertThat(workflowRun.getCancelUrl().getPath(), endsWith("/cancel"));
assertThat(workflowRun.getRerunUrl().getPath(), endsWith("/rerun"));
assertThat(workflowRun.getWorkflowUrl().getPath(), containsString("/actions/workflows/"));
assertThat(workflowRun.getHeadBranch(), equalTo("main"));
assertThat(workflowRun.getHeadCommit().getId(), notNullValue());
assertThat(workflowRun.getHeadCommit().getTreeId(), notNullValue());
assertThat(workflowRun.getHeadCommit().getMessage(), notNullValue());
assertThat(workflowRun.getHeadCommit().getTimestamp(), notNullValue());
assertThat(workflowRun.getHeadCommit().getAuthor().getEmail(), notNullValue());
assertThat(workflowRun.getHeadCommit().getCommitter().getEmail(), notNullValue());
assertThat(workflowRun.getEvent(), equalTo(GHEvent.WORKFLOW_DISPATCH));
assertThat(workflowRun.getStatus(), equalTo(GHWorkflowRun.Status.COMPLETED));
assertThat(workflowRun.getConclusion(), equalTo(GHWorkflowRun.Conclusion.SUCCESS));
assertThat(workflowRun.getHeadSha(), notNullValue());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
{
"id": 348651606,
"node_id": "MDEwOlJlcG9zaXRvcnkzNDg2NTE2MDY=",
"name": "GHWorkflowTest",
"full_name": "hub4j-test-org/GHWorkflowTest",
"private": false,
"owner": {
"login": "hub4j-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/hub4j-test-org",
"html_url": "https://github.com/hub4j-test-org",
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
"type": "Organization",
"site_admin": false
},
"html_url": "https://github.com/hub4j-test-org/GHWorkflowTest",
"description": "Repository used for GHWorkflowTest",
"fork": false,
"url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest",
"forks_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/forks",
"keys_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/teams",
"hooks_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/hooks",
"issue_events_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/issues/events{/number}",
"events_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/events",
"assignees_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/assignees{/user}",
"branches_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/branches{/branch}",
"tags_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/tags",
"blobs_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/statuses/{sha}",
"languages_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/languages",
"stargazers_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/stargazers",
"contributors_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/contributors",
"subscribers_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/subscribers",
"subscription_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/subscription",
"commits_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/contents/{+path}",
"compare_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/merges",
"archive_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/downloads",
"issues_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/issues{/number}",
"pulls_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/pulls{/number}",
"milestones_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/milestones{/number}",
"notifications_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/labels{/name}",
"releases_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/releases{/id}",
"deployments_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/deployments",
"created_at": "2021-03-17T09:32:03Z",
"updated_at": "2021-03-17T09:33:34Z",
"pushed_at": "2021-03-17T09:33:32Z",
"git_url": "git://github.com/hub4j-test-org/GHWorkflowTest.git",
"ssh_url": "[email protected]:hub4j-test-org/GHWorkflowTest.git",
"clone_url": "https://github.com/hub4j-test-org/GHWorkflowTest.git",
"svn_url": "https://github.com/hub4j-test-org/GHWorkflowTest",
"homepage": null,
"size": 1,
"stargazers_count": 0,
"watchers_count": 0,
"language": null,
"has_issues": true,
"has_projects": true,
"has_downloads": true,
"has_wiki": true,
"has_pages": false,
"forks_count": 0,
"mirror_url": null,
"archived": false,
"disabled": false,
"open_issues_count": 0,
"license": null,
"forks": 0,
"open_issues": 0,
"watchers": 0,
"default_branch": "main",
"permissions": {
"admin": true,
"push": true,
"pull": true
},
"temp_clone_token": "",
"allow_squash_merge": true,
"allow_merge_commit": true,
"allow_rebase_merge": true,
"delete_branch_on_merge": false,
"organization": {
"login": "hub4j-test-org",
"id": 7544739,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/hub4j-test-org",
"html_url": "https://github.com/hub4j-test-org",
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
"type": "Organization",
"site_admin": false
},
"network_count": 0,
"subscribers_count": 10
}
Loading

0 comments on commit 4d14405

Please sign in to comment.