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.

SetBaselineAction.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.projectanalysis.ws;
  21. import com.google.protobuf.Empty;
  22. import javax.annotation.Nullable;
  23. import org.sonar.api.server.ws.Request;
  24. import org.sonar.api.server.ws.Response;
  25. import org.sonar.api.server.ws.WebService;
  26. import org.sonar.api.web.UserRole;
  27. import org.sonar.core.util.Uuids;
  28. import org.sonar.db.DbClient;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.component.BranchDto;
  31. import org.sonar.db.component.BranchType;
  32. import org.sonar.db.component.ComponentDto;
  33. import org.sonar.db.component.SnapshotDto;
  34. import org.sonar.server.component.ComponentFinder;
  35. import org.sonar.server.exceptions.NotFoundException;
  36. import org.sonar.server.user.UserSession;
  37. import static com.google.common.base.Preconditions.checkArgument;
  38. import static java.lang.String.format;
  39. import static org.apache.commons.lang.StringUtils.trimToNull;
  40. import static org.sonar.server.component.ComponentFinder.ParamNames.PROJECT_ID_AND_KEY;
  41. import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_ANALYSIS;
  42. import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_BRANCH;
  43. import static org.sonar.server.projectanalysis.ws.ProjectAnalysesWsParameters.PARAM_PROJECT;
  44. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  45. public class SetBaselineAction implements ProjectAnalysesWsAction {
  46. private final DbClient dbClient;
  47. private final UserSession userSession;
  48. private final ComponentFinder componentFinder;
  49. public SetBaselineAction(DbClient dbClient, UserSession userSession, ComponentFinder componentFinder) {
  50. this.dbClient = dbClient;
  51. this.userSession = userSession;
  52. this.componentFinder = componentFinder;
  53. }
  54. @Override
  55. public void define(WebService.NewController context) {
  56. WebService.NewAction action = context.createAction("set_baseline")
  57. .setDescription("Set an analysis as the baseline of the New Code Period on a project or a long-lived branch.<br/>" +
  58. "This manually set baseline overrides the `sonar.leak.period` setting.<br/>" +
  59. "Requires one of the following permissions:" +
  60. "<ul>" +
  61. " <li>'Administer System'</li>" +
  62. " <li>'Administer' rights on the specified project</li>" +
  63. "</ul>")
  64. .setSince("7.7")
  65. .setPost(true)
  66. .setHandler(this);
  67. action.createParam(PARAM_PROJECT)
  68. .setDescription("Project key")
  69. .setRequired(true);
  70. action.createParam(PARAM_BRANCH)
  71. .setDescription("Branch key");
  72. action.createParam(PARAM_ANALYSIS)
  73. .setDescription("Analysis key")
  74. .setExampleValue(Uuids.UUID_EXAMPLE_01)
  75. .setRequired(true);
  76. }
  77. @Override
  78. public void handle(Request httpRequest, Response httpResponse) throws Exception {
  79. doHandle(httpRequest);
  80. writeProtobuf(Empty.newBuilder().build(), httpRequest, httpResponse);
  81. }
  82. private void doHandle(Request request) {
  83. String projectKey = request.mandatoryParam(PARAM_PROJECT);
  84. String branchKey = trimToNull(request.param(PARAM_BRANCH));
  85. String analysisUuid = request.mandatoryParam(PARAM_ANALYSIS);
  86. try (DbSession dbSession = dbClient.openSession(false)) {
  87. ComponentDto projectBranch = getProjectBranch(dbSession, projectKey, branchKey);
  88. SnapshotDto analysis = getAnalysis(dbSession, analysisUuid);
  89. checkRequest(dbSession, projectBranch, branchKey, analysis);
  90. dbClient.branchDao().updateManualBaseline(dbSession, projectBranch.uuid(), analysis.getUuid());
  91. dbSession.commit();
  92. }
  93. }
  94. private ComponentDto getProjectBranch(DbSession dbSession, String projectKey, @Nullable String branchKey) {
  95. if (branchKey == null) {
  96. return componentFinder.getByUuidOrKey(dbSession, null, projectKey, PROJECT_ID_AND_KEY);
  97. }
  98. ComponentDto project = componentFinder.getByKeyAndBranch(dbSession, projectKey, branchKey);
  99. BranchDto branchDto = dbClient.branchDao().selectByUuid(dbSession, project.uuid())
  100. .orElseThrow(() -> new NotFoundException(format("Branch '%s' is not found", branchKey)));
  101. checkArgument(branchDto.getBranchType() == BranchType.LONG,
  102. "Not a long-living branch: '%s'", branchKey);
  103. return project;
  104. }
  105. private SnapshotDto getAnalysis(DbSession dbSession, String analysisUuid) {
  106. return dbClient.snapshotDao().selectByUuid(dbSession, analysisUuid)
  107. .orElseThrow(() -> new NotFoundException(format("Analysis '%s' is not found", analysisUuid)));
  108. }
  109. private void checkRequest(DbSession dbSession, ComponentDto projectBranch, @Nullable String branchKey, SnapshotDto analysis) {
  110. userSession.checkComponentPermission(UserRole.ADMIN, projectBranch);
  111. ComponentDto project = dbClient.componentDao().selectByUuid(dbSession, analysis.getComponentUuid()).orElse(null);
  112. boolean analysisMatchesProjectBranch = project != null && projectBranch.uuid().equals(project.uuid());
  113. if (branchKey != null) {
  114. checkArgument (analysisMatchesProjectBranch,
  115. "Analysis '%s' does not belong to branch '%s' of project '%s'",
  116. analysis.getUuid(), branchKey, projectBranch.getKey());
  117. } else {
  118. checkArgument (analysisMatchesProjectBranch,
  119. "Analysis '%s' does not belong to project '%s'",
  120. analysis.getUuid(), projectBranch.getKey());
  121. }
  122. }
  123. }