]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9616 Handle branch in api/settings/reset
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 25 Aug 2017 07:52:55 +0000 (09:52 +0200)
committerJanos Gyerik <janos.gyerik@sonarsource.com>
Tue, 12 Sep 2017 09:34:53 +0000 (11:34 +0200)
server/sonar-server/src/main/java/org/sonar/server/setting/ws/ResetAction.java
server/sonar-server/src/test/java/org/sonar/server/setting/ws/ResetActionTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/setting/ResetRequest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsService.java
sonar-ws/src/test/java/org/sonarqube/ws/client/setting/ResetRequestTest.java
sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SettingsServiceTest.java

index 726c856c1f1e8d5756b73be2104be95b68f92122..9986076fabf43a1c0e860f1c0739caf7e04ced8b 100644 (file)
@@ -39,8 +39,10 @@ import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.client.setting.ResetRequest;
 
 import static java.util.Collections.emptyList;
+import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
 import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001;
 import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_RESET;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_BRANCH;
 import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT;
 import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_KEYS;
 
@@ -84,6 +86,11 @@ public class ResetAction implements SettingsWsAction {
       .setDescription("Component key")
       .setDeprecatedKey("componentKey", "6.3")
       .setExampleValue(KEY_PROJECT_EXAMPLE_001);
+    action.createParam(PARAM_BRANCH)
+      .setDescription("Branch key")
+      .setExampleValue(KEY_BRANCH_EXAMPLE_001)
+      .setInternal(true)
+      .setSince("6.6");
   }
 
   @Override
@@ -122,6 +129,7 @@ public class ResetAction implements SettingsWsAction {
     return ResetRequest.builder()
       .setKeys(request.paramAsStrings(PARAM_KEYS))
       .setComponent(request.param(PARAM_COMPONENT))
+      .setBranch(request.param(PARAM_BRANCH))
       .build();
   }
 
