Skip to content

Commit c19cfd9

Browse files
committed
Fix GHRepository#listWorkflows() and add a test
1 parent cdc0e2a commit c19cfd9

File tree

10 files changed

+418
-3
lines changed

10 files changed

+418
-3
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2904,9 +2904,7 @@ public GHIssueEvent getIssueEvent(long id) throws IOException {
29042904
* @return the paged iterable
29052905
*/
29062906
public PagedIterable<GHWorkflow> listWorkflows() {
2907-
return root.createRequest()
2908-
.withUrlPath(getApiTailUrl("actions/workflows"))
2909-
.toIterable(GHWorkflow[].class, item -> item.wrapUp(root));
2907+
return new GHWorkflowsIterable(this);
29102908
}
29112909

29122910
/**
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.kohsuke.github;
2+
3+
import java.net.MalformedURLException;
4+
import java.util.Iterator;
5+
6+
import javax.annotation.Nonnull;
7+
8+
/**
9+
* Iterable for workflows listing.
10+
*/
11+
class GHWorkflowsIterable extends PagedIterable<GHWorkflow> {
12+
private final transient GHRepository owner;
13+
14+
private GHWorkflowsPage result;
15+
16+
public GHWorkflowsIterable(GHRepository owner) {
17+
this.owner = owner;
18+
}
19+
20+
@Nonnull
21+
@Override
22+
public PagedIterator<GHWorkflow> _iterator(int pageSize) {
23+
try {
24+
GitHubRequest request = owner.getRoot()
25+
.createRequest()
26+
.withUrlPath(owner.getApiTailUrl("actions/workflows"))
27+
.build();
28+
29+
return new PagedIterator<>(
30+
adapt(GitHubPageIterator
31+
.create(owner.getRoot().getClient(), GHWorkflowsPage.class, request, pageSize)),
32+
null);
33+
} catch (MalformedURLException e) {
34+
throw new GHException("Malformed URL", e);
35+
}
36+
}
37+
38+
protected Iterator<GHWorkflow[]> adapt(final Iterator<GHWorkflowsPage> base) {
39+
return new Iterator<GHWorkflow[]>() {
40+
public boolean hasNext() {
41+
return base.hasNext();
42+
}
43+
44+
public GHWorkflow[] next() {
45+
GHWorkflowsPage v = base.next();
46+
if (result == null) {
47+
result = v;
48+
}
49+
return v.getWorkflows(owner);
50+
}
51+
};
52+
}
53+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.kohsuke.github;
2+
3+
/**
4+
* Represents the one page of workflow result when listing workflows.
5+
*/
6+
class GHWorkflowsPage {
7+
private int total_count;
8+
private GHWorkflow[] workflows;
9+
10+
public int getTotalCount() {
11+
return total_count;
12+
}
13+
14+
GHWorkflow[] getWorkflows(GHRepository owner) {
15+
for (GHWorkflow workflow : workflows) {
16+
workflow.wrapUp(owner);
17+
}
18+
return workflows;
19+
}
20+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.io.IOException;
88
import java.util.Collections;
9+
import java.util.List;
910

1011
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
1112
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
@@ -86,4 +87,21 @@ public void testDispatch() throws IOException {
8687
.withRequestBody(containing("parameter"))
8788
.withRequestBody(containing("value")));
8889
}
90+
91+
@Test
92+
public void testListWorkflows() throws IOException {
93+
List<GHWorkflow> workflows = repo.listWorkflows().toList();
94+
95+
GHWorkflow workflow = workflows.get(0);
96+
assertEquals(6817859L, workflow.getId());
97+
assertEquals("MDg6V29ya2Zsb3c2ODE3ODU5", workflow.getNodeId());
98+
assertEquals("test-workflow", workflow.getName());
99+
assertEquals(".github/workflows/test-workflow.yml", workflow.getPath());
100+
assertEquals("active", workflow.getState());
101+
assertEquals("/repos/hub4j-test-org/GHWorkflowTest/actions/workflows/6817859", workflow.getUrl().getPath());
102+
assertEquals("/hub4j-test-org/GHWorkflowTest/blob/main/.github/workflows/test-workflow.yml",
103+
workflow.getHtmlUrl().getPath());
104+
assertEquals("/hub4j-test-org/GHWorkflowTest/workflows/test-workflow/badge.svg",
105+
workflow.getBadgeUrl().getPath());
106+
}
89107
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
{
2+
"id": 348651606,
3+
"node_id": "MDEwOlJlcG9zaXRvcnkzNDg2NTE2MDY=",
4+
"name": "GHWorkflowTest",
5+
"full_name": "hub4j-test-org/GHWorkflowTest",
6+
"private": false,
7+
"owner": {
8+
"login": "hub4j-test-org",
9+
"id": 7544739,
10+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
11+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
12+
"gravatar_id": "",
13+
"url": "https://api.github.com/users/hub4j-test-org",
14+
"html_url": "https://github.com/hub4j-test-org",
15+
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
16+
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
17+
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
18+
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
19+
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
20+
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
21+
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
22+
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
23+
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
24+
"type": "Organization",
25+
"site_admin": false
26+
},
27+
"html_url": "https://github.com/hub4j-test-org/GHWorkflowTest",
28+
"description": "Repository used for GHWorkflowTest",
29+
"fork": false,
30+
"url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest",
31+
"forks_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/forks",
32+
"keys_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/keys{/key_id}",
33+
"collaborators_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/collaborators{/collaborator}",
34+
"teams_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/teams",
35+
"hooks_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/hooks",
36+
"issue_events_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/issues/events{/number}",
37+
"events_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/events",
38+
"assignees_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/assignees{/user}",
39+
"branches_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/branches{/branch}",
40+
"tags_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/tags",
41+
"blobs_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/git/blobs{/sha}",
42+
"git_tags_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/git/tags{/sha}",
43+
"git_refs_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/git/refs{/sha}",
44+
"trees_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/git/trees{/sha}",
45+
"statuses_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/statuses/{sha}",
46+
"languages_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/languages",
47+
"stargazers_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/stargazers",
48+
"contributors_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/contributors",
49+
"subscribers_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/subscribers",
50+
"subscription_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/subscription",
51+
"commits_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/commits{/sha}",
52+
"git_commits_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/git/commits{/sha}",
53+
"comments_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/comments{/number}",
54+
"issue_comment_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/issues/comments{/number}",
55+
"contents_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/contents/{+path}",
56+
"compare_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/compare/{base}...{head}",
57+
"merges_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/merges",
58+
"archive_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/{archive_format}{/ref}",
59+
"downloads_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/downloads",
60+
"issues_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/issues{/number}",
61+
"pulls_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/pulls{/number}",
62+
"milestones_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/milestones{/number}",
63+
"notifications_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/notifications{?since,all,participating}",
64+
"labels_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/labels{/name}",
65+
"releases_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/releases{/id}",
66+
"deployments_url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/deployments",
67+
"created_at": "2021-03-17T09:32:03Z",
68+
"updated_at": "2021-03-17T09:33:34Z",
69+
"pushed_at": "2021-03-17T09:33:32Z",
70+
"git_url": "git://github.com/hub4j-test-org/GHWorkflowTest.git",
71+
"ssh_url": "[email protected]:hub4j-test-org/GHWorkflowTest.git",
72+
"clone_url": "https://github.com/hub4j-test-org/GHWorkflowTest.git",
73+
"svn_url": "https://github.com/hub4j-test-org/GHWorkflowTest",
74+
"homepage": null,
75+
"size": 1,
76+
"stargazers_count": 0,
77+
"watchers_count": 0,
78+
"language": null,
79+
"has_issues": true,
80+
"has_projects": true,
81+
"has_downloads": true,
82+
"has_wiki": true,
83+
"has_pages": false,
84+
"forks_count": 0,
85+
"mirror_url": null,
86+
"archived": false,
87+
"disabled": false,
88+
"open_issues_count": 0,
89+
"license": null,
90+
"forks": 0,
91+
"open_issues": 0,
92+
"watchers": 0,
93+
"default_branch": "main",
94+
"permissions": {
95+
"admin": true,
96+
"push": true,
97+
"pull": true
98+
},
99+
"temp_clone_token": "",
100+
"allow_squash_merge": true,
101+
"allow_merge_commit": true,
102+
"allow_rebase_merge": true,
103+
"delete_branch_on_merge": false,
104+
"organization": {
105+
"login": "hub4j-test-org",
106+
"id": 7544739,
107+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
108+
"avatar_url": "https://avatars.githubusercontent.com/u/7544739?v=4",
109+
"gravatar_id": "",
110+
"url": "https://api.github.com/users/hub4j-test-org",
111+
"html_url": "https://github.com/hub4j-test-org",
112+
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
113+
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
114+
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
115+
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
116+
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
117+
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
118+
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
119+
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
120+
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
121+
"type": "Organization",
122+
"site_admin": false
123+
},
124+
"network_count": 0,
125+
"subscribers_count": 10
126+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"total_count": 1,
3+
"workflows": [
4+
{
5+
"id": 6817859,
6+
"node_id": "MDg6V29ya2Zsb3c2ODE3ODU5",
7+
"name": "test-workflow",
8+
"path": ".github/workflows/test-workflow.yml",
9+
"state": "active",
10+
"created_at": "2021-03-17T10:33:32.000+01:00",
11+
"updated_at": "2021-03-25T11:36:24.000+01:00",
12+
"url": "https://api.github.com/repos/hub4j-test-org/GHWorkflowTest/actions/workflows/6817859",
13+
"html_url": "https://github.com/hub4j-test-org/GHWorkflowTest/blob/main/.github/workflows/test-workflow.yml",
14+
"badge_url": "https://github.com/hub4j-test-org/GHWorkflowTest/workflows/test-workflow/badge.svg"
15+
}
16+
]
17+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"login": "gsmet",
3+
"id": 1279749,
4+
"node_id": "MDQ6VXNlcjEyNzk3NDk=",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/1279749?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/gsmet",
8+
"html_url": "https://github.com/gsmet",
9+
"followers_url": "https://api.github.com/users/gsmet/followers",
10+
"following_url": "https://api.github.com/users/gsmet/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/gsmet/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/gsmet/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/gsmet/subscriptions",
14+
"organizations_url": "https://api.github.com/users/gsmet/orgs",
15+
"repos_url": "https://api.github.com/users/gsmet/repos",
16+
"events_url": "https://api.github.com/users/gsmet/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/gsmet/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Guillaume Smet",
21+
"company": "Red Hat",
22+
"blog": "https://www.redhat.com/",
23+
"location": "Lyon, France",
24+
"email": "[email protected]",
25+
"hireable": null,
26+
"bio": "Happy camper at Red Hat, working on Quarkus and the Hibernate portfolio.",
27+
"twitter_username": "gsmet_",
28+
"public_repos": 102,
29+
"public_gists": 14,
30+
"followers": 126,
31+
"following": 3,
32+
"created_at": "2011-12-22T11:03:22Z",
33+
"updated_at": "2021-03-25T13:53:32Z",
34+
"private_gists": 14,
35+
"total_private_repos": 4,
36+
"owned_private_repos": 1,
37+
"disk_usage": 68258,
38+
"collaborators": 1,
39+
"two_factor_authentication": true,
40+
"plan": {
41+
"name": "free",
42+
"space": 976562499,
43+
"collaborators": 0,
44+
"private_repos": 10000
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
{
2+
"id": "df58f346-eeca-4472-aa08-1d09980919b3",
3+
"name": "repos_hub4j-test-org_ghworkflowtest",
4+
"request": {
5+
"url": "/repos/hub4j-test-org/GHWorkflowTest",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "repos_hub4j-test-org_ghworkflowtest-2.json",
16+
"headers": {
17+
"Server": "GitHub.com",
18+
"Date": "Mon, 29 Mar 2021 17:51:53 GMT",
19+
"Content-Type": "application/json; charset=utf-8",
20+
"Cache-Control": "private, max-age=60, s-maxage=60",
21+
"Vary": [
22+
"Accept, Authorization, Cookie, X-GitHub-OTP",
23+
"Accept-Encoding, Accept, X-Requested-With"
24+
],
25+
"ETag": "W/\"082e0c2ae9f1d8387e22ebe7279d627738b78d114a3badc2d9e60dfb0b2a6abf\"",
26+
"Last-Modified": "Wed, 17 Mar 2021 09:33:34 GMT",
27+
"X-OAuth-Scopes": "repo, user, workflow",
28+
"X-Accepted-OAuth-Scopes": "repo",
29+
"X-GitHub-Media-Type": "unknown, github.v3",
30+
"X-RateLimit-Limit": "5000",
31+
"X-RateLimit-Remaining": "4995",
32+
"X-RateLimit-Reset": "1617043912",
33+
"X-RateLimit-Used": "5",
34+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
35+
"X-Frame-Options": "deny",
36+
"X-Content-Type-Options": "nosniff",
37+
"X-XSS-Protection": "0",
38+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
39+
"Content-Security-Policy": "default-src 'none'",
40+
"X-GitHub-Request-Id": "C8C8:5652:A3CF5A:A72804:606213B9"
41+
}
42+
},
43+
"uuid": "df58f346-eeca-4472-aa08-1d09980919b3",
44+
"persistent": true,
45+
"insertionIndex": 2
46+
}

0 commit comments

Comments
 (0)