33import org .junit .Rule ;
44import org .junit .Test ;
55
6+ import java .io .IOException ;
67import java .text .SimpleDateFormat ;
78import java .util .Collections ;
89import 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