@@ -130,7 +138,7 @@ public class ResetAction implements SettingsWsAction {
     if (componentKey == null) {
       return Optional.empty();
     }
-    return Optional.of(componentFinder.getByKey(dbSession, componentKey));
+    return Optional.of(componentFinder.getByKeyAndOptionalBranch(dbSession, componentKey, request.getBranch()));
   }
 
   private void checkPermissions(Optional<ComponentDto> component) {
index 97816ea47c9e17cc02cb358bb54746c78b393d1d..b0b41495f5259962f9f7941056d1a2cb12412a9d 100644 (file)
@@ -28,6 +28,7 @@ import org.junit.rules.ExpectedException;
 import org.sonar.api.config.PropertyDefinition;
 import org.sonar.api.config.PropertyDefinitions;
 import org.sonar.api.server.ws.WebService;
+import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
@@ -200,6 +201,24 @@ public class ResetActionTest {
     assertGlobalPropertyDoesNotExist("foo");
   }
 
+  @Test
+  public void remove_setting_on_branch() throws Exception {
+    ComponentDto project = db.components().insertMainBranch();
+    ComponentDto branch = db.components().insertProjectBranch(project);
+    definitions.addComponent(PropertyDefinition.builder("foo").onQualifiers(PROJECT).build());
+    propertyDb.insertProperties(newComponentPropertyDto(branch).setKey("foo").setValue("value"));
+    userSession.logIn().addProjectPermission(ADMIN, project);
+
+    ws.newRequest()
+      .setMediaType(MediaTypes.PROTOBUF)
+      .setParam("keys", "foo")
+      .setParam("component", branch.getKey())
+      .setParam("branch", branch.getBranch())
+      .execute();
+
+    assertProjectPropertyDoesNotExist(branch, "foo");
+  }
+
   @Test
   public void empty_204_response() {
     logInAsSystemAdministrator();
@@ -218,7 +237,7 @@ public class ResetActionTest {
     assertThat(action.isInternal()).isFalse();
     assertThat(action.isPost()).isTrue();
     assertThat(action.responseExampleAsString()).isNull();
-    assertThat(action.params()).hasSize(2);
+    assertThat(action.params()).extracting(Param::key).containsExactlyInAnyOrder("keys", "component", "branch");
   }
 
   @Test
@@ -359,6 +378,34 @@ public class ResetActionTest {
       .execute();
   }
 
+  @Test
+  public void fail_when_component_not_found() {
+    expectedException.expect(NotFoundException.class);
+    expectedException.expectMessage("Component key 'unknown' not found");
+
+    ws.newRequest()
+      .setParam("keys", "foo")
+      .setParam("component", "unknown")
+      .execute();
+  }
+
+  @Test
+  public void fail_when_branch_not_found() {
+    ComponentDto project = db.components().insertMainBranch();
+    logInAsProjectAdmin(project);
+    ComponentDto branch = db.components().insertProjectBranch(project);
+    String settingKey = "not_allowed_on_branch";
+
+    expectedException.expect(NotFoundException.class);
+    expectedException.expectMessage(format("Component '%s' on branch 'unknown' not found", branch.getKey()));
+
+    ws.newRequest()
+      .setParam("keys", settingKey)
+      .setParam("component", branch.getKey())
+      .setParam("branch", "unknown")
+      .execute();
+  }
+
   private void succeedForPropertyWithoutDefinitionAndValidComponent(ComponentDto root, ComponentDto module) {
     logInAsProjectAdmin(root);
 
@@ -417,8 +464,12 @@ public class ResetActionTest {
     assertThat(dbClient.propertiesDao().selectGlobalProperty(dbSession, key)).isNotNull();
   }
 
+  private void assertProjectPropertyDoesNotExist(ComponentDto component, String key) {
+    assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentId(component.getId()).setKey(key).build(), dbSession)).isEmpty();
+  }
+
   private void assertProjectPropertyDoesNotExist(String key) {
-    assertThat(dbClient.propertiesDao().selectByQuery(PropertyQuery.builder().setComponentId(project.getId()).setKey(key).build(), dbSession)).isEmpty();
+    assertProjectPropertyDoesNotExist(project, key);
   }
 
   private void assertProjectPropertyExists(String key) {
index ab5565f2c1323d4b97a5c8f22ccac07547a7498d..696828eee7da77d3662881cc6d5949d5dbb2497e 100644 (file)
@@ -29,10 +29,12 @@ import static java.util.Arrays.asList;
 public class ResetRequest {
   private final List<String> keys;
   private final String component;
+  private final String branch;
 
   private ResetRequest(Builder builder) {
     this.keys = builder.keys;
     this.component = builder.component;
+    this.branch = builder.branch;
   }
 
   public List<String> getKeys() {
@@ -44,6 +46,11 @@ public class ResetRequest {
     return component;
   }
 
+  @CheckForNull
+  public String getBranch() {
+    return branch;
+  }
+
   public static Builder builder() {
     return new Builder();
   }
@@ -51,6 +58,7 @@ public class ResetRequest {
   public static class Builder {
     private List<String> keys;
     private String component;
+    private String branch;
 
     private Builder() {
       // enforce factory method use
@@ -71,6 +79,11 @@ public class ResetRequest {
       return this;
     }
 
+    public Builder setBranch(@Nullable String branch) {
+      this.branch = branch;
+      return this;
+    }
+
     public ResetRequest build() {
       checkArgument(keys != null && !keys.isEmpty(), "Setting keys is mandatory and must not be empty.");
       return new ResetRequest(this);
index f0ec6e354c18d0759cd8096dfd7dd38e6a578a1f..230dbd992a528b1df1a8ce95a3d97f3d27bc9b41 100644 (file)
@@ -71,7 +71,9 @@ public class SettingsService extends BaseService {
   public void reset(ResetRequest request) {
     call(new PostRequest(path(ACTION_RESET))
       .setParam(PARAM_KEYS, inlineMultipleParamValue(request.getKeys()))
-      .setParam(PARAM_COMPONENT, request.getComponent()));
+      .setParam(PARAM_COMPONENT, request.getComponent())
+      .setParam(PARAM_BRANCH, request.getBranch())
+    );
   }
 
 }
index 3d092f0fdc9a707d5ee0ebc54ed3b2f3b7917016..7d41d60ab3d63fb845725c1db9148b6b4a04bd7d 100644 (file)
@@ -37,6 +37,26 @@ public class ResetRequestTest {
     ResetRequest result = underTest.setKeys("my.key").build();
 
     assertThat(result.getKeys()).containsOnly("my.key");
+    assertThat(result.getComponent()).isNull();
+    assertThat(result.getBranch()).isNull();
+  }
+
+  @Test
+  public void component() {
+    ResetRequest result = underTest.setKeys("my.key").setComponent("project").build();
+
+    assertThat(result.getKeys()).containsOnly("my.key");
+    assertThat(result.getComponent()).isEqualTo("project");
+    assertThat(result.getBranch()).isNull();
+  }
+
+  @Test
+  public void component_and_branch() {
+    ResetRequest result = underTest.setKeys("my.key").setComponent("project").setBranch("branch").build();
+
+    assertThat(result.getKeys()).containsOnly("my.key");
+    assertThat(result.getComponent()).isEqualTo("project");
+    assertThat(result.getBranch()).isEqualTo("branch");
   }
 
   @Test
index d0b84d18afa691914e17c618f403dd508b91aa40..c3c1f9c051e40b90a9cde655d972abfb807f878e 100644 (file)
@@ -99,11 +99,13 @@ public class SettingsServiceTest {
     underTest.reset(ResetRequest.builder()
       .setKeys("sonar.debt")
       .setComponent("KEY")
+      .setBranch("BRANCH")
       .build());
 
     serviceTester.assertThat(serviceTester.getPostRequest())
       .hasParam(PARAM_KEYS, "sonar.debt")
       .hasParam(PARAM_COMPONENT, "KEY")
+      .hasParam(PARAM_BRANCH, "BRANCH")
       .andNoOtherParam();
   }