LaunchingRepositoryBuildVisitor extends AbstractRepositoryBuildVisitor { @Override protected void visitLaunching(RepositoryBuild build) throws Exception { LOG.info("Going to enqueue module builds for repository build {}", build.getId().get()); final Set<Module> activeModules = filterActive(moduleService.getByBranch(build.getBranchId())); if (!malformedFileService.getMalformedFiles(build.getBranchId()).isEmpty()) { failBranchAndModuleBuilds(build, activeModules); return; } if (activeModules.isEmpty()) { LOG.info("No active modules to build in branch {} - failing build", build.getId().get()); repositoryBuildService.fail(build); return; } final Optional<Long> interProjectBuildId; final Set<Module> toBuild; if (build.getBuildOptions().getBuildDownstreams() == BuildDownstreams.INTER_PROJECT) { toBuild = determineModulesToBuildUsingInterProjectBuildGraph(build, activeModules); InterProjectBuild ipb = InterProjectBuild.getQueuedBuild(ImmutableSet.copyOf(getIds(toBuild)), build.getBuildTrigger()); interProjectBuildId = Optional.of(interProjectBuildService.enqueue(ipb)); } else { interProjectBuildId = Optional.absent(); toBuild = findModulesToBuild(build, activeModules); } for (Module module : build.getDependencyGraph().get().orderByTopologicalSort(toBuild)) { enqueueModuleBuild(build, module); if (build.getBuildOptions().getBuildDownstreams() == BuildDownstreams.INTER_PROJECT) { interProjectBuildMappingService.insert(InterProjectBuildMapping.makeNewMapping(interProjectBuildId.get(), build.getBranchId(), build.getId(), module.getId().get())); } } Set<Module> skipped = Sets.difference(activeModules, toBuild); for (Module module : skipped) { moduleBuildService.skip(build, module); } repositoryBuildService.update(build.toBuilder().setState(State.IN_PROGRESS).build()); } @Inject  LaunchingRepositoryBuildVisitor(RepositoryBuildService repositoryBuildService,
                                         ModuleBuildService moduleBuildService,
                                         MalformedFileService malformedFileService,
                                         InterProjectBuildService interProjectBuildService,
                                         InterProjectBuildMappingService interProjectBuildMappingService,
                                         ModuleService moduleService,
                                         DependenciesService dependenciesService,
                                         GitHubHelper gitHubHelper);   }
@Test public void itFailsBuildOfBranchWhenItHasMalformedFiles() throws Exception { when(malformedFileService.getMalformedFiles(anyInt())) .thenReturn(ImmutableSet.of(new MalformedFile(1, "config", "/.blazar.yaml", "this is a test malformed file"))); when(moduleService.getByBranch(1)).thenReturn(ImmutableSet.of(activeModule, inactiveModule)); boolean[] moduleBuildFailed = {false}; doAnswer(invocation -> { RepositoryBuild build = (RepositoryBuild) invocation.getArguments()[0]; Module module = (Module) invocation.getArguments()[1]; assertThat(module.equals(activeModule)); assertThat(build.equals(launchingBuild)); moduleBuildFailed[0] = true; return null; }).when(moduleBuildService).createFailedBuild(any(), any()); boolean[] repoBuildFailed = {false}; doAnswer(invocation -> { RepositoryBuild build = (RepositoryBuild) invocation.getArguments()[0]; assertThat(build).isEqualTo(launchingBuild); repoBuildFailed[0] = true; return null; }).when(repositoryBuildService).fail(any()); buildVisitor.visitLaunching(launchingBuild); assertThat(repoBuildFailed[0]).isTrue(); assertThat(moduleBuildFailed[0]).isTrue(); }
@Test public void itFailsBuildOfBranchWithNoActiveModules() throws Exception { when(malformedFileService.getMalformedFiles(anyInt())).thenReturn(ImmutableSet.of()); when(moduleService.getByBranch(1)).thenReturn(ImmutableSet.of(inactiveModule)); boolean[] repoBuildFailed = {false}; doAnswer(invocation -> { RepositoryBuild build = (RepositoryBuild) invocation.getArguments()[0]; assertThat(build).isEqualTo(launchingBuild); repoBuildFailed[0] = true; return null; }).when(repositoryBuildService).fail(any()); buildVisitor.visitLaunching(launchingBuild); assertThat(repoBuildFailed[0]).isTrue(); }
@Test public void itEnqueuesModuleBuildsAndUpdatesRepositoryBuildToLaunching() throws Exception { when(malformedFileService.getMalformedFiles(anyInt())).thenReturn(ImmutableSet.of()); when(moduleService.getByBranch(1)).thenReturn(ImmutableSet.of(activeModule, inactiveModule)); doThrow(new RuntimeException("Build should not have been failed")).when(repositoryBuildService).fail(any()); RepositoryBuild[] savedRepositoryBuild = {null}; doAnswer(invocation -> { savedRepositoryBuild[0] = (RepositoryBuild) invocation.getArguments()[0]; return null; }).when(repositoryBuildService).update(anyObject()); List<Module> modulesThatWereEnqueued = new ArrayList<>(); doAnswer(invocation -> { modulesThatWereEnqueued.add((Module) invocation.getArguments()[1]); return null; }).when(moduleBuildService).enqueue(anyObject(), anyObject(), anyObject(), anyObject()); RepositoryBuild expectedRepositoryBuild = launchingBuild.toBuilder().setState(RepositoryBuild.State.IN_PROGRESS).build(); buildVisitor.visitLaunching(launchingBuild); assertThat(savedRepositoryBuild).isNotNull(); assertThat(savedRepositoryBuild[0]).isEqualTo(expectedRepositoryBuild); assertThat(modulesThatWereEnqueued).doesNotContain(inactiveModule); assertThat(modulesThatWereEnqueued).isEqualTo(ImmutableList.of(activeModule)); }