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.

PersistProjectLinksStep.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.step;
  21. import java.util.HashSet;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Optional;
  25. import java.util.Set;
  26. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
  27. import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
  28. import org.sonar.ce.task.projectanalysis.component.Component;
  29. import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
  30. import org.sonar.ce.task.step.ComputationStep;
  31. import org.sonar.core.util.UuidFactory;
  32. import org.sonar.db.DbClient;
  33. import org.sonar.db.DbSession;
  34. import org.sonar.db.component.ProjectLinkDto;
  35. import org.sonar.scanner.protocol.output.ScannerReport;
  36. import org.sonar.scanner.protocol.output.ScannerReport.ComponentLink.ComponentLinkType;
  37. import static com.google.common.base.Preconditions.checkArgument;
  38. public class PersistProjectLinksStep implements ComputationStep {
  39. private static final Map<ComponentLinkType, String> typesConverter = Map.of(
  40. ComponentLinkType.HOME, ProjectLinkDto.TYPE_HOME_PAGE,
  41. ComponentLinkType.SCM, ProjectLinkDto.TYPE_SOURCES,
  42. ComponentLinkType.CI, ProjectLinkDto.TYPE_CI,
  43. ComponentLinkType.ISSUE, ProjectLinkDto.TYPE_ISSUE_TRACKER);
  44. private final AnalysisMetadataHolder analysisMetadataHolder;
  45. private final DbClient dbClient;
  46. private final TreeRootHolder treeRootHolder;
  47. private final BatchReportReader reportReader;
  48. private final UuidFactory uuidFactory;
  49. public PersistProjectLinksStep(AnalysisMetadataHolder analysisMetadataHolder, DbClient dbClient, TreeRootHolder treeRootHolder,
  50. BatchReportReader reportReader, UuidFactory uuidFactory) {
  51. this.analysisMetadataHolder = analysisMetadataHolder;
  52. this.dbClient = dbClient;
  53. this.treeRootHolder = treeRootHolder;
  54. this.reportReader = reportReader;
  55. this.uuidFactory = uuidFactory;
  56. }
  57. @Override
  58. public void execute(ComputationStep.Context context) {
  59. if (!analysisMetadataHolder.getBranch().isMain()) {
  60. return;
  61. }
  62. try (DbSession session = dbClient.openSession(false)) {
  63. Component project = treeRootHolder.getRoot();
  64. ScannerReport.Component batchComponent = reportReader.readComponent(project.getReportAttributes().getRef());
  65. List<ProjectLinkDto> previousLinks = dbClient.projectLinkDao().selectByProjectUuid(session, project.getUuid());
  66. mergeLinks(session, project.getUuid(), batchComponent.getLinkList(), previousLinks);
  67. session.commit();
  68. }
  69. }
  70. private void mergeLinks(DbSession session, String componentUuid, List<ScannerReport.ComponentLink> links, List<ProjectLinkDto> previousLinks) {
  71. Set<String> linkType = new HashSet<>();
  72. links.forEach(
  73. link -> {
  74. String type = convertType(link.getType());
  75. checkArgument(!linkType.contains(type), "Link of type '%s' has already been declared on component '%s'", type, componentUuid);
  76. linkType.add(type);
  77. Optional<ProjectLinkDto> previousLink = previousLinks.stream()
  78. .filter(input -> input != null && input.getType().equals(convertType(link.getType())))
  79. .findFirst();
  80. if (previousLink.isPresent()) {
  81. previousLink.get().setHref(link.getHref());
  82. dbClient.projectLinkDao().update(session, previousLink.get());
  83. } else {
  84. dbClient.projectLinkDao().insert(session,
  85. new ProjectLinkDto()
  86. .setUuid(uuidFactory.create())
  87. .setProjectUuid(componentUuid)
  88. .setType(type)
  89. .setHref(link.getHref()));
  90. }
  91. });
  92. previousLinks.stream()
  93. .filter(dto -> !linkType.contains(dto.getType()))
  94. .filter(dto -> ProjectLinkDto.PROVIDED_TYPES.contains(dto.getType()))
  95. .forEach(dto -> dbClient.projectLinkDao().delete(session, dto.getUuid()));
  96. }
  97. private static String convertType(ComponentLinkType reportType) {
  98. String type = typesConverter.get(reportType);
  99. checkArgument(type != null, "Unsupported type %s", reportType.name());
  100. return type;
  101. }
  102. @Override
  103. public String getDescription() {
  104. return "Persist project links";
  105. }
  106. }