mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-11 00:11:25 +00:00
Compare commits
18 Commits
github-api
...
github-api
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
107a1b83c0 | ||
|
|
1f8e732d3a | ||
|
|
d5e52a2468 | ||
|
|
231a9b11ae | ||
|
|
a272ab93ec | ||
|
|
a693328ec9 | ||
|
|
7d1a043495 | ||
|
|
0180f8c352 | ||
|
|
7c630842b0 | ||
|
|
c7f3c5ef8b | ||
|
|
2b23bfd296 | ||
|
|
29bf00b202 | ||
|
|
2f97393b62 | ||
|
|
558d5d7934 | ||
|
|
2fdec0d484 | ||
|
|
06dd7c83f8 | ||
|
|
9a88e52260 | ||
|
|
ca071d2732 |
2
pom.xml
2
pom.xml
@@ -7,7 +7,7 @@
|
||||
</parent>
|
||||
|
||||
<artifactId>github-api</artifactId>
|
||||
<version>1.9</version>
|
||||
<version>1.12</version>
|
||||
<name>GitHub API for Java</name>
|
||||
<url>http://github-api.kohsuke.org/</url>
|
||||
<description>GitHub API for Java</description>
|
||||
|
||||
17
src/main/java/org/kohsuke/github/ApiVersion.java
Normal file
17
src/main/java/org/kohsuke/github/ApiVersion.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package org.kohsuke.github;
|
||||
|
||||
/**
|
||||
* Different API versions.
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
enum ApiVersion {
|
||||
V2("https://github.com/api/v2/json"),
|
||||
V3("https://api.github.com");
|
||||
|
||||
final String url;
|
||||
|
||||
ApiVersion(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ public class GHOrganization extends GHPerson {
|
||||
f.getSelectByName("team_id").getOptionByText(team).setSelected(true);
|
||||
f.submit(f.getButtonByCaption("Create Repository"));
|
||||
|
||||
return refreshRepository(name);
|
||||
return getRepository(name);
|
||||
|
||||
// GHRepository r = new Poster(root).withCredential()
|
||||
// .with("name", name).with("description", description).with("homepage", homepage)
|
||||
|
||||
@@ -17,43 +17,22 @@ public abstract class GHPerson {
|
||||
|
||||
protected int public_gist_count,public_repo_count,following_count,id;
|
||||
|
||||
/**
|
||||
* Repositories that this user owns.
|
||||
*/
|
||||
private transient Map<String,GHRepository> repositories;
|
||||
|
||||
/**
|
||||
* Gets the repositories this user owns.
|
||||
*/
|
||||
public synchronized Map<String,GHRepository> getRepositories() throws IOException {
|
||||
if (repositories==null) {
|
||||
repositories = Collections.synchronizedMap(new TreeMap<String, GHRepository>());
|
||||
for (int i=1; ; i++) {
|
||||
Map<String, GHRepository> map = root.retrieve("/repos/show/" + login + "?page=" + i, JsonRepositories.class).wrap(root);
|
||||
repositories.putAll(map);
|
||||
if (map.isEmpty()) break;
|
||||
}
|
||||
Map<String,GHRepository> repositories = new TreeMap<String, GHRepository>();
|
||||
for (int i=1; ; i++) {
|
||||
Map<String, GHRepository> map = root.retrieve("/repos/show/" + login + "?page=" + i, JsonRepositories.class).wrap(root);
|
||||
repositories.putAll(map);
|
||||
if (map.isEmpty()) break;
|
||||
}
|
||||
|
||||
return Collections.unmodifiableMap(repositories);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the repository of the given name from GitHub, and return it.
|
||||
*/
|
||||
protected GHRepository refreshRepository(String name) throws IOException {
|
||||
if (repositories==null) getRepositories(); // fetch the base first
|
||||
GHRepository r = fetchRepository(name);
|
||||
repositories.put(name,r);
|
||||
return r;
|
||||
}
|
||||
|
||||
protected GHRepository fetchRepository(String name) throws IOException {
|
||||
return root.retrieve("/repos/show/" + login + '/' + name, JsonRepository.class).wrap(root);
|
||||
}
|
||||
|
||||
public GHRepository getRepository(String name) throws IOException {
|
||||
return getRepositories().get(name);
|
||||
return root.retrieve("/repos/show/" + login + '/' + name, JsonRepository.class).wrap(root);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -32,6 +32,7 @@ import com.gargoylesoftware.htmlunit.html.HtmlInput;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.net.URL;
|
||||
@@ -47,6 +48,7 @@ import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
import static java.util.Arrays.*;
|
||||
import static org.kohsuke.github.ApiVersion.V3;
|
||||
|
||||
/**
|
||||
* A repository on GitHub.
|
||||
@@ -145,6 +147,23 @@ public class GHRepository {
|
||||
return Collections.unmodifiableSet(r);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the names of the collaborators on this repository.
|
||||
* This method deviates from the principle of this library but it works a lot faster than {@link #getCollaborators()}.
|
||||
*/
|
||||
public Set<String> getCollaboratorNames() throws IOException {
|
||||
Set<String> r = new HashSet<String>(root.retrieve("/repos/show/"+owner+"/"+name+"/collaborators",JsonCollaborators.class).collaborators);
|
||||
return Collections.unmodifiableSet(r);
|
||||
}
|
||||
|
||||
/**
|
||||
* If this repository belongs to an organization, return a set of teams.
|
||||
*/
|
||||
public Set<GHTeam> getTeams() throws IOException {
|
||||
return Collections.unmodifiableSet(root.retrieveWithAuth("/repos/show/"+owner+"/"+name+"/teams",JsonTeams.class).toSet(
|
||||
root.getOrganization(owner)));
|
||||
}
|
||||
|
||||
public void addCollaborators(GHUser... users) throws IOException {
|
||||
addCollaborators(asList(users));
|
||||
}
|
||||
@@ -188,6 +207,14 @@ public class GHRepository {
|
||||
.to("/repos/show/" + owner + "/" + name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables or disables Wiki for this repository.
|
||||
*/
|
||||
public void enableWiki(boolean v) throws IOException {
|
||||
new Poster(root).withCredential().with("values[has_wiki]",String.valueOf(v))
|
||||
.to("/repos/show/" + owner + "/" + name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes this repository.
|
||||
*/
|
||||
@@ -216,22 +243,8 @@ public class GHRepository {
|
||||
* Newly forked repository that belong to you.
|
||||
*/
|
||||
public GHRepository forkTo(GHOrganization org) throws IOException {
|
||||
WebClient wc = root.createWebClient();
|
||||
HtmlPage pg = (HtmlPage)wc.getPage(getUrl());
|
||||
for (HtmlForm f : pg.getForms()) {
|
||||
if (!f.getActionAttribute().endsWith("/fork")) continue;
|
||||
try {
|
||||
if (org.getLogin().equals(f.getInputByName("organization").getValueAttribute())) {
|
||||
// found it
|
||||
f.submit((HtmlButton)f.getElementsByTagName("button").get(0));
|
||||
return org.refreshRepository(name);
|
||||
}
|
||||
} catch (ElementNotFoundException e) {
|
||||
// continue
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Either you don't have the privilege to fork into "+org.getLogin()+" or there's a bug in HTML scraping");
|
||||
new Poster(root, V3).withCredential().to(String.format("/repos/%s/%s/forks?org=%s",owner,name,org.getLogin()));
|
||||
return org.getRepository(name);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -247,7 +260,7 @@ public class GHRepository {
|
||||
f.submit((HtmlButton)f.getElementsByTagName("button").get(0));
|
||||
|
||||
// overwrite fields
|
||||
final GHRepository r = getOwner().fetchRepository(newName);
|
||||
final GHRepository r = getOwner().getRepository(newName);
|
||||
for (Field fi : getClass().getDeclaredFields()) {
|
||||
if (Modifier.isStatic(fi.getModifiers())) continue;
|
||||
fi.setAccessible(true);
|
||||
|
||||
@@ -42,10 +42,13 @@ import java.net.MalformedURLException;
|
||||
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.*;
|
||||
|
||||
@@ -57,23 +60,37 @@ import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.*;
|
||||
public class GitHub {
|
||||
/*package*/ final String login;
|
||||
/*package*/ final String encodedAuthorization;
|
||||
final String password;
|
||||
/*package*/ final String password;
|
||||
/*package*/ final String apiToken;
|
||||
|
||||
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;
|
||||
this.apiToken = apiToken;
|
||||
this.password = password;
|
||||
|
||||
BASE64Encoder enc = new sun.misc.BASE64Encoder();
|
||||
if (apiToken!=null || password!=null) {
|
||||
String userpassword = apiToken!=null ? (login + "/token" + ":" + apiToken) : (login + ':'+password);
|
||||
String userpassword = password==null ? (login + "/token" + ":" + apiToken) : (login + ':'+password);
|
||||
encodedAuthorization = enc.encode(userpassword.getBytes());
|
||||
} else
|
||||
encodedAuthorization = null;
|
||||
}
|
||||
|
||||
private GitHub (String oauthAccessToken) throws IOException {
|
||||
|
||||
this.password = null;
|
||||
this.encodedAuthorization = null;
|
||||
|
||||
this.oauthAccessToken = oauthAccessToken;
|
||||
this.apiToken = oauthAccessToken;
|
||||
|
||||
this.login = getMyself().getLogin();
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains the credential from "~/.github"
|
||||
*/
|
||||
@@ -97,6 +114,9 @@ public class GitHub {
|
||||
return new GitHub(login,apiToken,password);
|
||||
}
|
||||
|
||||
public static GitHub connectUsingOAuth (String accessToken) throws IOException {
|
||||
return new GitHub(accessToken);
|
||||
}
|
||||
/**
|
||||
* Connects to GitHub anonymously.
|
||||
*
|
||||
@@ -107,12 +127,17 @@ public class GitHub {
|
||||
}
|
||||
|
||||
/*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);
|
||||
/*package*/ URL getApiURL(ApiVersion v, String tailApiUrl) throws IOException {
|
||||
if (oauthAccessToken != null) {
|
||||
// append the access token
|
||||
tailApiUrl = tailApiUrl + "?access_token=" + oauthAccessToken;
|
||||
}
|
||||
|
||||
return new URL(v.url+tailApiUrl);
|
||||
}
|
||||
|
||||
/*package*/ <T> T retrieve(String tailApiUrl, Class<T> type) throws IOException {
|
||||
@@ -129,10 +154,13 @@ public class GitHub {
|
||||
|
||||
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();
|
||||
|
||||
|
||||
HttpURLConnection uc = (HttpURLConnection) getApiURL(ApiVersion.V2,tailApiUrl).openConnection();
|
||||
|
||||
if (withAuth)
|
||||
if (withAuth && this.oauthAccessToken == null)
|
||||
uc.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
|
||||
|
||||
uc.setRequestMethod(method);
|
||||
|
||||
try {
|
||||
@@ -172,9 +200,18 @@ public class GitHub {
|
||||
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 +239,10 @@ public class GitHub {
|
||||
return o;
|
||||
}
|
||||
|
||||
public Map<String, GHOrganization> getMyOrganizations() throws IOException {
|
||||
return retrieveWithAuth("/organizations",JsonOrganizations.class).wrap(this);
|
||||
|
||||
}
|
||||
/**
|
||||
* Gets the {@link GHUser} that represents yourself.
|
||||
*/
|
||||
|
||||
44
src/main/java/org/kohsuke/github/JsonOrganizations.java
Normal file
44
src/main/java/org/kohsuke/github/JsonOrganizations.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package org.kohsuke.github;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
@@ -18,4 +20,13 @@ class JsonTeams {
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
Set<GHTeam> toSet(GHOrganization org) {
|
||||
Set<GHTeam> r = new HashSet<GHTeam>();
|
||||
for (GHTeam t : teams) {
|
||||
t.org = org;
|
||||
r.add(t);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,8 +46,15 @@ class Poster {
|
||||
private final List<String> args = new ArrayList<String>();
|
||||
private boolean authenticate;
|
||||
|
||||
Poster(GitHub root) {
|
||||
private final ApiVersion v;
|
||||
|
||||
Poster(GitHub root, ApiVersion v) {
|
||||
this.root = root;
|
||||
this.v = v;
|
||||
}
|
||||
|
||||
Poster(GitHub root) {
|
||||
this(root,ApiVersion.V2);
|
||||
}
|
||||
|
||||
public Poster withCredential() {
|
||||
@@ -89,12 +96,19 @@ class Poster {
|
||||
|
||||
public <T> T to(String tailApiUrl, Class<T> type, String method) throws IOException {
|
||||
while (true) {// loop while API rate limit is hit
|
||||
HttpURLConnection uc = (HttpURLConnection) root.getApiURL(tailApiUrl).openConnection();
|
||||
HttpURLConnection uc = (HttpURLConnection) root.getApiURL(v,tailApiUrl).openConnection();
|
||||
|
||||
uc.setDoOutput(true);
|
||||
uc.setRequestProperty("Content-type","application/x-www-form-urlencoded");
|
||||
if (authenticate)
|
||||
uc.setRequestProperty("Authorization", "Basic " + root.encodedAuthorization);
|
||||
if (authenticate) {
|
||||
if (v==ApiVersion.V3) {
|
||||
if (root.password==null)
|
||||
throw new IllegalArgumentException("V3 API doesn't support API token");
|
||||
uc.setRequestProperty("Authorization", "Basic " + root.encodedAuthorization);
|
||||
} else {
|
||||
uc.setRequestProperty("Authorization", "Basic " + root.encodedAuthorization);
|
||||
}
|
||||
}
|
||||
uc.setRequestMethod(method);
|
||||
|
||||
|
||||
|
||||
@@ -3,8 +3,10 @@ package org.kohsuke;
|
||||
import junit.framework.TestCase;
|
||||
import org.kohsuke.github.GHOrganization;
|
||||
import org.kohsuke.github.GHOrganization.Permission;
|
||||
import org.kohsuke.github.GHPullRequest;
|
||||
import org.kohsuke.github.GHRepository;
|
||||
import org.kohsuke.github.GHTeam;
|
||||
import org.kohsuke.github.GHUser;
|
||||
import org.kohsuke.github.GitHub;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -20,9 +22,35 @@ public class AppTest extends TestCase {
|
||||
assertFalse(GitHub.connect("totally","bogus").isCredentialValid());
|
||||
}
|
||||
|
||||
public void tryOrgFork() throws Exception {
|
||||
GitHub gh = GitHub.connect();
|
||||
gh.getUser("kohsuke").getRepository("rubywm").forkTo(gh.getOrganization("jenkinsci"));
|
||||
}
|
||||
|
||||
public void tryGetTeamsForRepo() throws Exception {
|
||||
GitHub gh = GitHub.connect();
|
||||
Set<GHTeam> o = gh.getOrganization("jenkinsci").getRepository("rubywm").getTeams();
|
||||
System.out.println(o);
|
||||
}
|
||||
|
||||
public void testMembership() throws Exception {
|
||||
GitHub gitHub = GitHub.connect();
|
||||
Set<String> members = gitHub.getOrganization("jenkinsci").getRepository("violations-plugin").getCollaboratorNames();
|
||||
System.out.println(members.contains("kohsuke"));
|
||||
}
|
||||
|
||||
public void testApp() throws IOException {
|
||||
GitHub gitHub = GitHub.connect();
|
||||
|
||||
// tryDisablingIssueTrackers(gitHub);
|
||||
|
||||
// tryDisablingWiki(gitHub);
|
||||
|
||||
// GHPullRequest i = gitHub.getOrganization("jenkinsci").getRepository("sandbox").getPullRequest(1);
|
||||
// for (GHIssueComment c : i.getComments())
|
||||
// System.out.println(c);
|
||||
// System.out.println(i);
|
||||
|
||||
// gitHub.getMyself().getRepository("perforce-plugin").setEmailServiceHook("kk@kohsuke.org");
|
||||
|
||||
// tryRenaming(gitHub);
|
||||
@@ -61,6 +89,15 @@ public class AppTest extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
private void tryDisablingWiki(GitHub gitHub) throws IOException {
|
||||
for (GHRepository r : gitHub.getOrganization("jenkinsci").getRepositories().values()) {
|
||||
if (r.hasWiki()) {
|
||||
System.out.println("DISABLED "+r.getName());
|
||||
r.enableWiki(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void tryUpdatingIssueTracker(GitHub gitHub) throws IOException {
|
||||
GHRepository r = gitHub.getOrganization("jenkinsci").getRepository("lib-task-reactor");
|
||||
System.out.println(r.hasIssues());
|
||||
|
||||
Reference in New Issue
Block a user