Compare commits

...

12 Commits

Author SHA1 Message Date
Kohsuke Kawaguchi
3a9ade667a [maven-release-plugin] prepare release github-api-1.26 2012-06-04 10:09:10 -07:00
Kohsuke Kawaguchi
057c32d410 added API to retrieve rate limit. 2012-06-04 10:08:31 -07:00
Kohsuke Kawaguchi
33657c9c92 made the authentication header optional.
This was needed now that GHPerson.getRepository() always try with authentication on, and it'll break if logged in anonymously
2012-06-04 10:05:03 -07:00
Kohsuke Kawaguchi
4c199256a5 [maven-release-plugin] prepare for next development iteration 2012-05-21 22:42:24 -07:00
Kohsuke Kawaguchi
9e62776905 [maven-release-plugin] prepare release github-api-1.25 2012-05-21 22:42:19 -07:00
Kohsuke Kawaguchi
9ba74b945d added permission check methods 2012-05-21 22:40:43 -07:00
Kohsuke Kawaguchi
66656ce612 when authenticated, repository returns additional information, so always send in a credential when one is available 2012-05-21 22:37:48 -07:00
Kohsuke Kawaguchi
73a20ad829 [maven-release-plugin] prepare for next development iteration 2012-05-21 22:16:52 -07:00
Kohsuke Kawaguchi
6fc9a546cb [maven-release-plugin] prepare release github-api-1.24 2012-05-21 22:16:47 -07:00
Kohsuke Kawaguchi
3f6c225948 updated URL 2012-05-21 22:16:09 -07:00
Kohsuke Kawaguchi
c7e9650a39 added code to list up public keys 2012-05-21 22:15:34 -07:00
Kohsuke Kawaguchi
fd28a36b74 [maven-release-plugin] prepare for next development iteration 2012-04-24 17:22:19 -07:00
11 changed files with 148 additions and 14 deletions

2
README
View File

@@ -1,3 +1,3 @@
Java API for GitHub
See http://kohsuke.org/github-api/ for more details
See http://github-api.kohsuke.org/ for more details

View File

@@ -7,7 +7,7 @@
</parent>
<artifactId>github-api</artifactId>
<version>1.23</version>
<version>1.26</version>
<name>GitHub API for Java</name>
<url>http://github-api.kohsuke.org/</url>
<description>GitHub API for Java</description>

View 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();
}
}

View File

@@ -26,6 +26,16 @@ public class GHMyself extends GHUser {
String[] addresses = root.retrieveWithAuth3("/user/emails",String[].class);
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 {
//// new Poster(root,ApiVersion.V3).withCredential().to("/user/emails");

View File

@@ -43,15 +43,6 @@ public class GHOrganization extends GHPerson {
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.
*/

View File

@@ -87,7 +87,7 @@ public abstract class GHPerson {
*/
public GHRepository getRepository(String name) throws IOException {
try {
return root.retrieve3("/repos/" + login + '/' + name, GHRepository.class).wrap(root);
return root.retrieveWithAuth3("/repos/" + login + '/' + name, GHRepository.class).wrap(root);
} catch (FileNotFoundException e) {
return null;
}

View 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;
}
}

View File

@@ -72,9 +72,16 @@ public class GHRepository {
private String created_at, pushed_at;
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 GHRepoPermission permissions;
private static class GHRepoPermission {
boolean pull,push,admin;
}
public String getDescription() {
return description;
}
@@ -113,6 +120,25 @@ public class GHRepository {
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 {
return root.getUser(owner.login); // because 'owner' isn't fully populated
}

View File

@@ -288,7 +288,10 @@ public class GitHub {
private HttpURLConnection setupConnection(String method, boolean withAuth, URL url) throws IOException {
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.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.
*/

View File

@@ -0,0 +1,8 @@
package org.kohsuke.github;
/**
* @author Kohsuke Kawaguchi
*/
class JsonRateLimit {
GHRateLimit rate;
}

View File

@@ -10,6 +10,7 @@ import org.kohsuke.github.GHEventPayload;
import org.kohsuke.github.GHHook;
import org.kohsuke.github.GHBranch;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHKey;
import org.kohsuke.github.GHMyself;
import org.kohsuke.github.GHOrganization;
import org.kohsuke.github.GHOrganization.Permission;
@@ -35,6 +36,10 @@ public class AppTest extends TestCase {
assertFalse(GitHub.connect("totally","bogus").isCredentialValid());
}
public void testRateLimit() throws IOException {
System.out.println(GitHub.connect().getRateLimit());
}
public void testFetchPullRequest() throws Exception {
GitHub gh = GitHub.connect();
GHRepository r = gh.getOrganization("jenkinsci").getRepository("jenkins");
@@ -42,6 +47,15 @@ public class AppTest extends TestCase {
r.getPullRequest(1);
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 {
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 {
GitHub gh = GitHub.connect();
gh.getUser("kohsuke").getRepository("rubywm").forkTo(gh.getOrganization("jenkinsci"));