You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BranchPersisterImpl.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.computation.task.projectanalysis.component;
  21. import java.util.Date;
  22. import javax.annotation.Nullable;
  23. import org.sonar.api.utils.System2;
  24. import org.sonar.db.DbClient;
  25. import org.sonar.db.DbSession;
  26. import org.sonar.db.component.BranchDto;
  27. import org.sonar.db.component.BranchType;
  28. import org.sonar.db.component.ComponentDto;
  29. import org.sonar.db.protobuf.DbProjectBranches;
  30. import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
  31. import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
  32. import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
  33. import static org.sonar.db.component.ComponentDto.UUID_PATH_SEPARATOR;
  34. public class BranchPersisterImpl implements BranchPersister {
  35. private final DbClient dbClient;
  36. private final System2 system2;
  37. private final TreeRootHolder treeRootHolder;
  38. private final AnalysisMetadataHolder analysisMetadataHolder;
  39. public BranchPersisterImpl(DbClient dbClient, System2 system2, TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder) {
  40. this.dbClient = dbClient;
  41. this.system2 = system2;
  42. this.treeRootHolder = treeRootHolder;
  43. this.analysisMetadataHolder = analysisMetadataHolder;
  44. }
  45. public void persist(DbSession dbSession) {
  46. Branch branch = analysisMetadataHolder.getBranch();
  47. String branchUuid = treeRootHolder.getRoot().getUuid();
  48. com.google.common.base.Optional<ComponentDto> branchComponentDtoOpt = dbClient.componentDao().selectByUuid(dbSession, branchUuid);
  49. ComponentDto branchComponentDto;
  50. if (branch.isMain()) {
  51. checkState(branchComponentDtoOpt.isPresent(), "Project has been deleted by end-user during analysis");
  52. branchComponentDto = branchComponentDtoOpt.get();
  53. } else {
  54. // inserts new row in table projects if it's the first time branch is analyzed
  55. branchComponentDto = branchComponentDtoOpt.or(() -> insertIntoProjectsTable(dbSession, branchUuid));
  56. }
  57. // insert or update in table project_branches
  58. dbClient.branchDao().upsert(dbSession, toBranchDto(branchComponentDto, branch));
  59. }
  60. private static void checkState(boolean condition, String msg) {
  61. if (!condition) {
  62. throw new IllegalStateException(msg);
  63. }
  64. }
  65. private static <T> T firstNonNull(@Nullable T first, T second) {
  66. return (first != null) ? first : second;
  67. }
  68. private BranchDto toBranchDto(ComponentDto componentDto, Branch branch) {
  69. BranchDto dto = new BranchDto();
  70. dto.setUuid(componentDto.uuid());
  71. // MainBranchProjectUuid will be null if it's a main branch
  72. dto.setProjectUuid(firstNonNull(componentDto.getMainBranchProjectUuid(), componentDto.projectUuid()));
  73. dto.setBranchType(branch.getType());
  74. // merge branch is only present if it's a short living branch
  75. dto.setMergeBranchUuid(branch.getMergeBranchUuid().orElse(null));
  76. if (branch.getType() == BranchType.PULL_REQUEST) {
  77. dto.setKey(analysisMetadataHolder.getPullRequestId());
  78. DbProjectBranches.PullRequestData pullRequestData = DbProjectBranches.PullRequestData.newBuilder()
  79. .setBranch(branch.getName())
  80. .setTitle(branch.getName())
  81. .build();
  82. dto.setPullRequestData(pullRequestData);
  83. } else {
  84. dto.setKey(branch.getName());
  85. }
  86. return dto;
  87. }
  88. private ComponentDto insertIntoProjectsTable(DbSession dbSession, String branchUuid) {
  89. String mainBranchProjectUuid = analysisMetadataHolder.getProject().getUuid();
  90. ComponentDto project = dbClient.componentDao().selectOrFailByUuid(dbSession, mainBranchProjectUuid);
  91. ComponentDto branchDto = project.copy();
  92. branchDto.setUuid(branchUuid);
  93. branchDto.setProjectUuid(branchUuid);
  94. branchDto.setRootUuid(branchUuid);
  95. branchDto.setUuidPath(UUID_PATH_OF_ROOT);
  96. branchDto.setModuleUuidPath(UUID_PATH_SEPARATOR + branchUuid + UUID_PATH_SEPARATOR);
  97. branchDto.setMainBranchProjectUuid(mainBranchProjectUuid);
  98. branchDto.setDbKey(treeRootHolder.getRoot().getKey());
  99. branchDto.setCreatedAt(new Date(system2.now()));
  100. dbClient.componentDao().insert(dbSession, branchDto);
  101. return branchDto;
  102. }
  103. }