選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ReferenceBranchSupplier.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.scanner.repository;
  21. import javax.annotation.CheckForNull;
  22. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  23. import org.sonar.api.utils.log.Logger;
  24. import org.sonar.api.utils.log.Loggers;
  25. import org.sonar.api.utils.log.Profiler;
  26. import org.sonar.scanner.scan.branch.BranchConfiguration;
  27. import org.sonar.scanner.scan.branch.ProjectBranches;
  28. import org.sonarqube.ws.NewCodePeriods;
  29. public class ReferenceBranchSupplier {
  30. private static final Logger LOG = Loggers.get(ReferenceBranchSupplier.class);
  31. private static final String LOG_MSG_WS = "Load New Code definition";
  32. private final NewCodePeriodLoader newCodePeriodLoader;
  33. private final BranchConfiguration branchConfiguration;
  34. private final DefaultInputProject project;
  35. private final ProjectBranches branches;
  36. public ReferenceBranchSupplier(NewCodePeriodLoader newCodePeriodLoader, BranchConfiguration branchConfiguration, DefaultInputProject project, ProjectBranches branches) {
  37. this.newCodePeriodLoader = newCodePeriodLoader;
  38. this.branchConfiguration = branchConfiguration;
  39. this.project = project;
  40. this.branches = branches;
  41. }
  42. @CheckForNull
  43. public String get() {
  44. // branches will be empty in CE
  45. if (branchConfiguration.isPullRequest() || branches.isEmpty()) {
  46. return null;
  47. }
  48. Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG_WS);
  49. String branchName = branchConfiguration.branchName() != null ? branchConfiguration.branchName() : branches.defaultBranchName();
  50. NewCodePeriods.ShowWSResponse newCode = newCodePeriodLoader.load(project.key(), branchName);
  51. profiler.stopInfo();
  52. if (newCode.getType() != NewCodePeriods.NewCodePeriodType.REFERENCE_BRANCH) {
  53. return null;
  54. }
  55. String referenceBranchName = newCode.getValue();
  56. if (branchName.equals(referenceBranchName)) {
  57. LOG.warn("New Code reference branch is set to the branch being analyzed. Skipping the computation of New Code");
  58. return null;
  59. }
  60. return referenceBranchName;
  61. }
  62. }