package org.sonar.server.qualitygate.ws;
import com.google.common.io.Resources;
+import java.util.Collection;
import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.server.ws.Change;
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.utils.text.JsonWriter;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.property.PropertyDto;
import org.sonar.db.qualitygate.QualityGateDto;
+import org.sonarqube.ws.Qualitygates.ListWsResponse;
+import static org.sonar.core.util.Protobuf.setNullable;
+import static org.sonar.core.util.stream.MoreCollectors.toList;
import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY;
-import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_ID;
-import static org.sonarqube.ws.client.qualitygate.QualityGatesWsParameters.PARAM_NAME;
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
public class ListAction implements QualityGatesWsAction {
@Override
public void handle(Request request, Response response) {
- try (DbSession dbSession = dbClient.openSession(false);
- JsonWriter writer = response.newJsonWriter()) {
- QualityGateDto defaultQgate = getDefault(dbSession);
- Long defaultQgateId = defaultQgate == null ? null : defaultQgate.getId();
- writer.beginObject().name("qualitygates").beginArray();
- for (QualityGateDto qualityGate : dbClient.qualityGateDao().selectAll(dbSession)) {
- writer.beginObject()
- .prop(PARAM_ID, qualityGate.getId())
- .prop(PARAM_NAME, qualityGate.getName())
- .prop("isDefault", qualityGate.getId().equals(defaultQgateId))
- .endObject();
- }
- writer.endArray();
- if (defaultQgateId != null) {
- writer.prop("default", defaultQgateId);
- }
- writer.endObject().close();
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ QualityGateDto defaultQualityGate = getDefault(dbSession);
+ Collection<QualityGateDto> qualityGates = dbClient.qualityGateDao().selectAll(dbSession);
+ writeProtobuf(buildResponse(qualityGates, defaultQualityGate), request, response);
}
+ }
+ private static ListWsResponse buildResponse(Collection<QualityGateDto> qualityGates, @Nullable QualityGateDto defaultQualityGate) {
+ Long defaultId = defaultQualityGate == null ? null : defaultQualityGate.getId();
+ ListWsResponse.Builder builder = ListWsResponse.newBuilder()
+ .addAllQualitygates(qualityGates.stream()
+ .map(qualityGate -> ListWsResponse.QualityGate.newBuilder()
+ .setId(qualityGate.getId())
+ .setName(qualityGate.getName())
+ .setIsDefault(qualityGate.getId().equals(defaultId))
+ .build())
+ .collect(toList()));
+ setNullable(defaultId, builder::setDefault);
+ return builder.build();
}
@CheckForNull
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
+import org.sonarqube.ws.Qualitygates.ListWsResponse.QualityGate;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.tuple;
import static org.sonar.test.JsonAssert.assertJson;
+import static org.sonarqube.ws.Qualitygates.ListWsResponse;
public class ListActionTest {
@Test
public void list_quality_gates() {
QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate("Sonar way");
- db.qualityGates().insertQualityGate("Sonar way - Without Coverage");
+ QualityGateDto otherQualityGate = db.qualityGates().insertQualityGate("Sonar way - Without Coverage");
db.qualityGates().setDefaultQualityGate(defaultQualityGate);
- String response = ws.newRequest()
- .execute()
- .getInput();
+ ListWsResponse response = ws.newRequest().executeProtobuf(ListWsResponse.class);
- assertJson(response).ignoreFields("id", "default")
- .isSimilarTo("{\n" +
- " \"qualitygates\": [\n" +
- " {\n" +
- " \"name\": \"Sonar way\",\n" +
- " \"isDefault\": true\n" +
- " },\n" +
- " {\n" +
- " \"id\": 4,\n" +
- " \"name\": \"Sonar way - Without Coverage\",\n" +
- " \"isDefault\": false\n" +
- " }\n" +
- " ]\n" +
- "}\n");
+ assertThat(response.getQualitygatesList())
+ .extracting(QualityGate::getId, QualityGate::getName, QualityGate::getIsDefault)
+ .containsExactlyInAnyOrder(
+ tuple(defaultQualityGate.getId(), defaultQualityGate.getName(), true),
+ tuple(otherQualityGate.getId(), otherQualityGate.getName(), false));
+ }
+
+ @Test
+ public void test_deprecated_default_field() {
+ QualityGateDto defaultQualityGate = db.qualityGates().insertQualityGate("Sonar way");
+ db.qualityGates().setDefaultQualityGate(defaultQualityGate);
+
+ ListWsResponse response = ws.newRequest().executeProtobuf(ListWsResponse.class);
+
+ assertThat(response.getDefault()).isEqualTo(defaultQualityGate.getId());
}
@Test
public void no_default_quality_gate() {
- db.qualityGates().insertQualityGate("Sonar way");
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate("Sonar way");
- String response = ws.newRequest()
- .execute()
- .getInput();
+ ListWsResponse response = ws.newRequest().executeProtobuf(ListWsResponse.class);
- assertJson(response).ignoreFields("id", "default")
- .isSimilarTo("{\n" +
- " \"qualitygates\": [\n" +
- " {\n" +
- " \"name\": \"Sonar way\",\n" +
- " \"isDefault\": false\n" +
- " }\n" +
- " ]\n" +
- "}\n");
+ assertThat(response.getQualitygatesList())
+ .extracting(QualityGate::getId, QualityGate::getName, QualityGate::getIsDefault)
+ .containsExactlyInAnyOrder(tuple(qualityGate.getId(), qualityGate.getName(), false));
}
@Test
public void empty() {
- String response = ws.newRequest()
- .execute()
- .getInput();
+ ListWsResponse response = ws.newRequest().executeProtobuf(ListWsResponse.class);
- assertJson(response).ignoreFields("id", "default")
- .isSimilarTo("{\n" +
- " \"qualitygates\": []\n" +
- "}\n");
+ assertThat(response.getQualitygatesList()).isEmpty();
}
}
}
}
+// GET api/qualitygates/list
+message ListWsResponse {
+ repeated QualityGate qualitygates = 1;
+ // Deprecated since 7.0
+ optional int64 default = 2;
+
+ message QualityGate {
+ optional int64 id = 1;
+ optional string name = 2;
+ optional bool isDefault = 3;
+ }
+}
+