diff --git a/src/main/java/org/kohsuke/github/GHTeam.java b/src/main/java/org/kohsuke/github/GHTeam.java index c578130fa6..54b416edd8 100644 --- a/src/main/java/org/kohsuke/github/GHTeam.java +++ b/src/main/java/org/kohsuke/github/GHTeam.java @@ -15,6 +15,7 @@ public class GHTeam { private String name,permission; private int id; + private GHOrganization organization; // populated by GET /user/teams where Teams+Orgs are returned together protected /*final*/ GHOrganization org; @@ -23,6 +24,11 @@ public class GHTeam { 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) { for (GHTeam t : teams) { t.wrapUp(owner); @@ -95,4 +101,8 @@ public void remove(GHRepository r) throws IOException { private String api(String tail) { return "/teams/"+id+tail; } + + public GHOrganization getOrganization() { + return org; + } } diff --git a/src/main/java/org/kohsuke/github/GitHub.java b/src/main/java/org/kohsuke/github/GitHub.java index 2488c982a2..136daf0df7 100644 --- a/src/main/java/org/kohsuke/github/GitHub.java +++ b/src/main/java/org/kohsuke/github/GitHub.java @@ -23,7 +23,8 @@ */ 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.FileInputStream; @@ -38,9 +39,11 @@ import java.util.Collection; import java.util.Date; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Properties; +import java.util.Set; import java.util.TimeZone; import org.apache.commons.codec.binary.Base64; @@ -328,6 +331,28 @@ public Map getMyOrganizations() throws IOException { 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> getMyTeams() throws IOException { + Map> allMyTeams = new HashMap>(); + for (GHTeam team : retrieve().to("/user/teams", GHTeam[].class)) { + team.wrapUp(this); + String orgLogin = team.getOrganization().getLogin(); + Set teamsPerOrg = allMyTeams.get(orgLogin); + if (teamsPerOrg == null) { + teamsPerOrg = new HashSet(); + } + teamsPerOrg.add(team); + allMyTeams.put(orgLogin, teamsPerOrg); + } + return allMyTeams; + } + /** * Public events visible to you. Equivalent of what's displayed on https://github.com/ */ diff --git a/src/test/java/org/kohsuke/github/AppTest.java b/src/test/java/org/kohsuke/github/AppTest.java index 0866a8e93a..07b386314b 100644 --- a/src/test/java/org/kohsuke/github/AppTest.java +++ b/src/test/java/org/kohsuke/github/AppTest.java @@ -3,19 +3,13 @@ import java.io.IOException; import java.net.URL; import java.util.ArrayList; -import java.util.Collection; +import java.util.Date; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; 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.Test; import org.kohsuke.github.GHCommit.File; @@ -24,8 +18,6 @@ import com.google.common.base.Predicate; import com.google.common.collect.Iterables; -import java.util.Date; - /** * Unit test for simple App. */ @@ -170,6 +162,35 @@ public void testMyOrganizations() throws IOException { System.out.println(org); } + @Test + public void testMyTeamsContainsAllMyOrganizations() throws IOException { + Map> teams = gitHub.getMyTeams(); + Map myOrganizations = gitHub.getMyOrganizations(); + assertEquals(teams.keySet(), myOrganizations.keySet()); + } + + @Test + public void testMyTeamsShouldIncludeMyself() throws IOException { + Map> teams = gitHub.getMyTeams(); + for (Entry> 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 public void testFetchPullRequest() throws Exception { GHRepository r = gitHub.getOrganization("jenkinsci").getRepository("jenkins");