Skip to content

Commit

Permalink
Merge pull request #321 from avimanyum/pr_exception
Browse files Browse the repository at this point in the history
Handling error messaging when PR creation fails with Invalid code error
  • Loading branch information
avimanyum authored Mar 14, 2022
2 parents 859c50c + 596a07d commit c498297
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
public class GitHubUtil {
private static final Logger log = LoggerFactory.getLogger(GitHubUtil.class);
public static final String NO_BRANCH_WARN_FORMAT = "Couldn't find branch `%s` in repo `%s`. Waiting a second...";
public static final String PR_INVALID_CODE = "The PR head has invalid data. This might be because the repository was migrated to another organization." +
" Please delete the fork and retry.";

private final GitHub github;

Expand Down Expand Up @@ -108,12 +110,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 = 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 {
// This case usually happens when the PR head has invalid data. Deleting the forked repo resolves it.
error = PR_INVALID_CODE;
}
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_INVALID_CODE)) {
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"));
}

@Test
public void testTryRetrievingRepository() throws Exception {
Expand Down

0 comments on commit c498297

Please sign in to comment.