Skip to content

Commit 81ac480

Browse files
committed
Update getAppInstallations in GHMyself to return PagedIterable response.
1 parent b71aac5 commit 81ac480

File tree

5 files changed

+75
-33
lines changed

5 files changed

+75
-33
lines changed

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

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.kohsuke.github;
2+
3+
import java.net.MalformedURLException;
4+
import java.util.Iterator;
5+
6+
import javax.annotation.Nonnull;
7+
8+
/**
9+
* Iterable for GHAppInstallation listing.
10+
*/
11+
class GHAppInstallationsIterable extends PagedIterable<GHAppInstallation> {
12+
public static final String APP_INSTALLATIONS_URL = "/user/installations";
13+
private final transient GitHub root;
14+
private GHAppInstallationsPage result;
15+
16+
public GHAppInstallationsIterable(GitHub root) {
17+
this.root = root;
18+
}
19+
20+
@Nonnull
21+
@Override
22+
public PagedIterator<GHAppInstallation> _iterator(int pageSize) {
23+
try {
24+
final GitHubRequest request = root.createRequest().withUrlPath(APP_INSTALLATIONS_URL).build();
25+
return new PagedIterator<>(
26+
adapt(GitHubPageIterator.create(root.getClient(), GHAppInstallationsPage.class, request, pageSize)),
27+
null);
28+
} catch (MalformedURLException e) {
29+
throw new GHException("Malformed URL", e);
30+
}
31+
}
32+
33+
protected Iterator<GHAppInstallation[]> adapt(final Iterator<GHAppInstallationsPage> base) {
34+
return new Iterator<GHAppInstallation[]>() {
35+
public boolean hasNext() {
36+
return base.hasNext();
37+
}
38+
39+
public GHAppInstallation[] next() {
40+
GHAppInstallationsPage v = base.next();
41+
if (result == null) {
42+
result = v;
43+
}
44+
return v.getInstallations(root);
45+
}
46+
};
47+
}
48+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.kohsuke.github;
2+
3+
/**
4+
* Represents the one page of GHAppInstallations.
5+
*/
6+
class GHAppInstallationsPage {
7+
private int total_count;
8+
private GHAppInstallation[] installations;
9+
10+
public int getTotalCount() {
11+
return total_count;
12+
}
13+
14+
GHAppInstallation[] getInstallations(GitHub root) {
15+
for (GHAppInstallation installation : installations) {
16+
installation.wrap(root);
17+
}
18+
return installations;
19+
}
20+
}

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,12 @@ public GHMembership getMembership(GHOrganization o) throws IOException {
245245
* use a user-to-server OAuth access token, created for a user who has authorized your GitHub App, to access this
246246
* endpoint.
247247
*
248-
* @throws IOException
249-
* the io exception
250-
* @return list of GHAppInstallation
248+
* @return the paged iterable
251249
* @see <a href=
252250
* "https://docs.github.com/en/rest/reference/apps#list-app-installations-accessible-to-the-user-access-token">List
253251
* app installations accessible to the user access token</a>
254252
*/
255-
public List<GHAppInstallation> getAppInstallations() throws IOException {
256-
GHAppInstallations ghAppInstallations = root.createRequest()
257-
.withUrlPath("/user/installations")
258-
.fetch(GHAppInstallations.class);
259-
return ghAppInstallations.getInstallations();
253+
public PagedIterable<GHAppInstallation> getAppInstallations() {
254+
return new GHAppInstallationsIterable(root);
260255
}
261256
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,11 @@ public void testGetAppInstallations() throws Exception {
389389
// To generate test data user-to-server OAuth access token was used
390390
// For more details pls read
391391
// https://docs.github.com/en/developers/apps/building-github-apps/identifying-and-authorizing-users-for-github-apps#identifying-users-on-your-site
392-
final List<GHAppInstallation> appInstallation = gitHub.getMyself().getAppInstallations();
392+
final PagedIterable<GHAppInstallation> appInstallation = gitHub.getMyself().getAppInstallations();
393393

394-
assertThat(appInstallation, is(not(empty())));
395-
assertThat(appInstallation.size(), is(1));
396-
final GHAppInstallation ghAppInstallation = appInstallation.get(0);
394+
assertThat(appInstallation.toList(), is(not(empty())));
395+
assertThat(appInstallation.toList().size(), is(1));
396+
final GHAppInstallation ghAppInstallation = appInstallation.toList().get(0);
397397
assertThat(ghAppInstallation.getAppId(), is(122478L));
398398
assertThat(ghAppInstallation.getAccount().getLogin(), is("t0m4uk1991"));
399399
}

0 commit comments

Comments
 (0)