3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.task.projectanalysis.scm;
22 import com.google.common.base.Optional;
23 import java.util.HashMap;
25 import org.sonar.api.utils.log.Logger;
26 import org.sonar.api.utils.log.Loggers;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.source.FileSourceDto;
30 import org.sonar.scanner.protocol.output.ScannerReport;
31 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
32 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
33 import org.sonar.server.computation.task.projectanalysis.component.Component;
34 import org.sonar.server.computation.task.projectanalysis.component.Component.Status;
35 import org.sonar.server.computation.task.projectanalysis.source.SourceHashRepository;
37 import static java.util.Objects.requireNonNull;
39 public class ScmInfoRepositoryImpl implements ScmInfoRepository {
41 private static final Logger LOGGER = Loggers.get(ScmInfoRepositoryImpl.class);
43 private final BatchReportReader batchReportReader;
44 private final AnalysisMetadataHolder analysisMetadataHolder;
45 private final DbClient dbClient;
46 private final SourceHashRepository sourceHashRepository;
48 private final Map<Component, ScmInfo> scmInfoCache = new HashMap<>();
50 public ScmInfoRepositoryImpl(BatchReportReader batchReportReader, AnalysisMetadataHolder analysisMetadataHolder, DbClient dbClient, SourceHashRepository sourceHashRepository) {
51 this.batchReportReader = batchReportReader;
52 this.analysisMetadataHolder = analysisMetadataHolder;
53 this.dbClient = dbClient;
54 this.sourceHashRepository = sourceHashRepository;
58 public Optional<ScmInfo> getScmInfo(Component component) {
59 requireNonNull(component, "Component cannot be bull");
60 return initializeScmInfoForComponent(component);
63 private Optional<ScmInfo> initializeScmInfoForComponent(Component component) {
64 if (component.getType() != Component.Type.FILE) {
65 return Optional.absent();
67 ScmInfo scmInfo = scmInfoCache.get(component);
68 if (scmInfo != null) {
69 return optionalOf(scmInfo);
72 scmInfo = getScmInfoForComponent(component);
73 scmInfoCache.put(component, scmInfo);
74 return optionalOf(scmInfo);
77 private static Optional<ScmInfo> optionalOf(ScmInfo scmInfo) {
78 if (scmInfo == NoScmInfo.INSTANCE) {
79 return Optional.absent();
81 return Optional.of(scmInfo);
84 private ScmInfo getScmInfoForComponent(Component component) {
85 ScannerReport.Changesets changesets = batchReportReader.readChangesets(component.getReportAttributes().getRef());
86 if (changesets == null) {
87 LOGGER.trace("No SCM info for file '{}'", component.getKey());
88 return NoScmInfo.INSTANCE;
90 if (changesets.getCopyFromPrevious()) {
91 return getScmInfoFromDb(component);
93 return getScmInfoFromReport(component, changesets);
96 private ScmInfo getScmInfoFromDb(Component file) {
97 if (analysisMetadataHolder.isFirstAnalysis()) {
98 return NoScmInfo.INSTANCE;
101 LOGGER.trace("Reading SCM info from db for file '{}'", file.getKey());
102 try (DbSession dbSession = dbClient.openSession(false)) {
103 FileSourceDto dto = dbClient.fileSourceDao().selectSourceByFileUuid(dbSession, file.getUuid());
104 if (dto == null || !isDtoValid(file, dto)) {
105 return NoScmInfo.INSTANCE;
107 return DbScmInfo.create(file, dto.getSourceData().getLinesList()).or(NoScmInfo.INSTANCE);
111 private boolean isDtoValid(Component file, FileSourceDto dto) {
112 if (analysisMetadataHolder.isIncrementalAnalysis() && file.getStatus() == Status.SAME) {
115 return sourceHashRepository.getRawSourceHash(file).equals(dto.getSrcHash());
118 private static ScmInfo getScmInfoFromReport(Component file, ScannerReport.Changesets changesets) {
119 LOGGER.trace("Reading SCM info from report for file '{}'", file.getKey());
120 return new ReportScmInfo(changesets);
124 * Internally used to populate cache when no ScmInfo exist.
126 private enum NoScmInfo implements ScmInfo {
129 public Changeset getLatestChangeset() {
130 return notImplemented();
134 public Changeset getChangesetForLine(int lineNumber) {
135 return notImplemented();
139 public boolean hasChangesetForLine(int lineNumber) {
140 return notImplemented();
144 public Iterable<Changeset> getAllChangesets() {
145 return notImplemented();
148 private <T> T notImplemented() {
149 throw new UnsupportedOperationException("NoScmInfo does not implement any method");