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.

RenameAction.java 3.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.branch.ws;
  21. import java.util.Optional;
  22. import org.sonar.api.server.ws.Request;
  23. import org.sonar.api.server.ws.Response;
  24. import org.sonar.api.server.ws.WebService;
  25. import org.sonar.api.server.ws.WebService.NewController;
  26. import org.sonar.api.web.UserRole;
  27. import org.sonar.db.DbClient;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.component.BranchDto;
  30. import org.sonar.db.component.ComponentDto;
  31. import org.sonar.server.component.ComponentFinder;
  32. import org.sonar.server.user.UserSession;
  33. import static com.google.common.base.Preconditions.checkArgument;
  34. import static org.sonar.server.branch.ws.BranchesWs.addProjectParam;
  35. import static org.sonar.server.branch.ws.ProjectBranchesParameters.ACTION_RENAME;
  36. import static org.sonar.server.branch.ws.ProjectBranchesParameters.PARAM_NAME;
  37. import static org.sonar.server.branch.ws.ProjectBranchesParameters.PARAM_PROJECT;
  38. public class RenameAction implements BranchWsAction {
  39. private final ComponentFinder componentFinder;
  40. private final UserSession userSession;
  41. private final DbClient dbClient;
  42. public RenameAction(DbClient dbClient, ComponentFinder componentFinder, UserSession userSession) {
  43. this.dbClient = dbClient;
  44. this.componentFinder = componentFinder;
  45. this.userSession = userSession;
  46. }
  47. @Override
  48. public void define(NewController context) {
  49. WebService.NewAction action = context.createAction(ACTION_RENAME)
  50. .setSince("6.6")
  51. .setDescription("Rename the main branch of a project.<br/>"
  52. + "Requires 'Administer' permission on the specified project.")
  53. .setPost(true)
  54. .setHandler(this);
  55. addProjectParam(action);
  56. action
  57. .createParam(PARAM_NAME)
  58. .setRequired(true)
  59. .setMaximumLength(255)
  60. .setDescription("New name of the main branch")
  61. .setExampleValue("branch1");
  62. }
  63. @Override
  64. public void handle(Request request, Response response) throws Exception {
  65. userSession.checkLoggedIn();
  66. String projectKey = request.mandatoryParam(PARAM_PROJECT);
  67. String newBranchName = request.mandatoryParam(PARAM_NAME);
  68. try (DbSession dbSession = dbClient.openSession(false)) {
  69. ComponentDto project = componentFinder.getRootComponentByUuidOrKey(dbSession, null, projectKey);
  70. checkPermission(project);
  71. Optional<BranchDto> existingBranch = dbClient.branchDao().selectByBranchKey(dbSession, project.uuid(), newBranchName);
  72. checkArgument(!existingBranch.filter(b -> !b.isMain()).isPresent(),
  73. "Impossible to update branch name: a branch with name \"%s\" already exists in the project.", newBranchName);
  74. dbClient.branchDao().updateMainBranchName(dbSession, project.uuid(), newBranchName);
  75. dbSession.commit();
  76. response.noContent();
  77. }
  78. }
  79. private void checkPermission(ComponentDto project) {
  80. userSession.checkComponentPermission(UserRole.ADMIN, project);
  81. }
  82. }