Skip to content

Commit 042038f

Browse files
committed
Implement GHWorkflow and GHWorkflowRun
Most of the actions are implemented but not all. Looks like a good first step.
1 parent fb03e74 commit 042038f

124 files changed

Lines changed: 35964 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,12 @@
409409
<version>4.13.2</version>
410410
<scope>test</scope>
411411
</dependency>
412+
<dependency>
413+
<groupId>org.awaitility</groupId>
414+
<artifactId>awaitility</artifactId>
415+
<version>4.0.3</version>
416+
<scope>test</scope>
417+
</dependency>
412418
<dependency>
413419
<groupId>com.fasterxml.jackson.core</groupId>
414420
<artifactId>jackson-databind</artifactId>

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2898,6 +2898,71 @@ public GHIssueEvent getIssueEvent(long id) throws IOException {
28982898
.wrapUp(root);
28992899
}
29002900

2901+
/**
2902+
* Lists all the workflows of this repository.
2903+
*
2904+
* @return the paged iterable
2905+
*/
2906+
public PagedIterable<GHWorkflow> listWorkflows() {
2907+
return root.createRequest()
2908+
.withUrlPath(getApiTailUrl("actions/workflows"))
2909+
.toIterable(GHWorkflow[].class, item -> item.wrapUp(root));
2910+
}
2911+
2912+
/**
2913+
* Gets a workflow by id.
2914+
*
2915+
* @param id
2916+
* the id of the workflow run
2917+
* @return the workflow run
2918+
* @throws IOException
2919+
* the io exception
2920+
*/
2921+
public GHWorkflow getWorkflow(long id) throws IOException {
2922+
return getWorkflow(String.valueOf(id));
2923+
}
2924+
2925+
/**
2926+
* Gets a workflow by name of the file.
2927+
*
2928+
* @param nameOrId
2929+
* either the name of the file (e.g. my-workflow.yml) or the id as a string
2930+
* @return the workflow run
2931+
* @throws IOException
2932+
* the io exception
2933+
*/
2934+
public GHWorkflow getWorkflow(String nameOrId) throws IOException {
2935+
return root.createRequest()
2936+
.withUrlPath(getApiTailUrl("actions/workflows"), nameOrId)
2937+
.fetch(GHWorkflow.class)
2938+
.wrapUp(this);
2939+
}
2940+
2941+
/**
2942+
* Retrieves workflow runs.
2943+
*
2944+
* @return the workflow run query builder
2945+
*/
2946+
public GHWorkflowRunQueryBuilder queryWorkflowRuns() {
2947+
return new GHWorkflowRunQueryBuilder(this);
2948+
}
2949+
2950+
/**
2951+
* Gets a workflow run.
2952+
*
2953+
* @param id
2954+
* the id of the workflow run
2955+
* @return the workflow run
2956+
* @throws IOException
2957+
* the io exception
2958+
*/
2959+
public GHWorkflowRun getWorkflowRun(long id) throws IOException {
2960+
return root.createRequest()
2961+
.withUrlPath(getApiTailUrl("actions/runs"), String.valueOf(id))
2962+
.fetch(GHWorkflowRun.class)
2963+
.wrapUp(this);
2964+
}
2965+
29012966
// Only used within listTopics().
29022967
private static class Topics {
29032968
public List<String> names;
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package org.kohsuke.github;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import org.apache.commons.lang3.StringUtils;
5+
6+
import java.io.IOException;
7+
import java.net.URL;
8+
import java.util.Collections;
9+
import java.util.Map;
10+
import java.util.Objects;
11+
12+
/**
13+
* A workflow.
14+
*
15+
* @author Guillaume Smet
16+
* @see GHRepository#getWorkflow(long)
17+
*/
18+
public class GHWorkflow extends GHObject {
19+
20+
// Not provided by the API.
21+
@JsonIgnore
22+
private GHRepository owner;
23+
24+
private String name;
25+
private String path;
26+
private String state;
27+
28+
private String htmlUrl;
29+
private String badgeUrl;
30+
31+
/**
32+
* The name of the workflow.
33+
*
34+
* @return the name
35+
*/
36+
public String getName() {
37+
return name;
38+
}
39+
40+
/**
41+
* The path of the workflow e.g. .github/workflows/blank.yaml
42+
*
43+
* @return the path
44+
*/
45+
public String getPath() {
46+
return path;
47+
}
48+
49+
/**
50+
* The state of the workflow.
51+
*
52+
* @return the state
53+
*/
54+
public String getState() {
55+
return state;
56+
}
57+
58+
@Override
59+
public URL getHtmlUrl() throws IOException {
60+
return GitHubClient.parseURL(htmlUrl);
61+
}
62+
63+
/**
64+
* The badge URL, like https://github.com/octo-org/octo-repo/workflows/CI/badge.svg
65+
*
66+
* @return the badge url
67+
*/
68+
public URL getBadgeUrl() {
69+
return GitHubClient.parseURL(badgeUrl);
70+
}
71+
72+
/**
73+
* Disable the workflow.
74+
*
75+
* @throws IOException
76+
* the io exception
77+
*/
78+
public void disable() throws IOException {
79+
root.createRequest().method("PUT").withUrlPath(getApiRoute(), "disable").fetchHttpStatusCode();
80+
}
81+
82+
/**
83+
* Enable the workflow.
84+
*
85+
* @throws IOException
86+
* the io exception
87+
*/
88+
public void enable() throws IOException {
89+
root.createRequest().method("PUT").withUrlPath(getApiRoute(), "enable").fetchHttpStatusCode();
90+
}
91+
92+
/**
93+
* Create a workflow dispatch event which triggers a manual workflow run.
94+
*
95+
* @param ref
96+
* the git reference for the workflow. The reference can be a branch or tag name.
97+
* @throws IOException
98+
* the io exception
99+
*/
100+
public void dispatch(String ref) throws IOException {
101+
dispatch(ref, Collections.emptyMap());
102+
}
103+
104+
/**
105+
* Create a workflow dispatch event which triggers a manual workflow run.
106+
*
107+
* @param ref
108+
* the git reference for the workflow. The reference can be a branch or tag name.
109+
* @param inputs
110+
* input keys and values configured in the workflow file. The maximum number of properties is 10. Any
111+
* default properties configured in the workflow file will be used when inputs are omitted.
112+
* @throws IOException
113+
* the io exception
114+
*/
115+
public void dispatch(String ref, Map<String, Object> inputs) throws IOException {
116+
Requester requester = root.createRequest()
117+
.method("POST")
118+
.withUrlPath(getApiRoute(), "dispatches")
119+
.with("ref", ref);
120+
121+
if (!inputs.isEmpty()) {
122+
requester.with("inputs", inputs);
123+
}
124+
125+
requester.fetchHttpStatusCode();
126+
}
127+
128+
private String getApiRoute() {
129+
if (owner == null) {
130+
// Workflow runs returned from search to do not have an owner. Attempt to use url.
131+
final URL url = Objects.requireNonNull(getUrl(), "Missing instance URL!");
132+
return StringUtils.prependIfMissing(url.toString().replace(root.getApiUrl(), ""), "/");
133+
134+
}
135+
return "/repos/" + owner.getOwnerName() + "/" + owner.getName() + "/actions/workflows/" + getId();
136+
}
137+
138+
GHWorkflow wrapUp(GHRepository owner) {
139+
this.owner = owner;
140+
return wrapUp(owner.root);
141+
}
142+
143+
GHWorkflow wrapUp(GitHub root) {
144+
this.root = root;
145+
if (owner != null)
146+
owner.wrap(root);
147+
return this;
148+
}
149+
}

0 commit comments

Comments
 (0)