diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index c5dae6ec0c..c4f5b893c4 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -42,10 +42,13 @@ import java.net.URL; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Collections; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Properties; +import java.util.Set; import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.*; @@ -61,6 +64,7 @@ public class GitHub { private final Map users = new HashMap(); private final Map orgs = new HashMap(); + private String oauthAccessToken; private GitHub(String login, String apiToken, String password) { this.login = login; @@ -74,6 +78,17 @@ private GitHub(String login, String apiToken, String password) { encodedAuthorization = null; } + private GitHub (String oauthAccessToken) throws IOException { + + this.password = null; + this.encodedAuthorization = null; + + this.oauthAccessToken = oauthAccessToken; + + this.login = getMyself().getLogin(); + + + } /** * Obtains the credential from "~/.github" */ @@ -97,6 +112,9 @@ public static GitHub connect(String login, String apiToken, String password) thr return new GitHub(login,apiToken,password); } + public static GitHub connectUsingOAuth (String accessToken) throws IOException { + return new GitHub(accessToken); + } /** * Connects to GitHub anonymously. * @@ -107,12 +125,20 @@ public static GitHub connectAnonymously() { } /*package*/ void requireCredential() { - if (login==null || encodedAuthorization==null) + if ((login==null || encodedAuthorization==null) && oauthAccessToken == null) throw new IllegalStateException("This operation requires a credential but none is given to the GitHub constructor"); } /*package*/ URL getApiURL(String tailApiUrl) throws IOException { - return new URL("http://github.com/api/v2/json"+tailApiUrl); + + if (oauthAccessToken != null) { + // append the access token + + tailApiUrl = tailApiUrl + "?access_token=" + oauthAccessToken; + } + + return new URL("https://github.com/api/v2/json"+tailApiUrl); + } /*package*/ T retrieve(String tailApiUrl, Class type) throws IOException { @@ -129,10 +155,13 @@ public static GitHub connectAnonymously() { private T _retrieve(String tailApiUrl, Class type, String method, boolean withAuth) throws IOException { while (true) {// loop while API rate limit is hit + + HttpURLConnection uc = (HttpURLConnection) getApiURL(tailApiUrl).openConnection(); - if (withAuth) + if (withAuth && this.oauthAccessToken == null) uc.setRequestProperty("Authorization", "Basic " + encodedAuthorization); + uc.setRequestMethod(method); try { @@ -172,9 +201,18 @@ private T _retrieve(String tailApiUrl, Class type, String method, boolean public GHUser getUser(String login) throws IOException { GHUser u = users.get(login); if (u==null) { + + if (oauthAccessToken != null) { + u = retrieve("/user/show",JsonUser.class).user; + u.root = this; + users.put(u.getLogin(),u); + } + else { u = retrieve("/user/show/"+login,JsonUser.class).user; u.root = this; users.put(login,u); + } + } return u; } @@ -202,6 +240,10 @@ public GHOrganization getOrganization(String name) throws IOException { return o; } + public Map getMyOrganizations() throws IOException { + return retrieveWithAuth("/organizations",JsonOrganizations.class).wrap(this); + + } /** * Gets the {@link GHUser} that represents yourself. */ diff --git a/src/main/java/org/kohsuke/github/JsonOrganizations.java b/src/main/java/org/kohsuke/github/JsonOrganizations.java new file mode 100644 index 0000000000..620e5e68d2 --- /dev/null +++ b/src/main/java/org/kohsuke/github/JsonOrganizations.java @@ -0,0 +1,44 @@ +/* + * 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; +import java.util.Map; +import java.util.TreeMap; + +/** + * + */ +class JsonOrganizations { + public List organizations; + + public Map wrap(GitHub root) { + Map map = new TreeMap(); + for (GHOrganization o : organizations) { + o.root = root; + map.put(o.getLogin(),o); + } + return map; + } +}