]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5489 Check a project's current profile association before modification
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 30 Mar 2015 19:37:54 +0000 (21:37 +0200)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Mon, 30 Mar 2015 19:37:54 +0000 (21:37 +0200)
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/QProfileProjectOperations.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/ProjectAssociationActions.java
server/sonar-server/src/test/java/org/sonar/server/qualityprofile/ws/QProfilesWsMediumTest.java
sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileDao.java
sonar-core/src/main/java/org/sonar/core/qualityprofile/db/QualityProfileMapper.java
sonar-core/src/main/resources/org/sonar/core/qualityprofile/db/QualityProfileMapper.xml

index 897d6264815f3301d669731e1b1aa13f6a1f13bd..be66271e52191777ae515fbb0ef32ee8c000ec12 100644 (file)
@@ -58,8 +58,19 @@ public class QProfileProjectOperations implements ServerComponent {
     checkPermission(userSession, project.key());
     QualityProfileDto qualityProfile = findNotNull(profileKey, session);
 
-    db.qualityProfileDao().insertProjectProfileAssociation(project.uuid(), qualityProfile.getKey(), session);
-    session.commit();
+    QualityProfileDto currentProfile = db.qualityProfileDao().getByProjectAndLanguage(project.key(), qualityProfile.getLanguage(), session);
+
+    boolean updated = false;
+    if (currentProfile == null) {
+      db.qualityProfileDao().insertProjectProfileAssociation(project.uuid(), qualityProfile.getKey(), session);
+      updated = true;
+    } else if (!profileKey.equals(currentProfile.getKey())) {
+      db.qualityProfileDao().updateProjectProfileAssociation(projectUuid, profileKey, session);
+      updated = true;
+    }
+    if (updated) {
+      session.commit();
+    }
   }
 
   public void removeProject(String profileKey, String projectUuid, UserSession userSession) {
index 8678d5a785c689f614e0bba7db4722761778856e..08e7411cf85c7c1bb45d04775d1fcd89d3f49c19 100644 (file)
@@ -61,7 +61,7 @@ public class ProjectAssociationActions implements ServerComponent {
   void define(WebService.NewController controller) {
     NewAction addProject = controller.createAction("add_project")
       .setSince("5.2")
-      .setDescription("Associate a project with a quality profile")
+      .setDescription("Associate a project with a quality profile.")
       .setHandler(new AssociationHandler(profileLookup, componentService) {
         @Override
         protected void changeAssociation(String profileKey, String projectUuid, UserSession userSession) {
@@ -72,7 +72,7 @@ public class ProjectAssociationActions implements ServerComponent {
 
     NewAction removeProject = controller.createAction("remove_project")
       .setSince("5.2")
-      .setDescription("Remove a project's association with a quality profile")
+      .setDescription("Remove a project's association with a quality profile.")
       .setHandler(new AssociationHandler(profileLookup, componentService) {
         @Override
         protected void changeAssociation(String profileKey, String projectUuid, UserSession userSession) {
@@ -85,15 +85,15 @@ public class ProjectAssociationActions implements ServerComponent {
   private void setCommonAttributes(NewAction action) {
     action.setPost(true);
     action.createParam(PARAM_PROJECT_UUID)
-      .setDescription("A project UUID. Either this parameter, or projectKey must be set");
+      .setDescription("A project UUID. Either this parameter, or projectKey must be set.");
     action.createParam(PARAM_PROJECT_KEY)
-      .setDescription("A project key. Either this parameter, or projectUuid must be set");
+      .setDescription("A project key. Either this parameter, or projectUuid must be set.");
     action.createParam(PARAM_PROFILE_KEY)
-      .setDescription("A quality profile key. Either this parameter, or a combination of profileName + language must be set");
+      .setDescription("A quality profile key. Either this parameter, or a combination of profileName + language must be set.");
     action.createParam(PARAM_PROFILE_NAME)
-      .setDescription("A quality profile name. If this parameter is set, profileKey must not be set and language must be set to disambiguate");
+      .setDescription("A quality profile name. If this parameter is set, profileKey must not be set and language must be set to disambiguate.");
     action.createParam(PARAM_LANGUAGE)
-      .setDescription("A quality profile language. If this parameter is set, profileKey must not be set and language must be set to disambiguate")
+      .setDescription("A quality profile language. If this parameter is set, profileKey must not be set and profileName must be set to disambiguate.")
       .setPossibleValues(Collections2.transform(Arrays.asList(languages.all()), new NonNullInputFunction<Language, String>() {
         @Override
         public String doApply(Language input) {
@@ -102,7 +102,7 @@ public class ProjectAssociationActions implements ServerComponent {
       }));
   }
 
-  private static abstract class AssociationHandler implements RequestHandler {
+  private abstract static class AssociationHandler implements RequestHandler {
 
     private final QProfileLookup profileLookup;
     private final ComponentService componentService;
index 170735e0bb0a6bf1d6b48690af998850b596b672..2e53f605067a9d34290cbccb2729f0ec4c595da3 100644 (file)
@@ -400,6 +400,37 @@ public class QProfilesWsMediumTest {
       .setParam("profileKey", profile.getKee()).setParam("projectUuid", project.uuid())
       .execute().assertNoContent();
     assertThat(tester.get(QProfileFactory.class).getByProjectAndLanguage(session, project.getKey(), "xoo").getKee()).isEqualTo(profile.getKee());
+
+    // Second call must not fail, do nothing
+    wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "add_project")
+      .setParam("profileKey", profile.getKee()).setParam("projectUuid", project.uuid())
+      .execute().assertNoContent();
+    assertThat(tester.get(QProfileFactory.class).getByProjectAndLanguage(session, project.getKey(), "xoo").getKee()).isEqualTo(profile.getKee());
+  }
+
+  @Test
+  public void change_project_association_with_key_and_uuid() throws Exception {
+    ComponentDto project = new ComponentDto()
+      .setId(1L)
+      .setUuid("ABCD")
+      .setKey("org.codehaus.sonar:sonar")
+      .setName("SonarQube")
+      .setLongName("SonarQube")
+      .setQualifier("TRK")
+      .setScope("TRK")
+      .setEnabled(true);
+    db.componentDao().insert(session, project);
+    QualityProfileDto profile1 = QProfileTesting.newXooP1();
+    QualityProfileDto profile2 = QProfileTesting.newXooP2();
+    db.qualityProfileDao().insert(session, profile1, profile2);
+    db.qualityProfileDao().insertProjectProfileAssociation(project.uuid(), profile1.getKey(), session);
+
+    session.commit();
+
+    wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "add_project")
+      .setParam("profileKey", profile2.getKee()).setParam("projectUuid", project.uuid())
+      .execute().assertNoContent();
+    assertThat(tester.get(QProfileFactory.class).getByProjectAndLanguage(session, project.getKey(), "xoo").getKee()).isEqualTo(profile2.getKee());
   }
 
   @Test
index 219b2147990b7759a5548fb758a9668f1ad65938..4e39c7d5d9ddacc18a1a5f3403068a9c35ba13ad 100644 (file)
@@ -327,6 +327,10 @@ public class QualityProfileDao implements ServerComponent, DaoComponent {
     session.getMapper(QualityProfileMapper.class).deleteProjectProfileAssociation(projectUuid, profileKey);
   }
 
+  public void updateProjectProfileAssociation(String projectUuid, String profileKey, DbSession session) {
+    session.getMapper(QualityProfileMapper.class).updateProjectProfileAssociation(projectUuid, profileKey);
+  }
+
   public void deleteAllProjectProfileAssociation(String profileKey, DbSession session) {
     session.getMapper(QualityProfileMapper.class).deleteAllProjectProfileAssociation(profileKey);
   }
index 2dfc8aa954265ec0630c9308afb8c4cf6b4bbb86..bbfda6f5a9ccf18cd6c2e5b8001d2e27a0bad7d3 100644 (file)
@@ -73,6 +73,8 @@ public interface QualityProfileMapper {
 
   void insertProjectProfileAssociation(@Param("projectUuid") String projectUuid, @Param("profileKey") String profileKey);
 
+  void updateProjectProfileAssociation(@Param("projectUuid") String projectUuid, @Param("profileKey") String profileKey);
+
   void deleteProjectProfileAssociation(@Param("projectUuid") String projectUuid, @Param("profileKey") String profileKey);
 
   void deleteAllProjectProfileAssociation(@Param("profileKey") String profileKey);
index 8f069d0b3665cd20089c86b4e60676bd6012861d..1b244cf4011d497aac72ad47cffe2379f90abcc3 100644 (file)
     INSERT INTO project_qprofiles (project_uuid, profile_key) VALUES (#{projectUuid}, #{profileKey})
   </insert>
 
+  <update id="updateProjectProfileAssociation">
+    UPDATE project_qprofiles SET profile_key=#{profileKey} WHERE project_uuid=#{projectUuid}
+  </update>
+
   <update id="deleteProjectProfileAssociation">
     DELETE FROM project_qprofiles WHERE project_uuid=#{projectUuid} AND profile_key=#{profileKey}
   </update>