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.

ComponentService.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 java.util.Set;
  22. import org.sonar.api.resources.Qualifiers;
  23. import org.sonar.api.resources.Scopes;
  24. import org.sonar.api.server.ServerSide;
  25. import org.sonar.api.web.UserRole;
  26. import org.sonar.core.util.stream.MoreCollectors;
  27. import org.sonar.db.DbClient;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.component.ComponentDto;
  30. import org.sonar.db.component.ComponentKeyUpdaterDao;
  31. import org.sonar.db.component.ResourceDto;
  32. import org.sonar.server.es.ProjectIndexer;
  33. import org.sonar.server.es.ProjectIndexers;
  34. import org.sonar.server.project.Project;
  35. import org.sonar.server.project.ProjectLifeCycleListeners;
  36. import org.sonar.server.project.RekeyedProject;
  37. import org.sonar.server.user.UserSession;
  38. import static java.util.Collections.emptyList;
  39. import static java.util.Collections.singleton;
  40. import static java.util.Collections.singletonList;
  41. import static org.sonar.core.component.ComponentKeys.isValidProjectKey;
  42. import static org.sonar.db.component.ComponentKeyUpdaterDao.checkIsProjectOrModule;
  43. import static org.sonar.server.ws.WsUtils.checkRequest;
  44. @ServerSide
  45. public class ComponentService {
  46. private final DbClient dbClient;
  47. private final UserSession userSession;
  48. private final ProjectIndexers projectIndexers;
  49. private final ProjectLifeCycleListeners projectLifeCycleListeners;
  50. public ComponentService(DbClient dbClient, UserSession userSession, ProjectIndexers projectIndexers, ProjectLifeCycleListeners projectLifeCycleListeners) {
  51. this.dbClient = dbClient;
  52. this.userSession = userSession;
  53. this.projectIndexers = projectIndexers;
  54. this.projectLifeCycleListeners = projectLifeCycleListeners;
  55. }
  56. public void updateKey(DbSession dbSession, ComponentDto projectOrModule, String newKey) {
  57. userSession.checkComponentPermission(UserRole.ADMIN, projectOrModule);
  58. checkIsProjectOrModule(projectOrModule);
  59. checkProjectOrModuleKeyFormat(newKey);
  60. dbClient.componentKeyUpdaterDao().updateKey(dbSession, projectOrModule.uuid(), newKey);
  61. projectIndexers.commitAndIndex(dbSession, singletonList(projectOrModule), ProjectIndexer.Cause.PROJECT_KEY_UPDATE);
  62. if (isMainProject(projectOrModule)) {
  63. Project newProject = new Project(projectOrModule.uuid(), newKey, projectOrModule.name(), projectOrModule.description(), projectOrModule.getTags());
  64. projectLifeCycleListeners.onProjectsRekeyed(singleton(new RekeyedProject(newProject, projectOrModule.getDbKey())));
  65. }
  66. }
  67. private static boolean isMainProject(ComponentDto projectOrModule) {
  68. return projectOrModule.isRootProject() && projectOrModule.getMainBranchProjectUuid() == null;
  69. }
  70. public void bulkUpdateKey(DbSession dbSession, ComponentDto projectOrModule, String stringToReplace, String replacementString) {
  71. Set<ComponentKeyUpdaterDao.RekeyedResource> rekeyedProjects = dbClient.componentKeyUpdaterDao().bulkUpdateKey(
  72. dbSession, projectOrModule.uuid(), stringToReplace, replacementString,
  73. ComponentService::isMainProject);
  74. projectIndexers.commitAndIndex(dbSession, singletonList(projectOrModule), ProjectIndexer.Cause.PROJECT_KEY_UPDATE);
  75. if (!rekeyedProjects.isEmpty()) {
  76. projectLifeCycleListeners.onProjectsRekeyed(rekeyedProjects.stream()
  77. .map(ComponentService::toRekeyedProject)
  78. .collect(MoreCollectors.toSet(rekeyedProjects.size())));
  79. }
  80. }
  81. private static boolean isMainProject(ComponentKeyUpdaterDao.RekeyedResource rekeyedResource) {
  82. ResourceDto resource = rekeyedResource.getResource();
  83. String resourceKey = resource.getKey();
  84. return Scopes.PROJECT.equals(resource.getScope())
  85. && Qualifiers.PROJECT.equals(resource.getQualifier())
  86. && !resourceKey.contains(ComponentDto.BRANCH_KEY_SEPARATOR)
  87. && !resourceKey.contains(ComponentDto.PULL_REQUEST_SEPARATOR);
  88. }
  89. private static RekeyedProject toRekeyedProject(ComponentKeyUpdaterDao.RekeyedResource rekeyedResource) {
  90. ResourceDto resource = rekeyedResource.getResource();
  91. Project project = new Project(resource.getUuid(), resource.getKey(), resource.getName(), resource.getDescription(), emptyList());
  92. return new RekeyedProject(project, rekeyedResource.getOldKey());
  93. }
  94. private static void checkProjectOrModuleKeyFormat(String key) {
  95. checkRequest(isValidProjectKey(key), "Malformed key for '%s'. It cannot be empty nor contain whitespaces.", key);
  96. }
  97. }