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.

ComponentKeyUpdaterDao.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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.db.component;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import com.google.common.collect.ImmutableSet;
  23. import com.google.common.collect.Lists;
  24. import java.util.Collection;
  25. import java.util.HashMap;
  26. import java.util.HashSet;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.Objects;
  30. import java.util.Set;
  31. import java.util.function.BiConsumer;
  32. import java.util.function.Function;
  33. import java.util.function.Predicate;
  34. import java.util.stream.Collectors;
  35. import javax.annotation.Nullable;
  36. import org.apache.commons.lang.StringUtils;
  37. import org.sonar.api.resources.Qualifiers;
  38. import org.sonar.db.Dao;
  39. import org.sonar.db.DbSession;
  40. import static com.google.common.base.Preconditions.checkArgument;
  41. import static org.sonar.core.component.ComponentKeys.checkProjectKey;
  42. /**
  43. * Class used to rename the key of a project and its resources.
  44. *
  45. * @since 3.2
  46. */
  47. public class ComponentKeyUpdaterDao implements Dao {
  48. private static final Set<String> PROJECT_OR_MODULE_QUALIFIERS = ImmutableSet.of(Qualifiers.PROJECT, Qualifiers.MODULE);
  49. public void updateKey(DbSession dbSession, String projectOrModuleUuid, String newKey) {
  50. ComponentKeyUpdaterMapper mapper = dbSession.getMapper(ComponentKeyUpdaterMapper.class);
  51. if (mapper.countResourceByKey(newKey) > 0) {
  52. throw new IllegalArgumentException("Impossible to update key: a component with key \"" + newKey + "\" already exists.");
  53. }
  54. // must SELECT first everything
  55. ResourceDto project = mapper.selectProjectByUuid(projectOrModuleUuid);
  56. String projectOldKey = project.getKey();
  57. List<ResourceDto> resources = mapper.selectProjectResources(projectOrModuleUuid);
  58. resources.add(project);
  59. // add branch components
  60. dbSession.getMapper(BranchMapper.class).selectByProjectUuid(projectOrModuleUuid)
  61. .stream()
  62. .filter(branch -> !projectOrModuleUuid.equals(branch.getUuid()))
  63. .forEach(branch -> {
  64. resources.addAll(mapper.selectProjectResources(branch.getUuid()));
  65. resources.add(mapper.selectProjectByUuid(branch.getUuid()));
  66. });
  67. // and then proceed with the batch UPDATE at once
  68. runBatchUpdateForAllResources(resources, projectOldKey, newKey, mapper, (resource, oldKey) -> {
  69. });
  70. }
  71. public static void checkIsProjectOrModule(ComponentDto component) {
  72. checkArgument(PROJECT_OR_MODULE_QUALIFIERS.contains(component.qualifier()), "Component updated must be a module or a key");
  73. }
  74. /**
  75. *
  76. * @return a map with currentKey/newKey is a bulk update was executed
  77. */
  78. public Map<String, String> simulateBulkUpdateKey(DbSession dbSession, String projectUuid, String stringToReplace, String replacementString) {
  79. return collectAllModules(projectUuid, stringToReplace, mapper(dbSession), false)
  80. .stream()
  81. .collect(Collectors.toMap(
  82. ResourceDto::getKey,
  83. component -> {
  84. String newKey = computeNewKey(component.getKey(), stringToReplace, replacementString);
  85. checkProjectKey(newKey);
  86. return newKey;
  87. }));
  88. }
  89. /**
  90. * @return a map with the component key as key, and boolean as true if key already exists in db
  91. */
  92. public Map<String, Boolean> checkComponentKeys(DbSession dbSession, List<String> newComponentKeys) {
  93. return newComponentKeys.stream().collect(Collectors.toMap(Function.identity(), key -> mapper(dbSession).countResourceByKey(key) > 0));
  94. }
  95. @VisibleForTesting
  96. static String computeNewKey(String key, String stringToReplace, String replacementString) {
  97. return key.replace(stringToReplace, replacementString);
  98. }
  99. public Set<RekeyedResource> bulkUpdateKey(DbSession session, String projectUuid, String stringToReplace, String replacementString,
  100. Predicate<RekeyedResource> rekeyedResourceFilter) {
  101. ComponentKeyUpdaterMapper mapper = session.getMapper(ComponentKeyUpdaterMapper.class);
  102. // must SELECT first everything
  103. Set<ResourceDto> modules = collectAllModules(projectUuid, stringToReplace, mapper, true);
  104. checkNewNameOfAllModules(modules, stringToReplace, replacementString, mapper);
  105. // add branches (no check should be done as branch keys cannot be changed by the user)
  106. Map<String, String> branchBaseKeys = new HashMap<>();
  107. session.getMapper(BranchMapper.class).selectByProjectUuid(projectUuid)
  108. .stream()
  109. .filter(branch -> !projectUuid.equals(branch.getUuid()))
  110. .forEach(branch -> {
  111. Set<ResourceDto> branchModules = collectAllModules(branch.getUuid(), stringToReplace, mapper, true);
  112. modules.addAll(branchModules);
  113. branchModules.forEach(module -> branchBaseKeys.put(module.getKey(), branchBaseKey(module.getKey())));
  114. });
  115. Map<ResourceDto, List<ResourceDto>> allResourcesByModuleMap = new HashMap<>();
  116. for (ResourceDto module : modules) {
  117. allResourcesByModuleMap.put(module, mapper.selectProjectResources(module.getUuid()));
  118. }
  119. Set<RekeyedResource> rekeyedResources = new HashSet<>();
  120. // and then proceed with the batch UPDATE at once
  121. for (ResourceDto module : modules) {
  122. String oldModuleKey = module.getKey();
  123. oldModuleKey = branchBaseKeys.getOrDefault(oldModuleKey, oldModuleKey);
  124. String newModuleKey = computeNewKey(oldModuleKey, stringToReplace, replacementString);
  125. Collection<ResourceDto> resources = Lists.newArrayList(module);
  126. resources.addAll(allResourcesByModuleMap.get(module));
  127. runBatchUpdateForAllResources(resources, oldModuleKey, newModuleKey, mapper,
  128. (resource, oldKey) -> {
  129. RekeyedResource rekeyedResource = new RekeyedResource(resource, oldKey);
  130. if (rekeyedResourceFilter.test(rekeyedResource)) {
  131. rekeyedResources.add(rekeyedResource);
  132. }
  133. });
  134. }
  135. return rekeyedResources;
  136. }
  137. private static String branchBaseKey(String key) {
  138. int index = key.lastIndexOf(ComponentDto.BRANCH_KEY_SEPARATOR);
  139. if (index > -1) {
  140. return key.substring(0, index);
  141. }
  142. index = key.lastIndexOf(ComponentDto.PULL_REQUEST_SEPARATOR);
  143. if (index > -1) {
  144. return key.substring(0, index);
  145. }
  146. return key;
  147. }
  148. private static void runBatchUpdateForAllResources(Collection<ResourceDto> resources, String oldKey, String newKey, ComponentKeyUpdaterMapper mapper,
  149. @Nullable BiConsumer<ResourceDto, String> consumer) {
  150. for (ResourceDto resource : resources) {
  151. String oldResourceKey = resource.getKey();
  152. String newResourceKey = newKey + oldResourceKey.substring(oldKey.length(), oldResourceKey.length());
  153. resource.setKey(newResourceKey);
  154. String oldResourceDeprecatedKey = resource.getDeprecatedKey();
  155. if (StringUtils.isNotBlank(oldResourceDeprecatedKey)) {
  156. String newResourceDeprecatedKey = newKey + oldResourceDeprecatedKey.substring(oldKey.length(), oldResourceDeprecatedKey.length());
  157. resource.setDeprecatedKey(newResourceDeprecatedKey);
  158. }
  159. mapper.update(resource);
  160. if (consumer != null) {
  161. consumer.accept(resource, oldResourceKey);
  162. }
  163. }
  164. }
  165. public static final class RekeyedResource {
  166. private final ResourceDto resource;
  167. private final String oldKey;
  168. public RekeyedResource(ResourceDto resource, String oldKey) {
  169. this.resource = resource;
  170. this.oldKey = oldKey;
  171. }
  172. public ResourceDto getResource() {
  173. return resource;
  174. }
  175. public String getOldKey() {
  176. return oldKey;
  177. }
  178. @Override
  179. public boolean equals(Object o) {
  180. if (this == o) {
  181. return true;
  182. }
  183. if (o == null || getClass() != o.getClass()) {
  184. return false;
  185. }
  186. RekeyedResource that = (RekeyedResource) o;
  187. return Objects.equals(resource.getUuid(), that.resource.getUuid());
  188. }
  189. @Override
  190. public int hashCode() {
  191. return resource.getUuid().hashCode();
  192. }
  193. }
  194. private static Set<ResourceDto> collectAllModules(String projectUuid, String stringToReplace, ComponentKeyUpdaterMapper mapper, boolean includeDisabled) {
  195. ResourceDto project = mapper.selectProjectByUuid(projectUuid);
  196. Set<ResourceDto> modules = new HashSet<>();
  197. if (project.getKey().contains(stringToReplace) && (project.isEnabled() || includeDisabled)) {
  198. modules.add(project);
  199. }
  200. for (ResourceDto submodule : mapper.selectDescendantProjects(projectUuid)) {
  201. modules.addAll(collectAllModules(submodule.getUuid(), stringToReplace, mapper, includeDisabled));
  202. }
  203. return modules;
  204. }
  205. private static void checkNewNameOfAllModules(Set<ResourceDto> modules, String stringToReplace, String replacementString, ComponentKeyUpdaterMapper mapper) {
  206. for (ResourceDto module : modules) {
  207. String newKey = computeNewKey(module.getKey(), stringToReplace, replacementString);
  208. checkProjectKey(newKey);
  209. if (mapper.countResourceByKey(newKey) > 0) {
  210. throw new IllegalArgumentException("Impossible to update key: a component with key \"" + newKey + "\" already exists.");
  211. }
  212. }
  213. }
  214. private static ComponentKeyUpdaterMapper mapper(DbSession dbSession) {
  215. return dbSession.getMapper(ComponentKeyUpdaterMapper.class);
  216. }
  217. }