Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand All @@ -61,6 +64,7 @@ public class GitHub {

private final Map<String,GHUser> users = new HashMap<String, GHUser>();
private final Map<String,GHOrganization> orgs = new HashMap<String, GHOrganization>();
private String oauthAccessToken;

private GitHub(String login, String apiToken, String password) {
this.login = login;
Expand All @@ -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"
*/
Expand All @@ -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.
*
Expand All @@ -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> T retrieve(String tailApiUrl, Class<T> type) throws IOException {
Expand All @@ -129,10 +155,13 @@ public static GitHub connectAnonymously() {

private <T> T _retrieve(String tailApiUrl, Class<T> 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 {
Expand Down Expand Up @@ -172,9 +201,18 @@ private <T> T _retrieve(String tailApiUrl, Class<T> 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;
}
Expand Down Expand Up @@ -202,6 +240,10 @@ public GHOrganization getOrganization(String name) throws IOException {
return o;
}

public Map<String, GHOrganization> getMyOrganizations() throws IOException {
return retrieveWithAuth("/organizations",JsonOrganizations.class).wrap(this);

}
/**
* Gets the {@link GHUser} that represents yourself.
*/
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/org/kohsuke/github/JsonOrganizations.java
Original file line number Diff line number Diff line change
@@ -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<GHOrganization> organizations;

public Map<String,GHOrganization> wrap(GitHub root) {
Map<String,GHOrganization> map = new TreeMap<String, GHOrganization>();
for (GHOrganization o : organizations) {
o.root = root;
map.put(o.getLogin(),o);
}
return map;
}
}