Skip to content
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

Merged
merged 5 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
Copy link
Contributor

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. Call has and get on that variable. This removes the need to call get(0).getAsJsonObject() twice.

Copy link
Contributor

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?

error = errorJson.get(0).getAsJsonObject().get("message").getAsString();
} else {
error = "PR head has invalid code.";
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.")) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is longer than 100 characters (found 113).

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
Expand Down