import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.ComponentUpdater;
import org.sonar.server.organization.DefaultOrganizationProvider;
import org.sonar.server.user.UserSession;
import org.sonarqube.ws.WsProjects.CreateWsResponse;
import org.sonarqube.ws.client.project.CreateRequest;
+import static java.util.Optional.ofNullable;
import static org.sonar.api.resources.Qualifiers.PROJECT;
import static org.sonar.core.permission.GlobalPermissions.PROVISIONING;
import static org.sonar.server.component.NewComponent.newComponentBuilder;
+import static org.sonar.server.project.ws.ProjectsWsSupport.PARAM_ORGANIZATION;
import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
import static org.sonar.server.ws.WsUtils.writeProtobuf;
import static org.sonarqube.ws.client.project.ProjectsWsParameters.ACTION_CREATE;
public class CreateAction implements ProjectsWsAction {
- public static final String DEPRECATED_PARAM_KEY = "key";
+ private static final String DEPRECATED_PARAM_KEY = "key";
+ private final ProjectsWsSupport support;
private final DbClient dbClient;
private final UserSession userSession;
private final ComponentUpdater componentUpdater;
private final DefaultOrganizationProvider defaultOrganizationProvider;
- public CreateAction(DbClient dbClient, UserSession userSession, ComponentUpdater componentUpdater, DefaultOrganizationProvider defaultOrganizationProvider) {
+ public CreateAction(ProjectsWsSupport support, DbClient dbClient, UserSession userSession, ComponentUpdater componentUpdater,
+ DefaultOrganizationProvider defaultOrganizationProvider) {
+ this.support = support;
this.dbClient = dbClient;
this.userSession = userSession;
this.componentUpdater = componentUpdater;
action.createParam(PARAM_BRANCH)
.setDescription("SCM Branch of the project. The key of the project will become key:branch, for instance 'SonarQube:branch-5.0'")
.setExampleValue("branch-5.0");
+
+ support.addOrganizationParam(action);
}
@Override
public void handle(Request request, Response response) throws Exception {
- userSession.checkPermission(PROVISIONING);
CreateRequest createRequest = toCreateRequest(request);
writeProtobuf(doHandle(createRequest), request, response);
}
private CreateWsResponse doHandle(CreateRequest request) {
try (DbSession dbSession = dbClient.openSession(false)) {
+ OrganizationDto organization = support.getOrganization(dbSession, ofNullable(request.getOrganization())
+ .orElseGet(defaultOrganizationProvider.get()::getKey));
+ userSession.checkOrganizationPermission(organization.getUuid(), PROVISIONING);
+
ComponentDto componentDto = componentUpdater.create(dbSession, newComponentBuilder()
- .setOrganizationUuid(defaultOrganizationProvider.get().getUuid())
+ .setOrganizationUuid(organization.getUuid())
.setKey(request.getKey())
.setName(request.getName())
.setBranch(request.getBranch())
private static CreateRequest toCreateRequest(Request request) {
return CreateRequest.builder()
+ .setOrganization(request.param(PARAM_ORGANIZATION))
.setKey(request.mandatoryParam(PARAM_PROJECT))
.setName(request.mandatoryParam(PARAM_NAME))
.setBranch(request.param(PARAM_BRANCH))
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.organization.OrganizationDto;
import org.sonar.server.component.ComponentUpdater;
import org.sonar.server.component.NewComponent;
import org.sonar.server.exceptions.BadRequestException;
import static org.sonar.core.permission.GlobalPermissions.PROVISIONING;
import static org.sonar.core.permission.GlobalPermissions.QUALITY_GATE_ADMIN;
import static org.sonar.core.util.Protobuf.setNullable;
+import static org.sonar.server.project.ws.ProjectsWsSupport.PARAM_ORGANIZATION;
import static org.sonar.test.JsonAssert.assertJson;
import static org.sonarqube.ws.client.WsRequest.Method.POST;
import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_NAME;
public DbTester db = DbTester.create(system2);
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
+
private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
private ComponentUpdater componentUpdater = mock(ComponentUpdater.class, Mockito.RETURNS_MOCKS);
private WsActionTester ws = new WsActionTester(
- new CreateAction(
- db.getDbClient(), userSession,
- componentUpdater,
- defaultOrganizationProvider));
+ new CreateAction(
+ new ProjectsWsSupport(db.getDbClient()),
+ db.getDbClient(), userSession,
+ componentUpdater,
+ defaultOrganizationProvider));
@Test
public void create_project() throws Exception {
- userSession.setGlobalPermissions(PROVISIONING);
+ userSession.addOrganizationPermission(db.getDefaultOrganization(), PROVISIONING);
expectSuccessfulCallToComponentUpdater();
CreateWsResponse response = call(CreateRequest.builder()
- .setKey(DEFAULT_PROJECT_KEY)
- .setName(DEFAULT_PROJECT_NAME)
- .build());
+ .setKey(DEFAULT_PROJECT_KEY)
+ .setName(DEFAULT_PROJECT_NAME)
+ .build());
assertThat(response.getProject().getKey()).isEqualTo(DEFAULT_PROJECT_KEY);
assertThat(response.getProject().getName()).isEqualTo(DEFAULT_PROJECT_NAME);
@Test
public void create_project_with_branch() throws Exception {
- userSession.setGlobalPermissions(PROVISIONING);
+ userSession.addOrganizationPermission(db.getDefaultOrganization(), PROVISIONING);
call(CreateRequest.builder()
- .setKey(DEFAULT_PROJECT_KEY)
- .setName(DEFAULT_PROJECT_NAME)
- .setBranch("origin/master")
- .build());
+ .setKey(DEFAULT_PROJECT_KEY)
+ .setName(DEFAULT_PROJECT_NAME)
+ .setBranch("origin/master")
+ .build());
NewComponent called = verifyCallToComponentUpdater();
assertThat(called.key()).isEqualTo(DEFAULT_PROJECT_KEY);
@Test
public void create_project_with_deprecated_parameter() throws Exception {
- userSession.setGlobalPermissions(PROVISIONING);
+ OrganizationDto organization = db.organizations().insert();
+ userSession.addOrganizationPermission(organization, PROVISIONING);
ws.newRequest()
- .setMethod(POST.name())
- .setParam("key", DEFAULT_PROJECT_KEY)
- .setParam(PARAM_NAME, DEFAULT_PROJECT_NAME)
- .execute();
+ .setMethod(POST.name())
+ .setParam("organization", organization.getKey())
+ .setParam("key", DEFAULT_PROJECT_KEY)
+ .setParam(PARAM_NAME, DEFAULT_PROJECT_NAME)
+ .execute();
NewComponent called = verifyCallToComponentUpdater();
assertThat(called.key()).isEqualTo(DEFAULT_PROJECT_KEY);
@Test
public void fail_when_project_already_exists() throws Exception {
- userSession.setGlobalPermissions(PROVISIONING);
+ OrganizationDto organization = db.organizations().insert();
when(componentUpdater.create(any(DbSession.class), any(NewComponent.class), anyLong())).thenThrow(new BadRequestException("already exists"));
+ userSession.addOrganizationPermission(organization, PROVISIONING);
expectedException.expect(BadRequestException.class);
call(CreateRequest.builder()
- .setKey(DEFAULT_PROJECT_KEY)
- .setName(DEFAULT_PROJECT_NAME)
- .build());
+ .setOrganization(organization.getKey())
+ .setKey(DEFAULT_PROJECT_KEY)
+ .setName(DEFAULT_PROJECT_NAME)
+ .build());
}
@Test
@Test
public void test_example() {
- userSession.setGlobalPermissions(PROVISIONING);
+ userSession.addOrganizationPermission(db.getDefaultOrganization(), PROVISIONING);
expectSuccessfulCallToComponentUpdater();
String result = ws.newRequest()
- .setParam("key", DEFAULT_PROJECT_KEY)
- .setParam("name", DEFAULT_PROJECT_NAME)
- .execute().getInput();
+ .setParam("key", DEFAULT_PROJECT_KEY)
+ .setParam("name", DEFAULT_PROJECT_NAME)
+ .execute().getInput();
assertJson(result).isSimilarTo(getClass().getResource("create-example.json"));
}
Assertions.assertThat(definition.since()).isEqualTo("4.0");
Assertions.assertThat(definition.isInternal()).isFalse();
Assertions.assertThat(definition.responseExampleAsString()).isNotEmpty();
- Assertions.assertThat(definition.params()).hasSize(3);
+
+ Assertions.assertThat(definition.params()).hasSize(4);
+
+ WebService.Param organization = definition.param(PARAM_ORGANIZATION);
+ Assertions.assertThat(organization.description()).isEqualTo("The key of the organization");
+ Assertions.assertThat(organization.isInternal()).isTrue();
+ Assertions.assertThat(organization.isRequired()).isFalse();
+ Assertions.assertThat(organization.since()).isEqualTo("6.3");
}
private CreateWsResponse call(CreateRequest request) {
TestRequest httpRequest = ws.newRequest()
- .setMethod(POST.name())
- .setMediaType(MediaTypes.PROTOBUF);
+ .setMethod(POST.name())
+ .setMediaType(MediaTypes.PROTOBUF);
+ setNullable(request.getOrganization(), e -> httpRequest.setParam("organization", e));
setNullable(request.getKey(), e -> httpRequest.setParam("project", e));
setNullable(request.getName(), e -> httpRequest.setParam("name", e));
setNullable(request.getBranch(), e -> httpRequest.setParam("branch", e));