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.

PersistAnalysisPropertiesStep.java 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.ArrayList;
  22. import java.util.List;
  23. import java.util.Set;
  24. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
  25. import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
  26. import org.sonar.ce.task.step.ComputationStep;
  27. import org.sonar.core.util.CloseableIterator;
  28. import org.sonar.core.util.UuidFactory;
  29. import org.sonar.db.DbClient;
  30. import org.sonar.db.DbSession;
  31. import org.sonar.db.component.AnalysisPropertyDto;
  32. import org.sonar.scanner.protocol.output.ScannerReport;
  33. import static org.sonar.core.config.CorePropertyDefinitions.SONAR_ANALYSIS;
  34. import static org.sonar.core.config.CorePropertyDefinitions.SONAR_ANALYSIS_DETECTEDCI;
  35. import static org.sonar.core.config.CorePropertyDefinitions.SONAR_ANALYSIS_DETECTEDSCM;
  36. /**
  37. * Persist analysis properties
  38. */
  39. public class PersistAnalysisPropertiesStep implements ComputationStep {
  40. private static final String SONAR_PULL_REQUEST = "sonar.pullrequest.";
  41. private static final Set<String> ANALYSIS_PROPERTIES_TO_PERSIST = Set.of(SONAR_ANALYSIS_DETECTEDSCM, SONAR_ANALYSIS_DETECTEDCI);
  42. private final DbClient dbClient;
  43. private final AnalysisMetadataHolder analysisMetadataHolder;
  44. private final BatchReportReader reportReader;
  45. private final UuidFactory uuidFactory;
  46. public PersistAnalysisPropertiesStep(DbClient dbClient, AnalysisMetadataHolder analysisMetadataHolder,
  47. BatchReportReader reportReader, UuidFactory uuidFactory) {
  48. this.dbClient = dbClient;
  49. this.analysisMetadataHolder = analysisMetadataHolder;
  50. this.reportReader = reportReader;
  51. this.uuidFactory = uuidFactory;
  52. }
  53. @Override
  54. public void execute(ComputationStep.Context context) {
  55. List<AnalysisPropertyDto> analysisPropertyDtos = new ArrayList<>();
  56. try (CloseableIterator<ScannerReport.ContextProperty> it = reportReader.readContextProperties()) {
  57. it.forEachRemaining(
  58. contextProperty -> {
  59. String propertyKey = contextProperty.getKey();
  60. if (propertyKey.startsWith(SONAR_ANALYSIS) || propertyKey.startsWith(SONAR_PULL_REQUEST) ||
  61. ANALYSIS_PROPERTIES_TO_PERSIST.contains(propertyKey)) {
  62. analysisPropertyDtos.add(new AnalysisPropertyDto()
  63. .setUuid(uuidFactory.create())
  64. .setKey(propertyKey)
  65. .setValue(contextProperty.getValue())
  66. .setAnalysisUuid(analysisMetadataHolder.getUuid()));
  67. }
  68. });
  69. }
  70. if (analysisPropertyDtos.isEmpty()) {
  71. return;
  72. }
  73. try (DbSession dbSession = dbClient.openSession(false)) {
  74. dbClient.analysisPropertiesDao().insert(dbSession, analysisPropertyDtos);
  75. dbSession.commit();
  76. }
  77. }
  78. @Override
  79. public String getDescription() {
  80. return "Persist analysis properties";
  81. }
  82. }