Skip to content

Commit a363a04

Browse files
refactor: Apply page pattern for handling sets of external groups
1 parent f291f48 commit a363a04

File tree

4 files changed

+46
-39
lines changed

4 files changed

+46
-39
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.kohsuke.github;
22

3+
import java.util.Arrays;
34
import java.util.Iterator;
45

56
import javax.annotation.Nonnull;
@@ -11,13 +12,11 @@
1112
*/
1213
class GHExternalGroupIterable extends PagedIterable<GHExternalGroup> {
1314

14-
private static final GHExternalGroup[] GH_EXTERNAL_GROUPS = new GHExternalGroup[0];
15-
1615
private final GHOrganization owner;
1716

1817
private final GitHubRequest request;
1918

20-
private GHExternalGroups result;
19+
private GHExternalGroupPage result;
2120

2221
/**
2322
* Instantiates a new GH external groups iterable.
@@ -43,7 +42,8 @@ class GHExternalGroupIterable extends PagedIterable<GHExternalGroup> {
4342
@Override
4443
public PagedIterator<GHExternalGroup> _iterator(int pageSize) {
4544
return new PagedIterator<>(
46-
adapt(GitHubPageIterator.create(owner.root().getClient(), GHExternalGroups.class, request, pageSize)),
45+
adapt(GitHubPageIterator
46+
.create(owner.root().getClient(), GHExternalGroupPage.class, request, pageSize)),
4747
null);
4848
}
4949

@@ -54,7 +54,7 @@ public PagedIterator<GHExternalGroup> _iterator(int pageSize) {
5454
* the base
5555
* @return the iterator
5656
*/
57-
private Iterator<GHExternalGroup[]> adapt(final Iterator<GHExternalGroups> base) {
57+
private Iterator<GHExternalGroup[]> adapt(final Iterator<GHExternalGroupPage> base) {
5858
return new Iterator<GHExternalGroup[]>() {
5959
public boolean hasNext() {
6060
try {
@@ -65,12 +65,12 @@ public boolean hasNext() {
6565
}
6666

6767
public GHExternalGroup[] next() {
68-
GHExternalGroups v = base.next();
68+
GHExternalGroupPage v = base.next();
6969
if (result == null) {
7070
result = v;
7171
}
72-
v.getGroups().forEach(g -> g.wrapUp(owner));
73-
return v.getGroups().toArray(GH_EXTERNAL_GROUPS);
72+
Arrays.stream(v.getGroups()).forEach(g -> g.wrapUp(owner));
73+
return v.getGroups();
7474
}
7575
};
7676
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.kohsuke.github;
2+
3+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
4+
5+
/**
6+
* A list of external groups.
7+
*
8+
* @author Miguel Esteban Gutiérrez
9+
*/
10+
public class GHExternalGroupPage {
11+
12+
private static final GHExternalGroup[] GH_EXTERNAL_GROUPS = new GHExternalGroup[0];
13+
14+
private GHExternalGroup[] groups;
15+
16+
GHExternalGroupPage() {
17+
this(GH_EXTERNAL_GROUPS);
18+
}
19+
20+
GHExternalGroupPage(GHExternalGroup[] groups) {
21+
this.groups = groups;
22+
}
23+
24+
/**
25+
* Gets the groups.
26+
*
27+
* @return the groups
28+
*/
29+
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
30+
public GHExternalGroup[] getGroups() {
31+
return groups;
32+
}
33+
34+
}

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

Lines changed: 0 additions & 28 deletions
This file was deleted.

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,11 +441,11 @@ public GHDiscussion.Creator createDiscussion(String title) throws IOException {
441441
*/
442442
public List<GHExternalGroup> getExternalGroups() throws IOException {
443443
try {
444-
return root().createRequest()
444+
return Collections.unmodifiableList(Arrays.asList(root().createRequest()
445445
.method("GET")
446446
.withUrlPath(publicApi(EXTERNAL_GROUPS))
447-
.fetch(GHExternalGroups.class)
448-
.getGroups();
447+
.fetch(GHExternalGroupPage.class)
448+
.getGroups()));
449449
} catch (final HttpException e) {
450450
throw EnterpriseManagedSupport.forOrganization(getOrganization())
451451
.filterException(e, "Could not retrieve team external groups")
@@ -570,4 +570,5 @@ public boolean equals(Object o) {
570570
public int hashCode() {
571571
return Objects.hash(name, getUrl(), permission, slug, description, privacy);
572572
}
573+
573574
}

0 commit comments

Comments
 (0)