Compare commits

...

7 Commits

Author SHA1 Message Date
Surya Gaddipati
7e49946bed [maven-release-plugin] prepare release github-api-1.57 2014-08-19 13:29:37 -05:00
Surya Gaddipati
1e81ab1017 Merge pull request #112 from lucamilanesio/master
Get all orgs/teams/permissions in a single GitHub API call
2014-08-19 12:57:39 -05:00
Luca Milanesio
0b92fa5615 Get all orgs/teams/permissions in a single GitHub API call
Exposes a new API call at GitHub root level to build the complete
set of organisations and teams that current user belongs to.

This change allows to massively reduce the number of calls to GitHub 
especially for people that belongs to multiple organisations with 
lots of teams and members.

Signed-off-by: Luca Milanesio <luca.milanesio@gmail.com>
2014-07-24 23:42:37 +01:00
Kohsuke Kawaguchi
590f7ba511 Merge pull request #106 from lucamilanesio/master
Implement pagination on list of private+public repos of a user.
2014-07-04 19:13:04 -07:00
Luca Milanesio
449909b0e8 Implement pagination on list of private+public repos of a user.
The paginated version of listRepositories() was 
missing at GHMyself: as side-effect of this bug
when requesting a paginated list of repositories
the ones privately owned by a user were not shown.

This was caused by the missing override of the 
listRepositories(final int pageSize) at GHMyself
that caused the GHPerson implementation to invoked.

The GHPerson version uses the /users/:org/repos?per_page=x
URL which *works fine* for organisations but unfortunately
*does not return* private repositories for users.

IMHO GitHub API are quite inconsistent form this 
point of view, but they are documented in this way
so that work (inconsistently) as designed.
2014-07-03 10:05:17 +01:00
Kohsuke Kawaguchi
40780525f8 Bumping up the version in the hope of fixing site plugin problem 2014-07-02 21:39:13 -07:00
Kohsuke Kawaguchi
0e3707d1c3 [maven-release-plugin] prepare for next development iteration 2014-07-02 21:28:38 -07:00
5 changed files with 83 additions and 15 deletions

View File

@@ -3,11 +3,11 @@
<parent> <parent>
<groupId>org.kohsuke</groupId> <groupId>org.kohsuke</groupId>
<artifactId>pom</artifactId> <artifactId>pom</artifactId>
<version>8</version> <version>9</version>
</parent> </parent>
<artifactId>github-api</artifactId> <artifactId>github-api</artifactId>
<version>1.56</version> <version>1.57</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>
@@ -16,7 +16,7 @@
<connection>scm:git:git@github.com/kohsuke/${project.artifactId}.git</connection> <connection>scm:git:git@github.com/kohsuke/${project.artifactId}.git</connection>
<developerConnection>scm:git:ssh://git@github.com/kohsuke/${project.artifactId}.git</developerConnection> <developerConnection>scm:git:ssh://git@github.com/kohsuke/${project.artifactId}.git</developerConnection>
<url>http://${project.artifactId}.kohsuke.org/</url> <url>http://${project.artifactId}.kohsuke.org/</url>
<tag>github-api-1.56</tag> <tag>HEAD</tag>
</scm> </scm>
<distributionManagement> <distributionManagement>

View File

