diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-04-28 11:39:40 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-04-28 18:36:09 +0200 |
commit | fdfa49be4133e34b6bb5849a137166eb29bf9109 (patch) | |
tree | 57c0ea40f099019e02e62b446f07a9f51142e845 /sonar-ws | |
parent | e4390c1826c0e886be3bda201cac7f4401771778 (diff) | |
download | sonarqube-fdfa49be4133e34b6bb5849a137166eb29bf9109.tar.gz sonarqube-fdfa49be4133e34b6bb5849a137166eb29bf9109.zip |
Add visibility parameter in ProjectsService#create()
Diffstat (limited to 'sonar-ws')
5 files changed, 92 insertions, 29 deletions
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/CreateRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/CreateRequest.java index 8a03fff7c13..61c89eeabfc 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/CreateRequest.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/CreateRequest.java @@ -19,14 +19,10 @@ */ package org.sonarqube.ws.client.project; -import java.util.Optional; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; -import static com.google.common.base.Preconditions.checkArgument; -import static java.util.Arrays.asList; - @Immutable public class CreateRequest { @@ -50,10 +46,12 @@ public class CreateRequest { return organization; } + @CheckForNull public String getKey() { return key; } + @CheckForNull public String getName() { return name; } @@ -63,8 +61,9 @@ public class CreateRequest { return branch; } - public Optional<String> getVisibility() { - return Optional.ofNullable(visibility); + @CheckForNull + public String getVisibility() { + return visibility; } public static Builder builder() { @@ -82,17 +81,17 @@ public class CreateRequest { private Builder() { } - public Builder setOrganization(String organization) { + public Builder setOrganization(@Nullable String organization) { this.organization = organization; return this; } - public Builder setKey(String key) { + public Builder setKey(@Nullable String key) { this.key = key; return this; } - public Builder setName(String name) { + public Builder setName(@Nullable String name) { this.name = name; return this; } @@ -103,7 +102,6 @@ public class CreateRequest { } public Builder setVisibility(@Nullable String visibility) { - checkArgument(visibility == null || asList("private", "public").contains(visibility), "Unexpected visibility '" + visibility + "'"); this.visibility = visibility; return this; } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsService.java index a0d4b389258..f46f0f5701e 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsService.java @@ -35,6 +35,7 @@ import static org.sonarqube.ws.client.project.ProjectsWsParameters.ACTION_BULK_U import static org.sonarqube.ws.client.project.ProjectsWsParameters.ACTION_CREATE; import static org.sonarqube.ws.client.project.ProjectsWsParameters.ACTION_SEARCH; import static org.sonarqube.ws.client.project.ProjectsWsParameters.ACTION_UPDATE_KEY; +import static org.sonarqube.ws.client.project.ProjectsWsParameters.ACTION_UPDATE_VISIBILITY; import static org.sonarqube.ws.client.project.ProjectsWsParameters.CONTROLLER; import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_BRANCH; import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_FROM; @@ -44,7 +45,7 @@ import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_PROJECT import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_PROJECT_ID; import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_QUALIFIERS; import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_TO; -import static org.sonarqube.ws.client.project.UpdateVisibilityRequest.Visibility.PUBLIC; +import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_VISIBILITY; /** * Maps web service {@code api/projects}. @@ -66,7 +67,8 @@ public class ProjectsService extends BaseService { .setParam(PARAM_ORGANIZATION, project.getOrganization()) .setParam(PARAM_PROJECT, project.getKey()) .setParam(PARAM_NAME, project.getName()) - .setParam(PARAM_BRANCH, project.getBranch()); + .setParam(PARAM_BRANCH, project.getBranch()) + .setParam(PARAM_VISIBILITY, project.getVisibility()); return call(request, CreateWsResponse.parser()); } @@ -109,13 +111,9 @@ public class ProjectsService extends BaseService { } public void updateVisibility(UpdateVisibilityRequest request) { - PostRequest post = new PostRequest(path("update_visibility")) + PostRequest post = new PostRequest(path(ACTION_UPDATE_VISIBILITY)) .setParam(PARAM_PROJECT, request.getProject()) - .setParam("visibility", - request.getVisibility() - .map(visibility -> visibility == PUBLIC ? "public" : "private") - .orElse(null)); - + .setParam(PARAM_VISIBILITY, request.getVisibility()); call(post); } } diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsWsParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsWsParameters.java index c9b4a988a03..f4e9c7bc438 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsWsParameters.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsWsParameters.java @@ -30,6 +30,7 @@ public class ProjectsWsParameters { public static final String ACTION_SEARCH = "search"; public static final String ACTION_UPDATE_KEY = "update_key"; public static final String ACTION_BULK_UPDATE_KEY = "bulk_update_key"; + public static final String ACTION_UPDATE_VISIBILITY = "update_visibility"; public static final String PARAM_PROJECT = "project"; public static final String PARAM_PROJECT_ID = "projectId"; @@ -41,6 +42,7 @@ public class ProjectsWsParameters { public static final String PARAM_TO = "to"; public static final String PARAM_DRY_RUN = "dryRun"; + public static final String PARAM_VISIBILITY = "visibility"; public static final String FILTER_LANGUAGES = "languages"; public static final String FILTER_TAGS = "tags"; diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/UpdateVisibilityRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/UpdateVisibilityRequest.java index 38763165ad0..342b7a547b3 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/UpdateVisibilityRequest.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/UpdateVisibilityRequest.java @@ -19,26 +19,48 @@ */ package org.sonarqube.ws.client.project; -import java.util.Optional; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; public class UpdateVisibilityRequest { private final String project; - private final Visibility visibility; + private final String visibility; - public enum Visibility { - PUBLIC, PRIVATE - } - - public UpdateVisibilityRequest(String project, Visibility visibility) { - this.project = project; - this.visibility = visibility; + public UpdateVisibilityRequest(Builder builder) { + this.project = builder.project; + this.visibility = builder.visibility; } + @CheckForNull public String getProject() { return project; } - public Optional<Visibility> getVisibility() { - return Optional.ofNullable(visibility); + @CheckForNull + public String getVisibility() { + return visibility; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + private String project; + private String visibility; + + public Builder setProject(@Nullable String project) { + this.project = project; + return this; + } + + public Builder setVisibility(@Nullable String visibility) { + this.visibility = visibility; + return this; + } + + public UpdateVisibilityRequest build() { + return new UpdateVisibilityRequest(this); + } } } diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/project/ProjectsServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/project/ProjectsServiceTest.java index 2576eb3eb09..b329c02b774 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/project/ProjectsServiceTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/project/ProjectsServiceTest.java @@ -85,6 +85,36 @@ public class ProjectsServiceTest { } @Test + public void creates_public_project() { + underTest.create(CreateRequest.builder() + .setKey("project_key") + .setName("Project Name") + .setVisibility("public") + .build()); + + assertThat(serviceTester.getPostRequest().getPath()).isEqualTo("api/projects/create"); + assertThat(serviceTester.getPostRequest().getParams()).containsOnly( + entry("project", "project_key"), + entry("name", "Project Name"), + entry("visibility", "public")); + } + + @Test + public void creates_private_project() { + underTest.create(CreateRequest.builder() + .setKey("project_key") + .setName("Project Name") + .setVisibility("private") + .build()); + + assertThat(serviceTester.getPostRequest().getPath()).isEqualTo("api/projects/create"); + assertThat(serviceTester.getPostRequest().getParams()).containsOnly( + entry("project", "project_key"), + entry("name", "Project Name"), + entry("visibility", "private")); + } + + @Test public void deletes_project_by_id() { underTest.delete(DeleteRequest.builder().setId("abc").build()); @@ -119,4 +149,17 @@ public class ProjectsServiceTest { .hasParam(PAGE_SIZE, 10) .andNoOtherParam(); } + + @Test + public void update_visibility() { + underTest.updateVisibility(UpdateVisibilityRequest.builder() + .setProject("project_key") + .setVisibility("public") + .build()); + + assertThat(serviceTester.getPostRequest().getPath()).isEqualTo("api/projects/update_visibility"); + assertThat(serviceTester.getPostRequest().getParams()).containsOnly( + entry("project", "project_key"), + entry("visibility", "public")); + } } |