From b9764c0a44ce40876d382d50682ed22ec26f8d88 Mon Sep 17 00:00:00 2001 From: t865095 Date: Tue, 8 Jul 2014 13:52:53 +0200 Subject: [PATCH 01/10] introduce pagination for all paged api calls and introduce cache invalidation --- src/main/java/org/kohsuke/github/GitHub.java | 10 ++++++- .../java/org/kohsuke/github/Requester.java | 26 ++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 2488c982a2..dce87ef487 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -280,6 +280,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}. */ @@ -332,7 +341,6 @@ public Map getMyOrganizations() 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); } From 0efb206881a41c57a52c6690228d76b5098ee2e4 Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Wed, 6 Aug 2014 04:27:35 +0200 Subject: [PATCH 02/10] get repository full name (including owner) --- src/main/java/org/kohsuke/github/GHRepository.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index b6985f99fa..0e772ac785 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; } From 311180d12ec1c7dac659ae1bdf43ee37ebfcf6a4 Mon Sep 17 00:00:00 2001 From: Bernd Ahlers Date: Mon, 18 Aug 2014 16:54:17 +0200 Subject: [PATCH 03/10] Add missing DEPLOYMENT, DEPLOYMENT_STATUS, RELEASE and STATUS events. --- src/main/java/org/kohsuke/github/GHEvent.java | 4 ++++ 1 file changed, 4 insertions(+) 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 } From 1f298a88b062d80e848cee29ecb4ace674ff7932 Mon Sep 17 00:00:00 2001 From: Surya Gaddipati Date: Tue, 19 Aug 2014 13:29:40 -0500 Subject: [PATCH 04/10] [maven-release-plugin] prepare for next development iteration --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index d976ca838b..8c5837db7d 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ github-api - 1.57 + 1.58-SNAPSHOT GitHub API for Java http://github-api.kohsuke.org/ GitHub API for Java From 94831fc10aa9612ba04ee47b5adc706bdd79bed5 Mon Sep 17 00:00:00 2001 From: David Tanner Date: Tue, 19 Aug 2014 13:22:25 -0600 Subject: [PATCH 05/10] Remove getPath() When getting the path and providing an enterprise url for the apiUrl, the /api/v3 portion gets duplicated. Since they will be combined on line 231 of GitHub.java there is no point just grabbing the path. See https://github.com/janinko/ghprb/issues/178, and https://issues.jenkins-ci.org/browse/JENKINS-24145?focusedCommentId=208270#comment-208270 --- src/main/java/org/kohsuke/github/GHPullRequest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) { From 077d69395941e8eefcbdf62424674e69d28aad78 Mon Sep 17 00:00:00 2001 From: Matt Farmer Date: Fri, 22 Aug 2014 23:27:40 -0400 Subject: [PATCH 06/10] Add support for updating a ref using the API. --- .../java/org/kohsuke/github/GHRepository.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index b6985f99fa..fe0405de12 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -209,6 +209,35 @@ public GHRef createRef(String name, String sha) throws IOException { .with("ref", name).with("sha", sha).method("POST").to(getApiTailUrl("git/refs"), GHRef.class); } + /** + * Updates a named ref, such as a tag, branch, etc. + * + * @param name + * The name of the fully qualified reference (ie: refs/heads/master). + * If it doesn't start with 'refs' and have at least two slashes, it will be rejected. + * @param sha + * The SHA1 value to set this reference to + */ + public GHRef updateRef(String name, String sha) throws IOException { + return updateRef(name, sha, false); + } + + /** + * Updates a named ref, such as a tag, branch, etc. + * + * @param name + * The name of the fully qualified reference (ie: refs/heads/master). + * If it doesn't start with 'refs' and have at least two slashes, it will be rejected. + * @param sha + * The SHA1 value to set this reference to + * @param force + * Whether or not to force this ref update. + */ + public GHRef updateRef(String name, String sha, Boolean force) throws IOException { + return new Requester(root) + .with("sha", sha).with("force", force).method("PATCH").to(getApiTailUrl("git/" + name), GHRef.class); + } + /** * @deprecated * use {@link #listReleases()} From 97a1c741de93fa2c4d333672f017c14674103ff0 Mon Sep 17 00:00:00 2001 From: Matt Farmer Date: Fri, 22 Aug 2014 23:33:04 -0400 Subject: [PATCH 07/10] Implement support for deleting a ref using the GitHub API. --- src/main/java/org/kohsuke/github/GHRepository.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index fe0405de12..3d21ff9f6e 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -238,6 +238,17 @@ public GHRef updateRef(String name, String sha, Boolean force) throws IOExceptio .with("sha", sha).with("force", force).method("PATCH").to(getApiTailUrl("git/" + name), GHRef.class); } + /** + * Deletes a particular ref from the repository using the GitHub API. + * + * @param name + * The name of the fully qualified reference (ie: refs/heads/master). + * If it doesn't start with 'refs' and have at least two slashes, it will be rejected. + */ + public void deleteRef(String name) throws IOException { + new Requester(root).method("DELETE").to(getApiTailUrl("git/" + name)); + } + /** * @deprecated * use {@link #listReleases()} From 31bebd4a7a398a8812279473843b3e683f83b4e6 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sat, 30 Aug 2014 14:03:34 -0700 Subject: [PATCH 08/10] Design pattern in this library demands that these methods be on GHRef --- src/main/java/org/kohsuke/github/GHRef.java | 44 ++++++++++++++++ .../java/org/kohsuke/github/GHRepository.java | 50 ++----------------- 2 files changed, 49 insertions(+), 45 deletions(-) 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 3d21ff9f6e..ec4fae5e26 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -206,47 +206,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); - } - - /** - * Updates a named ref, such as a tag, branch, etc. - * - * @param name - * The name of the fully qualified reference (ie: refs/heads/master). - * If it doesn't start with 'refs' and have at least two slashes, it will be rejected. - * @param sha - * The SHA1 value to set this reference to - */ - public GHRef updateRef(String name, String sha) throws IOException { - return updateRef(name, sha, false); - } - - /** - * Updates a named ref, such as a tag, branch, etc. - * - * @param name - * The name of the fully qualified reference (ie: refs/heads/master). - * If it doesn't start with 'refs' and have at least two slashes, it will be rejected. - * @param sha - * The SHA1 value to set this reference to - * @param force - * Whether or not to force this ref update. - */ - public GHRef updateRef(String name, String sha, Boolean force) throws IOException { - return new Requester(root) - .with("sha", sha).with("force", force).method("PATCH").to(getApiTailUrl("git/" + name), GHRef.class); - } - - /** - * Deletes a particular ref from the repository using the GitHub API. - * - * @param name - * The name of the fully qualified reference (ie: refs/heads/master). - * If it doesn't start with 'refs' and have at least two slashes, it will be rejected. - */ - public void deleteRef(String name) throws IOException { - new Requester(root).method("DELETE").to(getApiTailUrl("git/" + name)); + .with("ref", name).with("sha", sha).method("POST").to(getApiTailUrl("git/refs"), GHRef.class).wrap(root); } /** @@ -619,17 +579,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. @@ -642,7 +602,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. From d516597659ab59f4dba2a39dbd8c2ed2a3adf11a Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sat, 30 Aug 2014 14:13:45 -0700 Subject: [PATCH 09/10] See if this will get me release with Maven 3.1 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 8c5837db7d..2edeb6c706 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.kohsuke pom - 9 + 10 github-api From db845850b2c160e5f54413a4503c51e554f2d5b3 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sat, 30 Aug 2014 14:15:25 -0700 Subject: [PATCH 10/10] [maven-release-plugin] prepare release github-api-1.58 --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 2edeb6c706..1947c37495 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ github-api - 1.58-SNAPSHOT + 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