mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-11 15:50:17 +00:00
Compare commits
12 Commits
github-api
...
github-api
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3a9ade667a | ||
|
|
057c32d410 | ||
|
|
33657c9c92 | ||
|
|
4c199256a5 | ||
|
|
9e62776905 | ||
|
|
9ba74b945d | ||
|
|
66656ce612 | ||
|
|
73a20ad829 | ||
|
|
6fc9a546cb | ||
|
|
3f6c225948 | ||
|
|
c7e9650a39 | ||
|
|
fd28a36b74 |
2
README
2
README
@@ -1,3 +1,3 @@
|
|||||||
Java API for GitHub
|
Java API for GitHub
|
||||||
|
|
||||||
See http://kohsuke.org/github-api/ for more details
|
See http://github-api.kohsuke.org/ for more details
|
||||||
|
|||||||
2
pom.xml
2
pom.xml
@@ -7,7 +7,7 @@
|
|||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<artifactId>github-api</artifactId>
|
<artifactId>github-api</artifactId>
|
||||||
<version>1.23</version>
|
<version>1.26</version>
|
||||||
<name>GitHub API for Java</name>
|
<name>GitHub API for Java</name>
|
||||||
<url>http://github-api.kohsuke.org/</url>
|
<url>http://github-api.kohsuke.org/</url>
|
||||||
<description>GitHub API for Java</description>
|
<description>GitHub API for Java</description>
|
||||||
|
|||||||
48
src/main/java/org/kohsuke/github/GHKey.java
Normal file
48
src/main/java/org/kohsuke/github/GHKey.java
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package org.kohsuke.github;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.builder.ToStringBuilder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SSH public key.
|
||||||
|
*
|
||||||
|
* @author Kohsuke Kawaguchi
|
||||||
|
*/
|
||||||
|
public class GHKey {
|
||||||
|
/*package almost final*/ GitHub root;
|
||||||
|
|
||||||
|
private String url, key, title;
|
||||||
|
private boolean verified;
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getKey() {
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Something like "https://api.github.com/user/keys/73593"
|
||||||
|
*/
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isVerified() {
|
||||||
|
return verified;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*package*/ GHKey wrap(GitHub root) {
|
||||||
|
this.root = root;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
return new ToStringBuilder(this).append("title",title).append("id",id).append("key",key).toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,6 +26,16 @@ public class GHMyself extends GHUser {
|
|||||||
String[] addresses = root.retrieveWithAuth3("/user/emails",String[].class);
|
String[] addresses = root.retrieveWithAuth3("/user/emails",String[].class);
|
||||||
return Collections.unmodifiableList(Arrays.asList(addresses));
|
return Collections.unmodifiableList(Arrays.asList(addresses));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the read-only list of all the pulic keys of the current user.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* Always non-null.
|
||||||
|
*/
|
||||||
|
public List<GHKey> getPublicKeys() throws IOException {
|
||||||
|
return Collections.unmodifiableList(Arrays.asList(root.retrieveWithAuth3("/user/keys",GHKey[].class)));
|
||||||
|
}
|
||||||
|
|
||||||
// public void addEmails(Collection<String> emails) throws IOException {
|
// public void addEmails(Collection<String> emails) throws IOException {
|
||||||
//// new Poster(root,ApiVersion.V3).withCredential().to("/user/emails");
|
//// new Poster(root,ApiVersion.V3).withCredential().to("/user/emails");
|
||||||
|
|||||||
@@ -43,15 +43,6 @@ public class GHOrganization extends GHPerson {
|
|||||||
return root.retrieveWithAuth("/organizations/"+login+"/teams",JsonTeams.class).toMap(this);
|
return root.retrieveWithAuth("/organizations/"+login+"/teams",JsonTeams.class).toMap(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public GHRepository getRepository(String name) throws IOException {
|
|
||||||
try {
|
|
||||||
return root.retrieveWithAuth3("/repos/" + login + '/' + name, GHRepository.class).wrap(root);
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Publicizes the membership.
|
* Publicizes the membership.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ public abstract class GHPerson {
|
|||||||
*/
|
*/
|
||||||
public GHRepository getRepository(String name) throws IOException {
|
public GHRepository getRepository(String name) throws IOException {
|
||||||
try {
|
try {
|
||||||
return root.retrieve3("/repos/" + login + '/' + name, GHRepository.class).wrap(root);
|
return root.retrieveWithAuth3("/repos/" + login + '/' + name, GHRepository.class).wrap(root);
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
21
src/main/java/org/kohsuke/github/GHRateLimit.java
Normal file
21
src/main/java/org/kohsuke/github/GHRateLimit.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package org.kohsuke.github;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rate limit.
|
||||||
|
* @author Kohsuke Kawaguchi
|
||||||
|
*/
|
||||||
|
public class GHRateLimit {
|
||||||
|
/**
|
||||||
|
* Remaining calls that can be made.
|
||||||
|
*/
|
||||||
|
public int remaining;
|
||||||
|
/**
|
||||||
|
* Alotted API call per hour.
|
||||||
|
*/
|
||||||
|
public int limit;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return remaining+"/"+limit;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -72,9 +72,16 @@ public class GHRepository {
|
|||||||
private String created_at, pushed_at;
|
private String created_at, pushed_at;
|
||||||
private Map<Integer,GHMilestone> milestones = new HashMap<Integer, GHMilestone>();
|
private Map<Integer,GHMilestone> milestones = new HashMap<Integer, GHMilestone>();
|
||||||
|
|
||||||
private String master_branch;
|
private String master_branch,language;
|
||||||
private Map<String,GHCommit> commits = new HashMap<String, GHCommit>();
|
private Map<String,GHCommit> commits = new HashMap<String, GHCommit>();
|
||||||
|
|
||||||
|
private GHRepoPermission permissions;
|
||||||
|
|
||||||
|
private static class GHRepoPermission {
|
||||||
|
boolean pull,push,admin;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public String getDescription() {
|
public String getDescription() {
|
||||||
return description;
|
return description;
|
||||||
}
|
}
|
||||||
@@ -113,6 +120,25 @@ public class GHRepository {
|
|||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean hasPullAccess() {
|
||||||
|
return permissions!=null && permissions.pull;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasPushAccess() {
|
||||||
|
return permissions!=null && permissions.push;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasAdminAccess() {
|
||||||
|
return permissions!=null && permissions.admin;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the primary programming language.
|
||||||
|
*/
|
||||||
|
public String getLanguage() {
|
||||||
|
return language;
|
||||||
|
}
|
||||||
|
|
||||||
public GHUser getOwner() throws IOException {
|
public GHUser getOwner() throws IOException {
|
||||||
return root.getUser(owner.login); // because 'owner' isn't fully populated
|
return root.getUser(owner.login); // because 'owner' isn't fully populated
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -288,7 +288,10 @@ public class GitHub {
|
|||||||
private HttpURLConnection setupConnection(String method, boolean withAuth, URL url) throws IOException {
|
private HttpURLConnection setupConnection(String method, boolean withAuth, URL url) throws IOException {
|
||||||
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
|
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
|
||||||
|
|
||||||
if (withAuth && this.oauthAccessToken == null)
|
// if the authentication is needed but no credential is given, try it anyway (so that some calls
|
||||||
|
// that do work with anonymous access in the reduced form should still work.)
|
||||||
|
// if OAuth token is present, it'll be set in the URL, so need to set the Authorization header
|
||||||
|
if (withAuth && encodedAuthorization!=null && this.oauthAccessToken == null)
|
||||||
uc.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
|
uc.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
|
||||||
|
|
||||||
uc.setRequestMethod(method);
|
uc.setRequestMethod(method);
|
||||||
@@ -355,6 +358,13 @@ public class GitHub {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the current rate limit.
|
||||||
|
*/
|
||||||
|
public GHRateLimit getRateLimit() throws IOException {
|
||||||
|
return retrieveWithAuth3("/rate_limit",JsonRateLimit.class).rate;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the {@link GHUser} that represents yourself.
|
* Gets the {@link GHUser} that represents yourself.
|
||||||
*/
|
*/
|
||||||
|
|||||||
8
src/main/java/org/kohsuke/github/JsonRateLimit.java
Normal file
8
src/main/java/org/kohsuke/github/JsonRateLimit.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package org.kohsuke.github;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Kohsuke Kawaguchi
|
||||||
|
*/
|
||||||
|
class JsonRateLimit {
|
||||||
|
GHRateLimit rate;
|
||||||
|
}
|
||||||
@@ -10,6 +10,7 @@ import org.kohsuke.github.GHEventPayload;
|
|||||||
import org.kohsuke.github.GHHook;
|
import org.kohsuke.github.GHHook;
|
||||||
import org.kohsuke.github.GHBranch;
|
import org.kohsuke.github.GHBranch;
|
||||||
import org.kohsuke.github.GHIssueState;
|
import org.kohsuke.github.GHIssueState;
|
||||||
|
import org.kohsuke.github.GHKey;
|
||||||
import org.kohsuke.github.GHMyself;
|
import org.kohsuke.github.GHMyself;
|
||||||
import org.kohsuke.github.GHOrganization;
|
import org.kohsuke.github.GHOrganization;
|
||||||
import org.kohsuke.github.GHOrganization.Permission;
|
import org.kohsuke.github.GHOrganization.Permission;
|
||||||
@@ -35,6 +36,10 @@ public class AppTest extends TestCase {
|
|||||||
assertFalse(GitHub.connect("totally","bogus").isCredentialValid());
|
assertFalse(GitHub.connect("totally","bogus").isCredentialValid());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testRateLimit() throws IOException {
|
||||||
|
System.out.println(GitHub.connect().getRateLimit());
|
||||||
|
}
|
||||||
|
|
||||||
public void testFetchPullRequest() throws Exception {
|
public void testFetchPullRequest() throws Exception {
|
||||||
GitHub gh = GitHub.connect();
|
GitHub gh = GitHub.connect();
|
||||||
GHRepository r = gh.getOrganization("jenkinsci").getRepository("jenkins");
|
GHRepository r = gh.getOrganization("jenkinsci").getRepository("jenkins");
|
||||||
@@ -42,6 +47,15 @@ public class AppTest extends TestCase {
|
|||||||
r.getPullRequest(1);
|
r.getPullRequest(1);
|
||||||
r.getPullRequests(GHIssueState.OPEN);
|
r.getPullRequests(GHIssueState.OPEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testRepoPermissions() throws Exception {
|
||||||
|
GitHub gh = GitHub.connect();
|
||||||
|
GHRepository r = gh.getOrganization("jenkinsci").getRepository("jenkins");
|
||||||
|
assertTrue(r.hasPullAccess());
|
||||||
|
|
||||||
|
r = gh.getOrganization("github").getRepository("tire");
|
||||||
|
assertFalse(r.hasAdminAccess());
|
||||||
|
}
|
||||||
|
|
||||||
public void tryGetMyself() throws Exception {
|
public void tryGetMyself() throws Exception {
|
||||||
GitHub hub = GitHub.connect();
|
GitHub hub = GitHub.connect();
|
||||||
@@ -56,6 +70,12 @@ public class AppTest extends TestCase {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testPublicKeys() throws Exception {
|
||||||
|
GitHub gh = GitHub.connect();
|
||||||
|
List<GHKey> keys = gh.getMyself().getPublicKeys();
|
||||||
|
System.out.println(keys);
|
||||||
|
}
|
||||||
|
|
||||||
public void tryOrgFork() throws Exception {
|
public void tryOrgFork() throws Exception {
|
||||||
GitHub gh = GitHub.connect();
|
GitHub gh = GitHub.connect();
|
||||||
gh.getUser("kohsuke").getRepository("rubywm").forkTo(gh.getOrganization("jenkinsci"));
|
gh.getUser("kohsuke").getRepository("rubywm").forkTo(gh.getOrganization("jenkinsci"));
|
||||||
|
|||||||
Reference in New Issue
Block a user