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.

ComponentUpdater.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.component;
  21. import com.google.common.collect.ImmutableSet;
  22. import java.util.Date;
  23. import java.util.Locale;
  24. import java.util.Set;
  25. import javax.annotation.Nullable;
  26. import org.sonar.api.resources.Qualifiers;
  27. import org.sonar.api.resources.Scopes;
  28. import org.sonar.api.utils.System2;
  29. import org.sonar.core.i18n.I18n;
  30. import org.sonar.core.util.Uuids;
  31. import org.sonar.db.DbClient;
  32. import org.sonar.db.DbSession;
  33. import org.sonar.db.component.BranchDto;
  34. import org.sonar.db.component.BranchType;
  35. import org.sonar.db.component.ComponentDto;
  36. import org.sonar.server.es.ProjectIndexer.Cause;
  37. import org.sonar.server.es.ProjectIndexers;
  38. import org.sonar.server.favorite.FavoriteUpdater;
  39. import org.sonar.server.permission.PermissionTemplateService;
  40. import static java.util.Collections.singletonList;
  41. import static org.sonar.api.resources.Qualifiers.PROJECT;
  42. import static org.sonar.core.component.ComponentKeys.isValidProjectKey;
  43. import static org.sonar.server.ws.WsUtils.checkRequest;
  44. public class ComponentUpdater {
  45. private static final Set<String> MAIN_BRANCH_QUALIFIERS = ImmutableSet.of(Qualifiers.PROJECT, Qualifiers.APP);
  46. private final DbClient dbClient;
  47. private final I18n i18n;
  48. private final System2 system2;
  49. private final PermissionTemplateService permissionTemplateService;
  50. private final FavoriteUpdater favoriteUpdater;
  51. private final ProjectIndexers projectIndexers;
  52. public ComponentUpdater(DbClient dbClient, I18n i18n, System2 system2,
  53. PermissionTemplateService permissionTemplateService, FavoriteUpdater favoriteUpdater,
  54. ProjectIndexers projectIndexers) {
  55. this.dbClient = dbClient;
  56. this.i18n = i18n;
  57. this.system2 = system2;
  58. this.permissionTemplateService = permissionTemplateService;
  59. this.favoriteUpdater = favoriteUpdater;
  60. this.projectIndexers = projectIndexers;
  61. }
  62. /**
  63. * - Create component
  64. * - Apply default permission template
  65. * - Add component to favorite if the component has the 'Project Creators' permission
  66. * - Index component in es indexes
  67. */
  68. public ComponentDto create(DbSession dbSession, NewComponent newComponent, @Nullable Integer userId) {
  69. ComponentDto componentDto = createWithoutCommit(dbSession, newComponent, userId);
  70. commitAndIndex(dbSession, componentDto);
  71. return componentDto;
  72. }
  73. /**
  74. * Create component without committing.
  75. * Don't forget to call commitAndIndex(...) when ready to commit.
  76. */
  77. public ComponentDto createWithoutCommit(DbSession dbSession, NewComponent newComponent, @Nullable Integer userId) {
  78. checkKeyFormat(newComponent.qualifier(), newComponent.key());
  79. ComponentDto componentDto = createRootComponent(dbSession, newComponent);
  80. if (isRootProject(componentDto)) {
  81. createMainBranch(dbSession, componentDto.uuid());
  82. }
  83. handlePermissionTemplate(dbSession, componentDto, userId);
  84. return componentDto;
  85. }
  86. public void commitAndIndex(DbSession dbSession, ComponentDto componentDto) {
  87. projectIndexers.commitAndIndex(dbSession, singletonList(componentDto), Cause.PROJECT_CREATION);
  88. }
  89. private ComponentDto createRootComponent(DbSession session, NewComponent newComponent) {
  90. checkRequest(!dbClient.componentDao().selectByKey(session, newComponent.key()).isPresent(),
  91. "Could not create %s, key already exists: %s", getQualifierToDisplay(newComponent.qualifier()), newComponent.key());
  92. String uuid = Uuids.create();
  93. ComponentDto component = new ComponentDto()
  94. .setOrganizationUuid(newComponent.getOrganizationUuid())
  95. .setUuid(uuid)
  96. .setUuidPath(ComponentDto.UUID_PATH_OF_ROOT)
  97. .setRootUuid(uuid)
  98. .setModuleUuid(null)
  99. .setModuleUuidPath(ComponentDto.UUID_PATH_SEPARATOR + uuid + ComponentDto.UUID_PATH_SEPARATOR)
  100. .setProjectUuid(uuid)
  101. .setDbKey(newComponent.key())
  102. .setName(newComponent.name())
  103. .setLongName(newComponent.name())
  104. .setScope(Scopes.PROJECT)
  105. .setQualifier(newComponent.qualifier())
  106. .setPrivate(newComponent.isPrivate())
  107. .setCreatedAt(new Date(system2.now()));
  108. dbClient.componentDao().insert(session, component);
  109. return component;
  110. }
  111. private static boolean isRootProject(ComponentDto componentDto) {
  112. return Scopes.PROJECT.equals(componentDto.scope())
  113. && MAIN_BRANCH_QUALIFIERS.contains(componentDto.qualifier());
  114. }
  115. private void createMainBranch(DbSession session, String componentUuid) {
  116. BranchDto branch = new BranchDto()
  117. .setBranchType(BranchType.LONG)
  118. .setUuid(componentUuid)
  119. .setKey(BranchDto.DEFAULT_MAIN_BRANCH_NAME)
  120. .setMergeBranchUuid(null)
  121. .setProjectUuid(componentUuid);
  122. dbClient.branchDao().upsert(session, branch);
  123. }
  124. private void handlePermissionTemplate(DbSession dbSession, ComponentDto componentDto, @Nullable Integer userId) {
  125. permissionTemplateService.applyDefault(dbSession, componentDto, userId);
  126. if (componentDto.qualifier().equals(PROJECT)
  127. && permissionTemplateService.hasDefaultTemplateWithPermissionOnProjectCreator(dbSession, componentDto)) {
  128. favoriteUpdater.add(dbSession, componentDto, userId, false);
  129. }
  130. }
  131. private void checkKeyFormat(String qualifier, String key) {
  132. checkRequest(isValidProjectKey(key), "Malformed key for %s: '%s'. It cannot be empty nor contain whitespaces.", getQualifierToDisplay(qualifier), key);
  133. }
  134. private String getQualifierToDisplay(String qualifier) {
  135. return i18n.message(Locale.getDefault(), "qualifier." + qualifier, "Project");
  136. }
  137. }