aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/MetadataPublisher.java
blob: 6f6fe76ebd010b6a2c40c853fd6bb337ed9b282a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.scanner.report;

import java.io.File;
import java.nio.file.Path;
import java.util.Map.Entry;
import java.util.regex.Pattern;
import org.sonar.api.batch.fs.internal.AbstractProjectOrModule;
import org.sonar.api.batch.scm.ScmProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.scanner.ProjectInfo;
import org.sonar.scanner.bootstrap.ScannerPlugin;
import org.sonar.scanner.bootstrap.ScannerPluginRepository;
import org.sonar.scanner.cpd.CpdSettings;
import org.sonar.scanner.fs.InputModuleHierarchy;
import org.sonar.scanner.protocol.output.ScannerReport;
import org.sonar.scanner.protocol.output.ScannerReport.Metadata.BranchType;
import org.sonar.scanner.protocol.output.ScannerReportWriter;
import org.sonar.scanner.repository.ReferenceBranchSupplier;
import org.sonar.scanner.rule.QProfile;
import org.sonar.scanner.rule.QualityProfiles;
import org.sonar.scanner.scan.branch.BranchConfiguration;
import org.sonar.scanner.scan.filesystem.InputComponentStore;
import org.sonar.scanner.scm.ScmConfiguration;
import org.sonar.scanner.scm.ScmRevision;

public class MetadataPublisher implements ReportPublisherStep {

  private static final Logger LOG = LoggerFactory.getLogger(MetadataPublisher.class);

  private final QualityProfiles qProfiles;
  private final ProjectInfo projectInfo;
  private final InputModuleHierarchy moduleHierarchy;
  private final CpdSettings cpdSettings;
  private final ScannerPluginRepository pluginRepository;
  private final BranchConfiguration branchConfiguration;
  private final ScmRevision scmRevision;
  private final InputComponentStore componentStore;
  private final ScmConfiguration scmConfiguration;
  private final ReferenceBranchSupplier referenceBranchSupplier;

  public MetadataPublisher(ProjectInfo projectInfo, InputModuleHierarchy moduleHierarchy, QualityProfiles qProfiles,
    CpdSettings cpdSettings, ScannerPluginRepository pluginRepository, BranchConfiguration branchConfiguration,
    ScmRevision scmRevision, InputComponentStore componentStore, ScmConfiguration scmConfiguration, ReferenceBranchSupplier referenceBranchSupplier) {
    this.projectInfo = projectInfo;
    this.moduleHierarchy = moduleHierarchy;
    this.qProfiles = qProfiles;
    this.cpdSettings = cpdSettings;
    this.pluginRepository = pluginRepository;
    this.branchConfiguration = branchConfiguration;
    this.scmRevision = scmRevision;
    this.componentStore = componentStore;
    this.scmConfiguration = scmConfiguration;
    this.referenceBranchSupplier = referenceBranchSupplier;
  }

  @Override
  public void publish(ScannerReportWriter writer) {
    AbstractProjectOrModule rootProject = moduleHierarchy.root();
    ScannerReport.Metadata.Builder builder = ScannerReport.Metadata.newBuilder()
      .setAnalysisDate(projectInfo.getAnalysisDate().getTime())
      // Here we want key without branch
      .setProjectKey(rootProject.key())
      .setCrossProjectDuplicationActivated(cpdSettings.isCrossProjectDuplicationEnabled())
      .setRootComponentRef(rootProject.scannerId());
    projectInfo.getProjectVersion().ifPresent(builder::setProjectVersion);
    projectInfo.getBuildString().ifPresent(builder::setBuildString);

    if (branchConfiguration.branchName() != null) {
      addBranchInformation(builder);
    }

    String newCodeReferenceBranch = referenceBranchSupplier.getFromProperties();
    if (newCodeReferenceBranch != null) {
      builder.setNewCodeReferenceBranch(newCodeReferenceBranch);
    }

    addScmInformation(builder);
    addNotAnalyzedFileCountsByLanguage(builder);

    for (QProfile qp : qProfiles.findAll()) {
      builder.putQprofilesPerLanguage(qp.getLanguage(), ScannerReport.Metadata.QProfile.newBuilder()
        .setKey(qp.getKey())
        .setLanguage(qp.getLanguage())
        .setName(qp.getName())
        .setRulesUpdatedAt(qp.getRulesUpdatedAt().getTime()).build());
    }
    for (Entry<String, ScannerPlugin> pluginEntry : pluginRepository.getPluginsByKey().entrySet()) {
      builder.putPluginsByKey(pluginEntry.getKey(), ScannerReport.Metadata.Plugin.newBuilder()
        .setKey(pluginEntry.getKey())
        .setUpdatedAt(pluginEntry.getValue().getUpdatedAt()).build());
    }

    addRelativePathFromScmRoot(builder);

    writer.writeMetadata(builder.build());
  }

  private void addRelativePathFromScmRoot(ScannerReport.Metadata.Builder builder) {
    ScmProvider scmProvider = scmConfiguration.provider();
    if (scmProvider == null) {
      return;
    }

    Path projectBasedir = moduleHierarchy.root().getBaseDir();
    try {
      builder.setRelativePathFromScmRoot(toSonarQubePath(scmProvider.relativePathFromScmRoot(projectBasedir)));
    } catch (UnsupportedOperationException e) {
      LOG.debug(e.getMessage());
    }
  }

  private void addScmInformation(ScannerReport.Metadata.Builder builder) {
    try {
      scmRevision.get().ifPresent(revisionId -> {
        LOG.info("SCM revision ID '{}'", revisionId);
        builder.setScmRevisionId(revisionId);
      });
    } catch (UnsupportedOperationException e) {
      LOG.debug(e.getMessage());
    }
  }

  private void addNotAnalyzedFileCountsByLanguage(ScannerReport.Metadata.Builder builder) {
    builder.putAllNotAnalyzedFilesByLanguage(componentStore.getNotAnalysedFilesByLanguage());
  }

  private void addBranchInformation(ScannerReport.Metadata.Builder builder) {
    builder.setBranchName(branchConfiguration.branchName());
    BranchType branchType = toProtobufBranchType(branchConfiguration.branchType());
    builder.setBranchType(branchType);
    String referenceBranch = branchConfiguration.referenceBranchName();
    if (referenceBranch != null) {
      builder.setReferenceBranchName(referenceBranch);
    }
    String targetBranchName = branchConfiguration.targetBranchName();
    if (targetBranchName != null) {
      builder.setTargetBranchName(targetBranchName);
    }
    if (branchType == BranchType.PULL_REQUEST) {
      builder.setPullRequestKey(branchConfiguration.pullRequestKey());
    }
  }

  private static BranchType toProtobufBranchType(org.sonar.scanner.scan.branch.BranchType branchType) {
    if (branchType == org.sonar.scanner.scan.branch.BranchType.PULL_REQUEST) {
      return BranchType.PULL_REQUEST;
    }

    return BranchType.BRANCH;
  }

  private static String toSonarQubePath(Path path) {
    String pathAsString = path.toString();
    char sonarQubeSeparatorChar = '/';
    if (File.separatorChar != sonarQubeSeparatorChar) {
      return pathAsString.replaceAll(Pattern.quote(File.separator), String.valueOf(sonarQubeSeparatorChar));
    }
    return pathAsString;
  }

}