Compare commits

..

8 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
8 changed files with 82 additions and 13 deletions

View File

@@ -7,7 +7,7 @@
</parent>
<artifactId>github-api</artifactId>
<version>1.24</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

@@ -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

@@ -36,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");
@@ -43,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();