You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UpdateMainBranchStep.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.ce.task.projectanalysis.step;
  21. import java.util.Optional;
  22. import org.sonar.api.utils.log.Logger;
  23. import org.sonar.api.utils.log.Loggers;
  24. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
  25. import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
  26. import org.sonar.ce.task.step.ComputationStep;
  27. import org.sonar.db.DbClient;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.project.ProjectDto;
  30. public class UpdateMainBranchStep implements ComputationStep {
  31. private static final Logger LOGGER = Loggers.get(UpdateMainBranchStep.class);
  32. private final BatchReportReader batchReportReader;
  33. private final DbClient dbClient;
  34. private final AnalysisMetadataHolder analysisMetadataHolder;
  35. public UpdateMainBranchStep(BatchReportReader batchReportReader, DbClient dbClient, AnalysisMetadataHolder analysisMetadataHolder) {
  36. this.batchReportReader = batchReportReader;
  37. this.dbClient = dbClient;
  38. this.analysisMetadataHolder = analysisMetadataHolder;
  39. }
  40. @Override
  41. public void execute(Context context) {
  42. if (!analysisMetadataHolder.isFirstAnalysis()) {
  43. return;
  44. }
  45. String gitDefaultMainBranch = batchReportReader.readMetadata().getGitDefaultMainBranch();
  46. if (gitDefaultMainBranch.isEmpty()) {
  47. LOGGER.debug("GIT default main branch detected is empty");
  48. return;
  49. }
  50. LOGGER.debug(String.format("GIT default main branch detected is [%s]", gitDefaultMainBranch));
  51. updateProjectDefaultMainBranch(gitDefaultMainBranch);
  52. }
  53. private void updateProjectDefaultMainBranch(String gitDefaultMainBranch) {
  54. try (DbSession dbSession = dbClient.openSession(false)) {
  55. String projectKey = analysisMetadataHolder.getProject().getKey();
  56. Optional<ProjectDto> projectDto = dbClient.projectDao().selectProjectByKey(dbSession, projectKey);
  57. if (!projectDto.isPresent()) {
  58. throw new IllegalStateException(String.format("root component key [%s] is not a project", projectKey));
  59. }
  60. LOGGER.info(String.format("updating project [%s] default main branch to [%s]", projectKey, gitDefaultMainBranch));
  61. dbClient.branchDao().updateMainBranchName(dbSession, projectDto.get().getUuid(), gitDefaultMainBranch);
  62. dbSession.commit();
  63. }
  64. }
  65. @Override
  66. public String getDescription() {
  67. return "Update the project main branch name, based on GIT information. Only for the first project's analysis.";
  68. }
  69. }