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 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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 java.util.Collection;
  23. import java.util.List;
  24. import java.util.Objects;
  25. import java.util.function.BiConsumer;
  26. import javax.annotation.Nullable;
  27. import org.apache.commons.lang.StringUtils;
  28. import org.sonar.api.resources.Qualifiers;
  29. import org.sonar.api.resources.Scopes;
  30. import org.sonar.db.Dao;
  31. import org.sonar.db.DbSession;
  32. import org.sonar.db.audit.AuditPersister;
  33. import org.sonar.db.audit.model.ComponentKeyNewValue;
  34. import static org.sonar.db.component.ComponentDto.BRANCH_KEY_SEPARATOR;
  35. import static org.sonar.db.component.ComponentDto.generateBranchKey;
  36. /**
  37. * Class used to rename the key of a project and its resources.
  38. *
  39. * @since 3.2
  40. */
  41. public class ComponentKeyUpdaterDao implements Dao {
  42. private AuditPersister auditPersister;
  43. public ComponentKeyUpdaterDao() {
  44. }
  45. public ComponentKeyUpdaterDao(AuditPersister auditPersister) {
  46. this.auditPersister = auditPersister;
  47. }
  48. public void updateKey(DbSession dbSession, String projectUuid, String newKey) {
  49. ComponentKeyUpdaterMapper mapper = dbSession.getMapper(ComponentKeyUpdaterMapper.class);
  50. checkExistentKey(mapper, newKey);
  51. // must SELECT first everything
  52. ResourceDto project = mapper.selectProjectByUuid(projectUuid);
  53. String projectOldKey = project.getKey();
  54. List<ResourceDto> resources = mapper.selectProjectResources(projectUuid);
  55. resources.add(project);
  56. // add branch components
  57. dbSession.getMapper(BranchMapper.class).selectByProjectUuid(projectUuid).stream()
  58. .filter(branch -> !projectUuid.equals(branch.getUuid()))
  59. .forEach(branch -> {
  60. resources.addAll(mapper.selectProjectResources(branch.getUuid()));
  61. resources.add(mapper.selectProjectByUuid(branch.getUuid()));
  62. });
  63. // and then proceed with the batch UPDATE at once
  64. runBatchUpdateForAllResources(resources, projectOldKey, newKey, mapper, (resource, oldKey) -> {
  65. }, dbSession);
  66. }
  67. public void updateApplicationBranchKey(DbSession dbSession, String appBranchUuid, String appKey, String newBranchName) {
  68. ComponentKeyUpdaterMapper mapper = dbSession.getMapper(ComponentKeyUpdaterMapper.class);
  69. String newAppBranchKey = generateBranchKey(appKey, newBranchName);
  70. checkExistentKey(mapper, newAppBranchKey);
  71. ResourceDto appBranch = mapper.selectProjectByUuid(appBranchUuid);
  72. String appBranchOldKey = appBranch.getKey();
  73. appBranch.setKey(newAppBranchKey);
  74. mapper.updateComponent(appBranch);
  75. if(auditPersister != null) {
  76. auditPersister.componentKeyBranchUpdate(dbSession, new ComponentKeyNewValue(appBranchUuid, appBranchOldKey, newAppBranchKey), Qualifiers.APP);
  77. }
  78. String oldAppBranchFragment = appBranchOldKey.replace(BRANCH_KEY_SEPARATOR, "");
  79. String newAppBranchFragment = appKey + newBranchName;
  80. for (ResourceDto appBranchResource : mapper.selectProjectResources(appBranchUuid)) {
  81. String newKey = computeNewKey(appBranchResource.getKey(), oldAppBranchFragment, newAppBranchFragment);
  82. appBranchResource.setKey(newKey);
  83. mapper.updateComponent(appBranchResource);
  84. }
  85. }
  86. @VisibleForTesting
  87. static String computeNewKey(String key, String stringToReplace, String replacementString) {
  88. return key.replace(stringToReplace, replacementString);
  89. }
  90. private void runBatchUpdateForAllResources(Collection<ResourceDto> resources, String oldKey, String newKey, ComponentKeyUpdaterMapper mapper,
  91. @Nullable BiConsumer<ResourceDto, String> consumer, DbSession dbSession) {
  92. for (ResourceDto resource : resources) {
  93. String oldResourceKey = resource.getKey();
  94. String newResourceKey = newKey + oldResourceKey.substring(oldKey.length());
  95. resource.setKey(newResourceKey);
  96. String oldResourceDeprecatedKey = resource.getDeprecatedKey();
  97. if (StringUtils.isNotBlank(oldResourceDeprecatedKey)) {
  98. String newResourceDeprecatedKey = newKey + oldResourceDeprecatedKey.substring(oldKey.length());
  99. resource.setDeprecatedKey(newResourceDeprecatedKey);
  100. }
  101. mapper.updateComponent(resource);
  102. if (resource.getScope().equals(Scopes.PROJECT) && (resource.getQualifier().equals(Qualifiers.PROJECT) || resource.getQualifier().equals(Qualifiers.APP))) {
  103. if(auditPersister != null) {
  104. auditPersister.componentKeyUpdate(dbSession,
  105. new ComponentKeyNewValue(resource.getUuid(), oldResourceKey, newResourceKey), resource.getQualifier());
  106. }
  107. mapper.updateProject(oldResourceKey, newResourceKey);
  108. }
  109. if (consumer != null) {
  110. consumer.accept(resource, oldResourceKey);
  111. }
  112. }
  113. }
  114. public static final class RekeyedResource {
  115. private final ResourceDto resource;
  116. private final String oldKey;
  117. public RekeyedResource(ResourceDto resource, String oldKey) {
  118. this.resource = resource;
  119. this.oldKey = oldKey;
  120. }
  121. public ResourceDto getResource() {
  122. return resource;
  123. }
  124. public String getOldKey() {
  125. return oldKey;
  126. }
  127. @Override
  128. public boolean equals(Object o) {
  129. if (this == o) {
  130. return true;
  131. }
  132. if (o == null || getClass() != o.getClass()) {
  133. return false;
  134. }
  135. RekeyedResource that = (RekeyedResource) o;
  136. return Objects.equals(resource.getUuid(), that.resource.getUuid());
  137. }
  138. @Override
  139. public int hashCode() {
  140. return resource.getUuid().hashCode();
  141. }
  142. }
  143. private static ComponentKeyUpdaterMapper mapper(DbSession dbSession) {
  144. return dbSession.getMapper(ComponentKeyUpdaterMapper.class);
  145. }
  146. public static void checkExistentKey(ComponentKeyUpdaterMapper mapper, String resourceKey) {
  147. if (mapper.countResourceByKey(resourceKey) > 0) {
  148. throw new IllegalArgumentException("Impossible to update key: a component with key \"" + resourceKey + "\" already exists.");
  149. }
  150. }
  151. }