Skip to content

Commit 228ea7e

Browse files
committed
Add get and list workflow runs from github actions API
The WorkflowRunsClient provides the following: * listAllWorkflowRuns - get All workflows for the repo * listWorkflowRuns - get workflow runs for a given workflow * getWorkflowRun - get a workflow run by ID All calls support filtering via query parameters. Tests included.
1 parent 674d08e commit 228ea7e

File tree

9 files changed

+1597
-53
lines changed

9 files changed

+1597
-53
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.actions.workflowruns;
22+
23+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
24+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
25+
import com.spotify.github.GithubStyle;
26+
import com.spotify.github.Parameters;
27+
import org.immutables.value.Value;
28+
29+
import java.util.Optional;
30+
31+
@Value.Immutable
32+
@GithubStyle
33+
@JsonSerialize(as = ImmutableGetWorkflowRunQueryParams.class)
34+
@JsonDeserialize(as = ImmutableGetWorkflowRunQueryParams.class)
35+
public interface GetWorkflowRunQueryParams extends Parameters {
36+
/**
37+
* If true pull requests are omitted from the response (empty array).
38+
* >p<
39+
* Default: false
40+
*/
41+
Optional<Boolean> excludePullRequests();
42+
}

src/main/java/com/spotify/github/v3/actions/workflowruns/QueryParams.java renamed to src/main/java/com/spotify/github/v3/actions/workflowruns/ListWorkflowRunsQueryParams.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030

