Skip to content

Commit

Permalink
refactor: Apply page pattern for consuming collections of external gr…
Browse files Browse the repository at this point in the history
…oups
  • Loading branch information
mestebangutierrez committed Jun 14, 2024
1 parent 20dd034 commit 8fef802
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 39 deletions.
16 changes: 8 additions & 8 deletions src/main/java/org/kohsuke/github/GHExternalGroupIterable.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.kohsuke.github;

import java.util.Arrays;
import java.util.Iterator;

import javax.annotation.Nonnull;
Expand All @@ -11,13 +12,11 @@
*/
class GHExternalGroupIterable extends PagedIterable<GHExternalGroup> {

private static final GHExternalGroup[] GH_EXTERNAL_GROUPS = new GHExternalGroup[0];

private final GHOrganization owner;

private final GitHubRequest request;

private GHExternalGroups result;
private GHExternalGroupPage result;

/**
* Instantiates a new GH external groups iterable.
Expand All @@ -43,7 +42,8 @@ class GHExternalGroupIterable extends PagedIterable<GHExternalGroup> {
@Override
public PagedIterator<GHExternalGroup> _iterator(int pageSize) {
return new PagedIterator<>(
adapt(GitHubPageIterator.create(owner.root().getClient(), GHExternalGroups.class, request, pageSize)),
adapt(GitHubPageIterator
.create(owner.root().getClient(), GHExternalGroupPage.class, request, pageSize)),
null);
}

Expand All @@ -54,7 +54,7 @@ public PagedIterator<GHExternalGroup> _iterator(int pageSize) {
* the base
* @return the iterator
*/
private Iterator<GHExternalGroup[]> adapt(final Iterator<GHExternalGroups> base) {
private Iterator<GHExternalGroup[]> adapt(final Iterator<GHExternalGroupPage> base) {
return new Iterator<GHExternalGroup[]>() {
public boolean hasNext() {
try {
Expand All @@ -65,12 +65,12 @@ public boolean hasNext() {
}

public GHExternalGroup[] next() {
GHExternalGroups v = base.next();
GHExternalGroupPage v = base.next();
if (result == null) {
result = v;
}
v.getGroups().forEach(g -> g.wrapUp(owner));
return v.getGroups().toArray(GH_EXTERNAL_GROUPS);
Arrays.stream(v.getGroups()).forEach(g -> g.wrapUp(owner));
return v.getGroups();
}
};
}
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/org/kohsuke/github/GHExternalGroupPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.kohsuke.github;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

/**
* A list of external groups.
*
* @author Miguel Esteban Gutiérrez
*/
public class GHExternalGroupPage {

private static final GHExternalGroup[] GH_EXTERNAL_GROUPS = new GHExternalGroup[0];

private GHExternalGroup[] groups;

GHExternalGroupPage() {
this(GH_EXTERNAL_GROUPS);
}

GHExternalGroupPage(GHExternalGroup[] groups) {
this.groups = groups;
}

/**
* Gets the groups.
*
* @return the groups
*/
@SuppressFBWarnings(value = { "EI_EXPOSE_REP" }, justification = "Expected behavior")
public GHExternalGroup[] getGroups() {
return groups;
}

}
28 changes: 0 additions & 28 deletions src/main/java/org/kohsuke/github/GHExternalGroups.java

This file was deleted.

7 changes: 4 additions & 3 deletions src/main/java/org/kohsuke/github/GHTeam.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,11 +441,11 @@ public GHDiscussion.Creator createDiscussion(String title) throws IOException {
*/
public List<GHExternalGroup> getExternalGroups() throws IOException {
try {
return root().createRequest()
return Collections.unmodifiableList(Arrays.asList(root().createRequest()
.method("GET")
.withUrlPath(publicApi(EXTERNAL_GROUPS))
.fetch(GHExternalGroups.class)
.getGroups();
.fetch(GHExternalGroupPage.class)
.getGroups()));
} catch (final HttpException e) {
throw EnterpriseManagedSupport.forOrganization(getOrganization())
.filterException(e, "Could not retrieve team external groups")
Expand Down Expand Up @@ -570,4 +570,5 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(name, getUrl(), permission, slug, description, privacy);
}

}

0 comments on commit 8fef802

Please sign in to comment.