From f52b4a2e113657ab12b0c4d35550451a7dea390d Mon Sep 17 00:00:00 2001 From: Luca Milanesio Date: Mon, 16 Sep 2013 00:40:32 +0100 Subject: [PATCH 1/5] Allows to define page size for repository lists. Extension of the listRepositories() with the desired pageSize. This allows to reduce the number of calls to GitHub API for fetching the entire set of repositories browsing all the pages. Additionally allows to match the UX paging with the underlying GitHub API paging, increasing performance and reducing hourly API allowance. --- src/main/java/org/kohsuke/github/GHPerson.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHPerson.java b/src/main/java/org/kohsuke/github/GHPerson.java index 721eeb225..18a7313e3 100644 --- a/src/main/java/org/kohsuke/github/GHPerson.java +++ b/src/main/java/org/kohsuke/github/GHPerson.java @@ -55,14 +55,25 @@ public abstract class GHPerson { } /** - * Lists up all the repositories. + * Lists up all the repositories using a 30 items page size. * * Unlike {@link #getRepositories()}, this does not wait until all the repositories are returned. */ public PagedIterable listRepositories() { + return listRepositories(30); + } + + /** + * Lists up all the repositories using the specified page size. + * + * @param pageSize size for each page of items returned by GitHub. Maximum page size is 100. + * + * Unlike {@link #getRepositories()}, this does not wait until all the repositories are returned. + */ + public PagedIterable listRepositories(final int pageSize) { return new PagedIterable() { public PagedIterator iterator() { - return new PagedIterator(root.retrieve().asIterator("/users/" + login + "/repos", GHRepository[].class)) { + return new PagedIterator(root.retrieve().asIterator("/users/" + login + "/repos?per_page=" + pageSize, GHRepository[].class)) { @Override protected void wrapUp(GHRepository[] page) { for (GHRepository c : page) From 2fb3f3193074cc9b2dc4095a4578f0a3a2e36c5d Mon Sep 17 00:00:00 2001 From: Luca Milanesio Date: Fri, 27 Sep 2013 09:12:54 +0100 Subject: [PATCH 2/5] GHRepository owner is NOT a GHUser but more generically a GHPerson. When GitHub repositories are associated to organisations, the owner is NOT the user but the org itself. --- src/main/java/org/kohsuke/github/GHPerson.java | 2 +- src/main/java/org/kohsuke/github/GHRepository.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHPerson.java b/src/main/java/org/kohsuke/github/GHPerson.java index 18a7313e3..687093d1d 100644 --- a/src/main/java/org/kohsuke/github/GHPerson.java +++ b/src/main/java/org/kohsuke/github/GHPerson.java @@ -14,7 +14,7 @@ import java.util.TreeMap; * * @author Kohsuke Kawaguchi */ -public abstract class GHPerson { +public class GHPerson { /*package almost final*/ GitHub root; // core data fields that exist even for "small" user data (such as the user info in pull request) diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 5302413c3..0cce0837d 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 { private String description, homepage, name; private String url; // this is the API url private String html_url; // this is the UI - private GHUser owner; // not fully populated. beware. + private GHPerson owner; // not fully populated. beware. private boolean has_issues, has_wiki, fork, _private, has_downloads; private int watchers,forks,open_issues,size; private String created_at, pushed_at; From 096c96550bb4921d7932b3dcad5f20b3de1a626a Mon Sep 17 00:00:00 2001 From: Luca Milanesio Date: Fri, 27 Sep 2013 09:18:22 +0100 Subject: [PATCH 3/5] Allows fetching pull requests by knowing repo-name and owner. --- 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 0cce0837d..31f16785f 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -378,6 +378,17 @@ public class GHRepository { }; } + /** + * Retrieves all the pull requests of a particular state by knowing organisation and repository + */ + public static PagedIterable listPullRequests(final GitHub root, final GHPerson owner, final String repositoryName, final GHIssueState state) { + GHRepository repo = new GHRepository(); + repo.root = root; + repo.name = repositoryName; + repo.owner = owner; + return repo.listPullRequests(state); + } + /** * Retrieves the currently configured hooks. */ From 19ec3321ae12194948dedfa1a98d776b6f0e3134 Mon Sep 17 00:00:00 2001 From: Luca Milanesio Date: Wed, 9 Oct 2013 16:59:18 +0100 Subject: [PATCH 4/5] Gets commit details of a pull request. Retrieves the list of commits included in the pull request. Commit object returned is not a full GHCommit and includes just a small subset of information. --- .../org/kohsuke/github/GHPullRequest.java | 19 +++ .../github/GHPullRequestCommitDetail.java | 139 ++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 src/main/java/org/kohsuke/github/GHPullRequestCommitDetail.java diff --git a/src/main/java/org/kohsuke/github/GHPullRequest.java b/src/main/java/org/kohsuke/github/GHPullRequest.java index 7adaa9342..95674b8fa 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequest.java +++ b/src/main/java/org/kohsuke/github/GHPullRequest.java @@ -27,6 +27,7 @@ import java.io.IOException; import java.net.URL; import java.util.Collection; import java.util.Date; +import java.util.Locale; /** * A pull request. @@ -186,4 +187,22 @@ public class GHPullRequest extends GHIssue { root.retrieve().to(url, this); } + + /** + * Retrieves all the commits associated to this pull request. + */ + public PagedIterable listCommits() { + return new PagedIterable() { + public PagedIterator iterator() { + return new PagedIterator(root.retrieve().asIterator( + String.format("%s/commits", getApiURL().getPath()), + GHPullRequestCommitDetail[].class)) { + @Override + protected void wrapUp(GHPullRequestCommitDetail[] page) { + } + }; + } + }; + } + } diff --git a/src/main/java/org/kohsuke/github/GHPullRequestCommitDetail.java b/src/main/java/org/kohsuke/github/GHPullRequestCommitDetail.java new file mode 100644 index 000000000..8820bc3d3 --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHPullRequestCommitDetail.java @@ -0,0 +1,139 @@ +/* + * The MIT License + * + * Copyright (c) 2013, Luca Milanesio + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.kohsuke.github; + +import java.net.URL; +import java.util.Date; + +/** + * Commit detail inside a {@link GHPullRequest}. + * + * @author Luca Milanesio + */ +public class GHPullRequestCommitDetail { + + public static class Authorship { + String name; + String email; + String date; + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } + + public Date getDate() { + return GitHub.parseDate(date); + } + } + + public static class Tree { + String sha; + String url; + + public String getSha() { + return sha; + } + + public URL getUrl() { + return GitHub.parseURL(url); + } + } + + public static class Commit { + Authorship author; + Authorship committer; + String message; + Tree tree; + String url; + int comment_count; + + public Authorship getAuthor() { + return author; + } + + public Authorship getCommitter() { + return committer; + } + + public String getMessage() { + return message; + } + + public URL getUrl() { + return GitHub.parseURL(url); + } + + public int getComment_count() { + return comment_count; + } + } + + public static class CommitPointer { + String sha; + String url; + String html_url; + + public URL getUrl() { + return GitHub.parseURL(url); + } + + public URL getHtml_url() { + return GitHub.parseURL(html_url); + } + + public String getSha() { + return sha; + } + } + + String sha; + Commit commit; + String url; + String html_url; + String comments_url; + CommitPointer[] parents; + + public String getSha() { + return sha; + } + public Commit getCommit() { + return commit; + } + public URL getApiUrl() { + return GitHub.parseURL(url); + } + public URL getUrl() { + return GitHub.parseURL(html_url); + } + public URL getCommentsUrl() { + return GitHub.parseURL(comments_url); + } + public CommitPointer[] getParents() { + return parents; + } +} From ebf953cbc4426ccf3da3d047cc7d0e2ce26825b4 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Sat, 2 Nov 2013 18:13:40 -0700 Subject: [PATCH 5/5] Massaging the pull request. - GHPerson really should be an abstract class - static listPullRequests method does not fit the design of this library, although I'm very sympathetic as to why Luca wanted to do it. Nonetheless I'm removing this in favor of the lazy loading of the state to avoid unnecessary calls in the future. --- src/main/java/org/kohsuke/github/GHPerson.java | 3 +-- src/main/java/org/kohsuke/github/GHRepository.java | 13 +------------ 2 files changed, 2 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/kohsuke/github/GHPerson.java b/src/main/java/org/kohsuke/github/GHPerson.java index 687093d1d..92a2b90d2 100644 --- a/src/main/java/org/kohsuke/github/GHPerson.java +++ b/src/main/java/org/kohsuke/github/GHPerson.java @@ -14,7 +14,7 @@ import java.util.TreeMap; * * @author Kohsuke Kawaguchi */ -public class GHPerson { +public abstract class GHPerson { /*package almost final*/ GitHub root; // core data fields that exist even for "small" user data (such as the user info in pull request) @@ -235,5 +235,4 @@ public class GHPerson { populate(); return followers; } - } diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 31f16785f..5302413c3 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 { private String description, homepage, name; private String url; // this is the API url private String html_url; // this is the UI - private GHPerson owner; // not fully populated. beware. + private GHUser owner; // not fully populated. beware. private boolean has_issues, has_wiki, fork, _private, has_downloads; private int watchers,forks,open_issues,size; private String created_at, pushed_at; @@ -378,17 +378,6 @@ public class GHRepository { }; } - /** - * Retrieves all the pull requests of a particular state by knowing organisation and repository - */ - public static PagedIterable listPullRequests(final GitHub root, final GHPerson owner, final String repositoryName, final GHIssueState state) { - GHRepository repo = new GHRepository(); - repo.root = root; - repo.name = repositoryName; - repo.owner = owner; - return repo.listPullRequests(state); - } - /** * Retrieves the currently configured hooks. */