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.

LoadQualityGateStep.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.server.computation.task.projectanalysis.step;
  21. import java.util.Optional;
  22. import org.sonar.api.config.Configuration;
  23. import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
  24. import org.sonar.server.computation.task.projectanalysis.component.ConfigurationRepository;
  25. import org.sonar.server.computation.task.projectanalysis.qualitygate.MutableQualityGateHolder;
  26. import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGate;
  27. import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGateService;
  28. import org.sonar.server.computation.task.step.ComputationStep;
  29. import org.sonar.server.qualitygate.ShortLivingBranchQualityGate;
  30. import static org.apache.commons.lang.StringUtils.isBlank;
  31. /**
  32. * This step retrieves the QualityGate and stores it in
  33. * {@link MutableQualityGateHolder}.
  34. */
  35. public class LoadQualityGateStep implements ComputationStep {
  36. private static final String PROPERTY_PROJECT_QUALITY_GATE = "sonar.qualitygate";
  37. private final ConfigurationRepository configRepository;
  38. private final QualityGateService qualityGateService;
  39. private final MutableQualityGateHolder qualityGateHolder;
  40. private final AnalysisMetadataHolder analysisMetadataHolder;
  41. public LoadQualityGateStep(ConfigurationRepository settingsRepository, QualityGateService qualityGateService, MutableQualityGateHolder qualityGateHolder,
  42. AnalysisMetadataHolder analysisMetadataHolder) {
  43. this.configRepository = settingsRepository;
  44. this.qualityGateService = qualityGateService;
  45. this.qualityGateHolder = qualityGateHolder;
  46. this.analysisMetadataHolder = analysisMetadataHolder;
  47. }
  48. @Override
  49. public void execute() {
  50. Optional<QualityGate> qualityGate = getShortLivingBranchQualityGate();
  51. if (!qualityGate.isPresent()) {
  52. // Not on a short living branch, let's retrieve the QG of the project
  53. qualityGate = getProjectQualityGate();
  54. if (!qualityGate.isPresent()) {
  55. // No QG defined for the project, let's retrieve the QG on the organization
  56. qualityGate = Optional.of(getOrganizationDefaultQualityGate());
  57. }
  58. }
  59. qualityGateHolder.setQualityGate(qualityGate.orElseThrow(() -> new IllegalStateException("Quality gate not present")));
  60. }
  61. private Optional<QualityGate> getShortLivingBranchQualityGate() {
  62. if (analysisMetadataHolder.isShortLivingBranch() || analysisMetadataHolder.isPullRequest()) {
  63. Optional<QualityGate> qualityGate = qualityGateService.findById(ShortLivingBranchQualityGate.ID);
  64. if (qualityGate.isPresent()) {
  65. return qualityGate;
  66. } else {
  67. throw new IllegalStateException("Failed to retrieve hardcoded short living branch Quality Gate");
  68. }
  69. } else {
  70. return Optional.empty();
  71. }
  72. }
  73. private Optional<QualityGate> getProjectQualityGate() {
  74. Configuration config = configRepository.getConfiguration();
  75. String qualityGateSetting = config.get(PROPERTY_PROJECT_QUALITY_GATE).orElse(null);
  76. if (isBlank(qualityGateSetting)) {
  77. return Optional.empty();
  78. }
  79. try {
  80. long qualityGateId = Long.parseLong(qualityGateSetting);
  81. return qualityGateService.findById(qualityGateId);
  82. } catch (NumberFormatException e) {
  83. throw new IllegalStateException(
  84. String.format("Unsupported value (%s) in property %s", qualityGateSetting, PROPERTY_PROJECT_QUALITY_GATE), e);
  85. }
  86. }
  87. private QualityGate getOrganizationDefaultQualityGate() {
  88. return qualityGateService.findDefaultQualityGate(analysisMetadataHolder.getOrganization());
  89. }
  90. @Override
  91. public String getDescription() {
  92. return "Load Quality gate";
  93. }
  94. }