GitHubPRRepository extends GitHubRepository<GitHubPRRepository> { @RequirePOST public FormValidation doRebuild(StaplerRequest req) throws IOException { FormValidation result; try { if (!job.hasPermission(Item.BUILD)) { return FormValidation.error("Forbidden"); } final String prNumberParam = "prNumber"; int prId = 0; if (req.hasParameter(prNumberParam)) { prId = Integer.valueOf(req.getParameter(prNumberParam)); } Map<Integer, List<Run<?, ?>>> builds = getAllPrBuilds(); List<Run<?, ?>> prBuilds = builds.get(prId); if (prBuilds != null && !prBuilds.isEmpty()) { if (rebuild(prBuilds.get(0))) { result = FormValidation.ok("Rebuild scheduled"); } else { result = FormValidation.warning("Rebuild not scheduled"); } } else { result = FormValidation.warning("Build not found"); } } catch (Exception e) { LOG.error("Can't start rebuild", e); result = FormValidation.error(e, "Can't start rebuild: " + 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; }
@Ignore @Test public void doRebuildWithRebuildPerformed() throws IOException { GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); doRebuildCommonExpectations(true, true); getAllPrBuildsCommonExpectations(BUILD_MAP_SIZE); getAllPrBuildsNonNullCauseExpectations(BUILD_MAP_SIZE); when(run.getParent()).thenReturn(job); GitHubPRRepository repo = GitHubPRRepositoryFactoryTest.getRepo(factory.createFor(job)); FormValidation formValidation = repo.doRebuild(request); Assert.assertEquals(FormValidation.Kind.OK, formValidation.kind); }
@Test public void doRebuildWarnNotScheduled() throws IOException { GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); doRebuildCommonExpectations(false, true); getAllPrBuildsCommonExpectations(BUILD_MAP_SIZE); getAllPrBuildsNonNullCauseExpectations(BUILD_MAP_SIZE); when(run.getParent()).thenReturn(job); GitHubPRRepository repo = GitHubPRRepositoryFactoryTest.getRepo(factory.createFor(job)); FormValidation formValidation = repo.doRebuild(request); Assert.assertEquals(FormValidation.Kind.WARNING, formValidation.kind); }
@Test public void doRebuildWarnNotFound() throws IOException { GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); doRebuildCommonExpectations(true, true); getAllPrBuildsCommonExpectations(BUILD_MAP_SIZE); getAllPrBuildsNonNullCauseExpectations(0); when(run.getParent()).thenReturn(job); GitHubPRRepository repo = GitHubPRRepositoryFactoryTest.getRepo(factory.createFor(job)); FormValidation formValidation = repo.doRebuild(request); Assert.assertEquals(FormValidation.Kind.WARNING, formValidation.kind); }
@Test public void doRebuildWithException() throws IOException { GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); doRebuildCommonExpectations(true, true); getAllPrBuildsCommonExpectations(BUILD_MAP_SIZE); getAllPrBuildsNonNullCauseExpectations(BUILD_MAP_SIZE); when(run.getParent()).thenThrow(new RuntimeException("rebuild() test exception")); GitHubPRRepository repo = GitHubPRRepositoryFactoryTest.getRepo(factory.createFor(job)); FormValidation formValidation = repo.doRebuild(request); Assert.assertEquals(FormValidation.Kind.ERROR, formValidation.kind); }
@Test public void doRebuildForbidden() throws IOException { GitHubPRRepositoryFactoryTest.createForCommonExpectations(job, trigger); doRebuildCommonExpectations(true, false); GitHubPRRepository repo = GitHubPRRepositoryFactoryTest.getRepo(factory.createFor(job)); FormValidation formValidation = repo.doRebuild(request); Assert.assertEquals(FormValidation.Kind.ERROR, formValidation.kind); }