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 java.util.Optional;
23 import org.sonar.api.utils.log.Logger;
24 import org.sonar.api.utils.log.Loggers;
25 import org.sonar.db.DbClient;
26 import org.sonar.db.DbSession;
27 import org.sonar.db.source.FileSourceDto;
28 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
29 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
30 import org.sonar.server.computation.task.projectanalysis.component.Component;
31 import org.sonar.server.computation.task.projectanalysis.component.Component.Status;
32 import org.sonar.server.computation.task.projectanalysis.component.MergeBranchComponentUuids;
33 import org.sonar.server.computation.task.projectanalysis.scm.ScmInfoRepositoryImpl.NoScmInfo;
34 import org.sonar.server.computation.task.projectanalysis.source.SourceHashRepository;
36 public class ScmInfoDbLoader {
37 private static final Logger LOGGER = Loggers.get(ScmInfoDbLoader.class);
39 private final AnalysisMetadataHolder analysisMetadataHolder;
40 private final DbClient dbClient;
41 private final SourceHashRepository sourceHashRepository;
42 private final MergeBranchComponentUuids mergeBranchComponentUuid;
44 public ScmInfoDbLoader(AnalysisMetadataHolder analysisMetadataHolder, DbClient dbClient,
45 SourceHashRepository sourceHashRepository, MergeBranchComponentUuids mergeBranchComponentUuid) {
46 this.analysisMetadataHolder = analysisMetadataHolder;
47 this.dbClient = dbClient;
48 this.sourceHashRepository = sourceHashRepository;
49 this.mergeBranchComponentUuid = mergeBranchComponentUuid;
52 public ScmInfo getScmInfoFromDb(Component file) {
53 Optional<String> uuid = getFileUUid(file);
55 if (!uuid.isPresent()) {
56 return NoScmInfo.INSTANCE;
59 LOGGER.trace("Reading SCM info from db for file '{}'", uuid.get());
60 try (DbSession dbSession = dbClient.openSession(false)) {
61 FileSourceDto dto = dbClient.fileSourceDao().selectSourceByFileUuid(dbSession, uuid.get());
62 if (dto == null || !isDtoValid(file, dto)) {
63 return NoScmInfo.INSTANCE;
65 return DbScmInfo.create(file, dto.getSourceData().getLinesList()).or(NoScmInfo.INSTANCE);
69 private Optional<String> getFileUUid(Component file) {
70 if (!analysisMetadataHolder.isFirstAnalysis()) {
71 return Optional.of(file.getUuid());
74 Optional<Branch> branch = analysisMetadataHolder.getBranch();
75 if (branch.isPresent()) {
76 return Optional.ofNullable(mergeBranchComponentUuid.getUuid(file.getKey()));
79 return Optional.empty();
82 private boolean isDtoValid(Component file, FileSourceDto dto) {
83 if (file.getStatus() == Status.SAME) {
86 return sourceHashRepository.getRawSourceHash(file).equals(dto.getSrcHash());