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.

BulkUpdateKeyAction.java 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 com.google.common.collect.ImmutableList;
  22. import java.util.Comparator;
  23. import java.util.Map;
  24. import org.sonar.api.server.ws.Change;
  25. import org.sonar.api.server.ws.Request;
  26. import org.sonar.api.server.ws.Response;
  27. import org.sonar.api.server.ws.WebService;
  28. import org.sonar.api.web.UserRole;
  29. import org.sonar.db.DbClient;
  30. import org.sonar.db.DbSession;
  31. import org.sonar.db.component.ComponentDto;
  32. import org.sonar.db.component.ComponentKeyUpdaterDao;
  33. import org.sonar.server.component.ComponentFinder;
  34. import org.sonar.server.component.ComponentFinder.ParamNames;
  35. import org.sonar.server.component.ComponentService;
  36. import org.sonar.server.user.UserSession;
  37. import org.sonarqube.ws.Projects.BulkUpdateKeyWsResponse;
  38. import javax.annotation.CheckForNull;
  39. import javax.annotation.Nullable;
  40. import static com.google.common.base.Preconditions.checkArgument;
  41. import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
  42. import static org.sonar.db.component.ComponentKeyUpdaterDao.checkIsProjectOrModule;
  43. import static org.sonar.server.ws.WsUtils.checkRequest;
  44. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  45. import static org.sonarqube.ws.client.project.ProjectsWsParameters.ACTION_BULK_UPDATE_KEY;
  46. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_DRY_RUN;
  47. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_FROM;
  48. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_PROJECT;
  49. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_PROJECT_ID;
  50. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_TO;
  51. public class BulkUpdateKeyAction implements ProjectsWsAction {
  52. private final DbClient dbClient;
  53. private final ComponentFinder componentFinder;
  54. private final ComponentKeyUpdaterDao componentKeyUpdater;
  55. private final ComponentService componentService;
  56. private final UserSession userSession;
  57. public BulkUpdateKeyAction(DbClient dbClient, ComponentFinder componentFinder, ComponentService componentService, UserSession userSession) {
  58. this.dbClient = dbClient;
  59. this.componentKeyUpdater = dbClient.componentKeyUpdaterDao();
  60. this.componentFinder = componentFinder;
  61. this.componentService = componentService;
  62. this.userSession = userSession;
  63. }
  64. @Override
  65. public void define(WebService.NewController context) {
  66. doDefine(context);
  67. }
  68. public WebService.NewAction doDefine(WebService.NewController context) {
  69. WebService.NewAction action = context.createAction(ACTION_BULK_UPDATE_KEY)
  70. .setDescription("Bulk update a project or module key and all its sub-components keys. " +
  71. "The bulk update allows to replace a part of the current key by another string on the current project and all its sub-modules.<br>" +
  72. "It's possible to simulate the bulk update by setting the parameter '%s' at true. No key is updated with a dry run.<br>" +
  73. "Ex: to rename a project with key 'my_project' to 'my_new_project' and all its sub-components keys, call the WS with parameters:" +
  74. "<ul>" +
  75. " <li>%s: my_project</li>" +
  76. " <li>%s: my_</li>" +
  77. " <li>%s: my_new_</li>" +
  78. "</ul>" +
  79. "Either '%s' or '%s' must be provided.<br> " +
  80. "Requires one of the following permissions: " +
  81. "<ul>" +
  82. "<li>'Administer System'</li>" +
  83. "<li>'Administer' rights on the specified project</li>" +
  84. "</ul>",
  85. PARAM_DRY_RUN,
  86. PARAM_PROJECT, PARAM_FROM, PARAM_TO,
  87. PARAM_PROJECT_ID, PARAM_PROJECT)
  88. .setSince("6.1")
  89. .setPost(true)
  90. .setResponseExample(getClass().getResource("bulk_update_key-example.json"))
  91. .setHandler(this);
  92. action.setChangelog(
  93. new Change("6.4", "Moved from api/components/bulk_update_key to api/projects/bulk_update_key"));
  94. action.createParam(PARAM_PROJECT_ID)
  95. .setDescription("Project or module ID")
  96. .setDeprecatedKey("id", "6.4")
  97. .setDeprecatedSince("6.4")
  98. .setExampleValue(UUID_EXAMPLE_01);
  99. action.createParam(PARAM_PROJECT)
  100. .setDescription("Project or module key")
  101. .setDeprecatedKey("key", "6.4")
  102. .setExampleValue("my_old_project");
  103. action.createParam(PARAM_FROM)
  104. .setDescription("String to match in components keys")
  105. .setRequired(true)
  106. .setExampleValue("_old");
  107. action.createParam(PARAM_TO)
  108. .setDescription("String replacement in components keys")
  109. .setRequired(true)
  110. .setExampleValue("_new");
  111. action.createParam(PARAM_DRY_RUN)
  112. .setDescription("Simulate bulk update. No component key is updated.")
  113. .setBooleanPossibleValues()
  114. .setDefaultValue(false);
  115. return action;
  116. }
  117. @Override
  118. public void handle(Request request, Response response) throws Exception {
  119. writeProtobuf(doHandle(toWsRequest(request)), request, response);
  120. }
  121. private BulkUpdateKeyWsResponse doHandle(BulkUpdateKeyRequest request) {
  122. try (DbSession dbSession = dbClient.openSession(false)) {
  123. ComponentDto projectOrModule = componentFinder.getByUuidOrKey(dbSession, request.getId(), request.getKey(), ParamNames.ID_AND_KEY);
  124. checkIsProjectOrModule(projectOrModule);
  125. userSession.checkComponentPermission(UserRole.ADMIN, projectOrModule);
  126. Map<String, String> newKeysByOldKeys = componentKeyUpdater.simulateBulkUpdateKey(dbSession, projectOrModule.uuid(), request.getFrom(), request.getTo());
  127. Map<String, Boolean> newKeysWithDuplicateMap = componentKeyUpdater.checkComponentKeys(dbSession, ImmutableList.copyOf(newKeysByOldKeys.values()));
  128. if (!request.isDryRun()) {
  129. checkNoDuplicate(newKeysWithDuplicateMap);
  130. bulkUpdateKey(dbSession, request, projectOrModule);
  131. }
  132. return buildResponse(newKeysByOldKeys, newKeysWithDuplicateMap);
  133. }
  134. }
  135. private static void checkNoDuplicate(Map<String, Boolean> newKeysWithDuplicateMap) {
  136. newKeysWithDuplicateMap.entrySet().forEach(entry -> checkRequest(!entry.getValue(), "Impossible to update key: a component with key \"%s\" already exists.", entry.getKey()));
  137. }
  138. private void bulkUpdateKey(DbSession dbSession, BulkUpdateKeyRequest request, ComponentDto projectOrModule) {
  139. componentService.bulkUpdateKey(dbSession, projectOrModule, request.getFrom(), request.getTo());
  140. }
  141. private static BulkUpdateKeyWsResponse buildResponse(Map<String, String> newKeysByOldKeys, Map<String, Boolean> newKeysWithDuplicateMap) {
  142. BulkUpdateKeyWsResponse.Builder response = BulkUpdateKeyWsResponse.newBuilder();
  143. newKeysByOldKeys.entrySet().stream()
  144. // sort by old key
  145. .sorted(Comparator.comparing(Map.Entry::getKey))
  146. .forEach(
  147. entry -> {
  148. String newKey = entry.getValue();
  149. response.addKeysBuilder()
  150. .setKey(entry.getKey())
  151. .setNewKey(newKey)
  152. .setDuplicate(newKeysWithDuplicateMap.getOrDefault(newKey, false));
  153. });
  154. return response.build();
  155. }
  156. private static BulkUpdateKeyRequest toWsRequest(Request request) {
  157. return BulkUpdateKeyRequest.builder()
  158. .setId(request.param(PARAM_PROJECT_ID))
  159. .setKey(request.param(PARAM_PROJECT))
  160. .setFrom(request.mandatoryParam(PARAM_FROM))
  161. .setTo(request.mandatoryParam(PARAM_TO))
  162. .setDryRun(request.mandatoryParamAsBoolean(PARAM_DRY_RUN))
  163. .build();
  164. }
  165. private static class BulkUpdateKeyRequest {
  166. private final String id;
  167. private final String key;
  168. private final String from;
  169. private final String to;
  170. private final boolean dryRun;
  171. public BulkUpdateKeyRequest(Builder builder) {
  172. this.id = builder.id;
  173. this.key = builder.key;
  174. this.from = builder.from;
  175. this.to = builder.to;
  176. this.dryRun = builder.dryRun;
  177. }
  178. @CheckForNull
  179. public String getId() {
  180. return id;
  181. }
  182. @CheckForNull
  183. public String getKey() {
  184. return key;
  185. }
  186. public String getFrom() {
  187. return from;
  188. }
  189. public String getTo() {
  190. return to;
  191. }
  192. public boolean isDryRun() {
  193. return dryRun;
  194. }
  195. public static Builder builder() {
  196. return new Builder();
  197. }
  198. }
  199. public static class Builder {
  200. private String id;
  201. private String key;
  202. private String from;
  203. private String to;
  204. private boolean dryRun;
  205. private Builder() {
  206. // enforce method constructor
  207. }
  208. public Builder setId(@Nullable String id) {
  209. this.id = id;
  210. return this;
  211. }
  212. public Builder setKey(@Nullable String key) {
  213. this.key = key;
  214. return this;
  215. }
  216. public Builder setFrom(String from) {
  217. this.from = from;
  218. return this;
  219. }
  220. public Builder setTo(String to) {
  221. this.to = to;
  222. return this;
  223. }
  224. public Builder setDryRun(boolean dryRun) {
  225. this.dryRun = dryRun;
  226. return this;
  227. }
  228. public BulkUpdateKeyRequest build() {
  229. checkArgument(from != null && !from.isEmpty(), "The string to match must not be empty");
  230. checkArgument(to != null && !to.isEmpty(), "The string replacement must not be empty");
  231. return new BulkUpdateKeyRequest(this);
  232. }
  233. }
  234. }