class TravisRestService {

  @Transactional
  public void begin(BuildInfo bi) {
    Project project = projectRepository.findByAccountAndName(bi.getOwnerName(), bi.getRepoName());

    Branch branch = branchRepository.findByNameAndProject_Id(bi.getBranchName(), project.getId());
    if (branch == null) {
      branch = new Branch();
      branch.setName(bi.getBranchName());
      branch.setProject(project);
      branch = branchRepository.save(branch);
    }

    Build build = buildRepository.findByTravisBuildId(bi.getBuildId());
    if (build != null) {
      profContextDumpRepository.delete(build.getProfiles());
      sampleRepository.delete(build.getSamples());
      buildRepository.delete(build);
    }

    build = new Build();
    build.setLanguage(bi.getLang());
    build.setTravisBuildId(bi.getBuildId());
    build.setBranch(branch);
    build.setNumber(bi.getBuildNum());
    build.setNumSamples(bi.getNumSamples());
    build = buildRepository.save(build);

    jandyWorker.start(bi.getBuildId(), ProfService.class);

    logger.info("start build for profiling: {}/{} -> {}", bi.getOwnerName(), bi.getRepoName(), bi.getBuildId());
  }

}