import org.sonar.api.server.ws.WebService;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.db.qualitygate.QualityGateDto;
import org.sonar.server.qualitygate.QualityGateFinder;
import org.sonarqube.ws.Qualitygates.QualityGate;
.setMaximumLength(NAME_MAXIMUM_LENGTH)
.setDescription("New name of the quality gate")
.setExampleValue("My Quality Gate");
+
+ wsSupport.createOrganizationParam(action);
}
@Override
public void handle(Request request, Response response) {
long id = QualityGatesWs.parseId(request, PARAM_ID);
- QualityGateDto qualityGate = rename(id, request.mandatoryParam(PARAM_NAME));
- writeProtobuf(QualityGate.newBuilder()
- .setId(qualityGate.getId())
- .setName(qualityGate.getName())
- .build(), request, response);
- }
-
- 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);
- dbClient.qualityGateDao().update(qualityGate, dbSession);
- dbSession.commit();
- return qualityGate;
+ OrganizationDto organization = wsSupport.getOrganization(dbSession, request);
+ QualityGateDto qualityGate = rename(dbSession, organization, id, request.mandatoryParam(PARAM_NAME));
+ writeProtobuf(QualityGate.newBuilder()
+ .setId(qualityGate.getId())
+ .setName(qualityGate.getName())
+ .build(), request, response);
}
}
- private void checkNotAlreadyExists(DbSession dbSession, QualityGateDto qualityGate, String name) {
- QualityGateDto existingQgate = dbClient.qualityGateDao().selectByName(dbSession, name);
+ private QualityGateDto rename(DbSession dbSession, OrganizationDto organization, long id, String name) {
+ QGateWithOrgDto qualityGate = qualityGateFinder.getByOrganizationAndId(dbSession, organization, id);
+ wsSupport.checkCanEdit(qualityGate);
+ checkArgument(!isNullOrEmpty(name), CANT_BE_EMPTY_MESSAGE, "Name");
+ checkNotAlreadyExists(dbSession, organization, qualityGate, name);
+ qualityGate.setName(name);
+ dbClient.qualityGateDao().update(qualityGate, dbSession);
+ dbSession.commit();
+ return qualityGate;
+ }
+
+ private void checkNotAlreadyExists(DbSession dbSession, OrganizationDto organization, QualityGateDto qualityGate, String name) {
+ QualityGateDto existingQgate = dbClient.qualityGateDao().selectByOrganizationAndName(dbSession, organization, name);
boolean isModifyingCurrentQgate = existingQgate == null || existingQgate.getId().equals(qualityGate.getId());
checkArgument(isModifyingCurrentQgate, "Name '%s' has already been taken", name);
}
import org.sonar.api.server.ws.WebService;
import org.sonar.api.utils.System2;
import org.sonar.db.DbTester;
-import org.sonar.db.qualitygate.QualityGateDto;
+import org.sonar.db.organization.OrganizationDto;
+import org.sonar.db.qualitygate.QGateWithOrgDto;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.organization.DefaultOrganizationProvider;
assertThat(action.changelog()).isEmpty();
assertThat(action.params())
.extracting(WebService.Param::key, WebService.Param::isRequired)
- .containsExactlyInAnyOrder(tuple("id", true), tuple("name", true));
+ .containsExactlyInAnyOrder(
+ tuple("id", true),
+ tuple("name", true),
+ tuple("organization", false));
}
@Test
public void rename() {
- logAsQualityGateAdminister();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("old name"));
+ OrganizationDto organization = db.organizations().insert();
+ QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("old name"));
+ userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization);
ws.newRequest()
.setParam("id", qualityGate.getId().toString())
.setParam("name", "new name")
+ .setParam("organization", organization.getKey())
.execute();
assertThat(db.getDbClient().qualityGateDao().selectById(db.getSession(), qualityGate.getId()).getName()).isEqualTo("new name");
@Test
public void response_contains_quality_gate() {
- logAsQualityGateAdminister();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("old name"));
+ OrganizationDto organization = db.organizations().insert();
+ userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization);
+ QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("old name"));
QualityGate result = ws.newRequest()
.setParam("id", qualityGate.getId().toString())
.setParam("name", "new name")
+ .setParam("organization", organization.getKey())
.executeProtobuf(QualityGate.class);
assertThat(result.getId()).isEqualTo(qualityGate.getId());
@Test
public void rename_with_same_name() {
- logAsQualityGateAdminister();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("name"));
+ OrganizationDto organization = db.organizations().insert();
+ userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization);
+ QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("name"));
ws.newRequest()
.setParam("id", qualityGate.getId().toString())
.setParam("name", "name")
+ .setParam("organization", organization.getKey())
.execute();
assertThat(db.getDbClient().qualityGateDao().selectById(db.getSession(), qualityGate.getId()).getName()).isEqualTo("name");
}
+ @Test
+ public void default_organization_is_used_when_no_organization_parameter() {
+ QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(db.getDefaultOrganization());
+ OrganizationDto otherOrganization = db.organizations().insert();
+ QGateWithOrgDto otherQualityGate = db.qualityGates().insertQualityGate(otherOrganization);
+ userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, db.getDefaultOrganization());
+
+ QualityGate result = ws.newRequest()
+ .setParam("id", qualityGate.getId().toString())
+ .setParam("name", "new name")
+ .executeProtobuf(QualityGate.class);
+
+ assertThat(result.getId()).isEqualTo(qualityGate.getId());
+ assertThat(result.getName()).isEqualTo("new name");
+ }
+
@Test
public void fail_on_built_in_quality_gate() {
- logAsQualityGateAdminister();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true));
+ OrganizationDto organization = db.organizations().insert();
+ userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization);
+ QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, 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")
+ .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_on_empty_name() {
- logAsQualityGateAdminister();
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+ OrganizationDto organization = db.organizations().insert();
+ userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization);
+ QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Name can't be empty");
ws.newRequest()
.setParam("id", qualityGate.getId().toString())
.setParam("name", "")
+ .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_using_existing_name() {
- logAsQualityGateAdminister();
- QualityGateDto qualityGate1 = db.qualityGates().insertQualityGate();
- QualityGateDto qualityGate2 = db.qualityGates().insertQualityGate();
+ OrganizationDto organization = db.organizations().insert();
+ userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization);
+ QGateWithOrgDto qualityGate1 = db.qualityGates().insertQualityGate(organization);
+ QGateWithOrgDto qualityGate2 = db.qualityGates().insertQualityGate(organization);
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage(format("Name '%s' has already been taken", qualityGate2.getName()));
ws.newRequest()
.setParam("id", qualityGate1.getId().toString())
.setParam("name", qualityGate2.getName())
+ .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_on_unknown_quality_gate() {
- logAsQualityGateAdminister();
+ OrganizationDto organization = db.organizations().insert();
+ userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, organization);
expectedException.expect(NotFoundException.class);
ws.newRequest()
.setParam("id", "123")
.setParam("name", "new name")
+ .setParam("organization", organization.getKey())
.execute();
}
@Test
public void fail_when_not_quality_gates_administer() {
- userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, defaultOrganizationProvider.get().getUuid());
- QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setName("old name"));
+ OrganizationDto organization = db.organizations().insert();
+ userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, organization);
+ QGateWithOrgDto qualityGate = db.qualityGates().insertQualityGate(organization, qg -> qg.setName("old name"));
expectedException.expect(ForbiddenException.class);
ws.newRequest()
.setParam("id", qualityGate.getId().toString())
.setParam("name", "new name")
+ .setParam("organization", organization.getKey())
.execute();
}
- private void logAsQualityGateAdminister() {
- userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
- }
}