Compare commits

..

8 Commits

Author SHA1 Message Date
Kohsuke Kawaguchi
31028a31e6 [maven-release-plugin] prepare release github-api-1.3 2010-11-23 21:52:24 -08:00
Kohsuke Kawaguchi
efdabb819b shouldn't be public 2010-11-23 21:51:37 -08:00
Kohsuke Kawaguchi
e665365f11 introduced the common base type between GHOrganization and GHUser. 2010-11-23 21:51:14 -08:00
Kohsuke Kawaguchi
5159a21de1 doc improvement 2010-11-23 21:36:35 -08:00
Kohsuke Kawaguchi
ea97f8108f member addition and removal 2010-11-23 21:35:50 -08:00
Kohsuke Kawaguchi
9b1dd273fd added team support 2010-11-23 21:29:41 -08:00
Kohsuke Kawaguchi
7cd0d921cb adding support for creating repositories into an organization 2010-11-22 21:25:51 -08:00
Kohsuke Kawaguchi
959945a93d [maven-release-plugin] prepare for next development iteration 2010-04-19 09:38:25 -07:00
13 changed files with 346 additions and 61 deletions

14
pom.xml
View File

@@ -3,7 +3,7 @@
<groupId>org.kohsuke</groupId>
<artifactId>github-api</artifactId>
<packaging>jar</packaging>
<version>1.2</version>
<version>1.3</version>
<name>GitHub API for Java</name>
<url>http://kohsuke.org/github-api/</url>
<description>GitHub API for Java</description>
@@ -75,6 +75,18 @@
</licenses>
<dependencies>
<dependency>
<groupId>org.jvnet.hudson</groupId>
<artifactId>htmlunit</artifactId>
<version>2.6-hudson-2</version>
<exclusions>
<exclusion>
<!-- hides JDK DOM classes in Eclipse -->
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

View File

@@ -0,0 +1,46 @@
package org.kohsuke.github;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import java.io.IOException;
import java.util.Map;
/**
* @author Kohsuke Kawaguchi
*/
public class GHOrganization extends GHPerson {
/**
* Creates a new repository.
*
* @return
* Newly created repository.
*/
public GHRepository createRepository(String name, String description, String homepage, String team, boolean isPublic) throws IOException {
// such API doesn't exist, so fall back to HTML scraping
WebClient wc = root.createWebClient();
HtmlPage pg = (HtmlPage)wc.getPage("https://github.com/organizations/"+login+"/repositories/new");
HtmlForm f = pg.getForms().get(1);
f.getInputByName("repository[name]").setValueAttribute(name);
f.getInputByName("repository[description]").setValueAttribute(description);
f.getInputByName("repository[homepage]").setValueAttribute(homepage);
f.getSelectByName("team_id").getOptionByText(team).setSelected(true);
f.submit(f.getButtonByCaption("Create Repository"));
return root.getUser(login).getRepository(name);
// GHRepository r = new Poster(root).withCredential()
// .with("name", name).with("description", description).with("homepage", homepage)
// .with("public", isPublic ? 1 : 0).to(root.getApiURL("/organizations/"+login+"/repos/create"), JsonRepository.class).repository;
// r.root = root;
// return r;
}
/**
* Teams by their names.
*/
public Map<String,GHTeam> getTeams() throws IOException {
return root.retrieveWithAuth(root.getApiURL("/organizations/"+login+"/teams"),JsonTeams.class).toMap(this);
}
}

View File

@@ -0,0 +1,58 @@
package org.kohsuke.github;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
import static org.kohsuke.github.GitHub.*;
/**
* Common part of {@link GHUser} and {@link GHOrganization}.
*
* @author Kohsuke Kawaguchi
*/
public abstract class GHPerson {
/*package almost final*/ GitHub root;
protected String gravatar_id,login;
protected int public_gist_count,public_repo_count,following_count,id;
/**
* Repositories that this user owns.
*/
private transient Map<String,GHRepository> repositories;
/**
* Gets the repositories this user owns.
*/
public Map<String,GHRepository> getRepositories() throws IOException {
if (repositories==null) {
repositories = new TreeMap<String, GHRepository>();
URL url = new URL("http://github.com/api/v2/json/repos/show/" + login);
for (GHRepository r : MAPPER.readValue(url, JsonRepositories.class).repositories) {
r.root = root;
repositories.put(r.getName(),r);
}
}
return Collections.unmodifiableMap(repositories);
}
public GHRepository getRepository(String name) throws IOException {
return getRepositories().get(name);
}
/**
* Gravatar ID of this user, like 0cb9832a01c22c083390f3c5dcb64105
*/
public String getGravatarId() {
return gravatar_id;
}
public String getLogin() {
return login;
}
}

