RepositoryBuildLauncher { @VisibleForTesting CommitInfo calculateCommitInfoForBuild( GitInfo gitInfo, RepositoryBuild queuedBuild, Optional<RepositoryBuild> previousBuild) throws IOException, NonRetryableBuildException { LOG.info("Trying to fetch current sha for branch {}/{}", gitInfo.getRepository(), gitInfo.getBranch()); final GHRepository repository; try { repository = gitHubHelper.repositoryFor(gitInfo); } catch (FileNotFoundException e) { throw new NonRetryableBuildException("Couldn't find repository " + gitInfo.getFullRepositoryName(), e); } Optional<Commit> lastCommitInPreviousBuild = getCommitFromRepositoryBuild(previousBuild); if (queuedBuild.getBuildTrigger().getType() == INTER_PROJECT && lastCommitInPreviousBuild.isPresent()) { return new CommitInfo(lastCommitInPreviousBuild.get(), lastCommitInPreviousBuild, ImmutableList.of(), false); } Optional<String> sha = gitHubHelper.shaFor(repository, gitInfo); if (!sha.isPresent()) { String message = String.format("Couldn't find branch %s for repository %s", gitInfo.getBranch(), gitInfo.getFullRepositoryName()); throw new NonRetryableBuildException(message); } else { LOG.info("Found sha {} for branch {}/{}", sha.get(), gitInfo.getRepository(), gitInfo.getBranch()); Commit currentCommit = gitHubHelper.toCommit(repository.getCommit(sha.get())); return gitHubHelper.commitInfoFor(repository, currentCommit, lastCommitInPreviousBuild); } } @Inject  RepositoryBuildLauncher(RepositoryBuildService repositoryBuildService,
                                 BranchService branchService,
                                 ModuleService moduleService,
                                 DependenciesService dependenciesService,
                                 ModuleDiscoveryService moduleDiscoveryService,
                                 ModuleDiscoveryHandler moduleDiscoveryHandler,
                                 GitHubHelper gitHubHelper); void launch(RepositoryBuild queued, Optional<RepositoryBuild> previous);  }
@Test public void itUsesPreviousCommitWhenBuildIsPartOfInterProjectBuild() throws Exception { GitInfo branch = new GitInfo(Optional.of(1), "git.example.com", "example", "repository", 1, "master", true, 0L, 1L); String commitSha = "0000000000000000000000000000000000000000"; Commit commit = Commit.newBuilder().setId(commitSha).build(); CommitInfo commitInfo = new CommitInfo(commit, Optional.absent(), Collections.emptyList(), false); RepositoryBuild previousBuild = RepositoryBuild.newBuilder(1, 1, RepositoryBuild.State.SUCCEEDED, BuildTrigger.forCommit(commitSha), BuildOptions.defaultOptions()) .setCommitInfo(Optional.of(commitInfo)) .build(); Optional<RepositoryBuild> previousBuildOptional = Optional.of(previousBuild); BuildOptions ipbBuildOptions = new BuildOptions(ImmutableSet.of(1), BuildOptions.BuildDownstreams.NONE, false); RepositoryBuild currentBuild = RepositoryBuild.queuedBuild(branch, BuildTrigger.forInterProjectBuild(1), 2, ipbBuildOptions); RepositoryBuildLauncher launcher = new RepositoryBuildLauncher(repositoryBuildService, branchService, moduleService, dependenciesService, moduleDiscoveryService, moduleDiscoveryHandler, gitHubHelper); when(gitHubHelper.commitInfoFor(any(), any(), any())).thenThrow(new IllegalStateException("Previous build is present this should not be called")); when(gitHubHelper.shaFor(any(), any())).thenThrow(new IllegalStateException("Previous build is present this should not be called")); CommitInfo result = launcher.calculateCommitInfoForBuild(branch, currentBuild, previousBuildOptional); assertThat(result.getCurrent().getId()).isEqualTo(commitSha); assertThat(result.getPrevious().isPresent()).isTrue(); assertThat(result.getPrevious().get().getId()).isEqualTo(commitSha); }
@Test public void itUsesNewCommitWhenBranchBuildIsNotPartOfAInterProjectBuild() throws Exception { GitInfo branch = new GitInfo(Optional.of(1), "git.example.com", "example", "repository", 1, "master", true, 0L, 1L); String initialCommitSha = "0000000000000000000000000000000000000000"; String secondCommitSha = "1111111111111111111111111111111111111111"; Commit initialCommit = Commit.newBuilder().setId(initialCommitSha).build(); Commit secondCommit = Commit.newBuilder().setId(secondCommitSha).build(); CommitInfo previousCommitInfo = new CommitInfo(initialCommit, Optional.absent(), Collections.emptyList(), false); RepositoryBuild previousBuild = RepositoryBuild.newBuilder(1, 1, RepositoryBuild.State.SUCCEEDED, BuildTrigger.forCommit(initialCommitSha), BuildOptions.defaultOptions()) .setCommitInfo(Optional.of(previousCommitInfo)) .build(); Optional<RepositoryBuild> previousBuildOptional = Optional.of(previousBuild); RepositoryBuild currentBuild = RepositoryBuild.queuedBuild(branch, BuildTrigger.forCommit(secondCommitSha), 2, BuildOptions.defaultOptions()); RepositoryBuildLauncher launcher = new RepositoryBuildLauncher(repositoryBuildService, branchService, moduleService, dependenciesService, moduleDiscoveryService, moduleDiscoveryHandler, gitHubHelper); GHRepository repository = mock(GHRepository.class); when(gitHubHelper.shaFor(any(), any())).thenReturn(Optional.of(secondCommitSha)); when(gitHubHelper.repositoryFor(any())).thenReturn(repository); doAnswer(invocation -> { String sha = (String) invocation.getArguments()[0]; return new GHCommit() { @Override public String getSHA1() { return sha; } }; }).when(repository).getCommit(anyString()); doAnswer(invocation -> { GHCommit commit = (GHCommit) invocation.getArguments()[0]; return Commit.newBuilder().setId(commit.getSHA1()).build(); }).when(gitHubHelper).toCommit(any()); doAnswer(invocation -> { Commit currentCommit = (Commit) invocation.getArguments()[1]; Optional<Commit> previousCommit = (Optional<Commit>) invocation.getArguments()[2]; return new CommitInfo(currentCommit, previousCommit, ImmutableList.of(currentCommit), false); }).when(gitHubHelper).commitInfoFor(any(), any(), any()); CommitInfo result = launcher.calculateCommitInfoForBuild(branch, currentBuild, previousBuildOptional); assertThat(result.getCurrent().getId()).isEqualTo(secondCommitSha); assertThat(result.getPrevious().isPresent()).isTrue(); assertThat(result.getPrevious().get().getId()).isEqualTo(initialCommitSha); assertThat(result.getNewCommits().isEmpty()).isFalse(); assertThat(result.getNewCommits()).isEqualTo(ImmutableList.of(secondCommit)); }