@@ -101,12 +101,24 @@ public class GHMyself extends GHUser {
* Lists up all repositories this user owns (public and private). * Lists up all repositories this user owns (public and private).
* *
* Unlike {@link #getAllRepositories()}, this does not wait until all the repositories are returned. * Unlike {@link #getAllRepositories()}, this does not wait until all the repositories are returned.
* Repositories are returned by GitHub API with a 30 items per page.
*/ */
@Override @Override
public PagedIterable<GHRepository> listRepositories() { public PagedIterable<GHRepository> listRepositories() {
return listRepositories(30);
}
/**
* Lists up all the repositories this user owns (public and private) using the specified page size.
*
* @param pageSize size for each page of items returned by GitHub. Maximum page size is 100.
*
* Unlike {@link #getRepositories()}, this does not wait until all the repositories are returned.
*/
public PagedIterable<GHRepository> listRepositories(final int pageSize) {
return new PagedIterable<GHRepository>() { return new PagedIterable<GHRepository>() {
public PagedIterator<GHRepository> iterator() { public PagedIterator<GHRepository> iterator() {
return new PagedIterator<GHRepository>(root.retrieve().asIterator("/user/repos", GHRepository[].class)) { return new PagedIterator<GHRepository>(root.retrieve().asIterator("/user/repos?per_page=" + pageSize, GHRepository[].class)) {
@Override @Override
protected void wrapUp(GHRepository[] page) { protected void wrapUp(GHRepository[] page) {
for (GHRepository c : page) for (GHRepository c : page)

View File

@@ -15,6 +15,7 @@ import java.util.TreeMap;
public class GHTeam { public class GHTeam {
private String name,permission; private String name,permission;
private int id; private int id;
private GHOrganization organization; // populated by GET /user/teams where Teams+Orgs are returned together
protected /*final*/ GHOrganization org; protected /*final*/ GHOrganization org;
@@ -23,6 +24,11 @@ public class GHTeam {
return this; return this;
} }
/*package*/ GHTeam wrapUp(GitHub root) { // auto-wrapUp when organization is known from GET /user/teams
this.organization.wrapUp(root);
return wrapUp(organization);
}
/*package*/ static GHTeam[] wrapUp(GHTeam[] teams, GHOrganization owner) { /*package*/ static GHTeam[] wrapUp(GHTeam[] teams, GHOrganization owner) {
for (GHTeam t : teams) { for (GHTeam t : teams) {
t.wrapUp(owner); t.wrapUp(owner);
@@ -95,4 +101,8 @@ public class GHTeam {
private String api(String tail) { private String api(String tail) {
return "/teams/"+id+tail; return "/teams/"+id+tail;
} }
public GHOrganization getOrganization() {
return org;
}
} }

View File

@@ -23,7 +23,8 @@
*/ */
package org.kohsuke.github; package org.kohsuke.github;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.*; import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
@@ -38,9 +39,11 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.Set;
import java.util.TimeZone; import java.util.TimeZone;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
@@ -328,6 +331,28 @@ public class GitHub {
return r; return r;
} }
/**
* Gets complete map of organizations/teams that current user belongs to.
*
* Leverages the new GitHub API /user/teams made available recently to
* get in a single call the complete set of organizations, teams and permissions
* in a single call.
*/
public Map<String, Set<GHTeam>> getMyTeams() throws IOException {
Map<String, Set<GHTeam>> allMyTeams = new HashMap<String, Set<GHTeam>>();
for (GHTeam team : retrieve().to("/user/teams", GHTeam[].class)) {
team.wrapUp(this);
String orgLogin = team.getOrganization().getLogin();
Set<GHTeam> teamsPerOrg = allMyTeams.get(orgLogin);
if (teamsPerOrg == null) {
teamsPerOrg = new HashSet<GHTeam>();
}
teamsPerOrg.add(team);
allMyTeams.put(orgLogin, teamsPerOrg);
}
return allMyTeams;
}
/** /**
* Public events visible to you. Equivalent of what's displayed on https://github.com/ * Public events visible to you. Equivalent of what's displayed on https://github.com/
*/ */

View File

@@ -3,19 +3,13 @@ package org.kohsuke.github;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Assume; import org.junit.Assume;
import org.junit.Test; import org.junit.Test;
import org.kohsuke.github.GHCommit.File; import org.kohsuke.github.GHCommit.File;
@@ -24,8 +18,6 @@ import org.kohsuke.github.GHOrganization.Permission;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import java.util.Date;
/** /**
* Unit test for simple App. * Unit test for simple App.
*/ */
@@ -170,6 +162,35 @@ public class AppTest extends AbstractGitHubApiTestBase {
System.out.println(org); System.out.println(org);
} }
@Test
public void testMyTeamsContainsAllMyOrganizations() throws IOException {
Map<String, Set<GHTeam>> teams = gitHub.getMyTeams();
Map<String, GHOrganization> myOrganizations = gitHub.getMyOrganizations();
assertEquals(teams.keySet(), myOrganizations.keySet());
}
@Test
public void testMyTeamsShouldIncludeMyself() throws IOException {
Map<String, Set<GHTeam>> teams = gitHub.getMyTeams();
for (Entry<String, Set<GHTeam>> teamsPerOrg : teams.entrySet()) {
String organizationName = teamsPerOrg.getKey();
for (GHTeam team : teamsPerOrg.getValue()) {
String teamName = team.getName();
assertTrue("Team " + teamName + " in organization " + organizationName
+ " does not contain myself",
shouldBelongToTeam(organizationName, teamName));
}
}
}
private boolean shouldBelongToTeam(String organizationName, String teamName) throws IOException {
GHOrganization org = gitHub.getOrganization(organizationName);
assertNotNull(org);
GHTeam team = org.getTeamByName(teamName);
assertNotNull(team);
return team.hasMember(gitHub.getMyself());
}
@Test @Test
public void testFetchPullRequest() throws Exception { public void testFetchPullRequest() throws Exception {
GHRepository r = gitHub.getOrganization("jenkinsci").getRepository("jenkins"); GHRepository r = gitHub.getOrganization("jenkinsci").getRepository("jenkins");