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.3KB

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