View File

@@ -64,6 +64,10 @@ public class GHRepository {
return root.getUser(owner);
}
protected String getOwnerName() {
return owner;
}
public boolean hasIssues() {
return has_issues;
}
@@ -142,9 +146,7 @@ public class GHRepository {
* Forks this repository.
*/
public GHRepository fork() throws IOException {
GHRepository r = new Poster(root).withCredential().to(root.getApiURL("/repos/fork/" + owner + "/" + name), JsonRepository.class).repository;
r.root = root;
return r;
return new Poster(root).withCredential().to(root.getApiURL("/repos/fork/" + owner + "/" + name), JsonRepository.class).wrap(root);
}
private void verifyMine() throws IOException {

View File

@@ -0,0 +1,62 @@
package org.kohsuke.github;
import java.io.IOException;
import java.net.URL;
import java.util.Set;
/**
* A team in GitHub organization.
*
* @author Kohsuke Kawaguchi
*/
public class GHTeam {
private String name,permission;
private int id;
protected /*final*/ GHOrganization org;
public String getName() {
return name;
}
public String getPermission() {
return permission;
}
public int getId() {
return id;
}
/**
* Retrieves the current members.
*/
public Set<GHUser> getMembers() throws IOException {
return org.root.retrieveWithAuth(getApiURL("/members"),JsonUsersWithDetails.class).toSet(org.root);
}
/**
* Adds a member to the team.
*/
public void add(GHUser u) throws IOException {
org.root.retrieveWithAuth(getApiURL("/members?name="+u.getLogin()),null, "POST");
}
/**
* Removes a member to the team.
*/
public void remove(GHUser u) throws IOException {
org.root.retrieveWithAuth(getApiURL("/members?name="+u.getLogin()),null, "DELETE");
}
public void add(GHRepository r) throws IOException {
org.root.retrieveWithAuth(getApiURL("/repositories?name="+r.getOwnerName()+'/'+r.getName()),null, "POST");
}
public void remove(GHRepository r) throws IOException {
org.root.retrieveWithAuth(getApiURL("/repositories?name="+r.getOwnerName()+'/'+r.getName()),null, "DELETE");
}
private URL getApiURL(String tail) throws IOException {
return org.root.getApiURL("/organizations/"+org.getLogin()+"/teams/"+id+tail);
}
}

View File

@@ -24,36 +24,16 @@
package org.kohsuke.github;
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import static org.kohsuke.github.GitHub.MAPPER;
/**
* Represents an user of GitHub.
*
* @author Kohsuke Kawaguchi
*/
public class GHUser {
/*package almost final*/ GitHub root;
private String gravatar_id,name,company,location,created_at,blog,login,email;
private int public_gist_count,public_repo_count,following_count,id,followers_count;
/**
* Repositories that this user owns.
*/
private transient Map<String,GHRepository> repositories;
/**
* Gravatar ID of this user, like 0cb9832a01c22c083390f3c5dcb64105
*/
public String getGravatarId() {
return gravatar_id;
}
public class GHUser extends GHPerson {
private String name,company,location,created_at,blog,email;
private int followers_count;
/**
* Gets the human-readable name of the user, like "Kohsuke Kawaguchi"
@@ -124,26 +104,6 @@ public class GHUser {
return followers_count;
}
/**
* Gets the repositories this user owns.
*/
public Map<String,GHRepository> getRepositories() throws IOException {
if (repositories==null) {
repositories = new TreeMap<String, GHRepository>();
URL url = new URL("http://github.com/api/v2/json/repos/show/" + login);
for (GHRepository r : MAPPER.readValue(url, JsonRepositories.class).repositories) {
r.root = root;
repositories.put(r.getName(),r);
}
}
return Collections.unmodifiableMap(repositories);
}
public GHRepository getRepository(String name) throws IOException {
return getRepositories().get(name);
}
/**
* Follow this user.
*/

View File

@@ -23,15 +23,22 @@
*/
package org.kohsuke.github;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import org.apache.commons.io.IOUtils;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.introspect.VisibilityChecker.Std;
import sun.misc.BASE64Encoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
@@ -48,12 +55,15 @@ import static org.codehaus.jackson.annotate.JsonAutoDetect.Visibility.NONE;
public class GitHub {
/*package*/ final String login;
/*package*/ final String token;
/*package*/ final String password;
private final Map<String,GHUser> users = new HashMap<String, GHUser>();
private final Map<String,GHOrganization> orgs = new HashMap<String, GHOrganization>();
private GitHub(String login, String apiToken) {
private GitHub(String login, String apiToken, String password) {
this.login = login;
this.token = apiToken;
this.password = password;
}
/**
@@ -68,11 +78,11 @@ public class GitHub {
} finally {
IOUtils.closeQuietly(in);
}
return new GitHub(props.getProperty("login"),props.getProperty("token"));
return new GitHub(props.getProperty("login"),props.getProperty("token"),props.getProperty("password"));
}
public static GitHub connect(String login, String apiToken) throws IOException {
return new GitHub(login,apiToken);
return new GitHub(login,apiToken,null);
}
/**
@@ -81,7 +91,7 @@ public class GitHub {
* All operations that requires authentication will fail.
*/
public static GitHub connectAnonymously() {
return new GitHub(null,null);
return new GitHub(null,null,null);
}
/*package*/ void requireCredential() {
@@ -97,6 +107,30 @@ public class GitHub {
return MAPPER.readValue(getApiURL(tail),type);
}
/*package*/ <T> T retrieveWithAuth(URL url, Class<T> type) throws IOException {
return retrieveWithAuth(url,type,"GET");
}
/*package*/ <T> T retrieveWithAuth(URL url, Class<T> type, String method) throws IOException {
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
BASE64Encoder enc = new sun.misc.BASE64Encoder();
String userpassword = login + "/token" + ":" + token;
String encodedAuthorization = enc.encode(userpassword.getBytes());
uc.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
uc.setRequestMethod(method);
try {
InputStreamReader r = new InputStreamReader(uc.getInputStream(), "UTF-8");
if (type==null) {
String data = IOUtils.toString(r);
return null;
}
return MAPPER.readValue(r,type);
} catch (IOException e) {
throw (IOException)new IOException(IOUtils.toString(uc.getErrorStream(),"UTF-8")).initCause(e);
}
}
/**
* Obtains the object that represents the named user.
*/
@@ -110,6 +144,29 @@ public class GitHub {
return u;
}
/**
* Interns the given {@link GHUser}.
*/
protected GHUser getUser(GHUser orig) throws IOException {
GHUser u = users.get(orig.getLogin());
if (u==null) {
orig.root = this;
users.put(login,orig);
return orig;
}
return u;
}
public GHOrganization getOrganization(String name) throws IOException {
GHOrganization o = orgs.get(name);
if (o==null) {
o = MAPPER.readValue(getApiURL("/organizations/"+name), JsonOrganization.class).organization;
o.root = this;
orgs.put(name,o);
}
return o;
}
/**
* Gets the {@link GHUser} that represents yourself.
*/
@@ -125,11 +182,21 @@ public class GitHub {
* Newly created repository.
*/
public GHRepository createRepository(String name, String description, String homepage, boolean isPublic) throws IOException {
GHRepository r = new Poster(this).withCredential()
return new Poster(this).withCredential()
.with("name", name).with("description", description).with("homepage", homepage)
.with("public", isPublic ? 1 : 0).to(getApiURL("/repos/create"), JsonRepository.class).repository;
r.root = this;
return r;
.with("public", isPublic ? 1 : 0).to(getApiURL("/repos/create"), JsonRepository.class).wrap(this);
}
WebClient createWebClient() throws IOException {
WebClient wc = new WebClient();
wc.setJavaScriptEnabled(false);
wc.setCssEnabled(false);
HtmlPage pg = (HtmlPage)wc.getPage("https://github.com/login");
HtmlForm f = pg.getForms().get(0);
f.getInputByName("login").setValueAttribute(login);
f.getInputByName("password").setValueAttribute(password);
f.submit();
return wc;
}

View File

@@ -0,0 +1,8 @@
package org.kohsuke.github;
/**
* @author Kohsuke Kawaguchi
*/
class JsonOrganization {
public GHOrganization organization;
}

View File

@@ -28,4 +28,9 @@ package org.kohsuke.github;
*/
class JsonRepository {
public GHRepository repository;
public GHRepository wrap(GitHub root) {
repository.root = root;
return repository;
}
}

View File

@@ -0,0 +1,21 @@
package org.kohsuke.github;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
/**
* @author Kohsuke Kawaguchi
*/
class JsonTeams {
public List<GHTeam> teams;
Map<String, GHTeam> toMap(GHOrganization org) {
Map<String, GHTeam> r = new TreeMap<String, GHTeam>();
for (GHTeam t : teams) {
t.org = org;
r.put(t.getName(),t);
}
return r;
}
}

View File

@@ -0,0 +1,20 @@
package org.kohsuke.github;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* @author Kohsuke Kawaguchi
*/
class JsonUsersWithDetails {
public List<GHUser> users;
public Set<GHUser> toSet(GitHub root) throws IOException {
Set<GHUser> r = new HashSet<GHUser>();
for (GHUser u : users)
r.add(root.getUser(u));
return r;
}
}

View File

@@ -78,11 +78,15 @@ class Poster {
* {@link Reader} that reads the response.
*/
public <T> T to(URL url, Class<T> type) throws IOException {
return to(url,type,"POST");
}
public <T> T to(URL url, Class<T> type, String method) throws IOException {
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("Content-type","application/x-www-form-urlencoded");
uc.setRequestMethod("POST");
uc.setRequestMethod(method);
StringBuilder body = new StringBuilder();
@@ -100,7 +104,10 @@ class Poster {
try {
InputStreamReader r = new InputStreamReader(uc.getInputStream(), "UTF-8");
if (type==null) return null;
if (type==null) {
String data = IOUtils.toString(r);
return null;
}
return MAPPER.readValue(r,type);
} catch (IOException e) {
throw (IOException)new IOException(IOUtils.toString(uc.getErrorStream(),"UTF-8")).initCause(e);

View File

@@ -3,6 +3,9 @@ package org.kohsuke;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.kohsuke.github.GHOrganization;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHTeam;
import org.kohsuke.github.GitHub;
import java.io.IOException;
@@ -12,10 +15,24 @@ import java.io.IOException;
*/
public class AppTest extends TestCase {
public void testApp() throws IOException {
GitHub hub = GitHub.connectAnonymously();
// hub.createRepository("test","test repository",null,true);
// hub.getUser("kohsuke").getRepository("test").delete();
GitHub gitHub = GitHub.connect();
GHOrganization labs = gitHub.getOrganization("HudsonLabs");
GHTeam t = labs.getTeams().get("Core Developers");
System.out.println(hub.getUser("kohsuke").getRepository("hudson").getCollaborators());
t.add(labs.getRepository("xyz"));
// t.add(gitHub.getMyself());
// System.out.println(t.getMembers());
// t.remove(gitHub.getMyself());
// System.out.println(t.getMembers());
// GHRepository r = GitHub.connect().getOrganization("HudsonLabs").createRepository("auto-test", "some description", "http://kohsuke.org/", "Plugin Developers", true);
// r.
// GitHub hub = GitHub.connectAnonymously();
//// hub.createRepository("test","test repository",null,true);
//// hub.getUser("kohsuke").getRepository("test").delete();
//
// System.out.println(hub.getUser("kohsuke").getRepository("hudson").getCollaborators());
}
}