From f35f09a146fb2f75c4f2bf3f5d6ec0f02d00e06a Mon Sep 17 00:00:00 2001 From: Tim Jacomb <21194782+timja@users.noreply.github.com> Date: Sat, 15 Jan 2022 11:16:43 +0000 Subject: [PATCH] Head branch is not sent for forks (#237) --- .../checks/github/CheckRunGHEventSubscriber.java | 8 +++++--- .../github/CheckRunGHEventSubscriberTest.java | 15 ++++++--------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/checks/github/CheckRunGHEventSubscriber.java b/src/main/java/io/jenkins/plugins/checks/github/CheckRunGHEventSubscriber.java index 3b8172e0..4ca92fe6 100644 --- a/src/main/java/io/jenkins/plugins/checks/github/CheckRunGHEventSubscriber.java +++ b/src/main/java/io/jenkins/plugins/checks/github/CheckRunGHEventSubscriber.java @@ -76,12 +76,14 @@ protected Set events() { @SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE", justification = "Return value of parseEventPayload method is safe to cast.") protected void onEvent(final GHSubscriberEvent event) { final String payload = event.getPayload(); - final String branchName; - try { GHEventPayload.CheckRun checkRun = GitHub.offline().parseEventPayload(new StringReader(payload), GHEventPayload.CheckRun.class); JSONObject payloadJSON = new JSONObject(payload); - branchName = payloadJSON.getJSONObject("check_run").getJSONObject("check_suite").getString("head_branch"); + JSONObject checkSuite = payloadJSON.getJSONObject("check_run").getJSONObject("check_suite"); + String branchName = null; + if (checkSuite.has("head_branch")) { + branchName = checkSuite.getString("head_branch"); + } if (!RERUN_ACTION.equals(checkRun.getAction())) { LOGGER.log(Level.FINE, diff --git a/src/test/java/io/jenkins/plugins/checks/github/CheckRunGHEventSubscriberTest.java b/src/test/java/io/jenkins/plugins/checks/github/CheckRunGHEventSubscriberTest.java index 10752e2c..f33a9cfe 100644 --- a/src/test/java/io/jenkins/plugins/checks/github/CheckRunGHEventSubscriberTest.java +++ b/src/test/java/io/jenkins/plugins/checks/github/CheckRunGHEventSubscriberTest.java @@ -75,7 +75,7 @@ void shouldProcessCheckRunEventWithRerequestedAction() throws IOException { @Test void shouldThrowExceptionWhenCheckSuitesMissingFromPayload() throws IOException { assertThatThrownBy( - () -> { + () -> { new CheckRunGHEventSubscriber(mock(JenkinsFacade.class), mock(SCMFacade.class)) .onEvent(createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_PR_MISSING_CHECKSUITE)); }) @@ -84,14 +84,11 @@ void shouldThrowExceptionWhenCheckSuitesMissingFromPayload() throws IOException } @Test - void shouldThrowExceptionWhenHeadBranchMissingFromPayload() throws IOException { - assertThatThrownBy( - () -> { - new CheckRunGHEventSubscriber(mock(JenkinsFacade.class), mock(SCMFacade.class)) - .onEvent(createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_PR_MISSING_CHECKSUITE_HEAD_BRANCH)); - }) - .isInstanceOf(IllegalStateException.class) - .hasMessageContaining("Could not parse check run event:"); + void shouldIgnoreHeadBranchMissingFromPayload() throws IOException { + loggerRule.record(CheckRunGHEventSubscriber.class.getName(), Level.INFO).capture(1); + new CheckRunGHEventSubscriber(mock(JenkinsFacade.class), mock(SCMFacade.class)) + .onEvent(createEventWithRerunRequest(RERUN_REQUEST_JSON_FOR_PR_MISSING_CHECKSUITE_HEAD_BRANCH)); + assertThat(loggerRule.getMessages().get(0)).contains("Received rerun request through GitHub checks API."); } @Test