diff --git a/src/main/java/org/kohsuke/github/GHDeployment.java b/src/main/java/org/kohsuke/github/GHDeployment.java old mode 100644 new mode 100755 index a6045a32fe..753364409d --- a/src/main/java/org/kohsuke/github/GHDeployment.java +++ b/src/main/java/org/kohsuke/github/GHDeployment.java @@ -1,12 +1,53 @@ package org.kohsuke.github; -public class GHDeployment { + +import java.net.URL; + +public class GHDeployment extends Identifiable { private GHRepository owner; private GitHub root; + protected String sha; + protected String ref; + protected String task; + protected Object payload; + protected String environment; + protected String description; + protected String statuses_url; + protected String repository_url; + protected GHUser creator; + GHDeployment wrap(GHRepository owner) { this.owner = owner; this.root = owner.root; + if(creator != null) creator.wrapUp(root); return this; } + + public URL getStatusesUrl() { + return GitHub.parseURL(statuses_url); + } + + 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; + } } diff --git a/src/main/java/org/kohsuke/github/GHDeploymentBuilder.java b/src/main/java/org/kohsuke/github/GHDeploymentBuilder.java old mode 100644 new mode 100755 index 3a2830befa..d4b48518a4 --- a/src/main/java/org/kohsuke/github/GHDeploymentBuilder.java +++ b/src/main/java/org/kohsuke/github/GHDeploymentBuilder.java @@ -1,26 +1,45 @@ package org.kohsuke.github; import java.io.IOException; +import java.util.List; +//Based on https://developer.github.com/v3/repos/deployments/#create-a-deployment public class GHDeploymentBuilder { private final GHRepository repo; private final Requester builder; - public GHDeploymentBuilder(GHRepository repo) { + public GHDeploymentBuilder(GHRepository repo, String ref) { this.repo = repo; this.builder = new Requester(repo.root); + ref(ref); } public GHDeploymentBuilder ref(String branch) { builder.with("ref",branch); return this; } + public GHDeploymentBuilder task(String task) { + builder.with("task",task); + return this; + } + public GHDeploymentBuilder autoMerge(boolean autoMerge) { + builder.with("auto_merge",autoMerge); + return this; + } + public GHDeploymentBuilder requiredContexts(List requiredContexts) { + builder.with("required_contexts",requiredContexts); + return this; + } public GHDeploymentBuilder payload(String payload) { builder.with("payload",payload); return this; } + public GHDeploymentBuilder environment(String environment) { + builder.with("environment",environment); + return this; + } public GHDeploymentBuilder description(String description) { builder.with("description",description); return this; diff --git a/src/main/java/org/kohsuke/github/GHDeploymentState.java b/src/main/java/org/kohsuke/github/GHDeploymentState.java new file mode 100755 index 0000000000..ff53a6d490 --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHDeploymentState.java @@ -0,0 +1,8 @@ +package org.kohsuke.github; + +/** + * Represents the state of deployment + */ +public enum GHDeploymentState { + PENDING, SUCCESS, ERROR, FAILURE +} diff --git a/src/main/java/org/kohsuke/github/GHDeploymentStatus.java b/src/main/java/org/kohsuke/github/GHDeploymentStatus.java new file mode 100755 index 0000000000..055b3cb700 --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHDeploymentStatus.java @@ -0,0 +1,36 @@ +package org.kohsuke.github; + +import java.net.URL; + +public class GHDeploymentStatus extends Identifiable { + private GHRepository owner; + private GitHub root; + protected GHUser creator; + protected String state; + protected String description; + protected String target_url; + protected String deployment_url; + protected String repository_url; + public GHDeploymentStatus wrap(GHRepository owner) { + this.owner = owner; + this.root = owner.root; + if(creator != null) creator.wrapUp(root); + return this; + } + public URL getTargetUrl() { + return GitHub.parseURL(target_url); + } + + public URL getDeploymentUrl() { + return GitHub.parseURL(deployment_url); + } + + public URL getRepositoryUrl() { + return GitHub.parseURL(repository_url); + } + public GHDeploymentState getState() { + return GHDeploymentState.valueOf(state.toUpperCase()); + } + + +} diff --git a/src/main/java/org/kohsuke/github/GHDeploymentStatusBuilder.java b/src/main/java/org/kohsuke/github/GHDeploymentStatusBuilder.java new file mode 100755 index 0000000000..6adf170fad --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHDeploymentStatusBuilder.java @@ -0,0 +1,30 @@ +package org.kohsuke.github; + +import java.io.IOException; + +public class GHDeploymentStatusBuilder { + private final Requester builder; + private GHRepository repo; + private int deploymentId; + + public GHDeploymentStatusBuilder(GHRepository repo, int deploymentId, GHDeploymentState state) { + this.repo = repo; + this.deploymentId = deploymentId; + this.builder = new Requester(repo.root); + this.builder.with("state",state.toString().toLowerCase()); + } + + public GHDeploymentStatusBuilder description(String description) { + this.builder.with("description",description); + return this; + } + + public GHDeploymentStatusBuilder targetUrl(String targetUrl) { + this.builder.with("target_url",targetUrl); + return this; + } + + public GHDeploymentStatus create() throws IOException { + return builder.to(repo.getApiTailUrl("deployments")+"/"+deploymentId+"/statuses",GHDeploymentStatus.class).wrap(repo); + } +} diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java old mode 100644 new mode 100755 index 204a8004c0..82c75394ec --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -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; @@ -61,8 +62,57 @@ public class GHRepository { private GHRepoPermission permissions; - public GHDeploymentBuilder createDeployment() { - return new GHDeploymentBuilder(this); + public GHDeploymentBuilder createDeployment(String ref) { + return new GHDeploymentBuilder(this,ref); + } + + public PagedIterable getDeploymentStatuses(final int id) { + return new PagedIterable() { + public PagedIterator iterator() { + return new PagedIterator(root.retrieve().asIterator(getApiTailUrl("deployments")+"/"+id+"/statuses", GHDeploymentStatus[].class)) { + @Override + protected void wrapUp(GHDeploymentStatus[] page) { + for (GHDeploymentStatus c : page) + c.wrap(GHRepository.this); + } + }; + } + }; + } + + public PagedIterable listDeployments(String sha,String ref,String task,String environment){ + List 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() { + public PagedIterator iterator() { + return new PagedIterator(root.retrieve().asIterator(deploymentsUrl, GHDeployment[].class)) { + @Override + protected void wrapUp(GHDeployment[] page) { + for (GHDeployment c : page) + c.wrap(GHRepository.this); + } + }; + } + }; + + } + + private String join(List 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); } private static class GHRepoPermission { diff --git a/src/main/java/org/kohsuke/github/Identifiable.java b/src/main/java/org/kohsuke/github/Identifiable.java new file mode 100755 index 0000000000..1d80fe99ef --- /dev/null +++ b/src/main/java/org/kohsuke/github/Identifiable.java @@ -0,0 +1,27 @@ +package org.kohsuke.github; + +import java.net.URL; +import java.util.Date; + +public class Identifiable { + protected String url; + protected int id; + protected String created_at; + protected String updated_at; + + public Date getCreatedAt() { + return GitHub.parseDate(created_at); + } + + public URL getUrl() { + return GitHub.parseURL(url); + } + + public Date getUpdatedAt() { + return GitHub.parseDate(updated_at); + } + + public int getId() { + return id; + } +} diff --git a/src/test/java/org/kohsuke/github/AppTest.java b/src/test/java/org/kohsuke/github/AppTest.java old mode 100644 new mode 100755 index a92b40242f..361f32445a --- a/src/test/java/org/kohsuke/github/AppTest.java +++ b/src/test/java/org/kohsuke/github/AppTest.java @@ -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; @@ -78,15 +79,47 @@ public void testCreateIssue() throws IOException { @Test public void testCreateDeployment() throws IOException { - GHUser u = getUser(); GHRepository repository = getTestRepository(); - //GHMilestone milestone = repository.createMilestone(System.currentTimeMillis() + "", "Test Milestone"); - GHDeployment o = repository.createDeployment() - .ref("master") + GHDeployment deployment = repository.createDeployment("master") .payload("{\"user\":\"atmos\",\"room_id\":123456}") .description("question") .create(); - assertNotNull(o); + assertNotNull(deployment.getCreator()); + 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 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(); + GHDeployment deployment = repository.createDeployment("master") + .description("question") + .payload("{\"user\":\"atmos\",\"room_id\":123456}") + .create(); + GHDeploymentStatus ghDeploymentStatus = repository.createDeployStatus(deployment.getId(), GHDeploymentState.SUCCESS) + .description("success") + .targetUrl("http://www.github.com").create(); + Iterable deploymentStatuses = repository.getDeploymentStatuses(deployment.getId()); + assertNotNull(deploymentStatuses); + assertEquals(1,Iterables.size(deploymentStatuses)); + assertEquals(ghDeploymentStatus.getId(), Iterables.get(deploymentStatuses, 0).getId()); } @Test