private final DbClient dbClient;
private final QualityGateFinder qualityGateFinder;
+ private final QGateWsSupport wsSupport;
- public ShowAction(DbClient dbClient, QualityGateFinder qualityGateFinder) {
+ public ShowAction(DbClient dbClient, QualityGateFinder qualityGateFinder, QGateWsSupport wsSupport) {
this.dbClient = dbClient;
this.qualityGateFinder = qualityGateFinder;
+ this.wsSupport = wsSupport;
}
@Override
.setDescription("Display the details of a quality gate")
.setSince("4.3")
.setResponseExample(Resources.getResource(this.getClass(), "show-example.json"))
- .setChangelog(new Change("7.0", "'isBuiltIn' field is added to the response"))
+ .setChangelog(
+ new Change("7.0", "'isBuiltIn' field is added to the response"),
+ new Change("7.0", "'actions' field is added in the response"))
.setHandler(this);
action.createParam(PARAM_ID)
QualityGateDto qualityGate = qualityGateFinder.getByNameOrId(dbSession, name, id);
Collection<QualityGateConditionDto> conditions = getConditions(dbSession, qualityGate);
Map<Integer, MetricDto> metricsById = getMetricsById(dbSession, conditions);
- writeProtobuf(buildResponse(qualityGate, conditions, metricsById), request, response);
+ QualityGateDto defaultQualityGate = wsSupport.getDefault(dbSession);
+ writeProtobuf(buildResponse(qualityGate, defaultQualityGate, conditions, metricsById), request, response);
}
}
.collect(uniqueIndex(MetricDto::getId));
}
- private static ShowWsResponse buildResponse(QualityGateDto qualityGate, Collection<QualityGateConditionDto> conditions, Map<Integer, MetricDto> metricsById) {
+ private ShowWsResponse buildResponse(QualityGateDto qualityGate, @Nullable QualityGateDto defaultQualityGate, Collection<QualityGateConditionDto> conditions,
+ Map<Integer, MetricDto> metricsById) {
return ShowWsResponse.newBuilder()
.setId(qualityGate.getId())
.setName(qualityGate.getName())
.addAllConditions(conditions.stream()
.map(toWsCondition(metricsById))
.collect(toList()))
+ .setActions(wsSupport.getActions(qualityGate, defaultQualityGate))
.build();
}
import org.sonar.db.metric.MetricDto;
import org.sonar.db.qualitygate.QualityGateConditionDto;
import org.sonar.db.qualitygate.QualityGateDto;
+import org.sonar.server.organization.DefaultOrganizationProvider;
+import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.qualitygate.QualityGateFinder;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.ws.WsActionTester;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.tuple;
+import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES;
+import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
import static org.sonar.test.JsonAssert.assertJson;
+import static org.sonarqube.ws.Qualitygates.Actions;
public class ShowActionTest {
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private WsActionTester ws = new WsActionTester(new ShowAction(db.getDbClient(), new QualityGateFinder(db.getDbClient())));
+ private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
+
+ private WsActionTester ws = new WsActionTester(
+ new ShowAction(db.getDbClient(), new QualityGateFinder(db.getDbClient()), new QGateWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider)));
@Test
public void verify_definition() {
WebService.Action action = ws.getDef();
assertThat(action.since()).isEqualTo("4.3");
assertThat(action.changelog()).extracting(Change::getVersion, Change::getDescription)
- .containsExactlyInAnyOrder(tuple("7.0", "'isBuiltIn' field is added to the response"));
+ .containsExactlyInAnyOrder(
+ tuple("7.0", "'isBuiltIn' field is added to the response"),
+ tuple("7.0", "'actions' field is added in the response"));
assertThat(action.params())
.extracting(Param::key, Param::isRequired)
.containsExactlyInAnyOrder(tuple("id", false), tuple("name", false));
@Test
public void json_example() {
+ userSession.logIn("admin").addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
QualityGateDto qualityGate = db.qualityGates().insertQualityGate("My Quality Gate");
MetricDto blockerViolationsMetric = db.measures().insertMetric(m -> m.setKey("blocker_violations"));
MetricDto criticalViolationsMetric = db.measures().insertMetric(m -> m.setKey("critical_violations"));
assertThat(response.getConditionsList()).isEmpty();
}
+ @Test
+ public void actions() {
+ userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+
+ ShowWsResponse response = ws.newRequest().setParam("name", qualityGate.getName())
+ .executeProtobuf(ShowWsResponse.class);
+
+ Actions actions = response.getActions();
+ assertThat(actions.getEdit()).isTrue();
+ assertThat(actions.getCopy()).isTrue();
+ assertThat(actions.getSetAsDefault()).isTrue();
+ assertThat(actions.getAssociateProjects()).isTrue();
+ }
+
+ @Test
+ public void actions_on_default() {
+ userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ db.qualityGates().setDefaultQualityGate(qualityGate);
+
+ ShowWsResponse response = ws.newRequest().setParam("name", qualityGate.getName())
+ .executeProtobuf(ShowWsResponse.class);
+
+ Actions actions = response.getActions();
+ assertThat(actions.getEdit()).isTrue();
+ assertThat(actions.getCopy()).isTrue();
+ assertThat(actions.getSetAsDefault()).isFalse();
+ assertThat(actions.getAssociateProjects()).isFalse();
+ }
+
+ @Test
+ public void actions_on_built_in() {
+ userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
+
+ ShowWsResponse response = ws.newRequest().setParam("name", qualityGate.getName())
+ .executeProtobuf(ShowWsResponse.class);
+
+ Actions actions = response.getActions();
+ assertThat(actions.getEdit()).isFalse();
+ assertThat(actions.getCopy()).isTrue();
+ assertThat(actions.getSetAsDefault()).isTrue();
+ assertThat(actions.getAssociateProjects()).isTrue();
+ }
+
+ @Test
+ public void actions_when_not_quality_gate_administer() {
+ userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, defaultOrganizationProvider.get().getUuid());
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
+
+ ShowWsResponse response = ws.newRequest().setParam("name", qualityGate.getName())
+ .executeProtobuf(ShowWsResponse.class);
+
+ Actions actions = response.getActions();
+ assertThat(actions.getEdit()).isFalse();
+ assertThat(actions.getCopy()).isFalse();
+ assertThat(actions.getSetAsDefault()).isFalse();
+ assertThat(actions.getAssociateProjects()).isFalse();
+ }
+
@Test
public void fail_when_no_name_or_id() {
expectedException.expect(IllegalArgumentException.class);