GitHubPRRepository extends GitHubRepository<GitHubPRRepository> { @RequirePOST public FormValidation doRebuildAllFailed() throws IOException { FormValidation result; try { if (job.hasPermission(Item.BUILD)) { Map<Integer, List<Run<?, ?>>> builds = getAllPrBuilds(); for (List<Run<?, ?>> buildList : builds.values()) { if (!buildList.isEmpty() && Result.FAILURE.equals(buildList.get(0).getResult())) { Run<?, ?> lastBuild = buildList.get(0); rebuild(lastBuild); } } result = FormValidation.ok("Rebuild scheduled"); } else { result = FormValidation.error("Forbidden"); } } catch (Exception e) { LOG.error("Can't start rebuild", e); result = FormValidation.error(e, "Can't start rebuild: %s", e.getMessage()); } return result; } GitHubPRRepository(@Nonnull GHRepository ghRepository);  GitHubPRRepository(String repoFullName, URL githubUrl); @Nonnull Map<Integer, GitHubPRPullRequest> getPulls(); @Nonnull Map<Integer, List<Run<?, ?>>> getAllPrBuilds(); @Override String getIconFileName(); @Override String getDisplayName(); @Override String getUrlName(); @Override void actualiseOnChange(@Nonnull GHRepository ghRepository, @Nonnull TaskListener listener); @RequirePOST FormValidation doClearRepo(); @RequirePOST FormValidation doRunTrigger(); @RequirePOST FormValidation doRebuildAllFailed(); @Override FormValidation doBuild(StaplerRequest req); @RequirePOST FormValidation doRebuild(StaplerRequest req); void setJob(Job<?, ?> job); void setConfigFile(XmlFile configFile); static final String FILE; }
@Test public void doRebuildFailedNoRebuildNeeded() throws IOException { GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); hasPermissionExpectation(Item.BUILD, true); getAllPrBuildsCommonExpectations(BUILD_MAP_SIZE); GitHubPRRepository repo = GitHubPRRepositoryFactoryTest.getRepo(factory.createFor(job)); FormValidation formValidation = repo.doRebuildAllFailed(); Assert.assertEquals(FormValidation.Kind.OK, formValidation.kind); }
@Test public void doRebuildFailedWithRebuildPerformed() throws IOException { GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); hasPermissionExpectation(Item.BUILD, true); getAllPrBuildsCommonExpectations(BUILD_MAP_SIZE); getAllPrBuildsNonNullCauseExpectations(BUILD_MAP_SIZE); when(run.getResult()).thenReturn(Result.FAILURE); when(run.getParent()).thenReturn(job); GitHubPRRepository repo = GitHubPRRepositoryFactoryTest.getRepo(factory.createFor(job)); assertThat(repo, notNullValue()); FormValidation formValidation = repo.doRebuildAllFailed(); Assert.assertEquals(FormValidation.Kind.OK, formValidation.kind); }
@Test public void doRebuildFailedWithException() throws IOException { GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); hasPermissionExpectation(Item.BUILD, true); getAllPrBuildsCommonExpectations(BUILD_MAP_SIZE); getAllPrBuildsNonNullCauseExpectations(BUILD_MAP_SIZE); when(run.getResult()).thenThrow(new RuntimeException("run.getResult() test exception")); GitHubPRRepository repo = GitHubPRRepositoryFactoryTest.getRepo(factory.createFor(job)); FormValidation formValidation = repo.doRebuildAllFailed(); Assert.assertEquals(FormValidation.Kind.ERROR, formValidation.kind); }
@Test public void doRebuildFailedForbidden() throws IOException { GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); hasPermissionExpectation(Item.BUILD, false); GitHubPRRepository repo = GitHubPRRepositoryFactoryTest.getRepo(factory.createFor(job)); FormValidation formValidation = repo.doRebuildAllFailed(); Assert.assertEquals(FormValidation.Kind.ERROR, formValidation.kind); }