member addition and removal

This commit is contained in:
Kohsuke Kawaguchi
2010-11-23 21:35:50 -08:00
parent 9b1dd273fd
commit ea97f8108f
4 changed files with 28 additions and 3 deletions

View File

@@ -27,10 +27,21 @@ public class GHTeam {
return id;
}
/**
* Retrieves the current members.
*/
public Set<GHUser> getMembers() throws IOException {
return org.root.retrieveWithAuth(getApiURL("/members"),JsonUsersWithDetails.class).toSet(org.root);
}
public void add(GHUser u) throws IOException {
org.root.retrieveWithAuth(getApiURL("/members?name="+u.getLogin()),null, "POST");
}
public void remove(GHUser u) throws IOException {
org.root.retrieveWithAuth(getApiURL("/members?name="+u.getLogin()),null, "DELETE");
}
private URL getApiURL(String tail) throws IOException {
return org.root.getApiURL("/organizations/"+org.getLogin()+"/teams/"+id+tail);
}

View File

@@ -108,13 +108,17 @@ public class GitHub {
}
/*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) {

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

View File

@@ -4,6 +4,7 @@ import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHTeam;
import org.kohsuke.github.GitHub;
import java.io.IOException;
@@ -13,7 +14,12 @@ import java.io.IOException;
*/
public class AppTest extends TestCase {
public void testApp() throws IOException {
System.out.println(GitHub.connect().getOrganization("HudsonLabs").getTeams().get("Core Developers").getMembers());
GitHub gitHub = GitHub.connect();
GHTeam t = gitHub.getOrganization("HudsonLabs").getTeams().get("Core Developers");
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);