RepositoryBuildService { public long enqueue(GitInfo gitInfo, BuildTrigger trigger, BuildOptions buildOptions) { int branchId = gitInfo.getId().get(); List<RepositoryBuild> queuedBuilds = repositoryBuildDao.getByBranchAndState(branchId, State.QUEUED); int nextBuildNumber = determineNextBuildNumber(branchId, queuedBuilds); if (trigger.getType() == BuildTrigger.Type.PUSH) { for (RepositoryBuild build : queuedBuilds) { if (build.getBuildTrigger().getType() == BuildTrigger.Type.PUSH) { long existingBuildId = build.getId().get(); LOG.info("A push was received for branch {} but will not enqueue a repository build because there is already pending repository build {} that was triggered by a previous push to this branch", branchId, existingBuildId); return existingBuildId; } } } RepositoryBuild build = RepositoryBuild.queuedBuild(gitInfo, trigger, nextBuildNumber, buildOptions); LOG.info("Enqueuing repository build for branch {} and build number {}", branchId, nextBuildNumber); int expectedUpdateCount = queuedBuilds.isEmpty() ? 1 : 0; build = enqueue(build, expectedUpdateCount); LOG.info("Enqueued repository build with id {} for branch {}", build.getId().get(), branchId); return build.getId().get(); } @Inject  RepositoryBuildService(RepositoryBuildDao repositoryBuildDao, BranchDao branchDao, EventBus eventBus); Optional<RepositoryBuild> get(long id); List<RepositoryBuild> getByBranch(int branchId); Optional<RepositoryBuild> getByBranchAndNumber(int branchId, int buildNumber); BuildNumbers getBuildNumbers(int branchId); Optional<RepositoryBuild> getPreviousBuild(RepositoryBuild build); long enqueue(GitInfo gitInfo, BuildTrigger trigger, BuildOptions buildOptions); @Transactional void begin(RepositoryBuild build); @Transactional void update(RepositoryBuild build); @Transactional void fail(RepositoryBuild build); void cancel(RepositoryBuild build);  }
@Test public void itReturnsPendingBuildForSecondPushEvent() { long newBuildId = repositoryBuildService.enqueue(branchOne, buildTriggerTwo, buildOptionsTwo); assertThat(newBuildId).isEqualTo(buildIdOne); }