mirror of
https://github.com/jlengrand/github-api.git
synced 2026-03-10 08:21:21 +00:00
added team support
This commit is contained in:
@@ -5,6 +5,7 @@ import com.gargoylesoftware.htmlunit.html.HtmlForm;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Kohsuke Kawaguchi
|
||||
@@ -15,6 +16,10 @@ public class GHOrganization {
|
||||
private String gravatar_id,login;
|
||||
private int public_repo_count, public_gist_count, following_count, id;
|
||||
|
||||
public String getLogin() {
|
||||
return login;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new repository.
|
||||
*
|
||||
@@ -40,4 +45,12 @@ public class GHOrganization {
|
||||
// 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
37
src/main/java/org/kohsuke/github/GHTeam.java
Normal file
37
src/main/java/org/kohsuke/github/GHTeam.java
Normal file
@@ -0,0 +1,37 @@
|
||||
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;
|
||||
}
|
||||
|
||||
public Set<GHUser> getMembers() throws IOException {
|
||||
return org.root.retrieveWithAuth(getApiURL("/members"),JsonUsersWithDetails.class).toSet(org.root);
|
||||
}
|
||||
|
||||
private URL getApiURL(String tail) throws IOException {
|
||||
return org.root.getApiURL("/organizations/"+org.getLogin()+"/teams/"+id+tail);
|
||||
}
|
||||
}
|
||||
@@ -30,11 +30,14 @@ 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;
|
||||
@@ -104,6 +107,26 @@ public class GitHub {
|
||||
return MAPPER.readValue(getApiURL(tail),type);
|
||||
}
|
||||
|
||||
/*package*/ <T> T retrieveWithAuth(URL url, Class<T> type) 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);
|
||||
|
||||
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.
|
||||
*/
|
||||
@@ -117,6 +140,19 @@ 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) {
|
||||
|
||||
21
src/main/java/org/kohsuke/github/JsonTeams.java
Normal file
21
src/main/java/org/kohsuke/github/JsonTeams.java
Normal 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;
|
||||
}
|
||||
}
|
||||
20
src/main/java/org/kohsuke/github/JsonUsersWithDetails.java
Normal file
20
src/main/java/org/kohsuke/github/JsonUsersWithDetails.java
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -100,7 +100,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);
|
||||
|
||||
@@ -3,6 +3,7 @@ package org.kohsuke;
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
import org.kohsuke.github.GHRepository;
|
||||
import org.kohsuke.github.GitHub;
|
||||
|
||||
import java.io.IOException;
|
||||
@@ -12,10 +13,15 @@ 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();
|
||||
System.out.println(GitHub.connect().getOrganization("HudsonLabs").getTeams().get("Core Developers").getMembers());
|
||||
|
||||
System.out.println(hub.getUser("kohsuke").getRepository("hudson").getCollaborators());
|
||||
// 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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user