Skip to content

Commit ab24e6e

Browse files
author
sg012265
committed
Add get for all orgs
1 parent e25ae27 commit ab24e6e

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

src/main/java/org/kohsuke/github/GitHub.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,9 @@ protected GHUser getUser(GHUser orig) {
424424
return u;
425425
}
426426

427+
/**
428+
* Gets {@link GHOrganization} specified by name.
429+
*/
427430
public GHOrganization getOrganization(String name) throws IOException {
428431
GHOrganization o = orgs.get(name);
429432
if (o==null) {
@@ -433,6 +436,35 @@ public GHOrganization getOrganization(String name) throws IOException {
433436
return o;
434437
}
435438

439+
/**
440+
* Gets a list of all organizations.
441+
*/
442+
public PagedIterable<GHOrganization> getOrganizations() {
443+
return getOrganizations(null);
444+
}
445+
446+
/**
447+
* Gets a list of all organizations starting after the organization identifier specified by 'since'.
448+
*
449+
* @see <a href="https://developer.github.com/v3/orgs/#parameters">List All Orgs - Parameters</a>
450+
*/
451+
public PagedIterable<GHOrganization> getOrganizations(final String since) {
452+
return new PagedIterable<GHOrganization>() {
453+
@Override
454+
public PagedIterator<GHOrganization> _iterator(int pageSize) {
455+
System.out.println("page size: " + pageSize);
456+
return new PagedIterator<GHOrganization>(retrieve().with("since",since)
457+
.asIterator("/organizations", GHOrganization[].class, pageSize)) {
458+
@Override
459+
protected void wrapUp(GHOrganization[] page) {
460+
for (GHOrganization c : page)
461+
c.wrapUp(GitHub.this);
462+
}
463+
};
464+
}
465+
};
466+
}
467+
436468
/**
437469
* Gets the repository object from 'user/reponame' string that GitHub calls as "repository name"
438470
*

src/test/java/org/kohsuke/github/GitHubTest.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
import java.io.FileNotFoundException;
44
import java.io.IOException;
55
import java.lang.reflect.Field;
6-
import java.util.Collections;
7-
import java.util.HashMap;
8-
import java.util.Map;
6+
import java.util.*;
97

108
import com.google.common.collect.Iterables;
119
import org.junit.Test;
1210

11+
import static org.hamcrest.CoreMatchers.equalTo;
1312
import static org.hamcrest.CoreMatchers.notNullValue;
1413
import static org.hamcrest.MatcherAssert.assertThat;
1514
import static org.junit.Assert.assertEquals;
@@ -155,4 +154,16 @@ public void listUsers() throws IOException {
155154
System.out.println(u.getName());
156155
}
157156
}
157+
158+
@Test
159+
public void getOrgs() throws IOException {
160+
GitHub hub = GitHub.connect();
161+
int iterations = 10;
162+
Set<Long> orgIds = new HashSet<Long>();
163+
for (GHOrganization org : Iterables.limit(hub.getOrganizations().withPageSize(2), iterations)) {
164+
orgIds.add(org.getId());
165+
System.out.println(org.getName());
166+
}
167+
assertThat(orgIds.size(), equalTo(iterations));
168+
}
158169
}

0 commit comments

Comments
 (0)