DockerfileGitHubUtil { public void createPullReq(GHRepository origRepo, String branch, GHRepository forkRepo, PullRequestInfo pullRequestInfo) throws InterruptedException, IOException { while (true) { int pullRequestExitCode = gitHubUtil.createPullReq(origRepo, branch, forkRepo, pullRequestInfo.getTitle(), pullRequestInfo.getBody()); if (pullRequestExitCode == 0) { return; } else if (pullRequestExitCode == 1) { gitHubUtil.safeDeleteRepo(forkRepo); return; } } } DockerfileGitHubUtil(GitHubUtil gitHubUtil); GHRepository getOrCreateFork(GHRepository parent); GHMyself getMyself(); GHRepository getRepo(String repoName); PagedSearchIterable<GHContent> findFilesWithImage(String query, String org); List<GHRepository> getGHRepositories(Multimap<String, String> pathToDockerfileInParentRepo,
                                                            GHMyself currentUser); void modifyAllOnGithub(GHRepository repo, String branch,
                                  String img, String tag); GHContent tryRetrievingContent(GHRepository repo, String path, String branch); void modifyOnGithub(GHContent content,
                               String branch, String img, String tag, String customMessage); GitHubJsonStore getGitHubJsonStore(String store); void createPullReq(GHRepository origRepo,
                              String branch, GHRepository forkRepo,
                              PullRequestInfo pullRequestInfo); Optional<GHPullRequest> getPullRequestForImageBranch(GHRepository repository, GitForkBranch gitForkBranch); void createOrUpdateForkBranchToParentDefault(GHRepository parent, GHRepository fork, GitForkBranch gitForkBranch); boolean thisUserIsOwner(GHRepository repo); Optional<PagedSearchIterable<GHContent>> getGHContents(String org, String img);  }
@Test public void testCreatePullReq_Loop() throws Exception { gitHubUtil = mock(GitHubUtil.class); when(gitHubUtil.createPullReq(any(), anyString(), any(), anyString(), eq(Constants.PULL_REQ_ID))).thenReturn(-1, -1, -1, 0); dockerfileGitHubUtil = new DockerfileGitHubUtil(gitHubUtil); PullRequestInfo pullRequestInfo = new PullRequestInfo(null, null, null); dockerfileGitHubUtil.createPullReq(new GHRepository(), "branch", new GHRepository(), pullRequestInfo); verify(gitHubUtil, times(4)).createPullReq(any(), anyString(), any(), eq(DEFAULT_TITLE), eq(Constants.PULL_REQ_ID)); }
@Test public void testCreatePullReq_Delete() throws Exception { gitHubUtil = mock(GitHubUtil.class); GHRepository forkRepo = mock(GHRepository.class); GHPullRequestQueryBuilder prBuilder = mock(GHPullRequestQueryBuilder.class); when(prBuilder.state(GHIssueState.OPEN)).thenReturn(prBuilder); PagedIterable<GHPullRequest> iteratable = mock(PagedIterable.class); when(prBuilder.list()).thenReturn(iteratable); PagedIterator<GHPullRequest> iterator = mock(PagedIterator.class); when(iterator.hasNext()).thenReturn(false); when(iteratable.iterator()).thenReturn(iterator); when(forkRepo.queryPullRequests()).thenReturn(prBuilder); when(gitHubUtil.createPullReq(any(), anyString(), any(), anyString(), eq(Constants.PULL_REQ_ID))).thenReturn(1); doCallRealMethod().when(gitHubUtil).safeDeleteRepo(forkRepo); dockerfileGitHubUtil = new DockerfileGitHubUtil(gitHubUtil); PullRequestInfo pullRequestInfo = new PullRequestInfo(null, null, null); dockerfileGitHubUtil.createPullReq(new GHRepository(), "branch", forkRepo, pullRequestInfo); verify(gitHubUtil, times(1)).createPullReq(any(), anyString(), any(), anyString(), eq(Constants.PULL_REQ_ID)); verify(forkRepo, times(1)).delete(); }