From da37df9edfe873200363856eb3a60392f3e2dc3f Mon Sep 17 00:00:00 2001 From: Nayeon Hyun <61166764+hyunnaye@users.noreply.github.com> Date: Thu, 18 Jul 2024 12:16:09 -0400 Subject: [PATCH] SEAB-6452: fix null error (#496) * fix * changed if case * changed if case * fix * fix * fix * fix * test * test * test * test * test * test * test * test * fix * fix * fix * fix * fix * remove println * review feedback * review feedback * review feedback * review feedback --- .../GithubDeliveryS3Client.java | 52 +++++++++++-------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/githubdelivery/src/main/java/io/dockstore/githubdelivery/GithubDeliveryS3Client.java b/githubdelivery/src/main/java/io/dockstore/githubdelivery/GithubDeliveryS3Client.java index e0896f30..0929249f 100644 --- a/githubdelivery/src/main/java/io/dockstore/githubdelivery/GithubDeliveryS3Client.java +++ b/githubdelivery/src/main/java/io/dockstore/githubdelivery/GithubDeliveryS3Client.java @@ -124,26 +124,26 @@ private String getObject(String key) throws IOException { ResponseInputStream object = s3Client.getObject(objectRequest); return IOUtils.toString(object, StandardCharsets.UTF_8); } - private PushPayload getGitHubPushPayloadByKey(String body, String key) throws IOException, NoSuchKeyException { + private PushPayload getGitHubPushPayloadByKey(String eventType, String body, String key) throws IOException, NoSuchKeyException { try { PushPayload pushPayload; pushPayload = MAPPER.readValue(body, PushPayload.class); if (pushPayload == null) { - logReadError(key); + logReadError(eventType, key); } return pushPayload; } catch (JsonSyntaxException e) { - exceptionMessage(e, String.format("Could not read github event from key %s", key), 1); + exceptionReadError(e, eventType, key); } return null; } - private InstallationRepositoriesPayload getGitHubInstallationRepositoriesPayloadByKey(String body, String key) throws IOException, NoSuchKeyException { + private InstallationRepositoriesPayload getGitHubInstallationRepositoriesPayloadByKey(String eventType, String body, String key) throws IOException, NoSuchKeyException { try { InstallationRepositoriesPayload installationRepositoriesPayload; installationRepositoriesPayload = MAPPER.readValue(body, InstallationRepositoriesPayload.class); return installationRepositoriesPayload; } catch (JsonSyntaxException e) { - exceptionMessage(e, String.format("Could not read github event from key %s", key), 1); + exceptionReadError(e, eventType, key); } return null; } @@ -179,32 +179,35 @@ private void submitGitHubDeliveryEventsByHour(String prefix, WorkflowsApi workfl private void submitGitHubDeliveryEventsByKey(String key, WorkflowsApi workflowsApi) { String deliveryid = key.split("/")[2]; //since key is in YYYY-MM-DD/HH/deliveryid format try { - String body = getObject(key); - JsonObject jsonObject = GSON.fromJson(body, JsonObject.class); - if (jsonObject.get("action").getAsString().equals("added") || jsonObject.get("action").getAsString().equals("removed")) { - InstallationRepositoriesPayload payload = getGitHubInstallationRepositoriesPayloadByKey(body, key); + String s3GithubObject = getObject(key); + JsonObject jsonObject = GSON.fromJson(s3GithubObject, JsonObject.class); + JsonObject body = jsonObject.get("body").getAsJsonObject(); + String bodyString = body.toString(); + String eventType = jsonObject.get("eventType").getAsString(); + if ("installation_repositories".equals(eventType)) { + InstallationRepositoriesPayload payload = getGitHubInstallationRepositoriesPayloadByKey(eventType, bodyString, key); if (payload != null) { workflowsApi.handleGitHubInstallation(payload, deliveryid); } else { - logReadError(key); + logReadError(eventType, key); } - } else { - if (jsonObject.get("deleted").getAsBoolean()) { - PushPayload payload = getGitHubPushPayloadByKey(body, key); - if (payload != null) { + } else if ("push".equals(eventType)) { + //push events + PushPayload payload = getGitHubPushPayloadByKey(eventType, bodyString, key); + if (payload != null) { + if (body.get("deleted").getAsBoolean()) { workflowsApi.handleGitHubBranchDeletion(payload.getRepository().getFullName(), payload.getSender().getLogin(), payload.getRef(), deliveryid, payload.getInstallation().getId()); } else { - logReadError(key); - } - } else { - PushPayload payload = getGitHubPushPayloadByKey(body, key); - if (payload != null) { workflowsApi.handleGitHubRelease(payload, deliveryid); - } else { - logReadError(key); } + } else { + logReadError(eventType, key); } + + } else { + LOG.error("Invalid eventType {} format for key {}", eventType, key); + return; } LOG.info("Successfully submitted events for key {}", key); } catch (IOException e) { @@ -213,7 +216,10 @@ private void submitGitHubDeliveryEventsByKey(String key, WorkflowsApi workflowsA LOG.error("Could not submit github event from key {}", key, e); } } - private void logReadError(String key) { - LOG.error("Could not read github event from key {}", key); + private void logReadError(String eventType, String key) { + LOG.error("Could not read github {} event from key {}", eventType, key); + } + private void exceptionReadError(Exception e, String eventType, String key) { + exceptionMessage(e, String.format("Could not read github %s event from key %s", eventType, key), 1); } }