Skip to content

Commit

Permalink
Add get for all orgs
Browse files Browse the repository at this point in the history
  • Loading branch information
sg012265 committed Oct 30, 2017
1 parent e25ae27 commit ab24e6e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
32 changes: 32 additions & 0 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ protected GHUser getUser(GHUser orig) {
return u;
}

/**
* Gets {@link GHOrganization} specified by name.
*/
public GHOrganization getOrganization(String name) throws IOException {
GHOrganization o = orgs.get(name);
if (o==null) {
Expand All @@ -433,6 +436,35 @@ public GHOrganization getOrganization(String name) throws IOException {
return o;
}

/**
* Gets a list of all organizations.
*/
public PagedIterable<GHOrganization> getOrganizations() {
return getOrganizations(null);
}

/**
* Gets a list of all organizations starting after the organization identifier specified by 'since'.
*
* @see <a href="https://developer.github.com/v3/orgs/#parameters">List All Orgs - Parameters</a>
*/
public PagedIterable<GHOrganization> getOrganizations(final String since) {
return new PagedIterable<GHOrganization>() {
@Override
public PagedIterator<GHOrganization> _iterator(int pageSize) {
System.out.println("page size: " + pageSize);
return new PagedIterator<GHOrganization>(retrieve().with("since",since)
.asIterator("/organizations", GHOrganization[].class, pageSize)) {
@Override
protected void wrapUp(GHOrganization[] page) {
for (GHOrganization c : page)
c.wrapUp(GitHub.this);
}
};
}
};
}

/**
* Gets the repository object from 'user/reponame' string that GitHub calls as "repository name"
*
Expand Down
17 changes: 14 additions & 3 deletions src/test/java/org/kohsuke/github/GitHubTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

import com.google.common.collect.Iterables;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -155,4 +154,16 @@ public void listUsers() throws IOException {
System.out.println(u.getName());
}
}

@Test
public void getOrgs() throws IOException {
GitHub hub = GitHub.connect();
int iterations = 10;
Set<Long> orgIds = new HashSet<Long>();
for (GHOrganization org : Iterables.limit(hub.getOrganizations().withPageSize(2), iterations)) {
orgIds.add(org.getId());
System.out.println(org.getName());
}
assertThat(orgIds.size(), equalTo(iterations));
}
}

0 comments on commit ab24e6e

Please sign in to comment.