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 | |
|---|---|---|---|
|
|
a395697980 | ||
|
|
834a6eb865 | ||
|
|
e1c6927fd8 | ||
|
|
f02f2d7585 | ||
|
|
d4c099d447 | ||
|
|
5756133840 | ||
|
|
6cc966adee | ||
|
|
107a1b83c0 | ||
|
|
1f8e732d3a | ||
|
|
d5e52a2468 | ||
|
|
231a9b11ae | ||
|
|
a272ab93ec | ||
|
|
a693328ec9 | ||
|
|
7d1a043495 | ||
|
|
0180f8c352 | ||
|
|
7c630842b0 | ||
|
|
c7f3c5ef8b | ||
|
|
2b23bfd296 |
2
pom.xml
2
pom.xml
@@ -7,7 +7,7 @@
|
||||
</parent>
|
||||
|
||||
<artifactId>github-api</artifactId>
|
||||
<version>1.10</version>
|
||||
<version>1.14</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;
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,10 @@ public class GHPullRequest extends GHIssue {
|
||||
return head;
|
||||
}
|
||||
|
||||
public Date getIssueUpdatedAt() {
|
||||
return GitHub.parseDate(issue_updated_at);
|
||||
}
|
||||
|
||||
/**
|
||||
* The HTML page of this pull request,
|
||||
* like https://github.com/jenkinsci/jenkins/pull/100
|
||||
|
||||
@@ -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.
|
||||
@@ -158,7 +160,7 @@ public class GHRepository {
|
||||
* If this repository belongs to an organization, return a set of teams.
|
||||
*/
|
||||
public Set<GHTeam> getTeams() throws IOException {
|
||||
return Collections.unmodifiableSet(root.retrieve("/repos/show/"+owner+"/"+name+"/teams",JsonTeams.class).toSet(
|
||||
return Collections.unmodifiableSet(root.retrieveWithAuth("/repos/show/"+owner+"/"+name+"/teams",JsonTeams.class).toSet(
|
||||
root.getOrganization(owner)));
|
||||
}
|
||||
|
||||
@@ -188,9 +190,9 @@ public class GHRepository {
|
||||
public void setEmailServiceHook(String address) throws IOException {
|
||||
WebClient wc = root.createWebClient();
|
||||
HtmlPage pg = (HtmlPage)wc.getPage(getUrl()+"/admin");
|
||||
HtmlInput email = (HtmlInput)pg.getElementById("Email_address");
|
||||
HtmlInput email = (HtmlInput)pg.getElementById("email_address");
|
||||
email.setValueAttribute(address);
|
||||
HtmlCheckBoxInput active = (HtmlCheckBoxInput)pg.getElementById("Email[active]");
|
||||
HtmlCheckBoxInput active = (HtmlCheckBoxInput)pg.getElementById("email[active]");
|
||||
active.setChecked(true);
|
||||
|
||||
final HtmlForm f = email.getEnclosingFormOrDie();
|
||||
@@ -205,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.
|
||||
*/
|
||||
@@ -233,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.getRepository(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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -60,7 +60,8 @@ 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>();
|
||||
@@ -68,11 +69,12 @@ public class GitHub {
|
||||
|
||||
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;
|
||||
@@ -84,11 +86,11 @@ public class GitHub {
|
||||
this.encodedAuthorization = null;
|
||||
|
||||
this.oauthAccessToken = oauthAccessToken;
|
||||
this.apiToken = oauthAccessToken;
|
||||
|
||||
this.login = getMyself().getLogin();
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains the credential from "~/.github"
|
||||
*/
|
||||
@@ -129,16 +131,13 @@ public class GitHub {
|
||||
throw new IllegalStateException("This operation requires a credential but none is given to the GitHub constructor");
|
||||
}
|
||||
|
||||
/*package*/ URL getApiURL(String tailApiUrl) throws IOException {
|
||||
|
||||
/*package*/ URL getApiURL(ApiVersion v, String tailApiUrl) throws IOException {
|
||||
if (oauthAccessToken != null) {
|
||||
// append the access token
|
||||
|
||||
tailApiUrl = tailApiUrl + "?access_token=" + oauthAccessToken;
|
||||
}
|
||||
|
||||
return new URL("https://github.com/api/v2/json"+tailApiUrl);
|
||||
|
||||
return new URL(v.url+tailApiUrl);
|
||||
}
|
||||
|
||||
/*package*/ <T> T retrieve(String tailApiUrl, Class<T> type) throws IOException {
|
||||
@@ -157,7 +156,7 @@ public class GitHub {
|
||||
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 && this.oauthAccessToken == null)
|
||||
uc.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
|
||||
|
||||
@@ -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,6 +3,7 @@ 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;
|
||||
@@ -21,6 +22,17 @@ 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();
|
||||
@@ -29,6 +41,11 @@ public class AppTest extends TestCase {
|
||||
|
||||
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);
|
||||
@@ -72,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