您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CreateAction.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 javax.annotation.CheckForNull;
  22. import javax.annotation.Nullable;
  23. import org.sonar.api.server.ws.Change;
  24. import org.sonar.api.server.ws.Request;
  25. import org.sonar.api.server.ws.Response;
  26. import org.sonar.api.server.ws.WebService;
  27. import org.sonar.db.DbClient;
  28. import org.sonar.db.DbSession;
  29. import org.sonar.db.component.ComponentDto;
  30. import org.sonar.server.component.ComponentUpdater;
  31. import org.sonar.server.project.ProjectDefaultVisibility;
  32. import org.sonar.server.project.Visibility;
  33. import org.sonar.server.user.UserSession;
  34. import org.sonarqube.ws.Projects.CreateWsResponse;
  35. import static java.util.Objects.requireNonNull;
  36. import static org.apache.commons.lang.StringUtils.abbreviate;
  37. import static org.sonar.api.resources.Qualifiers.PROJECT;
  38. import static org.sonar.core.component.ComponentKeys.MAX_COMPONENT_KEY_LENGTH;
  39. import static org.sonar.db.component.ComponentValidator.MAX_COMPONENT_NAME_LENGTH;
  40. import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
  41. import static org.sonar.server.component.NewComponent.newComponentBuilder;
  42. import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
  43. import static org.sonar.server.ws.WsUtils.writeProtobuf;
  44. import static org.sonarqube.ws.client.project.ProjectsWsParameters.ACTION_CREATE;
  45. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NAME;
  46. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_PROJECT;
  47. import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_VISIBILITY;
  48. public class CreateAction implements ProjectsWsAction {
  49. private final DbClient dbClient;
  50. private final UserSession userSession;
  51. private final ComponentUpdater componentUpdater;
  52. private final ProjectDefaultVisibility projectDefaultVisibility;
  53. public CreateAction(DbClient dbClient, UserSession userSession, ComponentUpdater componentUpdater,
  54. ProjectDefaultVisibility projectDefaultVisibility) {
  55. this.dbClient = dbClient;
  56. this.userSession = userSession;
  57. this.componentUpdater = componentUpdater;
  58. this.projectDefaultVisibility = projectDefaultVisibility;
  59. }
  60. @Override
  61. public void define(WebService.NewController context) {
  62. WebService.NewAction action = context.createAction(ACTION_CREATE)
  63. .setDescription("Create a project.<br/>" +
  64. "Requires 'Create Projects' permission")
  65. .setSince("4.0")
  66. .setPost(true)
  67. .setResponseExample(getClass().getResource("create-example.json"))
  68. .setHandler(this);
  69. action.setChangelog(
  70. new Change("7.1", "The 'visibility' parameter is public"));
  71. action.createParam(PARAM_PROJECT)
  72. .setDescription("Key of the project")
  73. .setRequired(true)
  74. .setMaximumLength(MAX_COMPONENT_KEY_LENGTH)
  75. .setExampleValue(KEY_PROJECT_EXAMPLE_001);
  76. action.createParam(PARAM_NAME)
  77. .setDescription("Name of the project. If name is longer than %d, it is abbreviated.", MAX_COMPONENT_NAME_LENGTH)
  78. .setRequired(true)
  79. .setExampleValue("SonarQube");
  80. action.createParam(PARAM_VISIBILITY)
  81. .setDescription("Whether the created project should be visible to everyone, or only specific user/groups.<br/>" +
  82. "If no visibility is specified, the default project visibility will be used.")
  83. .setRequired(false)
  84. .setSince("6.4")
  85. .setPossibleValues(Visibility.getLabels());
  86. }
  87. @Override
  88. public void handle(Request request, Response response) throws Exception {
  89. CreateRequest createRequest = toCreateRequest(request);
  90. writeProtobuf(doHandle(createRequest), request, response);
  91. }
  92. private CreateWsResponse doHandle(CreateRequest request) {
  93. try (DbSession dbSession = dbClient.openSession(false)) {
  94. userSession.checkPermission(PROVISION_PROJECTS);
  95. String visibility = request.getVisibility();
  96. boolean changeToPrivate = visibility == null ? projectDefaultVisibility.get(dbSession).isPrivate() : "private".equals(visibility);
  97. ComponentDto componentDto = componentUpdater.create(dbSession, newComponentBuilder()
  98. .setKey(request.getProjectKey())
  99. .setName(request.getName())
  100. .setPrivate(changeToPrivate)
  101. .setQualifier(PROJECT)
  102. .build(),
  103. userSession.isLoggedIn() ? userSession.getUuid() : null,
  104. userSession.isLoggedIn() ? userSession.getLogin() : null);
  105. return toCreateResponse(componentDto);
  106. }
  107. }
  108. private static CreateRequest toCreateRequest(Request request) {
  109. return CreateRequest.builder()
  110. .setProjectKey(request.mandatoryParam(PARAM_PROJECT))
  111. .setName(abbreviate(request.mandatoryParam(PARAM_NAME), MAX_COMPONENT_NAME_LENGTH))
  112. .setVisibility(request.param(PARAM_VISIBILITY))
  113. .build();
  114. }
  115. private static CreateWsResponse toCreateResponse(ComponentDto componentDto) {
  116. return CreateWsResponse.newBuilder()
  117. .setProject(CreateWsResponse.Project.newBuilder()
  118. .setKey(componentDto.getDbKey())
  119. .setName(componentDto.name())
  120. .setQualifier(componentDto.qualifier())
  121. .setVisibility(Visibility.getLabel(componentDto.isPrivate())))
  122. .build();
  123. }
  124. static class CreateRequest {
  125. private final String projectKey;
  126. private final String name;
  127. @CheckForNull
  128. private final String visibility;
  129. private CreateRequest(Builder builder) {
  130. this.projectKey = builder.projectKey;
  131. this.name = builder.name;
  132. this.visibility = builder.visibility;
  133. }
  134. public String getProjectKey() {
  135. return projectKey;
  136. }
  137. public String getName() {
  138. return name;
  139. }
  140. @CheckForNull
  141. public String getVisibility() {
  142. return visibility;
  143. }
  144. public static Builder builder() {
  145. return new Builder();
  146. }
  147. }
  148. static class Builder {
  149. private String projectKey;
  150. private String name;
  151. @CheckForNull
  152. private String visibility;
  153. private Builder() {
  154. }
  155. public Builder setProjectKey(String projectKey) {
  156. requireNonNull(projectKey);
  157. this.projectKey = projectKey;
  158. return this;
  159. }
  160. public Builder setName(String name) {
  161. requireNonNull(name);
  162. this.name = name;
  163. return this;
  164. }
  165. public Builder setVisibility(@Nullable String visibility) {
  166. this.visibility = visibility;
  167. return this;
  168. }
  169. public CreateRequest build() {
  170. requireNonNull(projectKey);
  171. requireNonNull(name);
  172. return new CreateRequest(this);
  173. }
  174. }
  175. }