]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10134 Add organization parameter in rename action
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 7 Dec 2017 15:05:25 +0000 (16:05 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 14 Dec 2017 16:03:35 +0000 (17:03 +0100)
server/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGateFinder.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/QualityGatesWsSupport.java
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/RenameAction.java
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/RenameActionTest.java

index 6a914c6cc14fd23eb88adb310dba0c0a2c56cc64..7bafdce2aa4d37ee30762d1cc94eb72daa9bcea9 100644 (file)
@@ -49,7 +49,8 @@ public class QualityGateFinder {
   public Optional<QualityGateData> getQualityGate(DbSession dbSession, long componentId) {
     Optional<Long> qualityGateId = dbClient.projectQgateAssociationDao().selectQGateIdByComponentId(dbSession, componentId);
     if (qualityGateId.isPresent()) {
-      return Optional.of(new QualityGateData(getById(dbSession, qualityGateId.get()), false));
+      QualityGateDto qualityGate = checkFound(dbClient.qualityGateDao().selectById(dbSession, qualityGateId.get()), "No quality gate has been found for id %s", qualityGateId);
+      return Optional.of(new QualityGateData(qualityGate, false));
     } else {
       Optional<QualityGateDto> defaultQualityGate = getDefault(dbSession);
       if (!defaultQualityGate.isPresent()) {
@@ -59,14 +60,6 @@ public class QualityGateFinder {
     }
   }
 
-  /**
-   * @deprecated Use {@link #getByOrganizationAndId(DbSession, OrganizationDto, long)}
-   */
-  @Deprecated
-  public QualityGateDto getById(DbSession dbSession, long qualityGateId) {
-    return checkFound(dbClient.qualityGateDao().selectById(dbSession, qualityGateId), "No quality gate has been found for id %s", qualityGateId);
-  }
-
   public QGateWithOrgDto getByOrganizationAndId(DbSession dbSession, OrganizationDto organization, long qualityGateId) {
     return checkFound(dbClient.qualityGateDao().selectByOrganizationAndId(dbSession, organization, qualityGateId),
       "No quality gate has been found for id %s in organization %s", qualityGateId, organization.getName());
index efade3cdd909b9ff6fa1c4e460986dd9ab6d7696..2b49421ecd7dcc90475567037956e4bf7fbea270 100644 (file)
@@ -98,15 +98,6 @@ public class QualityGatesWsSupport {
     return checkFoundWithOptional(organizationDto, "No organization with key '%s'", organizationKey);
   }
 
-  /**
-   * @deprecated use {@link #checkCanEdit(QGateWithOrgDto)} instead
-   */
-  @Deprecated
-  void checkCanEdit(QualityGateDto qualityGate) {
-    checkNotBuiltIn(qualityGate);
-    userSession.checkPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid());
-  }
-
   void checkCanEdit(QGateWithOrgDto qualityGate) {
     checkNotBuiltIn(qualityGate);
     userSession.checkPermission(ADMINISTER_QUALITY_GATES, qualityGate.getOrganizationUuid());
@@ -120,7 +111,7 @@ public class QualityGatesWsSupport {
     throw insufficientPrivilegesException();
   }
 
-  void checkProjectBelongsToOrganization(OrganizationDto organization, ComponentDto project){
+  void checkProjectBelongsToOrganization(OrganizationDto organization, ComponentDto project) {
     if (project.getOrganizationUuid().equals(organization.getUuid())) {
       return;
     }
index 212330cc98ffd784c3105fc67d5f015aec7eb971..f0ed2f2253e26f8035d0ebc5018a1f044a4f4f08 100644 (file)
@@ -24,6 +24,8 @@ import org.sonar.api.server.ws.Response;
 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;
@@ -67,33 +69,36 @@ public class RenameAction implements QualityGatesWsAction {
       .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);
   }
index f1405906ccbb7ec49ce3fd59f64294cb77233847..adddf7c3185c2e4a2a30be2bc2aac3fba7bdf25f 100644 (file)
@@ -26,7 +26,8 @@ import org.junit.rules.ExpectedException;
 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;
@@ -64,17 +65,22 @@ public class RenameActionTest {
     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");
@@ -82,12 +88,14 @@ public class RenameActionTest {
 
   @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());
@@ -96,21 +104,40 @@ public class RenameActionTest {
 
   @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()));
@@ -118,13 +145,15 @@ public class RenameActionTest {
     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");
@@ -132,14 +161,16 @@ public class RenameActionTest {
     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()));
@@ -147,35 +178,37 @@ public class RenameActionTest {
     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());
-  }
 }