Skip to content

Commit 5430f3d

Browse files
committed
Merge pull request hub4j#391
2 parents 41c028d + 43075fa commit 5430f3d

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import java.util.Date;
4949
import java.util.HashMap;
5050
import java.util.HashSet;
51-
import java.util.Hashtable;
5251
import java.util.List;
5352
import java.util.Map;
5453
import java.util.Set;
@@ -424,6 +423,9 @@ protected GHUser getUser(GHUser orig) {
424423
return u;
425424
}
426425

426+
/**
427+
* Gets {@link GHOrganization} specified by name.
428+
*/
427429
public GHOrganization getOrganization(String name) throws IOException {
428430
GHOrganization o = orgs.get(name);
429431
if (o==null) {
@@ -433,6 +435,35 @@ public GHOrganization getOrganization(String name) throws IOException {
433435
return o;
434436
}
435437

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

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
import java.lang.reflect.Field;
66
import java.util.Collections;
77
import java.util.HashMap;
8+
import java.util.HashSet;
89
import java.util.Map;
10+
import java.util.Set;
911

1012
import com.google.common.collect.Iterables;
1113
import org.junit.Test;
1214

15+
import static org.hamcrest.CoreMatchers.equalTo;
1316
import static org.hamcrest.CoreMatchers.notNullValue;
1417
import static org.hamcrest.MatcherAssert.assertThat;
1518
import static org.junit.Assert.assertEquals;
@@ -155,4 +158,16 @@ public void listUsers() throws IOException {
155158
System.out.println(u.getName());
156159
}
157160
}
161+
162+
@Test
163+
public void getOrgs() throws IOException {
164+
GitHub hub = GitHub.connect();
165+
int iterations = 10;
166+
Set<Long> orgIds = new HashSet<Long>();
167+
for (GHOrganization org : Iterables.limit(hub.listOrganizations().withPageSize(2), iterations)) {
168+
orgIds.add(org.getId());
169+
System.out.println(org.getName());
170+
}
171+
assertThat(orgIds.size(), equalTo(iterations));
172+
}
158173
}

0 commit comments

Comments
 (0)