Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 3737845

Browse files
committed
Merge pull request hub4j#143
2 parents 052902f + a30c78c commit 3737845

File tree

8 files changed

+256
-8
lines changed

8 files changed

+256
-8
lines changed

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

100644100755
Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,53 @@
11
package org.kohsuke.github;
22

3-
public class GHDeployment {
3+
4+
import java.net.URL;
5+
6+
public class GHDeployment extends Identifiable {
47
private GHRepository owner;
58
private GitHub root;
9+
protected String sha;
10+
protected String ref;
11+
protected String task;
12+
protected Object payload;
13+
protected String environment;
14+
protected String description;
15+
protected String statuses_url;
16+
protected String repository_url;
17+
protected GHUser creator;
18+
619

720
GHDeployment wrap(GHRepository owner) {
821
this.owner = owner;
922
this.root = owner.root;
23+
if(creator != null) creator.wrapUp(root);
1024
return this;
1125
}
26+
27+
public URL getStatusesUrl() {
28+
return GitHub.parseURL(statuses_url);
29+
}
30+
31+
public URL getRepositoryUrl() {
32+
return GitHub.parseURL(repository_url);
33+
}
34+
35+
public String getTask() {
36+
return task;
37+
}
38+
public String getPayload() {
39+
return (String) payload;
40+
}
41+
public String getEnvironment() {
42+
return environment;
43+
}
44+
public GHUser getCreator() {
45+
return creator;
46+
}
47+
public String getRef() {
48+
return ref;
49+
}
50+
public String getSha(){
51+
return sha;
52+
}
1253
}

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

100644100755
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.kohsuke.github;
22

33
import java.io.IOException;
4+
import java.util.List;
45

6+
//Based on https://developer.github.com/v3/repos/deployments/#create-a-deployment
57
public class GHDeploymentBuilder {
68
private final GHRepository repo;
79
private final Requester builder;
@@ -11,16 +13,37 @@ public GHDeploymentBuilder(GHRepository repo) {
1113
this.builder = new Requester(repo.root);
1214
}
1315

16+
public GHDeploymentBuilder(GHRepository repo, String ref) {
17+
this(repo);
18+
ref(ref);
19+
}
20+
1421
public GHDeploymentBuilder ref(String branch) {
1522
builder.with("ref",branch);
1623
return this;
1724
}
25+
public GHDeploymentBuilder task(String task) {
26+
builder.with("task",task);
27+
return this;
28+
}
29+
public GHDeploymentBuilder autoMerge(boolean autoMerge) {
30+
builder.with("auto_merge",autoMerge);
31+
return this;
32+
}
1833

34+
public GHDeploymentBuilder requiredContexts(List<String> requiredContexts) {
35+
builder.with("required_contexts",requiredContexts);
36+
return this;
37+
}
1938
public GHDeploymentBuilder payload(String payload) {
2039
builder.with("payload",payload);
2140
return this;
2241
}
2342

43+
public GHDeploymentBuilder environment(String environment) {
44+
builder.with("environment",environment);
45+
return this;
46+
}
2447
public GHDeploymentBuilder description(String description) {
2548
builder.with("description",description);
2649
return this;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.kohsuke.github;
2+
3+
/**
4+
* Represents the state of deployment
5+
*/
6+
public enum GHDeploymentState {
7+
PENDING, SUCCESS, ERROR, FAILURE
8+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.kohsuke.github;
2+
3+
import java.net.URL;
4+
5+
public class GHDeploymentStatus extends Identifiable {
6+
private GHRepository owner;
7+
private GitHub root;
8+
protected GHUser creator;
9+
protected String state;
10+
protected String description;
11+
protected String target_url;
12+
protected String deployment_url;
13+
protected String repository_url;
14+
public GHDeploymentStatus wrap(GHRepository owner) {
15+
this.owner = owner;
16+
this.root = owner.root;
17+
if(creator != null) creator.wrapUp(root);
18+
return this;
19+
}
20+
public URL getTargetUrl() {
21+
return GitHub.parseURL(target_url);
22+
}
23+
24+
public URL getDeploymentUrl() {
25+
return GitHub.parseURL(deployment_url);
26+
}
27+
28+
public URL getRepositoryUrl() {
29+
return GitHub.parseURL(repository_url);
30+
}
31+
public GHDeploymentState getState() {
32+
return GHDeploymentState.valueOf(state.toUpperCase());
33+
}
34+
35+
36+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.kohsuke.github;
2+
3+
import java.io.IOException;
4+
5+
public class GHDeploymentStatusBuilder {
6+
private final Requester builder;
7+
private GHRepository repo;
8+
private int deploymentId;
9+
10+
public GHDeploymentStatusBuilder(GHRepository repo, int deploymentId, GHDeploymentState state) {
11+
this.repo = repo;
12+
this.deploymentId = deploymentId;
13+
this.builder = new Requester(repo.root);
14+
this.builder.with("state",state.toString().toLowerCase());
15+
}
16+
17+
public GHDeploymentStatusBuilder description(String description) {
18+
this.builder.with("description",description);
19+
return this;
20+
}
21+
22+
public GHDeploymentStatusBuilder targetUrl(String targetUrl) {
23+
this.builder.with("target_url",targetUrl);
24+
return this;
25+
}
26+
27+
public GHDeploymentStatus create() throws IOException {
28+
return builder.to(repo.getApiTailUrl("deployments")+"/"+deploymentId+"/statuses",GHDeploymentStatus.class).wrap(repo);
29+
}
30+
}

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

100644100755
Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import com.fasterxml.jackson.annotation.JsonProperty;
2727
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
28+
import org.apache.commons.lang.StringUtils;
2829

2930
import javax.xml.bind.DatatypeConverter;
3031
import java.io.FileNotFoundException;
@@ -61,8 +62,57 @@ public class GHRepository {
6162

6263
private GHRepoPermission permissions;
6364

64-
public GHDeploymentBuilder createDeployment() {
65-
return new GHDeploymentBuilder(this);
65+
public GHDeploymentBuilder createDeployment(String ref) {
66+
return new GHDeploymentBuilder(this,ref);
67+
}
68+
69+
public PagedIterable<GHDeploymentStatus> getDeploymentStatuses(final int id) {
70+
return new PagedIterable<GHDeploymentStatus>() {
71+
public PagedIterator<GHDeploymentStatus> iterator() {
72+
return new PagedIterator<GHDeploymentStatus>(root.retrieve().asIterator(getApiTailUrl("deployments")+"/"+id+"/statuses", GHDeploymentStatus[].class)) {
73+
@Override
74+
protected void wrapUp(GHDeploymentStatus[] page) {
75+
for (GHDeploymentStatus c : page)
76+
c.wrap(GHRepository.this);
77+
}
78+
};
79+
}
80+
};
81+
}
82+
83+
public PagedIterable<GHDeployment> listDeployments(String sha,String ref,String task,String environment){
84+
List<String> params = Arrays.asList(getParam("sha", sha), getParam("ref", ref), getParam("task", task), getParam("environment", environment));
85+
final String deploymentsUrl = getApiTailUrl("deployments") + "?"+ join(params,"&");
86+
return new PagedIterable<GHDeployment>() {
87+
public PagedIterator<GHDeployment> iterator() {
88+
return new PagedIterator<GHDeployment>(root.retrieve().asIterator(deploymentsUrl, GHDeployment[].class)) {
89+
@Override
90+
protected void wrapUp(GHDeployment[] page) {
91+
for (GHDeployment c : page)
92+
c.wrap(GHRepository.this);
93+
}
94+
};
95+
}
96+
};
97+
98+
}
99+
100+
private String join(List<String> params, String joinStr) {
101+
StringBuilder output = new StringBuilder();
102+
for(String param: params){
103+
if(param != null){
104+
output.append(param+joinStr);
105+
}
106+
}
107+
return output.toString();
108+
}
109+
110+
private String getParam(String name, String value) {
111+
return StringUtils.trimToNull(value)== null? null: name+"="+value;
112+
}
113+
114+
public GHDeploymentStatusBuilder createDeployStatus(int deploymentId, GHDeploymentState ghDeploymentState) {
115+
return new GHDeploymentStatusBuilder(this,deploymentId,ghDeploymentState);
66116
}
67117

68118
private static class GHRepoPermission {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.kohsuke.github;
2+
3+
import java.net.URL;
4+
import java.util.Date;
5+
6+
public class Identifiable {
7+
protected String url;
8+
protected int id;
9+
protected String created_at;
10+
protected String updated_at;
11+
12+
public Date getCreatedAt() {
13+
return GitHub.parseDate(created_at);
14+
}
15+
16+
public URL getUrl() {
17+
return GitHub.parseURL(url);
18+
}
19+
20+
public Date getUpdatedAt() {
21+
return GitHub.parseDate(updated_at);
22+
}
23+
24+
public int getId() {
25+
return id;
26+
}
27+
}

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

100644100755
Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.common.base.Predicate;
44
import com.google.common.collect.Iterables;
5+
import com.google.common.collect.Lists;
56
import org.junit.Assume;
67
import org.junit.Test;
78
import org.kohsuke.github.GHCommit.File;
@@ -78,15 +79,47 @@ public void testCreateIssue() throws IOException {
7879

7980
@Test
8081
public void testCreateDeployment() throws IOException {
81-
GHUser u = getUser();
8282
GHRepository repository = getTestRepository();
83-
//GHMilestone milestone = repository.createMilestone(System.currentTimeMillis() + "", "Test Milestone");
84-
GHDeployment o = repository.createDeployment()
85-
.ref("master")
83+
GHDeployment deployment = repository.createDeployment("master")
8684
.payload("{\"user\":\"atmos\",\"room_id\":123456}")
8785
.description("question")
8886
.create();
89-
assertNotNull(o);
87+
assertNotNull(deployment.getCreator());
88+
assertNotNull(deployment.getId());
89+
}
90+
91+
@Test
92+
public void testListDeployments() throws IOException {
93+
GHRepository repository = getTestRepository();
94+
GHDeployment deployment = repository.createDeployment("master")
95+
.payload("{\"user\":\"atmos\",\"room_id\":123456}")
96+
.description("question")
97+
.environment("unittest")
98+
.create();
99+
assertNotNull(deployment.getCreator());
100+
assertNotNull(deployment.getId());
101+
ArrayList<GHDeployment> deployments = Lists.newArrayList(repository.listDeployments(null, "master", null, "unittest"));
102+
assertNotNull(deployments);
103+
assertFalse(Iterables.isEmpty(deployments));
104+
GHDeployment unitTestDeployment = deployments.get(0);
105+
assertEquals("unittest",unitTestDeployment.getEnvironment());
106+
assertEquals("master",unitTestDeployment.getRef());
107+
}
108+
109+
@Test
110+
public void testGetDeploymentStatuses() throws IOException {
111+
GHRepository repository = getTestRepository();
112+
GHDeployment deployment = repository.createDeployment("master")
113+
.description("question")
114+
.payload("{\"user\":\"atmos\",\"room_id\":123456}")
115+
.create();
116+
GHDeploymentStatus ghDeploymentStatus = repository.createDeployStatus(deployment.getId(), GHDeploymentState.SUCCESS)
117+
.description("success")
118+
.targetUrl("http://www.github.com").create();
119+
Iterable<GHDeploymentStatus> deploymentStatuses = repository.getDeploymentStatuses(deployment.getId());
120+
assertNotNull(deploymentStatuses);
121+
assertEquals(1,Iterables.size(deploymentStatuses));
122+
assertEquals(ghDeploymentStatus.getId(), Iterables.get(deploymentStatuses, 0).getId());
90123
}
91124

92125
@Test

0 commit comments

Comments
 (0)