-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handling error messaging when PR creation fails with Invalid code error #321
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,12 +108,17 @@ 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 = errorJson.get(0).getAsJsonObject().get("message").getAsString(); | ||
String error = ""; | ||
if (errorJson.get(0).getAsJsonObject().has("message")) { | ||
error = errorJson.get(0).getAsJsonObject().get("message").getAsString(); | ||
} else { | ||
error = "PR head has invalid code."; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does "invalid code" mean? What action should be taken to resolve if any? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error message is trying to communicate that the field/keyword called "head" contains invalid data. When i saw this message, I manually deleted the fork and the next run was successful. This change will also do the same - deleted the fork. |
||
} | ||
log.info("error: {}", error); | ||
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")) { | ||
} else if (error.startsWith("No commits between") || error.startsWith("PR head has invalid code.")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is longer than 100 characters (found 113). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Repeated error message. Let's make it a constant in this class |
||
log.warn("NOTE: {} Pull request was not created.", error); | ||
return 1; | ||
} else { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,6 +120,19 @@ public void testCreatePullReq_errorCase1() throws Exception { | |
assertEquals(gitHubUtil.createPullReq(origRepo, "branch", forkRepo, "title", "body"), 1); | ||
verify(origRepo, times(1)).createPullRequest(eq("title"), eq("owner:branch"), eq("master"), eq("body")); | ||
} | ||
@Test | ||
public void testCreatePullReq_errorCase1_withInvalidCode() throws Exception { | ||
GitHub github = mock(GitHub.class); | ||
GitHubUtil gitHubUtil = new GitHubUtil(github); | ||
GHRepository origRepo = mock(GHRepository.class); | ||
when(origRepo.getDefaultBranch()).thenReturn("master"); | ||
when(origRepo.createPullRequest(eq("title"), eq("owner:branch"), eq("master"), eq("body"))) | ||
.thenThrow(new IOException("{\"message\":\"Validation Failed\",\"errors\":[{\"resource\":\"PullRequest\",\"field\":\"head\",\"code\":\"invalid\"}],\"documentation_url\":\"https://developer.github.com/enterprise/2.6/v3/pulls/#create-a-pull-request\"}")); | ||
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")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. times(1) is the default for verify, you can remove the second parameter. |
||
} | ||
|
||
@Test | ||
public void testTryRetrievingRepository() throws Exception { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Store the output of
errorJson.get(0).getAsJsonObject()
as a local variable. Callhas
andget
on that variable. This removes the need to callget(0).getAsJsonObject()
twice.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we ignoring other errors if multiple were returned?