diff --git a/pom.xml b/pom.xml index d976ca838b..1947c37495 100644 --- a/pom.xml +++ b/pom.xml @@ -3,11 +3,11 @@ org.kohsuke pom - 9 + 10 github-api - 1.57 + 1.58 GitHub API for Java http://github-api.kohsuke.org/ GitHub API for Java @@ -16,7 +16,7 @@ scm:git:git@github.com/kohsuke/${project.artifactId}.git scm:git:ssh://git@github.com/kohsuke/${project.artifactId}.git http://${project.artifactId}.kohsuke.org/ - HEAD + github-api-1.58 diff --git a/src/main/java/org/kohsuke/github/GHEvent.java b/src/main/java/org/kohsuke/github/GHEvent.java index aeb0afe7e3..13ef432f8f 100644 --- a/src/main/java/org/kohsuke/github/GHEvent.java +++ b/src/main/java/org/kohsuke/github/GHEvent.java @@ -12,6 +12,8 @@ public enum GHEvent { COMMIT_COMMENT, CREATE, DELETE, + DEPLOYMENT, + DEPLOYMENT_STATUS, DOWNLOAD, FOLLOW, FORK, @@ -25,6 +27,8 @@ public enum GHEvent { PULL_REQUEST, PULL_REQUEST_REVIEW_COMMENT, PUSH, + RELEASE, + STATUS, TEAM_ADD, WATCH } diff --git a/src/main/java/org/kohsuke/github/GHPullRequest.java b/src/main/java/org/kohsuke/github/GHPullRequest.java index 75c54611fb..1dbc42555a 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequest.java +++ b/src/main/java/org/kohsuke/github/GHPullRequest.java @@ -196,7 +196,7 @@ public PagedIterable listCommits() { return new PagedIterable() { public PagedIterator iterator() { return new PagedIterator(root.retrieve().asIterator( - String.format("%s/commits", getApiURL().getPath()), + String.format("%s/commits", getApiURL()), GHPullRequestCommitDetail[].class)) { @Override protected void wrapUp(GHPullRequestCommitDetail[] page) { diff --git a/src/main/java/org/kohsuke/github/GHRef.java b/src/main/java/org/kohsuke/github/GHRef.java index 9d19c7e57c..f052cdf903 100644 --- a/src/main/java/org/kohsuke/github/GHRef.java +++ b/src/main/java/org/kohsuke/github/GHRef.java @@ -1,5 +1,6 @@ package org.kohsuke.github; +import java.io.IOException; import java.net.URL; /** @@ -8,6 +9,7 @@ * @author Michael Clarke */ public class GHRef { + /*package almost final*/ GitHub root; private String ref, url; private GHObject object; @@ -33,6 +35,48 @@ public GHObject getObject() { return object; } + /** + * Updates this ref to the specified commit. + * + * @param sha + * The SHA1 value to set this reference to + */ + public void updateTo(String sha) throws IOException { + updateTo(sha, false); + } + + /** + * Updates this ref to the specified commit. + * + * @param sha + * The SHA1 value to set this reference to + * @param force + * Whether or not to force this ref update. + */ + public void updateTo(String sha, Boolean force) throws IOException { + new Requester(root) + .with("sha", sha).with("force", force).method("PATCH").to(url, GHRef.class).wrap(root); + } + + /** + * Deletes this ref from the repository using the GitHub API. + */ + public void delete() throws IOException { + new Requester(root).method("DELETE").to(url); + } + + /*package*/ GHRef wrap(GitHub root) { + this.root = root; + return this; + } + + /*package*/ static GHRef[] wrap(GHRef[] in, GitHub root) { + for (GHRef r : in) { + r.wrap(root); + } + return in; + } + public static class GHObject { private String type, sha, url; diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index b6985f99fa..57b4a2043c 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -57,7 +57,7 @@ public class GHRepository { /*package almost final*/ GitHub root; - private String description, homepage, name; + private String description, homepage, name, full_name; private String url; // this is the API url private String html_url; // this is the UI private String git_url, ssh_url, clone_url, svn_url; @@ -131,6 +131,13 @@ public String getName() { return name; } + /** + * Full repository name including the owner or organization. For example 'jenkinsci/jenkins' in case of http://github.com/jenkinsci/jenkins + */ + public String getFullName() { + return full_name; + } + public boolean hasPullAccess() { return permissions!=null && permissions.pull; } @@ -206,7 +213,7 @@ public GHReleaseBuilder createRelease(String tag) { */ public GHRef createRef(String name, String sha) throws IOException { return new Requester(root) - .with("ref", name).with("sha", sha).method("POST").to(getApiTailUrl("git/refs"), GHRef.class); + .with("ref", name).with("sha", sha).method("POST").to(getApiTailUrl("git/refs"), GHRef.class).wrap(root); } /** @@ -579,17 +586,17 @@ public GHCompare getCompare(GHBranch id1, GHBranch id2) throws IOException { * @throws IOException on failure communicating with GitHub */ public GHRef[] getRefs() throws IOException { - return root.retrieve().to(String.format("/repos/%s/%s/git/refs", owner.login, name), GHRef[].class); + return GHRef.wrap(root.retrieve().to(String.format("/repos/%s/%s/git/refs", owner.login, name), GHRef[].class),root); } /** - * Retrienved all refs of the given type for the current GitHub repository. + * Retrieves all refs of the given type for the current GitHub repository. * @param refType the type of reg to search for e.g. tags or commits * @return an array of all refs matching the request type * @throws IOException on failure communicating with GitHub, potentially due to an invalid ref type being requested */ public GHRef[] getRefs(String refType) throws IOException { - return root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", owner.login, name, refType), GHRef[].class); + return GHRef.wrap(root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", owner.login, name, refType), GHRef[].class),root); } /** * Retrive a ref of the given type for the current GitHub repository. @@ -602,7 +609,7 @@ public GHRef[] getRefs(String refType) throws IOException { * invalid ref type being requested */ public GHRef getRef(String refName) throws IOException { - return root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", owner.login, name, refName), GHRef.class); + return root.retrieve().to(String.format("/repos/%s/%s/git/refs/%s", owner.login, name, refName), GHRef.class).wrap(root); } /** * Gets a commit object in this repository. diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 136daf0df7..5404b1f309 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -283,6 +283,15 @@ public GHUser getUser(String login) throws IOException { return u; } + + /** + * clears all cached data in order for external changes (modifications and del + */ + public void refreshCache() { + users.clear(); + orgs.clear(); + } + /** * Interns the given {@link GHUser}. */ @@ -357,7 +366,6 @@ public Map> getMyTeams() throws IOException { * Public events visible to you. Equivalent of what's displayed on https://github.com/ */ public List getEvents() throws IOException { - // TODO: pagination GHEventInfo[] events = retrieve().to("/events", GHEventInfo[].class); for (GHEventInfo e : events) e.wrapUp(this); diff --git a/src/main/java/org/kohsuke/github/Requester.java b/src/main/java/org/kohsuke/github/Requester.java index 10cb2bfb74..c15485d305 100644 --- a/src/main/java/org/kohsuke/github/Requester.java +++ b/src/main/java/org/kohsuke/github/Requester.java @@ -32,6 +32,7 @@ import java.io.InterruptedIOException; import java.io.Reader; import java.io.UnsupportedEncodingException; +import java.lang.reflect.Array; import java.lang.reflect.Field; import java.net.HttpURLConnection; import java.net.MalformedURLException; @@ -47,10 +48,14 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.zip.GZIPInputStream; import org.apache.commons.io.IOUtils; +import javax.net.ssl.HttpsURLConnection; + /** * A builder pattern for making HTTP call and parsing its output. * @@ -177,7 +182,26 @@ private T _to(String tailApiUrl, Class type, T instance) throws IOExcepti buildRequest(uc); try { - return parse(uc,type,instance); + T result = parse(uc, type, instance); + if (type != null && type.isArray()) { // we might have to loop for pagination - done through recursion + final String links = uc.getHeaderField("link"); + if (links != null && links.contains("rel=\"next\"")) { + Pattern nextLinkPattern = Pattern.compile(".*<(.*)>; rel=\"next\""); + Matcher nextLinkMatcher = nextLinkPattern.matcher(links); + if (nextLinkMatcher.find()) { + final String link = nextLinkMatcher.group(1); + T nextResult = _to(link, type, instance); + + final int resultLength = Array.getLength(result); + final int nextResultLength = Array.getLength(nextResult); + T concatResult = (T) Array.newInstance(type.getComponentType(), resultLength + nextResultLength); + System.arraycopy(result, 0, concatResult, 0, resultLength); + System.arraycopy(nextResult, 0, concatResult, resultLength, nextResultLength); + result = concatResult; + } + } + } + return result; } catch (IOException e) { handleApiError(e,uc); }