3131
@Value.Immutable
3232
@GithubStyle
33-
@JsonSerialize(as = ImmutableQueryParams.class)
34-
@JsonDeserialize(as = ImmutableQueryParams.class)
35-
public interface QueryParams extends Parameters {
33+
@JsonSerialize(as = ImmutableListWorkflowRunsQueryParams.class)
34+
@JsonDeserialize(as = ImmutableListWorkflowRunsQueryParams.class)
35+
public interface ListWorkflowRunsQueryParams extends Parameters {
3636
/**
3737
* Returns someone's workflow runs. Use the login for the user who created the push associated with the check suite or workflow run.
3838
*/
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.base.Strings;
24+
import com.google.common.collect.ImmutableMap;
25+
import com.spotify.github.v3.actions.workflowruns.GetWorkflowRunQueryParams;
26+
import com.spotify.github.v3.actions.workflowruns.ListWorkflowRunsQueryParams;
27+
import com.spotify.github.v3.actions.workflowruns.WorkflowRunResponse;
28+
import com.spotify.github.v3.actions.workflowruns.WorkflowRunsResponseList;
29+
30+
import javax.annotation.Nullable;
31+
import javax.ws.rs.core.HttpHeaders;
32+
import java.util.Map;
33+
import java.util.concurrent.CompletableFuture;
34+
35+
/**
36+
* Workflow Runs API client
37+
*/
38+
public class WorkflowRunsClient {
39+
private static final String LIST_REPOSITORY_WORKFLOW_RUNS_URI = "/repos/%s/%s/actions/runs";
40+
private static final String LIST_WORKFLOW_RUNS_URI = "/repos/%s/%s/actions/workflow/%s/runs";
41+
private static final String GET_WORKFLOW_RUN_URI = "/repos/%s/%s/actions/runs/%s";
42+
43+
private final GitHubClient github;
44+
private final String owner;
45+
private final String repo;
46+
47+
private final Map<String, String> extraHeaders =
48+
ImmutableMap.of(HttpHeaders.ACCEPT, "application/vnd.github+json");
49+
50+
public WorkflowRunsClient(final GitHubClient github, final String owner, final String repo) {
51+
this.github = github;
52+
this.owner = owner;
53+
this.repo = repo;
54+
}
55+
56+
static WorkflowRunsClient create(final GitHubClient github, final String owner, final String repo) {
57+
return new WorkflowRunsClient(github, owner, repo);
58+
}
59+
60+
/**
61+
* List all workflow runs for a repository.
62+
*
63+
* @param queryParams optional parameters to add to the query. Can be null.
64+
* @return a list of workflow runs for the repository
65+
*/
66+
public CompletableFuture<WorkflowRunsResponseList> listAllWorkflowRuns(@Nullable final ListWorkflowRunsQueryParams queryParams) {
67+
final String serial = (queryParams == null ? "" : queryParams.serialize());
68+
final String path = String.format(LIST_REPOSITORY_WORKFLOW_RUNS_URI, owner, repo) + (Strings.isNullOrEmpty(serial) ? "" : "?" + serial);
69+
return github.request(path, WorkflowRunsResponseList.class, extraHeaders);
70+
}
71+
72+
/**
73+
* List workflow runs for the given workflow.
74+
*
75+
* @param workflowId the workflow id to get the workflow runs for
76+
* @param queryParams optional parameters to add to the query. Can be null.
77+
* @return a list of workflow runs for the given workflow
78+
*/
79+
public CompletableFuture<WorkflowRunsResponseList> listWorkflowRuns(final int workflowId, @Nullable final ListWorkflowRunsQueryParams queryParams) {
80+
final String serial = (queryParams == null ? "" : queryParams.serialize());
81+
final String path = String.format(LIST_WORKFLOW_RUNS_URI, owner, repo, workflowId) + (Strings.isNullOrEmpty(serial) ? "" : "?" + serial);
82+
83+
return github.request(path, WorkflowRunsResponseList.class, extraHeaders);
84+
}
85+
86+
/**
87+
* Gets a workflow by id.
88+
*
89+
* @param runId the workflow run id to be retrieved
90+
* @return a WorkflowRunResponse
91+
*/
92+
public CompletableFuture<WorkflowRunResponse> getWorkflowRun(final long runId, @Nullable final GetWorkflowRunQueryParams queryParams) {
93+
final String serial = (queryParams == null ? "" : queryParams.serialize());
94+
final String path = String.format(GET_WORKFLOW_RUN_URI, owner, repo, runId) + (Strings.isNullOrEmpty(serial) ? "" : "?" + serial);
95+
return github.request(path, WorkflowRunResponse.class, extraHeaders);
96+
}
97+
}

src/main/java/com/spotify/github/v3/workflows/WorkflowsResponse.java

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -31,64 +31,73 @@
3131
@GithubStyle
3232
@JsonDeserialize(as = ImmutableWorkflowsResponse.class)
3333
public interface WorkflowsResponse {
34-
/**
35-
* The Workflow ID.
36-
*
37-
* @return the int
38-
*/
39-
int id();
34+
/**
35+
* The Workflow ID.
36+
*
37+
* @return the int
38+
*/
39+
int id();
4040

41-
/** Node ID */
42-
String nodeId();
41+
/**
42+
* Node ID
43+
*/
44+
String nodeId();
4345

44-
/** Name. */
45-
String name();
46+
/**
47+
* Name.
48+
*/
49+
String name();
4650

47-
/** The workflow path. */
48-
String path();
51+
/**
52+
* The workflow path.
53+
*/
54+
String path();
4955

50-
/** Indicates the state of the workflow. */
51-
WorkflowsState state();
56+
/**
57+
* Indicates the state of the workflow.
58+
*/
59+
WorkflowsState state();
5260

53-
/**
54-
* Created At
55-
*
56-
* @return The time when the workflow was created
57-
*/
58-
ZonedDateTime createdAt();
61+
/**
62+
* Created At
63+
*
64+
* @return The time when the workflow was created
65+
*/
66+
ZonedDateTime createdAt();
5967

60-
/**
61-
* Updated At
62-
*
63-
* @return The time when the workflow was updated
64-
*/
65-
ZonedDateTime updatedAt();
68+
/**
69+
* Updated At
70+
*
71+
* @return The time when the workflow was updated
72+
*/
73+
ZonedDateTime updatedAt();
6674

67-
/**
68-
* Deleted At
69-
*
70-
* @return The time when the workflow was deleted
71-
*/
72-
@Nullable ZonedDateTime deletedAt();
75+
/**
76+
* Deleted At
77+
*
78+
* @return The time when the workflow was deleted
79+
*/
80+
@Nullable
81+
ZonedDateTime deletedAt();
7382

74-
/**
75-
* Url string.
76-
*
77-
* @return the string
78-
*/
79-
String url();
83+
/**
84+
* Url string.
85+
*
86+
* @return the string
87+
*/
88+
String url();
8089

81-
/**
82-
* Html url string.
83-
*
84-
* @return the string
85-
*/
86-
String htmlUrl();
90+
/**
91+
* Html url string.
92+
*
93+
* @return the string
94+
*/
95+
String htmlUrl();
8796

88-
/**
89-
* Badge Url string.
90-
*
91-
* @return the string
92-
*/
93-
String badgeUrl();
97+
/**
98+
* Badge Url string.
99+
*
100+
* @return the string
101+
*/
102+
String badgeUrl();
94103
}

0 commit comments

Comments
 (0)