3 * Copyright (C) 2009-2020 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.ce.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.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
26 import org.sonar.ce.task.projectanalysis.analysis.Branch;
27 import org.sonar.ce.task.projectanalysis.component.Component;
28 import org.sonar.ce.task.projectanalysis.component.ReferenceBranchComponentUuids;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.source.FileSourceDto;
33 public class ScmInfoDbLoader {
34 private static final Logger LOGGER = Loggers.get(ScmInfoDbLoader.class);
36 private final AnalysisMetadataHolder analysisMetadataHolder;
37 private final DbClient dbClient;
38 private final ReferenceBranchComponentUuids referenceBranchComponentUuid;
40 public ScmInfoDbLoader(AnalysisMetadataHolder analysisMetadataHolder, DbClient dbClient, ReferenceBranchComponentUuids referenceBranchComponentUuid) {
41 this.analysisMetadataHolder = analysisMetadataHolder;
42 this.dbClient = dbClient;
43 this.referenceBranchComponentUuid = referenceBranchComponentUuid;
46 public Optional<DbScmInfo> getScmInfo(Component file) {
47 Optional<String> uuid = getFileUUid(file);
48 if (!uuid.isPresent()) {
49 return Optional.empty();
52 LOGGER.trace("Reading SCM info from DB for file '{}'", uuid.get());
53 try (DbSession dbSession = dbClient.openSession(false)) {
54 FileSourceDto dto = dbClient.fileSourceDao().selectByFileUuid(dbSession, uuid.get());
56 return Optional.empty();
58 return DbScmInfo.create(dto.getSourceData().getLinesList(), dto.getLineCount(), dto.getSrcHash());
62 private Optional<String> getFileUUid(Component file) {
63 if (!analysisMetadataHolder.isFirstAnalysis() && !analysisMetadataHolder.isPullRequest()) {
64 return Optional.of(file.getUuid());
67 // at this point, it's the first analysis of a branch with copyFromPrevious flag true or any analysis of a PR
68 Branch branch = analysisMetadataHolder.getBranch();
69 if (!branch.isMain()) {
70 return Optional.ofNullable(referenceBranchComponentUuid.getComponentUuid(file.getDbKey()));
73 return Optional.empty();