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.

UpdateKeyAction.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.project.ws;
  21. import java.util.Optional;
  22. import org.sonar.api.server.ws.Change;
  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.db.DbClient;
  27. import org.sonar.db.DbSession;
  28. import org.sonar.db.component.ComponentDto;
  29. import org.sonar.server.component.ComponentService;
  30. import org.sonar.server.exceptions.NotFoundException;
  31. import static com.google.common.base.Preconditions.checkArgument;
  32. import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
  33. import static org.sonarqube.ws.client.project.ProjectsWsParameters.ACTION_UPDATE_KEY;
  34. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_FROM;
  35. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_PROJECT_ID;
  36. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_TO;
  37. public class UpdateKeyAction implements ProjectsWsAction {
  38. private final DbClient dbClient;
  39. private final ComponentService componentService;
  40. public UpdateKeyAction(DbClient dbClient, ComponentService componentService) {
  41. this.dbClient = dbClient;
  42. this.componentService = componentService;
  43. }
  44. @Override
  45. public void define(WebService.NewController context) {
  46. doDefine(context);
  47. }
  48. public WebService.NewAction doDefine(WebService.NewController context) {
  49. WebService.NewAction action = context.createAction(ACTION_UPDATE_KEY)
  50. .setDescription("Update a project or module key and all its sub-components keys.<br>" +
  51. "Either '%s' or '%s' must be provided.<br> " +
  52. "Requires one of the following permissions: " +
  53. "<ul>" +
  54. "<li>'Administer System'</li>" +
  55. "<li>'Administer' rights on the specified project</li>" +
  56. "</ul>",
  57. PARAM_FROM, PARAM_PROJECT_ID)
  58. .setSince("6.1")
  59. .setPost(true)
  60. .setHandler(this);
  61. action.setChangelog(
  62. new Change("6.4", "Move from api/components/update_key to api/projects/update_key"),
  63. new Change("7.1", "Ability to update key of a disabled module"));
  64. action.createParam(PARAM_PROJECT_ID)
  65. .setDescription("Project or module id")
  66. .setDeprecatedKey("id", "6.4")
  67. .setDeprecatedSince("6.4")
  68. .setExampleValue(UUID_EXAMPLE_01);
  69. action.createParam(PARAM_FROM)
  70. .setDescription("Project or module key")
  71. .setDeprecatedKey("key", "6.4")
  72. .setExampleValue("my_old_project");
  73. action.createParam(PARAM_TO)
  74. .setDescription("New component key")
  75. .setRequired(true)
  76. .setDeprecatedKey("newKey", "6.4")
  77. .setExampleValue("my_new_project");
  78. return action;
  79. }
  80. @Override
  81. public void handle(Request request, Response response) throws Exception {
  82. String uuid = request.param(PARAM_PROJECT_ID);
  83. String key = request.param(PARAM_FROM);
  84. String newKey = request.mandatoryParam(PARAM_TO);
  85. checkArgument(uuid != null ^ key != null, "Either '%s' or '%s' must be provided", PARAM_PROJECT_ID, PARAM_FROM);
  86. try (DbSession dbSession = dbClient.openSession(false)) {
  87. Optional<ComponentDto> component;
  88. if (uuid != null) {
  89. component = dbClient.componentDao().selectByUuid(dbSession, uuid);
  90. } else {
  91. component = dbClient.componentDao().selectByKey(dbSession, key);
  92. }
  93. if (!component.isPresent() || component.get().getMainBranchProjectUuid() != null) {
  94. throw new NotFoundException("Component not found");
  95. }
  96. componentService.updateKey(dbSession, component.get(), newKey);
  97. }
  98. response.noContent();
  99. }
  100. }