From dcbd93f05394e11e64a0884d4f9417a60a511ed1 Mon Sep 17 00:00:00 2001 From: Avimanyu Mukhopadhyay Date: Mon, 14 Mar 2022 14:35:52 -0700 Subject: [PATCH] Addressing PR comments --- .../dockerfileimageupdate/utils/GitHubUtil.java | 16 +++++++++------- .../utils/GitHubUtilTest.java | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/GitHubUtil.java b/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/GitHubUtil.java index 7c788458..1cda3d9d 100644 --- a/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/GitHubUtil.java +++ b/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/GitHubUtil.java @@ -11,6 +11,7 @@ import com.google.common.collect.Multimap; import com.google.gson.JsonArray; import com.google.gson.JsonElement; +import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.kohsuke.github.*; import org.slf4j.Logger; @@ -21,6 +22,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Arrays; import java.util.concurrent.TimeUnit; /** @@ -110,18 +112,18 @@ public int createPullReq(GHRepository origRepo, String branch, log.warn("Handling error with pull request creation... {}", e.getMessage()); JsonElement root = JsonParser.parseString(e.getMessage()); JsonArray errorJson = root.getAsJsonObject().get("errors").getAsJsonArray(); - String error = ""; - if (errorJson.get(0).getAsJsonObject().has("message")) { - error = errorJson.get(0).getAsJsonObject().get("message").getAsString(); - } else { - // This case usually happens when the PR head has invalid data. Deleting the forked repo resolves it. - error = PR_INVALID_CODE; + String error = PR_INVALID_CODE; + JsonObject errorMessage = errorJson.get(0).getAsJsonObject(); + if (errorMessage.has("message")) { + error = errorMessage.get("message").getAsString(); } log.info("error: {}", error); + String finalError = error; + List errorPrefixes = Arrays.asList(PR_INVALID_CODE, "No commits between"); if (error.startsWith("A pull request already exists")) { log.info("NOTE: {} New commits may have been added to the pull request.", error); return 0; - } else if (error.startsWith("No commits between") || error.startsWith(PR_INVALID_CODE)) { + } else if (errorPrefixes.stream().anyMatch(prefix -> finalError.startsWith(prefix))) { log.warn("NOTE: {} Pull request was not created.", error); return 1; } else { diff --git a/dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/GitHubUtilTest.java b/dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/GitHubUtilTest.java index 382ae040..57804bf3 100644 --- a/dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/GitHubUtilTest.java +++ b/dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/GitHubUtilTest.java @@ -131,7 +131,7 @@ public void testCreatePullReq_errorCase1_withInvalidCode() throws Exception { GHRepository forkRepo = mock(GHRepository.class); when(forkRepo.getOwnerName()).thenReturn("owner"); assertEquals(gitHubUtil.createPullReq(origRepo, "branch", forkRepo, "title", "body"), 1); - verify(origRepo, times(1)).createPullRequest(eq("title"), eq("owner:branch"), eq("master"), eq("body")); + verify(origRepo).createPullRequest(eq("title"), eq("owner:branch"), eq("master"), eq("body")); } @Test