Compare commits

...

8 Commits

Author SHA1 Message Date
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
8 changed files with 104 additions and 13 deletions

2
README
View File

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

View File

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

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

@@ -27,6 +27,16 @@ public class GHMyself extends GHUser {
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");
// root.retrieveWithAuth3() // root.retrieveWithAuth3()

View File

@@ -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.
*/ */

View File

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

View File

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

View File

@@ -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;
@@ -43,6 +44,15 @@ public class AppTest extends TestCase {
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();
GHMyself me = hub.getMyself(); GHMyself me = hub.getMyself();
@@ -56,6 +66,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"));