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 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.server.project.ws;
  21. import org.sonar.api.server.ws.Change;
  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.db.DbClient;
  26. import org.sonar.db.DbSession;
  27. import org.sonar.db.project.ProjectDto;
  28. import org.sonar.server.component.ComponentFinder;
  29. import org.sonar.server.component.ComponentService;
  30. import static org.sonarqube.ws.client.project.ProjectsWsParameters.ACTION_UPDATE_KEY;
  31. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_FROM;
  32. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_TO;
  33. public class UpdateKeyAction implements ProjectsWsAction {
  34. private final DbClient dbClient;
  35. private final ComponentService componentService;
  36. private final ComponentFinder componentFinder;
  37. public UpdateKeyAction(DbClient dbClient, ComponentService componentService, ComponentFinder componentFinder) {
  38. this.dbClient = dbClient;
  39. this.componentService = componentService;
  40. this.componentFinder = componentFinder;
  41. }
  42. @Override
  43. public void define(WebService.NewController context) {
  44. doDefine(context);
  45. }
  46. public WebService.NewAction doDefine(WebService.NewController context) {
  47. WebService.NewAction action = context.createAction(ACTION_UPDATE_KEY)
  48. .setDescription("Update a project all its sub-components keys.<br>" +
  49. "Requires one of the following permissions: " +
  50. "<ul>" +
  51. "<li>'Administer System'</li>" +
  52. "<li>'Administer' rights on the specified project</li>" +
  53. "</ul>")
  54. .setSince("6.1")
  55. .setPost(true)
  56. .setHandler(this);
  57. action.setChangelog(
  58. new Change("7.1", "Ability to update key of a disabled module"));
  59. action.createParam(PARAM_FROM)
  60. .setDescription("Project key")
  61. .setRequired(true)
  62. .setExampleValue("my_old_project");
  63. action.createParam(PARAM_TO)
  64. .setDescription("New project key")
  65. .setRequired(true)
  66. .setExampleValue("my_new_project");
  67. return action;
  68. }
  69. @Override
  70. public void handle(Request request, Response response) throws Exception {
  71. String key = request.mandatoryParam(PARAM_FROM);
  72. String newKey = request.mandatoryParam(PARAM_TO);
  73. try (DbSession dbSession = dbClient.openSession(false)) {
  74. ProjectDto project = componentFinder.getProjectByKey(dbSession, key);
  75. componentService.updateKey(dbSession, project, newKey);
  76. }
  77. response.noContent();
  78. }
  79. }