From bc518a9ae8281edd559cb2b0a0d7036d9b22ab45 Mon Sep 17 00:00:00 2001 From: Surya Gaddipati Date: Sat, 20 Dec 2014 11:52:49 -0600 Subject: [PATCH 1/4] Complete implementation of create deployment api --- .../java/org/kohsuke/github/GHDeployment.java | 44 +++++++++++++++++++ .../kohsuke/github/GHDeploymentBuilder.java | 21 ++++++++- .../java/org/kohsuke/github/GHRepository.java | 4 +- src/test/java/org/kohsuke/github/AppTest.java | 8 ++-- 4 files changed, 69 insertions(+), 8 deletions(-) mode change 100644 => 100755 src/main/java/org/kohsuke/github/GHDeployment.java mode change 100644 => 100755 src/main/java/org/kohsuke/github/GHDeploymentBuilder.java mode change 100644 => 100755 src/main/java/org/kohsuke/github/GHRepository.java mode change 100644 => 100755 src/test/java/org/kohsuke/github/AppTest.java 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..0a56ccb361 --- a/src/main/java/org/kohsuke/github/GHDeployment.java +++ b/src/main/java/org/kohsuke/github/GHDeployment.java @@ -1,12 +1,56 @@ package org.kohsuke.github; +import java.net.URL; +import java.util.Date; + public class GHDeployment { private GHRepository owner; private GitHub root; + protected String url; + protected String sha; + protected int id; + protected String task; + protected String payload; + protected String environment; + protected String description; + protected String statuses_url; + protected String repository_url; + protected String created_at; + protected String updated_at; + protected GHUser creator; + GHDeployment wrap(GHRepository owner) { this.owner = owner; this.root = owner.root; + if(creator != null) creator.wrapUp(root); return this; } + public Date getCreatedAt() { + return GitHub.parseDate(created_at); + } + + public URL getUrl() { + return GitHub.parseURL(url); + } + + public URL getStatusesUrl() { + return GitHub.parseURL(statuses_url); + } + + public URL getRepositoryUrl() { + return GitHub.parseURL(repository_url); + } + + public GHUser getCreator() { + return creator; + } + + public Date getUpdatedAt() { + return GitHub.parseDate(updated_at); + } + + public int getId() { + return id; + } } 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/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java old mode 100644 new mode 100755 index 204a8004c0..8bed568299 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -61,8 +61,8 @@ public class GHRepository { private GHRepoPermission permissions; - public GHDeploymentBuilder createDeployment() { - return new GHDeploymentBuilder(this); + public GHDeploymentBuilder createDeployment(String ref) { + return new GHDeploymentBuilder(this,ref); } private static class GHRepoPermission { 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..a3b96e8064 --- a/src/test/java/org/kohsuke/github/AppTest.java +++ b/src/test/java/org/kohsuke/github/AppTest.java @@ -78,15 +78,13 @@ 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 From 2f318152d82a5cabb810acc46aa2ebc164a0525f Mon Sep 17 00:00:00 2001 From: Surya Gaddipati Date: Sat, 20 Dec 2014 17:09:59 -0600 Subject: [PATCH 2/4] Complete api implementation for setting/retriving deployment status on a deployment --- .../java/org/kohsuke/github/GHDeployment.java | 21 ++--------- .../org/kohsuke/github/GHDeploymentState.java | 8 +++++ .../kohsuke/github/GHDeploymentStatus.java | 36 +++++++++++++++++++ .../github/GHDeploymentStatusBuilder.java | 30 ++++++++++++++++ .../java/org/kohsuke/github/GHRepository.java | 18 ++++++++++ .../java/org/kohsuke/github/Identifiable.java | 27 ++++++++++++++ src/test/java/org/kohsuke/github/AppTest.java | 16 +++++++++ 7 files changed, 137 insertions(+), 19 deletions(-) create mode 100755 src/main/java/org/kohsuke/github/GHDeploymentState.java create mode 100755 src/main/java/org/kohsuke/github/GHDeploymentStatus.java create mode 100755 src/main/java/org/kohsuke/github/GHDeploymentStatusBuilder.java create mode 100755 src/main/java/org/kohsuke/github/Identifiable.java diff --git a/src/main/java/org/kohsuke/github/GHDeployment.java b/src/main/java/org/kohsuke/github/GHDeployment.java index 0a56ccb361..7471a03e17 100755 --- a/src/main/java/org/kohsuke/github/GHDeployment.java +++ b/src/main/java/org/kohsuke/github/GHDeployment.java @@ -1,22 +1,19 @@ package org.kohsuke.github; + import java.net.URL; import java.util.Date; -public class GHDeployment { +public class GHDeployment extends Identifiable { private GHRepository owner; private GitHub root; - protected String url; protected String sha; - protected int id; protected String task; protected String payload; protected String environment; protected String description; protected String statuses_url; protected String repository_url; - protected String created_at; - protected String updated_at; protected GHUser creator; @@ -26,13 +23,6 @@ GHDeployment wrap(GHRepository owner) { if(creator != null) creator.wrapUp(root); return this; } - public Date getCreatedAt() { - return GitHub.parseDate(created_at); - } - - public URL getUrl() { - return GitHub.parseURL(url); - } public URL getStatusesUrl() { return GitHub.parseURL(statuses_url); @@ -46,11 +36,4 @@ public GHUser getCreator() { return creator; } - public Date getUpdatedAt() { - return GitHub.parseDate(updated_at); - } - - public int getId() { - return id; - } } 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..d50d0fe6a1 --- /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 GHCommitState getState() { + return GHCommitState.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 index 8bed568299..91bb796d07 100755 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -65,6 +65,24 @@ 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 GHDeploymentStatusBuilder createDeployStatus(int deploymentId, GHDeploymentState ghDeploymentState) { + return new GHDeploymentStatusBuilder(this,deploymentId,ghDeploymentState); + } + private static class GHRepoPermission { boolean pull,push,admin; } 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 index a3b96e8064..399415b3d7 100755 --- a/src/test/java/org/kohsuke/github/AppTest.java +++ b/src/test/java/org/kohsuke/github/AppTest.java @@ -87,6 +87,22 @@ public void testCreateDeployment() throws IOException { assertNotNull(deployment.getId()); } + @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 public void testGetIssues() throws Exception { List closedIssues = gitHub.getUser("kohsuke").getRepository("github-api").getIssues(GHIssueState.CLOSED); From 5d838940560d5df05a648f3a23eb58875bef9d97 Mon Sep 17 00:00:00 2001 From: Surya Gaddipati Date: Fri, 2 Jan 2015 14:47:02 -0600 Subject: [PATCH 3/4] Add method for listing deployments --- .../java/org/kohsuke/github/GHDeployment.java | 20 ++++++++++-- .../java/org/kohsuke/github/GHRepository.java | 32 +++++++++++++++++++ src/test/java/org/kohsuke/github/AppTest.java | 21 +++++++++++- 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHDeployment.java b/src/main/java/org/kohsuke/github/GHDeployment.java index 7471a03e17..753364409d 100755 --- a/src/main/java/org/kohsuke/github/GHDeployment.java +++ b/src/main/java/org/kohsuke/github/GHDeployment.java @@ -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; @@ -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; + } } diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 91bb796d07..82c75394ec 100755 --- 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; @@ -79,6 +80,37 @@ protected void wrapUp(GHDeploymentStatus[] page) { }; } + 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); } diff --git a/src/test/java/org/kohsuke/github/AppTest.java b/src/test/java/org/kohsuke/github/AppTest.java index 399415b3d7..361f32445a 100755 --- 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; @@ -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 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(); @@ -100,7 +119,7 @@ public void testGetDeploymentStatuses() throws IOException { Iterable 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 From 58143c26bc592c31f39e50f6daf9a974fa954694 Mon Sep 17 00:00:00 2001 From: Surya Gaddipati Date: Mon, 12 Jan 2015 10:04:01 -0600 Subject: [PATCH 4/4] Use GHCommitState for deployment status state --- src/main/java/org/kohsuke/github/GHDeploymentStatus.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHDeploymentStatus.java b/src/main/java/org/kohsuke/github/GHDeploymentStatus.java index d50d0fe6a1..055b3cb700 100755 --- a/src/main/java/org/kohsuke/github/GHDeploymentStatus.java +++ b/src/main/java/org/kohsuke/github/GHDeploymentStatus.java @@ -28,8 +28,8 @@ public URL getDeploymentUrl() { public URL getRepositoryUrl() { return GitHub.parseURL(repository_url); } - public GHCommitState getState() { - return GHCommitState.valueOf(state.toUpperCase()); + public GHDeploymentState getState() { + return GHDeploymentState.valueOf(state.toUpperCase()); }