import org.sonar.server.user.UserSession;
import org.sonarqube.ws.Qualitygates;
+import static com.google.common.base.Preconditions.checkArgument;
import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES;
import static org.sonar.server.qualitygate.QualityGates.SONAR_QUALITYGATE_PROPERTY;
return Long.valueOf(defaultQgate.getValue());
}
- void checkCanEdit() {
+ void checkCanEdit(QualityGateDto qualityGate) {
+ checkNotBuiltInt(qualityGate);
userSession.checkPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
}
+ private static void checkNotBuiltInt(QualityGateDto qualityGate) {
+ checkArgument(!qualityGate.isBuiltIn(), "Operation forbidden for built-in Quality Gate '%s'", qualityGate.getName());
+ }
+
}
@Override
public void handle(Request request, Response response) {
- wsSupport.checkCanEdit();
long id = QualityGatesWs.parseId(request, PARAM_ID);
QualityGateDto qualityGate = rename(id, request.mandatoryParam(PARAM_NAME));
writeProtobuf(QualityGate.newBuilder()
private QualityGateDto rename(long id, String name) {
try (DbSession dbSession = dbClient.openSession(false)) {
QualityGateDto qualityGate = qualityGateFinder.getById(dbSession, id);
+ wsSupport.checkCanEdit(qualityGate);
checkArgument(!isNullOrEmpty(name), CANT_BE_EMPTY_MESSAGE, "Name");
checkNotAlreadyExists(dbSession, qualityGate, name);
qualityGate.setName(name);
assertThat(db.getDbClient().qualityGateDao().selectById(db.getSession(), qualityGate.getId()).getName()).isEqualTo("name");
}
+ @Test
+ public void fail_on_built_in_quality_gate() {
+ logAsQualityGateAdminister();
+ QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
+
+ expectedException.expect(IllegalArgumentException.class);
+ expectedException.expectMessage(format("Operation forbidden for built-in Quality Gate '%s'", qualityGate.getName()));
+
+ ws.newRequest()
+ .setParam("id", qualityGate.getId().toString())
+ .setParam("name", "name")
+ .execute();
+ }
+
@Test
public void fail_on_empty_name() {
logAsQualityGateAdminister();