GitHubUtil { public int createPullReq(GHRepository origRepo, String branch, GHRepository forkRepo, String title, String body) throws InterruptedException { log.info("Creating Pull Request on {} from {}...", origRepo.getFullName(), forkRepo.getFullName()); try { GHPullRequest pullRequest = origRepo.createPullRequest(title, forkRepo.getOwnerName() + ":" + branch, origRepo.getDefaultBranch(), body); log.info("A pull request has been created at {}", pullRequest.getHtmlUrl()); return 0; } catch (IOException e) { 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(); 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")) { log.warn("NOTE: {} Pull request was not created.", error); return 1; } else { log.warn("An error occurred in pull request: {} Trying again...", error); waitFor(TimeUnit.SECONDS.toMillis(3)); return -1; } } } GitHubUtil(GitHub gitHub); GitHub getGithub(); GHRepository getRepo(String repo); GHRepository createPublicRepo(String repoName); GHMyself getMyself(); GHContentSearchBuilder startSearch(); GHRepository createFork(GHRepository repo); void safeDeleteRepo(GHRepository repo); int createPullReq(GHRepository origRepo, String branch,
                                 GHRepository forkRepo, String title, String body); GHRepository tryRetrievingRepository(String repoName); GHContent tryRetrievingContent(GHRepository repo, String path, String branch); List<GHRepository> getGHRepositories(Multimap<String, String> pathToDockerfileInParentRepo,
                                                 GHMyself currentUser); boolean repoHasBranch(GHRepository repo, String branchName); Map<String, GHRepository> getReposForUserAtCurrentInstant(GHMyself user); static final String NO_BRANCH_WARN_FORMAT; }
@Test public void testCreatePullReq_correctCallToPullRequest() 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(any(), any(), any(), any())).thenReturn(mock(GHPullRequest.class)); GHRepository forkRepo = mock(GHRepository.class); when(forkRepo.getOwnerName()).thenReturn("owner"); assertEquals(gitHubUtil.createPullReq(origRepo, "branch", forkRepo, "title", "body"), 0); verify(origRepo, times(1)).createPullRequest(eq("title"), eq("owner:branch"), eq("master"), eq("body")); }
@Test public void testCreatePullReq_errorCase0() 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\",\"code\":\"custom\",\"message\":\"A pull request already exists for someone:somebranch.\"}],\"documentation_url\":\"https: GHRepository forkRepo = mock(GHRepository.class); when(forkRepo.getOwnerName()).thenReturn("owner"); assertEquals(gitHubUtil.createPullReq(origRepo, "branch", forkRepo, "title", "body"), 0); verify(origRepo, times(1)).createPullRequest(eq("title"), eq("owner:branch"), eq("master"), eq("body")); }
@Test public void testCreatePullReq_errorCase1() 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\",\"code\":\"custom\",\"message\":\"No commits between thisrepo and thatrepo.\"}],\"documentation_url\":\"https: 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")); }