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.

ContextPropertiesPublisher.java 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 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.scanner.report;
  21. import java.util.AbstractMap;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.stream.Collectors;
  26. import org.sonar.api.batch.scm.ScmProvider;
  27. import org.sonar.core.config.CorePropertyDefinitions;
  28. import org.sonar.scanner.ci.CiConfiguration;
  29. import org.sonar.scanner.config.DefaultConfiguration;
  30. import org.sonar.scanner.protocol.output.ScannerReport;
  31. import org.sonar.scanner.protocol.output.ScannerReportWriter;
  32. import org.sonar.scanner.repository.ContextPropertiesCache;
  33. import org.sonar.scanner.scm.ScmConfiguration;
  34. import static org.sonar.core.config.CorePropertyDefinitions.SONAR_ANALYSIS_DETECTEDCI;
  35. import static org.sonar.core.config.CorePropertyDefinitions.SONAR_ANALYSIS_DETECTEDSCM;
  36. public class ContextPropertiesPublisher implements ReportPublisherStep {
  37. private final ContextPropertiesCache cache;
  38. private final DefaultConfiguration config;
  39. private final ScmConfiguration scmConfiguration;
  40. private final CiConfiguration ciConfiguration;
  41. public ContextPropertiesPublisher(ContextPropertiesCache cache, DefaultConfiguration config, ScmConfiguration scmConfiguration, CiConfiguration ciConfiguration) {
  42. this.cache = cache;
  43. this.config = config;
  44. this.scmConfiguration = scmConfiguration;
  45. this.ciConfiguration = ciConfiguration;
  46. }
  47. @Override
  48. public void publish(ScannerReportWriter writer) {
  49. List<Map.Entry<String, String>> properties = new ArrayList<>(cache.getAll().entrySet());
  50. properties.add(constructScmInfo());
  51. properties.add(constructCiInfo());
  52. // properties that are automatically included to report so that
  53. // they can be included to webhook payloads
  54. properties.addAll(config.getProperties().entrySet()
  55. .stream()
  56. .filter(e -> e.getKey().startsWith(CorePropertyDefinitions.SONAR_ANALYSIS))
  57. .collect(Collectors.toList()));
  58. writer.writeContextProperties(properties
  59. .stream()
  60. .map(e -> ScannerReport.ContextProperty.newBuilder()
  61. .setKey(e.getKey())
  62. .setValue(e.getValue())
  63. .build())
  64. .collect(Collectors.toList()));
  65. }
  66. private Map.Entry<String, String> constructScmInfo() {
  67. ScmProvider scmProvider = scmConfiguration.provider();
  68. if (scmProvider != null) {
  69. return new AbstractMap.SimpleEntry<>(SONAR_ANALYSIS_DETECTEDSCM, scmProvider.key());
  70. } else {
  71. return new AbstractMap.SimpleEntry<>(SONAR_ANALYSIS_DETECTEDSCM, "undetected");
  72. }
  73. }
  74. private Map.Entry<String, String> constructCiInfo() {
  75. return new AbstractMap.SimpleEntry<>(SONAR_ANALYSIS_DETECTEDCI, ciConfiguration.getCiName());
  76. }
  77. }