您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

NewCodeReferenceBranchComponentUuids.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.ce.task.projectanalysis.period;
  21. import com.google.common.base.Preconditions;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Optional;
  26. import javax.annotation.CheckForNull;
  27. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
  28. import org.sonar.db.DbClient;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.component.BranchDto;
  31. import org.sonar.db.component.ComponentDto;
  32. import org.sonar.db.newcodeperiod.NewCodePeriodType;
  33. import static org.sonar.db.component.ComponentDto.removeBranchAndPullRequestFromKey;
  34. /**
  35. * Cache a map between component keys and uuids in the reference branch
  36. */
  37. public class NewCodeReferenceBranchComponentUuids {
  38. private final DbClient dbClient;
  39. private final AnalysisMetadataHolder analysisMetadataHolder;
  40. private final PeriodHolder periodHolder;
  41. private Map<String, String> referenceBranchComponentsUuidsByKey;
  42. public NewCodeReferenceBranchComponentUuids(AnalysisMetadataHolder analysisMetadataHolder, PeriodHolder periodHolder, DbClient dbClient) {
  43. this.analysisMetadataHolder = analysisMetadataHolder;
  44. this.periodHolder = periodHolder;
  45. this.dbClient = dbClient;
  46. }
  47. private void lazyInit() {
  48. if (referenceBranchComponentsUuidsByKey == null) {
  49. Preconditions.checkState(periodHolder.hasPeriod() && periodHolder.getPeriod().getMode().equals(NewCodePeriodType.REFERENCE_BRANCH.name()));
  50. referenceBranchComponentsUuidsByKey = new HashMap<>();
  51. try (DbSession dbSession = dbClient.openSession(false)) {
  52. String referenceKey = periodHolder.getPeriod().getModeParameter() != null ? periodHolder.getPeriod().getModeParameter() : "";
  53. Optional<BranchDto> opt = dbClient.branchDao().selectByBranchKey(dbSession, analysisMetadataHolder.getProject().getUuid(), referenceKey);
  54. if (opt.isPresent()) {
  55. init(opt.get().getUuid(), dbSession);
  56. }
  57. }
  58. }
  59. }
  60. private void init(String referenceBranchUuid, DbSession dbSession) {
  61. boolean hasReferenceBranchAnalysis = dbClient.snapshotDao().selectLastAnalysisByRootComponentUuid(dbSession, referenceBranchUuid).isPresent();
  62. if (hasReferenceBranchAnalysis) {
  63. List<ComponentDto> components = dbClient.componentDao().selectByProjectUuid(referenceBranchUuid, dbSession);
  64. for (ComponentDto dto : components) {
  65. referenceBranchComponentsUuidsByKey.put(dto.getKey(), dto.uuid());
  66. }
  67. }
  68. }
  69. @CheckForNull
  70. public String getComponentUuid(String dbKey) {
  71. lazyInit();
  72. String cleanComponentKey = removeBranchAndPullRequestFromKey(dbKey);
  73. return referenceBranchComponentsUuidsByKey.get(cleanComponentKey);
  74. }
  75. }