Skip to content

Commit dfb0a52

Browse files
authored
Merge pull request #909 from JKalash/jkalash/master
GHCheckRun.getPullRequests public
2 parents bffa78c + b177d98 commit dfb0a52

File tree

15 files changed

+1209
-17
lines changed

15 files changed

+1209
-17
lines changed

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

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

3+
import com.fasterxml.jackson.annotation.JsonProperty;
34
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
45

56
import java.io.IOException;
67
import java.net.URL;
8+
import java.util.Arrays;
9+
import java.util.Collections;
710
import java.util.Date;
11+
import java.util.List;
812

913
/**
1014
* Represents a check run.
@@ -14,6 +18,8 @@
1418
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD", "URF_UNREAD_FIELD" },
1519
justification = "JSON API")
1620
public class GHCheckRun extends GHObject {
21+
22+
@JsonProperty("repository")
1723
GHRepository owner;
1824
GitHub root;
1925

@@ -34,15 +40,32 @@ public class GHCheckRun extends GHObject {
3440

3541
GHCheckRun wrap(GHRepository owner) {
3642
this.owner = owner;
37-
this.root = owner.root;
43+
wrap(owner.root);
3844
return this;
3945
}
4046

4147
GHCheckRun wrap(GitHub root) {
4248
this.root = root;
4349
if (owner != null) {
4450
owner.wrap(root);
51+
if (pullRequests != null && pullRequests.length != 0) {
52+
for (GHPullRequest singlePull : pullRequests) {
53+
singlePull.wrap(owner);
54+
}
55+
}
56+
57+
}
58+
if (checkSuite != null) {
59+
if (owner != null) {
60+
checkSuite.wrap(owner);
61+
} else {
62+
checkSuite.wrap(root);
63+
}
4564
}
65+
if (app != null) {
66+
app.wrapUp(root);
67+
}
68+
4669
return this;
4770
}
4871

@@ -105,15 +128,22 @@ public String getHeadSha() {
105128
/**
106129
* Gets the pull requests participated in this check run.
107130
*
108-
* @return Pull requests of this check run
131+
* Note this field is only populated for events. When getting a {@link GHCheckRun} outside of an event, this is
132+
* always empty.
133+
*
134+
* @return the list of {@link GHPullRequest}s for this check run. Only populated for events.
135+
* @throws IOException
136+
* the io exception
109137
*/
110-
GHPullRequest[] getPullRequests() throws IOException {
138+
public List<GHPullRequest> getPullRequests() throws IOException {
111139
if (pullRequests != null && pullRequests.length != 0) {
112140
for (GHPullRequest singlePull : pullRequests) {
113-
singlePull.refresh();
141+
// Only refresh if we haven't do so before
142+
singlePull.refresh(singlePull.getTitle());
114143
}
144+
return Collections.unmodifiableList(Arrays.asList(pullRequests));
115145
}
116-
return pullRequests;
146+
return Collections.emptyList();
117147
}
118148

119149
/**

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

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

3+
import com.fasterxml.jackson.annotation.JsonProperty;
34
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
45

56
import java.io.IOException;
67
import java.net.URL;
8+
import java.util.Arrays;
9+
import java.util.Collections;
710
import java.util.Date;
11+
import java.util.List;
812

913
/**
1014
* Represents a check suite.
@@ -14,6 +18,8 @@
1418
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD", "NP_UNWRITTEN_FIELD", "URF_UNREAD_FIELD" },
1519
justification = "JSON API")
1620
public class GHCheckSuite extends GHObject {
21+
22+
@JsonProperty("repository")
1723
GHRepository owner;
1824
GitHub root;
1925

@@ -32,14 +38,22 @@ public class GHCheckSuite extends GHObject {
3238

3339
GHCheckSuite wrap(GHRepository owner) {
3440
this.owner = owner;
35-
this.root = owner.root;
41+
this.wrap(owner.root);
3642
return this;
3743
}
3844

3945
GHCheckSuite wrap(GitHub root) {
4046
this.root = root;
4147
if (owner != null) {
4248
owner.wrap(root);
49+
if (pullRequests != null && pullRequests.length != 0) {
50+
for (GHPullRequest singlePull : pullRequests) {
51+
singlePull.wrap(owner);
52+
}
53+
}
54+
}
55+
if (app != null) {
56+
app.wrapUp(root);
4357
}
4458
return this;
4559
}
@@ -153,15 +167,22 @@ public GHApp getApp() {
153167
/**
154168
* Gets the pull requests participated in this check suite.
155169
*
156-
* @return Pull requests
170+
* Note this field is only populated for events. When getting a {@link GHCheckSuite} outside of an event, this is
171+
* always empty.
172+
*
173+
* @return the list of {@link GHPullRequest}s for this check suite. Only populated for events.
174+
* @throws IOException
175+
* the io exception
157176
*/
158-
GHPullRequest[] getPullRequests() throws IOException {
177+
public List<GHPullRequest> getPullRequests() throws IOException {
159178
if (pullRequests != null && pullRequests.length != 0) {
160179
for (GHPullRequest singlePull : pullRequests) {
161-
singlePull.refresh();
180+
// Only refresh if we haven't do so before
181+
singlePull.refresh(singlePull.getTitle());
162182
}
183+
return Collections.unmodifiableList(Arrays.asList(pullRequests));
163184
}
164-
return pullRequests;
185+
return Collections.emptyList();
165186
}
166187

167188
/**

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

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.Rule;
44
import org.junit.Test;
55

6+
import java.io.IOException;
67
import java.text.SimpleDateFormat;
78
import java.util.Collections;
89
import java.util.TimeZone;
@@ -387,7 +388,25 @@ public void status() throws Exception {
387388
@Payload("check-run")
388389
public void checkRunEvent() throws Exception {
389390
GHEventPayload.CheckRun event = GitHub.offline()
390-
.parseEventPayload(payload.asReader(), GHEventPayload.CheckRun.class);
391+
.parseEventPayload(payload.asReader(mockGitHub::mapToMockGitHub), GHEventPayload.CheckRun.class);
392+
GHCheckRun checkRun = verifyBasicCheckRunEvent(event);
393+
assertThat("pull body not populated offline", checkRun.getPullRequests().get(0).getBody(), nullValue());
394+
assertThat("using offline github", mockGitHub.getRequestCount(), equalTo(0));
395+
396+
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl()).build();
397+
event = gitHub.parseEventPayload(payload.asReader(mockGitHub::mapToMockGitHub), GHEventPayload.CheckRun.class);
398+
checkRun = verifyBasicCheckRunEvent(event);
399+
400+
int expectedRequestCount = mockGitHub.isUseProxy() ? 3 : 2;
401+
assertThat("pull body should be populated",
402+
checkRun.getPullRequests().get(0).getBody(),
403+
equalTo("This is a pretty simple change that we need to pull into master."));
404+
assertThat("multiple getPullRequests() calls are made, the pull is populated only once",
405+
mockGitHub.getRequestCount(),
406+
equalTo(expectedRequestCount));
407+
}
408+
409+
private GHCheckRun verifyBasicCheckRunEvent(GHEventPayload.CheckRun event) throws IOException {
391410
assertThat(event.getRepository().getName(), is("Hello-World"));
392411
assertThat(event.getRepository().getOwner().getLogin(), is("Codertocat"));
393412
assertThat(event.getAction(), is("created"));
@@ -406,9 +425,9 @@ public void checkRunEvent() throws Exception {
406425
assertThat(formatter.format(checkRun.getCompletedAt()), is("2019-05-15T20:22:22Z"));
407426

408427
assertThat(checkRun.getConclusion(), is("success"));
409-
assertThat(checkRun.getUrl().toString(),
410-
is("https://api.github.com/repos/Codertocat/Hello-World/check-runs/128620228"));
411-
assertThat(checkRun.getHtmlUrl().toString(), is("https://github.com/Codertocat/Hello-World/runs/128620228"));
428+
assertThat(checkRun.getUrl().toString(), endsWith("/repos/Codertocat/Hello-World/check-runs/128620228"));
429+
assertThat(checkRun.getHtmlUrl().toString(),
430+
endsWith("https://github.com/Codertocat/Hello-World/runs/128620228"));
412431
assertThat(checkRun.getDetailsUrl().toString(), is("https://octocoders.io"));
413432
assertThat(checkRun.getApp().getId(), is(29310L));
414433
assertThat(checkRun.getCheckSuite().getId(), is(118578147L));
@@ -417,18 +436,41 @@ public void checkRunEvent() throws Exception {
417436
assertThat(checkRun.getOutput().getText(), nullValue());
418437
assertThat(checkRun.getOutput().getAnnotationsCount(), is(0));
419438
assertThat(checkRun.getOutput().getAnnotationsUrl().toString(),
420-
is("https://api.github.com/repos/Codertocat/Hello-World/check-runs/128620228/annotations"));
439+
endsWith("/repos/Codertocat/Hello-World/check-runs/128620228/annotations"));
421440

422441
// Checks the deserialization of sender
423442
assertThat(event.getSender().getId(), is(21031067L));
443+
444+
assertThat(checkRun.getPullRequests(), notNullValue());
445+
assertThat(checkRun.getPullRequests().size(), equalTo(1));
446+
assertThat(checkRun.getPullRequests().get(0).getNumber(), equalTo(2));
447+
return checkRun;
424448
}
425449

426450
@Test
427451
@Payload("check-suite")
428452
public void checkSuiteEvent() throws Exception {
429453
GHEventPayload.CheckSuite event = GitHub.offline()
430-
.parseEventPayload(payload.asReader(), GHEventPayload.CheckSuite.class);
454+
.parseEventPayload(payload.asReader(mockGitHub::mapToMockGitHub), GHEventPayload.CheckSuite.class);
455+
GHCheckSuite checkSuite = verifyBasicCheckSuiteEvent(event);
456+
assertThat("pull body not populated offline", checkSuite.getPullRequests().get(0).getBody(), nullValue());
457+
assertThat("using offline github", mockGitHub.getRequestCount(), equalTo(0));
431458

459+
gitHub = getGitHubBuilder().withEndpoint(mockGitHub.apiServer().baseUrl()).build();
460+
event = gitHub.parseEventPayload(payload.asReader(mockGitHub::mapToMockGitHub),
461+
GHEventPayload.CheckSuite.class);
462+
checkSuite = verifyBasicCheckSuiteEvent(event);
463+
464+
int expectedRequestCount = mockGitHub.isUseProxy() ? 3 : 2;
465+
assertThat("pull body should be populated",
466+
checkSuite.getPullRequests().get(0).getBody(),
467+
equalTo("This is a pretty simple change that we need to pull into master."));
468+
assertThat("multiple getPullRequests() calls are made, the pull is populated only once",
469+
mockGitHub.getRequestCount(),
470+
lessThanOrEqualTo(expectedRequestCount));
471+
}
472+
473+
private GHCheckSuite verifyBasicCheckSuiteEvent(GHEventPayload.CheckSuite event) throws IOException {
432474
assertThat(event.getRepository().getName(), is("Hello-World"));
433475
assertThat(event.getRepository().getOwner().getLogin(), is("Codertocat"));
434476
assertThat(event.getAction(), is("completed"));
@@ -445,7 +487,7 @@ public void checkSuiteEvent() throws Exception {
445487
assertThat(checkSuite.getAfter(), is("ec26c3e57ca3a959ca5aad62de7213c562f8c821"));
446488
assertThat(checkSuite.getLatestCheckRunsCount(), is(1));
447489
assertThat(checkSuite.getCheckRunsUrl().toString(),
448-
is("https://api.github.com/repos/Codertocat/Hello-World/check-suites/118578147/check-runs"));
490+
endsWith("/repos/Codertocat/Hello-World/check-suites/118578147/check-runs"));
449491
assertThat(checkSuite.getHeadCommit().getMessage(), is("Update README.md"));
450492
assertThat(checkSuite.getHeadCommit().getId(), is("ec26c3e57ca3a959ca5aad62de7213c562f8c821"));
451493
assertThat(checkSuite.getHeadCommit().getTreeId(), is("31b122c26a97cf9af023e9ddab94a82c6e77b0ea"));
@@ -457,6 +499,11 @@ public void checkSuiteEvent() throws Exception {
457499
assertThat(formatter.format(checkSuite.getHeadCommit().getTimestamp()), is("2019-05-15T15:20:30Z"));
458500

459501
assertThat(checkSuite.getApp().getId(), is(29310L));
502+
503+
assertThat(checkSuite.getPullRequests(), notNullValue());
504+
assertThat(checkSuite.getPullRequests().size(), equalTo(1));
505+
assertThat(checkSuite.getPullRequests().get(0).getNumber(), equalTo(2));
506+
return checkSuite;
460507
}
461508

462509
@Test

0 commit comments

Comments
 (0)