Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complete api implementation for the new github deployment api #143

Merged
merged 4 commits into from
Feb 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion src/main/java/org/kohsuke/github/GHDeployment.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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;
}
}
21 changes: 20 additions & 1 deletion src/main/java/org/kohsuke/github/GHDeploymentBuilder.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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<String> 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;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/kohsuke/github/GHDeploymentState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.kohsuke.github;

/**
* Represents the state of deployment
*/
public enum GHDeploymentState {
PENDING, SUCCESS, ERROR, FAILURE
}
36 changes: 36 additions & 0 deletions src/main/java/org/kohsuke/github/GHDeploymentStatus.java
Original file line number Diff line number Diff line change
@@ -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());
}


}
30 changes: 30 additions & 0 deletions src/main/java/org/kohsuke/github/GHDeploymentStatusBuilder.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
54 changes: 52 additions & 2 deletions src/main/java/org/kohsuke/github/GHRepository.java
100644 → 100755
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 @@ -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<GHDeploymentStatus> getDeploymentStatuses(final int id) {
return new PagedIterable<GHDeploymentStatus>() {
public PagedIterator<GHDeploymentStatus> iterator() {
return new PagedIterator<GHDeploymentStatus>(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<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);
}

private static class GHRepoPermission {
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/kohsuke/github/Identifiable.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
43 changes: 38 additions & 5 deletions src/test/java/org/kohsuke/github/AppTest.java
100644 → 100755
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 @@ -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<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();
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<GHDeploymentStatus> deploymentStatuses = repository.getDeploymentStatuses(deployment.getId());
assertNotNull(deploymentStatuses);
assertEquals(1,Iterables.size(deploymentStatuses));
assertEquals(ghDeploymentStatus.getId(), Iterables.get(deploymentStatuses, 0).getId());
}

@Test
Expand Down