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.

ComponentUuidFactoryWithMigration.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 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.ce.task.projectanalysis.component;
  21. import java.util.Collections;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.function.Function;
  26. import javax.annotation.CheckForNull;
  27. import javax.annotation.Nullable;
  28. import org.apache.commons.lang.StringUtils;
  29. import org.sonar.api.resources.Qualifiers;
  30. import org.sonar.api.resources.Scopes;
  31. import org.sonar.ce.task.projectanalysis.analysis.Branch;
  32. import org.sonar.core.util.Uuids;
  33. import org.sonar.db.DbClient;
  34. import org.sonar.db.DbSession;
  35. import org.sonar.db.component.BranchType;
  36. import org.sonar.db.component.ComponentDto;
  37. import org.sonar.db.component.ComponentWithModuleUuidDto;
  38. import org.sonar.db.component.KeyWithUuidDto;
  39. public class ComponentUuidFactoryWithMigration implements ComponentUuidFactory {
  40. private final Map<String, String> uuidsByDbKey = new HashMap<>();
  41. private final Map<String, String> uuidsByMigratedKey = new HashMap<>();
  42. public ComponentUuidFactoryWithMigration(DbClient dbClient, DbSession dbSession, String rootKey, Branch branch,
  43. Function<String, String> pathToKey, Map<String, String> reportModulesPath) {
  44. Map<String, String> modulePathsByUuid;
  45. List<KeyWithUuidDto> keys;
  46. if (branch.isMain()) {
  47. keys = dbClient.componentDao().selectUuidsByKeyFromProjectKey(dbSession, rootKey);
  48. } else if (branch.getType() == BranchType.PULL_REQUEST) {
  49. keys = dbClient.componentDao().selectUuidsByKeyFromProjectKeyAndPullRequest(dbSession, rootKey, branch.getPullRequestKey());
  50. } else {
  51. keys = dbClient.componentDao().selectUuidsByKeyFromProjectKeyAndBranch(dbSession, rootKey, branch.getName());
  52. }
  53. keys.forEach(dto -> uuidsByDbKey.put(dto.key(), dto.uuid()));
  54. if (!reportModulesPath.isEmpty()) {
  55. modulePathsByUuid = loadModulePathsByUuid(dbClient, dbSession, rootKey, branch, reportModulesPath);
  56. if (!modulePathsByUuid.isEmpty()) {
  57. doMigration(dbClient, dbSession, rootKey, pathToKey, modulePathsByUuid);
  58. }
  59. }
  60. }
  61. private void doMigration(DbClient dbClient, DbSession dbSession, String rootKey, Function<String, String> pathToKey, Map<String, String> modulePathsByUuid) {
  62. List<ComponentWithModuleUuidDto> dtos = loadComponentsWithModuleUuid(dbClient, dbSession, rootKey);
  63. for (ComponentWithModuleUuidDto dto : dtos) {
  64. if ("/".equals(dto.path())) {
  65. // skip root folders
  66. continue;
  67. }
  68. if (Scopes.PROJECT.equals(dto.scope())) {
  69. String modulePathFromRootProject = modulePathsByUuid.get(dto.uuid());
  70. if (modulePathFromRootProject != null || StringUtils.isEmpty(dto.moduleUuid())) {
  71. // means that it's a root or a module with a valid path (to avoid overwriting key of root)
  72. pathToKey.apply(modulePathFromRootProject);
  73. uuidsByMigratedKey.put(pathToKey.apply(modulePathFromRootProject), dto.uuid());
  74. }
  75. } else {
  76. String modulePathFromRootProject = modulePathsByUuid.get(dto.moduleUuid());
  77. String componentPath = createComponentPath(dto, modulePathFromRootProject);
  78. uuidsByMigratedKey.put(pathToKey.apply(componentPath), dto.uuid());
  79. }
  80. }
  81. }
  82. @CheckForNull
  83. private static String createComponentPath(ComponentWithModuleUuidDto dto, @Nullable String modulePathFromRootProject) {
  84. if (StringUtils.isEmpty(modulePathFromRootProject)) {
  85. return dto.path();
  86. }
  87. if (StringUtils.isEmpty(dto.path())) {
  88. // will be the case for modules
  89. return modulePathFromRootProject;
  90. }
  91. return modulePathFromRootProject + "/" + dto.path();
  92. }
  93. private static List<ComponentWithModuleUuidDto> loadComponentsWithModuleUuid(DbClient dbClient, DbSession dbSession, String rootKey) {
  94. return dbClient.componentDao().selectEnabledComponentsWithModuleUuidFromProjectKey(dbSession, rootKey);
  95. }
  96. private static Map<String, String> loadModulePathsByUuid(DbClient dbClient, DbSession dbSession, String rootKey,
  97. Branch branch, Map<String, String> pathByModuleKey) {
  98. String branchKey = branch.getType() == BranchType.BRANCH ? branch.getName() : null;
  99. String prKey = branch.getType() == BranchType.PULL_REQUEST ? branch.getPullRequestKey() : null;
  100. List<ComponentDto> moduleDtos = dbClient.componentDao()
  101. .selectProjectAndModulesFromProjectKey(dbSession, rootKey, true, branchKey, prKey).stream()
  102. .filter(c -> Qualifiers.MODULE.equals(c.qualifier()))
  103. .toList();
  104. if (moduleDtos.isEmpty()) {
  105. return Collections.emptyMap();
  106. }
  107. Map<String, String> modulePathByUuid = new HashMap<>();
  108. for (ComponentDto dto : moduleDtos) {
  109. String relativePath = pathByModuleKey.get(dto.getKey());
  110. if (relativePath != null) {
  111. modulePathByUuid.put(dto.uuid(), relativePath);
  112. }
  113. }
  114. return modulePathByUuid;
  115. }
  116. /**
  117. * Get UUID from component having the same key in database if it exists, otherwise look for migrated keys, and finally generate a new one.
  118. */
  119. @Override
  120. public String getOrCreateForKey(String key) {
  121. return uuidsByDbKey.computeIfAbsent(key, k1 -> uuidsByMigratedKey.computeIfAbsent(k1, k2 -> Uuids.create()));
  122. }
  123. }