diff --git a/pom.xml b/pom.xml index 2dfa10b3b6..89825f5056 100644 --- a/pom.xml +++ b/pom.xml @@ -1,110 +1,30 @@ 4.0.0 - org.kohsuke + + org.kohsuke + pom + 1 + + github-api - jar - 1.8 + 1.9 GitHub API for Java - http://kohsuke.org/github-api/ + http://github-api.kohsuke.org/ GitHub API for Java + + scm:git:git@github.com/kohsuke/${project.artifactId}.git + scm:git:ssh://git@github.com/kohsuke/${project.artifactId}.git + http://${project.artifactId}.kohsuke.org/ + + - - maven.jenkins-ci.org - http://maven.jenkins-ci.org:8081/content/repositories/releases/ - - kohsuke.org - scp://kohsuke.org/home/kohsuke/kohsuke.org/github-api/ + github-pages + gitsite:git@github.com/kohsuke/${project.artifactId}.git - - - m.g.o-public - http://maven.glassfish.org/content/groups/public/ - - true - - - false - - - - - - - m.g.o-public - http://maven.glassfish.org/content/groups/public/ - - true - - - false - - - - - - - - maven-compiler-plugin - - 1.5 - 1.5 - - - - org.apache.maven.plugins - maven-release-plugin - 2.0 - - - org.apache.maven.scm - maven-scm-provider-gitexe - 1.2 - - - - - org.apache.maven.plugins - maven-scm-plugin - 1.3 - - - - - org.jvnet.wagon-svn - wagon-svn - 1.9 - - - org.apache.maven.wagon - wagon-ssh - 1.0-beta-7 - - - - - - scm:git:git@github.com:kohsuke/github-api.git - - - - - kohsuke - Kohsuke Kawaguchi - - - - - - MIT License - repository - http://www.opensource.org/licenses/mit-license.php - - - org.jvnet.hudson diff --git a/src/main/java/org/kohsuke/github/GHIssue.java b/src/main/java/org/kohsuke/github/GHIssue.java new file mode 100644 index 0000000000..842071b7eb --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHIssue.java @@ -0,0 +1,129 @@ +/* + * The MIT License + * + * Copyright (c) 2011, Eric Maupin, 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.io.IOException; +import java.net.URL; +import java.util.Collection; +import java.util.Collections; +import java.util.Date; +import java.util.List; + +/** + * Represents an issue on GitHub. + * + * @author Eric Maupin + * @author Kohsuke Kawaguchi + */ +public class GHIssue { + GitHub root; + GHRepository owner; + + private String gravatar_id,body,title,state,created_at,updated_at,html_url; + private List labels; + private int number,votes,comments; + private int position; + + /** + * Repository to which the issue belongs. + */ + public GHRepository getRepository() { + return owner; + } + + /** + * The description of this pull request. + */ + public String getBody() { + return body; + } + + /** + * ID. + */ + public int getNumber() { + return number; + } + + /** + * The HTML page of this issue, + * like https://github.com/jenkinsci/jenkins/issues/100 + */ + public URL getUrl() { + return GitHub.parseURL(html_url); + } + + public String getTitle() { + return title; + } + + public GHIssueState getState() { + return Enum.valueOf(GHIssueState.class, state); + } + + public Collection getLabels() { + return Collections.unmodifiableList(labels); + } + + public Date getCreatedAt() { + return GitHub.parseDate(created_at); + } + + public Date getUpdatedAt() { + return GitHub.parseDate(updated_at); + } + + /** + * Updates the issue by adding a comment. + */ + public void comment(String message) throws IOException { + new Poster(root).withCredential().with("comment",message).to(getApiRoute("comment")); + } + + /** + * Closes this issue. + */ + public void close() throws IOException { + new Poster(root).withCredential().to(getApiRoute("close")); + } + + /** + * Reopens this issue. + */ + public void reopen() throws IOException { + new Poster(root).withCredential().to(getApiRoute("reopen")); + } + + /** + * Obtains all the comments associated with this issue. + */ + public List getComments() throws IOException { + return root.retrieve(getApiRoute("comments"), JsonIssueComments.class).wrap(this); + } + + private String getApiRoute(String verb) { + return "/issues/"+verb+"/"+owner.getOwnerName()+"/"+owner.getName()+"/"+number; + } +} \ No newline at end of file diff --git a/src/main/java/org/kohsuke/github/GHIssueComment.java b/src/main/java/org/kohsuke/github/GHIssueComment.java new file mode 100644 index 0000000000..d17c0a2952 --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHIssueComment.java @@ -0,0 +1,79 @@ +/* + * 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.io.IOException; +import java.util.Date; + +/** + * Comment to the issue + * + * @author Kohsuke Kawaguchi + */ +public class GHIssueComment { + GHIssue owner; + + private String body, gravatar_id, user, created_at, updated_at; + private int id; + + /** + * Gets the issue to which this comment is associated. + */ + public GHIssue getParent() { + return owner; + } + + /** + * The comment itself. + */ + public String getBody() { + return body; + } + + public Date getCreatedAt() { + return GitHub.parseDate(created_at); + } + + public Date getUpdatedAt() { + return GitHub.parseDate(updated_at); + } + + public int getId() { + return id; + } + + /** + * Gets the ID of the user who posted this comment. + */ + public String getUserName() { + return user; + } + + /** + * Gets the user who posted this comment. + */ + public GHUser getUser() throws IOException { + return owner.root.getUser(user); + } +} diff --git a/src/main/java/org/kohsuke/github/GHIssueState.java b/src/main/java/org/kohsuke/github/GHIssueState.java new file mode 100644 index 0000000000..d0af550ead --- /dev/null +++ b/src/main/java/org/kohsuke/github/GHIssueState.java @@ -0,0 +1,30 @@ +/* + * The MIT License + * + * Copyright (c) 2011, Eric Maupin + * + * 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; + +public enum GHIssueState { + OPEN, + CLOSED +} \ No newline at end of file diff --git a/src/main/java/org/kohsuke/github/GHOrganization.java b/src/main/java/org/kohsuke/github/GHOrganization.java index a8bfb7f1e9..04e4cd068e 100644 --- a/src/main/java/org/kohsuke/github/GHOrganization.java +++ b/src/main/java/org/kohsuke/github/GHOrganization.java @@ -4,7 +4,6 @@ import com.gargoylesoftware.htmlunit.html.HtmlAnchor; import com.gargoylesoftware.htmlunit.html.HtmlForm; import com.gargoylesoftware.htmlunit.html.HtmlPage; -import org.kohsuke.github.GHPullRequest.State; import java.io.IOException; import java.util.ArrayList; @@ -88,7 +87,7 @@ public List getRepositoriesWithOpenPullRequests() throws IOExcepti public List getPullRequests() throws IOException { List all = new ArrayList(); for (GHRepository r : getRepositoriesWithOpenPullRequests()) { - all.addAll(r.getPullRequests(State.OPEN)); + all.addAll(r.getPullRequests(GHIssueState.OPEN)); } return all; } diff --git a/src/main/java/org/kohsuke/github/GHPullRequest.java b/src/main/java/org/kohsuke/github/GHPullRequest.java index 995c4b8aa3..ff8f13b6fb 100644 --- a/src/main/java/org/kohsuke/github/GHPullRequest.java +++ b/src/main/java/org/kohsuke/github/GHPullRequest.java @@ -33,26 +33,12 @@ * @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; +public class GHPullRequest extends GHIssue { + private String closed_at, patch_url, issue_updated_at; 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; - } + private String mergeable, diff_url; /** * The URL of the patch file. @@ -62,13 +48,6 @@ public URL getPatchUrl() { return GitHub.parseURL(patch_url); } - /** - * ID. - */ - public int getNumber() { - return number; - } - /** * User who submitted a pull request. */ @@ -76,13 +55,6 @@ public GHUser getUser() { return user; } - /** - * Repository to which the pull request was sent. - */ - public GHRepository getRepository() { - return getBase().getRepository(); - } - /** * This points to where the change should be pulled into, * but I'm not really sure what exactly it means. @@ -103,11 +75,7 @@ public GHCommitPointer getHead() { * like https://github.com/jenkinsci/jenkins/pull/100 */ public URL getUrl() { - return GitHub.parseURL(html_url); - } - - public String getTitle() { - return title; + return super.getUrl(); } /** @@ -121,16 +89,4 @@ public URL getDiffUrl() { 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 1c49524f92..f40843ebb5 100644 --- a/src/main/java/org/kohsuke/github/GHRepository.java +++ b/src/main/java/org/kohsuke/github/GHRepository.java @@ -59,7 +59,7 @@ public class GHRepository { private String description, homepage, url, name, owner; private boolean has_issues, has_wiki, fork, _private, has_downloads; - private int watchers,forks; + private int watchers,forks,open_issues; private String created_at, pushed_at; public String getDescription() { @@ -85,6 +85,10 @@ public GHUser getOwner() throws IOException { return root.getUser(owner); } + public List getIssues(GHIssueState state) throws IOException { + return root.retrieve("/issues/list/" + owner + "/" + name + "/" + state.toString().toLowerCase(), JsonIssues.class).wrap(this); + } + protected String getOwnerName() { return owner; } @@ -117,6 +121,10 @@ public int getWatchers() { return watchers; } + public int getOpenIssueCount() { + return open_issues; + } + public Date getPushedAt() { return GitHub.parseDate(pushed_at); } @@ -172,6 +180,14 @@ public void setEmailServiceHook(String address) throws IOException { f.submit((HtmlButton) f.getElementsByTagName("button").get(0)); } + /** + * Enables or disables the issue tracker for this repository. + */ + public void enableIssueTracker(boolean v) throws IOException { + new Poster(root).withCredential().with("values[has_issues]",String.valueOf(v)) + .to("/repos/show/" + owner + "/" + name); + } + /** * Deletes this repository. */ @@ -255,14 +271,14 @@ public void renameTo(String newName) throws IOException { * Retrieves a specified pull request. */ public GHPullRequest getPullRequest(int i) throws IOException { - return root.retrieveWithAuth("/pulls/" + owner + '/' + name + "/" + i, JsonPullRequest.class).wrap(root); + return root.retrieveWithAuth("/pulls/" + owner + '/' + name + "/" + i, JsonPullRequest.class).wrap(this); } /** * 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); + public List getPullRequests(GHIssueState state) throws IOException { + return root.retrieveWithAuth("/pulls/"+owner+'/'+name+"/"+state.name().toLowerCase(Locale.ENGLISH),JsonPullRequests.class).wrap(this); } // this is no different from getPullRequests(OPEN) diff --git a/src/main/java/org/kohsuke/github/JsonIssue.java b/src/main/java/org/kohsuke/github/JsonIssue.java new file mode 100644 index 0000000000..330b4a6ce1 --- /dev/null +++ b/src/main/java/org/kohsuke/github/JsonIssue.java @@ -0,0 +1,38 @@ +/* + * The MIT License + * + * Copyright (c) 2011, Eric Maupin + * + * 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 Eric Maupin + */ +class JsonIssue { + GHIssue issue; + + GHIssue wrap(GHRepository r) { + issue.owner = r; + issue.root = r.root; + return issue; + } +} \ No newline at end of file diff --git a/src/main/java/org/kohsuke/github/JsonIssueComments.java b/src/main/java/org/kohsuke/github/JsonIssueComments.java new file mode 100644 index 0000000000..f982708e23 --- /dev/null +++ b/src/main/java/org/kohsuke/github/JsonIssueComments.java @@ -0,0 +1,16 @@ +package org.kohsuke.github; + +import java.util.List; + +/** + * @author Kohsuke Kawaguchi + */ +class JsonIssueComments { + List comments; + + List wrap(GHIssue owner) { + for (GHIssueComment c : comments) + c.owner = owner; + return comments; + } +} diff --git a/src/main/java/org/kohsuke/github/JsonIssues.java b/src/main/java/org/kohsuke/github/JsonIssues.java new file mode 100644 index 0000000000..783745f2bd --- /dev/null +++ b/src/main/java/org/kohsuke/github/JsonIssues.java @@ -0,0 +1,38 @@ +/* + * The MIT License + * + * Copyright (c) 2011, Eric Maupin + * + * 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; + +class JsonIssues { + List issues; + + public List wrap(GHRepository owner) { + for (GHIssue issue : issues) { + issue.owner = owner; + issue.root = owner.root; + } + return issues; + } +} diff --git a/src/main/java/org/kohsuke/github/JsonPullRequest.java b/src/main/java/org/kohsuke/github/JsonPullRequest.java index 7e70c48aca..80cc367b2a 100644 --- a/src/main/java/org/kohsuke/github/JsonPullRequest.java +++ b/src/main/java/org/kohsuke/github/JsonPullRequest.java @@ -29,8 +29,9 @@ class JsonPullRequest { public GHPullRequest pull; - public GHPullRequest wrap(GitHub root) { - pull.root = root; + public GHPullRequest wrap(GHRepository owner) { + pull.owner = owner; + pull.root = owner.root; return pull; } } diff --git a/src/main/java/org/kohsuke/github/JsonPullRequests.java b/src/main/java/org/kohsuke/github/JsonPullRequests.java index 24cc048690..fd5e47eb58 100644 --- a/src/main/java/org/kohsuke/github/JsonPullRequests.java +++ b/src/main/java/org/kohsuke/github/JsonPullRequests.java @@ -31,9 +31,11 @@ class JsonPullRequests { public List pulls; - public List wrap(GitHub root) { - for (GHPullRequest pull : pulls) - pull.root = root; + public List wrap(GHRepository owner) { + for (GHPullRequest pull : pulls) { + pull.owner = owner; + pull.root = owner.root; + } return pulls; } } diff --git a/src/main/java/org/kohsuke/github/JsonTeam.java b/src/main/java/org/kohsuke/github/JsonTeam.java index 63fde02765..4949d845c2 100644 --- a/src/main/java/org/kohsuke/github/JsonTeam.java +++ b/src/main/java/org/kohsuke/github/JsonTeam.java @@ -3,7 +3,7 @@ /** * @author Kohsuke Kawaguchi */ -public class JsonTeam { +class JsonTeam { public GHTeam team; GHTeam wrap(GHOrganization org) { diff --git a/src/site/site.xml b/src/site/site.xml index 5c5214dd9e..252c697b6c 100644 --- a/src/site/site.xml +++ b/src/site/site.xml @@ -2,21 +2,19 @@ GitHub API for Java - http://kohsuke.org/github-api + http://github-api.kohsuke.org/ org.kohsuke maven-skin - 1.1 + 1.2 - - - + - - - + + + diff --git a/src/test/java/org/kohsuke/AppTest.java b/src/test/java/org/kohsuke/AppTest.java index 5852dab638..a039efacfe 100644 --- a/src/test/java/org/kohsuke/AppTest.java +++ b/src/test/java/org/kohsuke/AppTest.java @@ -3,20 +3,13 @@ import junit.framework.TestCase; import org.kohsuke.github.GHOrganization; import org.kohsuke.github.GHOrganization.Permission; -import org.kohsuke.github.GHPullRequest; -import org.kohsuke.github.GHPullRequest.State; import org.kohsuke.github.GHRepository; import org.kohsuke.github.GHTeam; import org.kohsuke.github.GitHub; import java.io.IOException; import java.net.URL; -import java.util.Collections; -import java.util.Comparator; -import java.util.Date; -import java.util.List; import java.util.Set; -import java.util.concurrent.TimeUnit; /** * Unit test for simple App. @@ -55,6 +48,26 @@ public void testApp() throws IOException { // System.out.println(hub.getUser("kohsuke").getRepository("hudson").getCollaborators()); } + private void tryDisablingIssueTrackers(GitHub gitHub) throws IOException { + for (GHRepository r : gitHub.getOrganization("jenkinsci").getRepositories().values()) { + if (r.hasIssues()) { + if (r.getOpenIssueCount()==0) { + System.out.println("DISABLED "+r.getName()); + r.enableIssueTracker(false); + } else { + System.out.println("UNTOUCHED "+r.getName()); + } + } + } + } + + private void tryUpdatingIssueTracker(GitHub gitHub) throws IOException { + GHRepository r = gitHub.getOrganization("jenkinsci").getRepository("lib-task-reactor"); + System.out.println(r.hasIssues()); + System.out.println(r.getOpenIssueCount()); + r.enableIssueTracker(false); + } + private void tryRenaming(GitHub gitHub) throws IOException { gitHub.getUser("kohsuke").getRepository("test").renameTo("test2"); }