From 559cc37055f754980d278f7e7ccba542b83c3e3e Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Thu, 16 Jun 2011 21:31:19 -0700 Subject: [PATCH] added pull request support --- .../org/kohsuke/github/GHCommitPointer.java | 71 ++++++++++ .../org/kohsuke/github/GHPullRequest.java | 129 ++++++++++++++++++ .../java/org/kohsuke/github/GHRepository.java | 24 +++- .../org/kohsuke/github/JsonPullRequest.java | 36 +++++ .../org/kohsuke/github/JsonPullRequests.java | 39 ++++++ 5 files changed, 298 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/kohsuke/github/GHCommitPointer.java create mode 100644 src/main/java/org/kohsuke/github/GHPullRequest.java create mode 100644 src/main/java/org/kohsuke/github/JsonPullRequest.java create mode 100644 src/main/java/org/kohsuke/github/JsonPullRequests.java diff --git a/src/main/java/org/kohsuke/github/GHCommitPointer.java b/src/main/java/org/kohsuke/github/GHCommitPointer.java new file mode 100644 index 000000000..3e0a5d4ad --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHCommitPointer.java @@ -0,0 +1,71 @@ +/* + * The MIT License + * + * Copyright (c) 2010, Kohsuke Kawaguchi + * + * 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; + +/** + * Identifies a commit in {@link GHPullRequest}. + * + * @author Kohsuke Kawaguchi + */ +public class GHCommitPointer { + private String ref, sha, label; + private GHUser user; + private GHRepository repository; + + /** + * This points to the user who owns + * the {@link #repository}. + */ + public GHUser getUser() { + return user; + } + + /** + * The repository that contains the commit. + */ + public GHRepository getRepository() { + return repository; + } + + /** + * Named ref to the commit. + */ + public String getRef() { + return ref; + } + + /** + * SHA1 of the commit. + */ + public String getSha() { + return sha; + } + + /** + * String that looks like "USERNAME:REF". + */ + public String getLabel() { + return label; + } +} diff --git a/src/main/java/org/kohsuke/github/GHPullRequest.java b/src/main/java/org/kohsuke/github/GHPullRequest.java new file mode 100644 index 000000000..6e6119b6e --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHPullRequest.java @@ -0,0 +1,129 @@ +/* + * The MIT License + * + * Copyright (c) 2010, Kohsuke Kawaguchi + * + * 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; +import java.util.Locale; + +/** + * A pull request. + * + * @author Kohsuke Kawaguchi + */ +@SuppressWarnings({"UnusedDeclaration"}) +public class GHPullRequest { + /*package almost final*/ GitHub root; + + private String gravatar_id, closed_at, state, body, created_at, patch_url, issue_updated_at; + private int number, position, comments, votes; + private GHUser issue_user, user; + // labels?? + private GHCommitPointer base, head; + private String mergeable, updated_at, html_url, title, diff_url; + + public enum State { + OPEN, CLOSED + } + + /** + * The description of this pull request. + */ + public String getBody() { + return body; + } + + /** + * The URL of the patch file. + * like https://github.com/jenkinsci/jenkins/pull/100.patch + */ + public URL getPatchUrl() { + return GitHub.parseURL(patch_url); + } + + /** + * ID. + */ + public int getNumber() { + return number; + } + + /** + * User who submitted a pull request. + */ + public GHUser getUser() { + return user; + } + + /** + * This points to where the change should be pulled into, + * but I'm not really sure what exactly it means. + */ + public GHCommitPointer getBase() { + return base; + } + + /** + * The change that should be pulled. + */ + public GHCommitPointer getHead() { + return head; + } + + /** + * The HTML page of this pull request, + * like https://github.com/jenkinsci/jenkins/pull/100 + */ + public URL getUrl() { + return GitHub.parseURL(html_url); + } + + public String getTitle() { + return title; + } + + /** + * The diff file, + * like https://github.com/jenkinsci/jenkins/pull/100.diff + */ + public URL getDiffUrl() { + return GitHub.parseURL(diff_url); + } + + public Date getClosedAt() { + return GitHub.parseDate(closed_at); + } + + public Date getCreatedAt() { + return GitHub.parseDate(created_at); + } + + public Date getUpdatedAt() { + return GitHub.parseDate(updated_at); + } + + public State getState() { + return State.valueOf(state.toUpperCase(Locale.ENGLISH)); + } +} diff --git a/src/main/java/org/kohsuke/github/GHRepository.java b/src/main/java/org/kohsuke/github/GHRepository.java index 317228552..570dfa259 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -43,6 +43,7 @@ import java.util.Date; import java.util.HashSet; import java.util.Iterator; import java.util.List; +import java.util.Locale; import java.util.Set; import static java.util.Arrays.*; @@ -52,6 +53,7 @@ import static java.util.Arrays.*; * * @author Kohsuke Kawaguchi */ +@SuppressWarnings({"UnusedDeclaration"}) public class GHRepository { /*package almost final*/ GitHub root; @@ -167,7 +169,7 @@ public class GHRepository { active.setChecked(true); final HtmlForm f = email.getEnclosingFormOrDie(); - f.submit((HtmlButton)f.getElementsByTagName("button").get(0)); + f.submit((HtmlButton) f.getElementsByTagName("button").get(0)); } /** @@ -249,6 +251,26 @@ public class GHRepository { throw new IllegalArgumentException("Either you don't have the privilege to rename "+owner+'/'+name+" or there's a bug in HTML scraping"); } + /** + * Retrieves a specified pull request. + */ + public GHPullRequest getPullRequest(int i) throws IOException { + return root.retrieveWithAuth("/pulls/" + owner + '/' + name + "/" + i, JsonPullRequest.class).wrap(root); + } + + /** + * Retrieves all the pull requests of a particular state. + */ + public List getPullRequests(GHPullRequest.State state) throws IOException { + return root.retrieveWithAuth("/pulls/"+owner+'/'+name+"/"+state.name().toLowerCase(Locale.ENGLISH),JsonPullRequests.class).wrap(root); + } + + /** + * Retrieves all the pull requests. + */ + public List getPullRequests() throws IOException { + return root.retrieveWithAuth("/pulls/"+owner+'/'+name,JsonPullRequests.class).wrap(root); + } private void verifyMine() throws IOException { if (!root.login.equals(owner)) diff --git a/src/main/java/org/kohsuke/github/JsonPullRequest.java b/src/main/java/org/kohsuke/github/JsonPullRequest.java new file mode 100644 index 000000000..7e70c48ac --- /dev/null +++ b/src/main/java/org/kohsuke/github/JsonPullRequest.java @@ -0,0 +1,36 @@ +/* + * The MIT License + * + * Copyright (c) 2010, Kohsuke Kawaguchi + * + * 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; + +/** + * @author Kohsuke Kawaguchi + */ +class JsonPullRequest { + public GHPullRequest pull; + + public GHPullRequest wrap(GitHub root) { + pull.root = root; + return pull; + } +} diff --git a/src/main/java/org/kohsuke/github/JsonPullRequests.java b/src/main/java/org/kohsuke/github/JsonPullRequests.java new file mode 100644 index 000000000..24cc04869 --- /dev/null +++ b/src/main/java/org/kohsuke/github/JsonPullRequests.java @@ -0,0 +1,39 @@ +/* + * The MIT License + * + * Copyright (c) 2010, Kohsuke Kawaguchi + * + * 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.util.List; + +/** + * @author Kohsuke Kawaguchi + */ +class JsonPullRequests { + public List pulls; + + public List wrap(GitHub root) { + for (GHPullRequest pull : pulls) + pull.root = root; + return pulls; + } +}