diff options
3 files changed, 48 insertions, 22 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/project/ws/ProvisionedAction.java b/server/sonar-server/src/main/java/org/sonar/server/project/ws/ProvisionedAction.java index a56ea71fccb..28288cb63b9 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/project/ws/ProvisionedAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/project/ws/ProvisionedAction.java @@ -23,6 +23,8 @@ import com.google.common.io.Resources; import java.util.Date; import java.util.List; import java.util.Set; +import java.util.function.Consumer; +import javax.annotation.Nullable; import org.apache.ibatis.session.RowBounds; import org.sonar.api.resources.Qualifiers; import org.sonar.api.server.ws.Change; @@ -30,7 +32,7 @@ import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService.Param; -import org.sonar.api.utils.text.JsonWriter; +import org.sonar.api.utils.DateUtils; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; @@ -38,11 +40,16 @@ import org.sonar.db.organization.OrganizationDto; import org.sonar.server.es.SearchOptions; import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.user.UserSession; +import org.sonarqube.ws.WsComponents.ProvisionedWsResponse; +import org.sonarqube.ws.WsComponents.ProvisionedWsResponse.Component; import static com.google.common.collect.Sets.newHashSet; -import static org.sonar.server.es.SearchOptions.MAX_LIMIT; +import static java.util.Optional.ofNullable; +import static org.sonar.core.util.stream.MoreCollectors.toList; import static org.sonar.db.permission.OrganizationPermission.PROVISION_PROJECTS; +import static org.sonar.server.es.SearchOptions.MAX_LIMIT; import static org.sonar.server.project.ws.ProjectsWsSupport.PARAM_ORGANIZATION; +import static org.sonar.server.ws.WsUtils.writeProtobuf; public class ProvisionedAction implements ProjectsWsAction { @@ -98,36 +105,35 @@ public class ProvisionedAction implements ProjectsWsAction { RowBounds rowBounds = new RowBounds(options.getOffset(), options.getLimit()); List<ComponentDto> projects = dbClient.componentDao().selectProvisioned(dbSession, organization.getUuid(), query, QUALIFIERS_FILTER, rowBounds); int nbOfProjects = dbClient.componentDao().countProvisioned(dbSession, organization.getUuid(), query, QUALIFIERS_FILTER); - JsonWriter json = response.newJsonWriter().beginObject(); - writeProjects(projects, json, desiredFields); - options.writeJson(json, nbOfProjects); - json.endObject().close(); + ProvisionedWsResponse result = ProvisionedWsResponse.newBuilder() + .addAllProjects(writeProjects(projects, desiredFields)) + .setTotal(nbOfProjects) + .setP(options.getPage()) + .setPs(options.getLimit()) + .build(); + writeProtobuf(result, request, response); } } - private static void writeProjects(List<ComponentDto> projects, JsonWriter json, Set<String> desiredFields) { - json.name("projects"); - json.beginArray(); - for (ComponentDto project : projects) { - json.beginObject(); - json.prop("uuid", project.uuid()); - writeIfNeeded(json, "key", project.key(), desiredFields); - writeIfNeeded(json, "name", project.name(), desiredFields); - writeIfNeeded(json, "creationDate", project.getCreatedAt(), desiredFields); - json.endObject(); - } - json.endArray(); + private static List<Component> writeProjects(List<ComponentDto> projects, Set<String> desiredFields) { + return projects.stream().map(project -> { + Component.Builder compBuilder = Component.newBuilder().setUuid(project.uuid()); + writeIfNeeded("key", project.key(), compBuilder::setKey, desiredFields); + writeIfNeeded("name", project.name(), compBuilder::setName, desiredFields); + writeIfNeeded("creationDate", project.getCreatedAt(), compBuilder::setCreationDate, desiredFields); + return compBuilder.build(); + }).collect(toList()); } - private static void writeIfNeeded(JsonWriter json, String fieldName, String value, Set<String> desiredFields) { + private static void writeIfNeeded(String fieldName, String value, Consumer<String> setter, Set<String> desiredFields) { if (desiredFields.contains(fieldName)) { - json.prop(fieldName, value); + setter.accept(value); } } - private static void writeIfNeeded(JsonWriter json, String fieldName, Date date, Set<String> desiredFields) { + private static void writeIfNeeded(String fieldName, @Nullable Date value, Consumer<String> setter, Set<String> desiredFields) { if (desiredFields.contains(fieldName)) { - json.propDateTime(fieldName, date); + ofNullable(value).map(DateUtils::formatDateTime).ifPresent(setter); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/ProvisionedActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/ProvisionedActionTest.java index 29d89453a9b..63b75b04eb2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/ProvisionedActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/ProvisionedActionTest.java @@ -24,6 +24,7 @@ import org.apache.commons.lang.StringUtils; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService.Param; import org.sonar.api.utils.DateUtils; @@ -43,6 +44,7 @@ import org.sonar.server.ws.TestResponse; import org.sonar.server.ws.WsActionTester; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.tuple; import static org.sonar.db.permission.OrganizationPermission.PROVISION_PROJECTS; import static org.sonar.db.permission.OrganizationPermission.SCAN; import static org.sonar.test.JsonAssert.assertJson; @@ -69,6 +71,9 @@ public class ProvisionedActionTest { assertThat(action.description()).isEqualTo("Get the list of provisioned projects.<br /> " + "Require 'Create Projects' permission."); assertThat(action.since()).isEqualTo("5.2"); + assertThat(action.changelog()).extracting(Change::getVersion, Change::getDescription).containsExactlyInAnyOrder( + tuple("6.4", "The 'uuid' field is deprecated in the response") + ); assertThat(action.params()).hasSize(5); diff --git a/sonar-ws/src/main/protobuf/ws-components.proto b/sonar-ws/src/main/protobuf/ws-components.proto index ee32ad94df5..27a36617708 100644 --- a/sonar-ws/src/main/protobuf/ws-components.proto +++ b/sonar-ws/src/main/protobuf/ws-components.proto @@ -87,6 +87,21 @@ message SearchProjectsWsResponse { optional sonarqube.ws.commons.Facets facets = 3; } +// WS api/components/provisioned +message ProvisionedWsResponse { + repeated Component projects = 1; + optional int64 total = 2; + optional int32 p = 3; + optional int64 ps = 4; + + message Component { + optional string uuid = 1; + optional string key = 2; + optional string name = 3; + optional string creationDate = 4; + } +} + message Component { optional string organization = 12; optional string id = 1; |