Skip to content

Commit fbcd82c

Browse files
authored
Add get and list workflows from github actions API (#197)
* Add get and list workflows from github actions API * Update workflows client hierarchy according to github API * Fix import
1 parent e00d227 commit fbcd82c

File tree

10 files changed

+474
-0
lines changed

10 files changed

+474
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2020 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.clients;
22+
23+
public class ActionsClient {
24+
private final String owner;
25+
private final String repo;
26+
private final GitHubClient github;
27+
28+
ActionsClient(final GitHubClient github, final String owner, final String repo) {
29+
this.github = github;
30+
this.owner = owner;
31+
this.repo = repo;
32+
}
33+
34+
static ActionsClient create(final GitHubClient github, final String owner, final String repo) {
35+
return new ActionsClient(github, owner, repo);
36+
}
37+
38+
/**
39+
* Workflows API client
40+
*
41+
* @return Workflows API client
42+
*/
43+
public WorkflowsClient createWorkflowsClient() {
44+
return WorkflowsClient.create(github, owner, repo);
45+
}
46+
}

src/main/java/com/spotify/github/v3/clients/RepositoryClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,15 @@ public ChecksClient createChecksApiClient() {
150150
return new ChecksClient(github, owner, repo);
151151
}
152152

153+
/**
154+
* Actions API client
155+
*
156+
* @return Actions API client
157+
*/
158+
public ActionsClient createActionsClient() {
159+
return ActionsClient.create(github, owner, repo);
160+
}
161+
153162
/**
154163
* Get information about this repository.
155164
*
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2020 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.clients;
22+
23+
import com.google.common.collect.ImmutableMap;
24+
import com.spotify.github.v3.workflows.WorkflowsRepositoryResponseList;
25+
import com.spotify.github.v3.workflows.WorkflowsResponse;
26+
27+
import javax.ws.rs.core.HttpHeaders;
28+
import java.util.Map;
29+
import java.util.concurrent.CompletableFuture;
30+
31+
/** Workflows API client */
32+
public class WorkflowsClient {
33+
private static final String LIST_REPOSITORY_WORKFLOWS_URI = "/repos/%s/%s/actions/workflows";
34+
private static final String GET_WORKFLOW_URI = "/repos/%s/%s/actions/workflows/%s";
35+
36+
private final GitHubClient github;
37+
private final String owner;
38+
private final String repo;
39+
40+
private final Map<String, String> extraHeaders =
41+
ImmutableMap.of(HttpHeaders.ACCEPT, "application/vnd.github+json");
42+
43+
public WorkflowsClient(final GitHubClient github, final String owner, final String repo) {
44+
this.github = github;
45+
this.owner = owner;
46+
this.repo = repo;
47+
}
48+
49+
static WorkflowsClient create(final GitHubClient github, final String owner, final String repo) {
50+
return new WorkflowsClient(github, owner, repo);
51+
}
52+
53+
/**
54+
* List workflows for a repository.
55+
*
56+
* @return a list of workflows for the repository
57+
*/
58+
public CompletableFuture<WorkflowsRepositoryResponseList> listWorkflows() {
59+
final String path = String.format(LIST_REPOSITORY_WORKFLOWS_URI, owner, repo);
60+
return github.request(path, WorkflowsRepositoryResponseList.class, extraHeaders);
61+
}
62+
63+
/**
64+
* Gets a workflow by id.
65+
*
66+
* @param id the workflow id
67+
* @return a WorkflowsResponse
68+
*/
69+
public CompletableFuture<WorkflowsResponse> getWorkflow(final int id) {
70+
final String path = String.format(GET_WORKFLOW_URI, owner, repo, id);
71+
return github.request(path, WorkflowsResponse.class, extraHeaders);
72+
}
73+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2020 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.workflows;
22+
23+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
24+
import com.spotify.github.GithubStyle;
25+
import org.immutables.value.Value;
26+
27+
import java.util.List;
28+
29+
/**
30+
* The WorkflowsResponse list resource
31+
*
32+
* @see "https://docs.github.com/en/rest/actions/workflows#list-repository-workflows"
33+
*/
34+
@Value.Immutable
35+
@GithubStyle
36+
@JsonDeserialize(as = ImmutableWorkflowsRepositoryResponseList.class)
37+
public interface WorkflowsRepositoryResponseList {
38+
/**
39+
* The count of workflows in the response
40+
*
41+
* @return the int
42+
*/
43+
int totalCount();
44+
45+
/**
46+
* Workflows list.
47+
*
48+
* @return the list of workflows
49+
*/
50+
List<WorkflowsResponse> workflows();
51+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2020 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.workflows;
22+
23+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
24+
import com.spotify.github.GithubStyle;
25+
import org.immutables.value.Value;
26+
27+
import java.time.ZonedDateTime;
28+
29+
@Value.Immutable
30+
@GithubStyle
31+
@JsonDeserialize(as = ImmutableWorkflowsResponse.class)
32+
public interface WorkflowsResponse {
33+
/**
34+
* The Workflow ID.
35+
*
36+
* @return the int
37+
*/
38+
int id();
39+
40+
/** Node ID */
41+
String nodeId();
42+
43+
/** Name. */
44+
String name();
45+
46+
/** The workflow path. */
47+
String path();
48+
49+
/** Indicates the state of the workflow. */
50+
WorkflowsState state();
51+
52+
/**
53+
* Created At
54+
*
55+
* @return The time when the workflow was created
56+
*/
57+
ZonedDateTime createdAt();
58+
59+
/**
60+
* Updated At
61+
*
62+
* @return The time when the workflow was updated
63+
*/
64+
ZonedDateTime updatedAt();
65+
66+
/**
67+
* Deleted At
68+
*
69+
* @return The time when the workflow was deleted
70+
*/
71+
ZonedDateTime deletedAt();
72+
73+
/**
74+
* Url string.
75+
*
76+
* @return the string
77+
*/
78+
String url();
79+
80+
/**
81+
* Html url string.
82+
*
83+
* @return the string
84+
*/
85+
String htmlUrl();
86+
87+
/**
88+
* Badge Url string.
89+
*
90+
* @return the string
91+
*/
92+
String badgeUrl();
93+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2020 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.workflows;
22+
23+
/** The Workflow State. */
24+
public enum WorkflowsState {
25+
active,
26+
deleted,
27+
disabled_fork,
28+
disabled_inactivity,
29+
disabled_manually
30+
}

src/test/java/com/spotify/github/v3/clients/GitHubClientTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
import com.spotify.github.v3.exceptions.RequestNotOkException;
4141
import com.spotify.github.v3.repos.CommitItem;
4242
import com.spotify.github.v3.repos.RepositoryInvitation;
43+
import com.spotify.github.v3.workflows.WorkflowsResponse;
44+
import com.spotify.github.v3.workflows.WorkflowsState;
4345

4446
import java.io.File;
4547
import java.io.IOException;
@@ -233,6 +235,35 @@ public void testGetCheckSuites() throws Throwable {
233235

234236
}
235237

238+
@Test
239+
public void testGetWorkflow() throws Throwable {
240+
final Call call = mock(Call.class);
241+
final ArgumentCaptor<Callback> callbackCapture = ArgumentCaptor.forClass(Callback.class);
242+
doNothing().when(call).enqueue(callbackCapture.capture());
243+
244+
final Response response = new okhttp3.Response.Builder()
245+
.code(200)
246+
.body(
247+
ResponseBody.create(
248+
MediaType.get("application/json"),
249+
getFixture("../workflows/workflows-get-workflow-response.json")))
250+
.message("")
251+
.protocol(Protocol.HTTP_1_1)
252+
.request(new Request.Builder().url("http://localhost/").build())
253+
.build();
254+
255+
when(client.newCall(any())).thenReturn(call);
256+
WorkflowsClient client = github.withTracer(tracer).createRepositoryClient("testorg", "testrepo")
257+
.createActionsClient().createWorkflowsClient();
258+
259+
CompletableFuture<WorkflowsResponse> future = client.getWorkflow(161335);
260+
callbackCapture.getValue().onResponse(call, response);
261+
var result = future.get();
262+
263+
assertThat(result.id(), is(161335));
264+
assertThat(result.state(), is(WorkflowsState.active));
265+
}
266+
236267
@Test
237268
void asAppScopedClientGetsUserClientIfOrgClientNotFound() {
238269
var appGithub = GitHubClient.create(client, URI.create("http://bogus"), new byte[] {}, 1);

0 commit comments

Comments
 (0)