completed the commit status API

This commit is contained in:
Kohsuke Kawaguchi
2012-09-05 18:00:50 -07:00
parent 892d2acaa2
commit 3f1bb1a214
5 changed files with 59 additions and 26 deletions

View File

@@ -235,10 +235,17 @@ public class GHCommit {
}
/**
* Gets the status of this commit.
* Gets the status of this commit, newer ones first.
*/
public GHCommitStatus getStatus() throws IOException {
return owner.getCommitStatus(sha);
public PagedIterable<GHCommitStatus> listStatuses() throws IOException {
return owner.listCommitStatuses(sha);
}
/**
* Gets the last status of this commit, which is what gets shown in the UI.
*/
public GHCommitStatus getLastStatus() throws IOException {
return owner.getLastCommitStatus(sha);
}
GHCommit wrapUp(GHRepository owner) {

View File

@@ -42,6 +42,11 @@ public class GHCommitStatus {
throw new IllegalStateException("Unexpected state: "+state);
}
/**
* The URL that this status is linked to.
*
* This is the URL specified when creating a commit status.
*/
public String getTargetUrl() {
return target_url;
}
@@ -64,22 +69,4 @@ public class GHCommitStatus {
public GHUser getCreator() {
return creator;
}
/**
* Updates the description.
*
* TODO: verify if this actually works, and create setTargetUrl, too.
*/
public void setDescription(String description) throws IOException {
new Poster(root)
.with("description",description)
.withCredential()
.to(url,null,"PATCH");
this.description = description;
}
// TODO: verify if it works
public void delete() throws IOException {
new Poster(root).withCredential().to(url,null,"DELETE");
}
}

View File

@@ -442,8 +442,29 @@ public class GHRepository {
};
}
public GHCommitStatus getCommitStatus(String sha1) throws IOException {
return root.retrieve(String.format("/repos/%s/%s/statuses/%s", owner.login, name, sha1), GHCommitStatus.class).wrapUp(root);
/**
* Lists all the commit statues attached to the given commit, newer ones first.
*/
public PagedIterable<GHCommitStatus> listCommitStatuses(final String sha1) throws IOException {
return new PagedIterable<GHCommitStatus>() {
public PagedIterator<GHCommitStatus> iterator() {
return new PagedIterator<GHCommitStatus>(root.retrievePaged(String.format("/repos/%s/%s/statuses/%s", owner.login, name, sha1),GHCommitStatus[].class,false)) {
@Override
protected void wrapUp(GHCommitStatus[] page) {
for (GHCommitStatus c : page)
c.wrapUp(root);
}
};
}
};
}
/**
* Gets the last status of this commit, which is what gets shown in the UI.
*/
public GHCommitStatus getLastCommitStatus(String sha1) throws IOException {
List<GHCommitStatus> v = listCommitStatuses(sha1).asList();
return v.isEmpty() ? null : v.get(0);
}
/**

View File

@@ -163,8 +163,11 @@ public class GitHub {
// append the access token
tailApiUrl = tailApiUrl + (tailApiUrl.indexOf('?')>=0 ?'&':'?') + "access_token=" + oauthAccessToken;
}
return new URL("https://api."+githubServer+tailApiUrl);
if (tailApiUrl.startsWith("/"))
return new URL("https://api."+githubServer+tailApiUrl);
else
return new URL(tailApiUrl);
}
/*package*/ <T> T retrieve(String tailApiUrl, Class<T> type) throws IOException {

View File

@@ -4,6 +4,8 @@ import junit.framework.TestCase;
import org.kohsuke.github.GHCommit;
import org.kohsuke.github.GHCommit.File;
import org.kohsuke.github.GHCommitComment;
import org.kohsuke.github.GHCommitState;
import org.kohsuke.github.GHCommitStatus;
import org.kohsuke.github.GHEvent;
import org.kohsuke.github.GHEventInfo;
import org.kohsuke.github.GHEventPayload;
@@ -45,7 +47,7 @@ public class AppTest extends TestCase {
public void testCredentialValid() throws IOException {
assertTrue(GitHub.connect().isCredentialValid());
assertFalse(GitHub.connect("totally","bogus").isCredentialValid());
assertFalse(GitHub.connect("totally", "bogus").isCredentialValid());
}
public void testRateLimit() throws IOException {
@@ -308,4 +310,17 @@ public class AppTest extends TestCase {
// t.add(labs.getRepository("xyz"));
}
public void testCommitStatus() throws Exception {
GitHub gitHub = GitHub.connect();
GHRepository r = gitHub.getUser("kohsuke").getRepository("test");
GHCommitStatus state;
// state = r.createCommitStatus("edacdd76b06c5f3f0697a22ca75803169f25f296", GHCommitState.FAILURE, "http://jenkins-ci.org/", "oops!");
List<GHCommitStatus> lst = r.listCommitStatuses("edacdd76b06c5f3f0697a22ca75803169f25f296").asList();
state = lst.get(0);
System.out.println(state);
assertEquals("oops!",state.getDescription());
assertEquals("http://jenkins-ci.org/",state.getTargetUrl());
}
}