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

Commit 2f31815

Browse files
Complete api implementation for setting/retriving deployment status on a deployment
1 parent bc518a9 commit 2f31815

7 files changed

Lines changed: 137 additions & 19 deletions

File tree

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
package org.kohsuke.github;
22

3+
34
import java.net.URL;
45
import java.util.Date;
56

6-
public class GHDeployment {
7+
public class GHDeployment extends Identifiable {
78
private GHRepository owner;
89
private GitHub root;
9-
protected String url;
1010
protected String sha;
11-
protected int id;
1211
protected String task;
1312
protected String payload;
1413
protected String environment;
1514
protected String description;
1615
protected String statuses_url;
1716
protected String repository_url;
18-
protected String created_at;
19-
protected String updated_at;
2017
protected GHUser creator;
2118

2219

@@ -26,13 +23,6 @@ GHDeployment wrap(GHRepository owner) {
2623
if(creator != null) creator.wrapUp(root);
2724
return this;
2825
}
29-
public Date getCreatedAt() {
30-
return GitHub.parseDate(created_at);
31-
}
32-
33-
public URL getUrl() {
34-
return GitHub.parseURL(url);
35-
}
3626

3727
public URL getStatusesUrl() {
3828
return GitHub.parseURL(statuses_url);
@@ -46,11 +36,4 @@ public GHUser getCreator() {
4636
return creator;
4737
}
4838

49-
public Date getUpdatedAt() {
50-
return GitHub.parseDate(updated_at);
51-
}
52-
53-
public int getId() {
54-
return id;
55-
}
5639
}
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 GHCommitState getState() {
32+
return GHCommitState.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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ public GHDeploymentBuilder createDeployment(String ref) {
6565
return new GHDeploymentBuilder(this,ref);
6666
}
6767

68+
public PagedIterable<GHDeploymentStatus> getDeploymentStatuses(final int id) {
69+
return new PagedIterable<GHDeploymentStatus>() {
70+
public PagedIterator<GHDeploymentStatus> iterator() {
71+
return new PagedIterator<GHDeploymentStatus>(root.retrieve().asIterator(getApiTailUrl("deployments")+"/"+id+"/statuses", GHDeploymentStatus[].class)) {
72+
@Override
73+
protected void wrapUp(GHDeploymentStatus[] page) {
74+
for (GHDeploymentStatus c : page)
75+
c.wrap(GHRepository.this);
76+
}
77+
};
78+
}
79+
};
80+
}
81+
82+
public GHDeploymentStatusBuilder createDeployStatus(int deploymentId, GHDeploymentState ghDeploymentState) {
83+
return new GHDeploymentStatusBuilder(this,deploymentId,ghDeploymentState);
84+
}
85+
6886
private static class GHRepoPermission {
6987
boolean pull,push,admin;
7088
}
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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ public void testCreateDeployment() throws IOException {
8787
assertNotNull(deployment.getId());
8888
}
8989

90+
@Test
91+
public void testGetDeploymentStatuses() throws IOException {
92+
GHRepository repository = getTestRepository();
93+
GHDeployment deployment = repository.createDeployment("master")
94+
.description("question")
95+
.payload("{\"user\":\"atmos\",\"room_id\":123456}")
96+
.create();
97+
GHDeploymentStatus ghDeploymentStatus = repository.createDeployStatus(deployment.getId(), GHDeploymentState.SUCCESS)
98+
.description("success")
99+
.targetUrl("http://www.github.com").create();
100+
Iterable<GHDeploymentStatus> deploymentStatuses = repository.getDeploymentStatuses(deployment.getId());
101+
assertNotNull(deploymentStatuses);
102+
assertEquals(1,Iterables.size(deploymentStatuses));
103+
assertEquals(ghDeploymentStatus.getId(),Iterables.get(deploymentStatuses,0).getId());
104+
}
105+
90106
@Test
91107
public void testGetIssues() throws Exception {
92108
List<GHIssue> closedIssues = gitHub.getUser("kohsuke").getRepository("github-api").getIssues(GHIssueState.CLOSED);

0 commit comments

Comments
 (0)