Skip to content

Commit

Permalink
Add method for listing deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
Surya Gaddipati committed Jan 2, 2015
1 parent 2f31815 commit 5d83894
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/main/java/org/kohsuke/github/GHDeployment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@


import java.net.URL;
import java.util.Date;

public class GHDeployment extends Identifiable {
private GHRepository owner;
private GitHub root;
protected String sha;
protected String ref;
protected String task;
protected String payload;
protected Object payload;
protected String environment;
protected String description;
protected String statuses_url;
Expand All @@ -32,8 +32,22 @@ public URL getRepositoryUrl() {
return GitHub.parseURL(repository_url);
}

public String getTask() {
return task;
}
public String getPayload() {
return (String) payload;
}
public String getEnvironment() {
return environment;
}
public GHUser getCreator() {
return creator;
}

public String getRef() {
return ref;
}
public String getSha(){
return sha;
}
}
32 changes: 32 additions & 0 deletions src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import org.apache.commons.lang.StringUtils;

import javax.xml.bind.DatatypeConverter;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -79,6 +80,37 @@ protected void wrapUp(GHDeploymentStatus[] page) {
};
}

public PagedIterable<GHDeployment> listDeployments(String sha,String ref,String task,String environment){
List<String> params = Arrays.asList(getParam("sha", sha), getParam("ref", ref), getParam("task", task), getParam("environment", environment));
final String deploymentsUrl = getApiTailUrl("deployments") + "?"+ join(params,"&");
return new PagedIterable<GHDeployment>() {
public PagedIterator<GHDeployment> iterator() {
return new PagedIterator<GHDeployment>(root.retrieve().asIterator(deploymentsUrl, GHDeployment[].class)) {
@Override
protected void wrapUp(GHDeployment[] page) {
for (GHDeployment c : page)
c.wrap(GHRepository.this);
}
};
}
};

}

private String join(List<String> params, String joinStr) {
StringBuilder output = new StringBuilder();
for(String param: params){
if(param != null){
output.append(param+joinStr);
}
}
return output.toString();
}

private String getParam(String name, String value) {
return StringUtils.trimToNull(value)== null? null: name+"="+value;
}

public GHDeploymentStatusBuilder createDeployStatus(int deploymentId, GHDeploymentState ghDeploymentState) {
return new GHDeploymentStatusBuilder(this,deploymentId,ghDeploymentState);
}
Expand Down
21 changes: 20 additions & 1 deletion src/test/java/org/kohsuke/github/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.junit.Assume;
import org.junit.Test;
import org.kohsuke.github.GHCommit.File;
Expand Down Expand Up @@ -87,6 +88,24 @@ public void testCreateDeployment() throws IOException {
assertNotNull(deployment.getId());
}

@Test
public void testListDeployments() throws IOException {
GHRepository repository = getTestRepository();
GHDeployment deployment = repository.createDeployment("master")
.payload("{\"user\":\"atmos\",\"room_id\":123456}")
.description("question")
.environment("unittest")
.create();
assertNotNull(deployment.getCreator());
assertNotNull(deployment.getId());
ArrayList<GHDeployment> deployments = Lists.newArrayList(repository.listDeployments(null, "master", null, "unittest"));
assertNotNull(deployments);
assertFalse(Iterables.isEmpty(deployments));
GHDeployment unitTestDeployment = deployments.get(0);
assertEquals("unittest",unitTestDeployment.getEnvironment());
assertEquals("master",unitTestDeployment.getRef());
}

@Test
public void testGetDeploymentStatuses() throws IOException {
GHRepository repository = getTestRepository();
Expand All @@ -100,7 +119,7 @@ public void testGetDeploymentStatuses() throws IOException {
Iterable<GHDeploymentStatus> deploymentStatuses = repository.getDeploymentStatuses(deployment.getId());
assertNotNull(deploymentStatuses);
assertEquals(1,Iterables.size(deploymentStatuses));
assertEquals(ghDeploymentStatus.getId(),Iterables.get(deploymentStatuses,0).getId());
assertEquals(ghDeploymentStatus.getId(), Iterables.get(deploymentStatuses, 0).getId());
}

@Test
Expand Down

0 comments on commit 5d83894

Please sign in to comment.