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.

MergeBranchComponentUuids.java 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.computation.task.projectanalysis.component;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Optional;
  25. import javax.annotation.CheckForNull;
  26. import org.sonar.db.DbClient;
  27. import org.sonar.db.DbSession;
  28. import org.sonar.db.component.BranchDto;
  29. import org.sonar.db.component.ComponentDto;
  30. import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
  31. import static com.google.common.base.Preconditions.checkState;
  32. import static org.sonar.db.component.ComponentDto.removeBranchAndPullRequestFromKey;
  33. /**
  34. * Cache a map between component keys and uuids in the merge branch
  35. */
  36. public class MergeBranchComponentUuids {
  37. private final AnalysisMetadataHolder analysisMetadataHolder;
  38. private final DbClient dbClient;
  39. private Map<String, String> uuidsByKey;
  40. private String mergeBranchName;
  41. public MergeBranchComponentUuids(AnalysisMetadataHolder analysisMetadataHolder, DbClient dbClient) {
  42. this.analysisMetadataHolder = analysisMetadataHolder;
  43. this.dbClient = dbClient;
  44. }
  45. private void lazyInit() {
  46. if (uuidsByKey == null) {
  47. String mergeBranchUuid = analysisMetadataHolder.getBranch().getMergeBranchUuid().get();
  48. uuidsByKey = new HashMap<>();
  49. try (DbSession dbSession = dbClient.openSession(false)) {
  50. List<ComponentDto> components = dbClient.componentDao().selectByProjectUuid(mergeBranchUuid, dbSession);
  51. for (ComponentDto dto : components) {
  52. uuidsByKey.put(dto.getKey(), dto.uuid());
  53. }
  54. Optional<BranchDto> opt = dbClient.branchDao().selectByUuid(dbSession, mergeBranchUuid);
  55. checkState(opt.isPresent(), "Merge branch '%s' does not exist", mergeBranchUuid);
  56. mergeBranchName = opt.get().getKey();
  57. }
  58. }
  59. }
  60. public String getMergeBranchName() {
  61. lazyInit();
  62. return mergeBranchName;
  63. }
  64. @CheckForNull
  65. public String getUuid(String dbKey) {
  66. lazyInit();
  67. String cleanComponentKey = removeBranchAndPullRequestFromKey(dbKey);
  68. return uuidsByKey.get(cleanComponentKey);
  69. }
  70. }