3 * Copyright (C) 2009-2021 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.step;
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.batch.BatchReportReader;
27 import org.sonar.ce.task.step.ComputationStep;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.project.ProjectDto;
32 public class UpdateMainBranchStep implements ComputationStep {
34 private static final Logger LOGGER = Loggers.get(UpdateMainBranchStep.class);
36 private final BatchReportReader batchReportReader;
37 private final DbClient dbClient;
38 private final AnalysisMetadataHolder analysisMetadataHolder;
40 public UpdateMainBranchStep(BatchReportReader batchReportReader, DbClient dbClient, AnalysisMetadataHolder analysisMetadataHolder) {
41 this.batchReportReader = batchReportReader;
42 this.dbClient = dbClient;
43 this.analysisMetadataHolder = analysisMetadataHolder;
47 public void execute(Context context) {
49 if (!analysisMetadataHolder.isFirstAnalysis()) {
53 String gitDefaultMainBranch = batchReportReader.readMetadata().getGitDefaultMainBranch();
54 if (gitDefaultMainBranch.isEmpty()) {
55 LOGGER.debug("GIT default main branch detected is empty");
58 LOGGER.debug(String.format("GIT default main branch detected is [%s]", gitDefaultMainBranch));
59 updateProjectDefaultMainBranch(gitDefaultMainBranch);
62 private void updateProjectDefaultMainBranch(String gitDefaultMainBranch) {
63 try (DbSession dbSession = dbClient.openSession(false)) {
64 String projectKey = analysisMetadataHolder.getProject().getKey();
65 Optional<ProjectDto> projectDto = dbClient.projectDao().selectProjectByKey(dbSession, projectKey);
66 if (!projectDto.isPresent()) {
67 throw new IllegalStateException(String.format("root component key [%s] is not a project", projectKey));
69 LOGGER.info(String.format("updating project [%s] default main branch to [%s]", projectKey, gitDefaultMainBranch));
70 dbClient.branchDao().updateMainBranchName(dbSession, projectDto.get().getUuid(), gitDefaultMainBranch);
76 public String getDescription() {
77 return "Update the project main branch name, based on GIT information. Only for the first project's analysis.";