From: Julien Lancelot Date: Fri, 25 Aug 2017 07:52:55 +0000 (+0200) Subject: SONAR-9616 Handle branch in api/settings/reset X-Git-Tag: 6.6-RC1~380^2~44 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b56de575ec830f18533a61d0ccd8523c0766a240;p=sonarqube.git SONAR-9616 Handle branch in api/settings/reset --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/ResetAction.java b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/ResetAction.java index 726c856c1f1..9986076fabf 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/setting/ws/ResetAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/setting/ws/ResetAction.java @@ -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 component) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/ResetActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/ResetActionTest.java index 97816ea47c9..b0b41495f52 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/ResetActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/ResetActionTest.java @@ -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) { diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/ResetRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/ResetRequest.java index ab5565f2c13..696828eee7d 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/ResetRequest.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/ResetRequest.java @@ -29,10 +29,12 @@ import static java.util.Arrays.asList; public class ResetRequest { private final List 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 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 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); diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsService.java index f0ec6e354c1..230dbd992a5 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsService.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/setting/SettingsService.java @@ -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()) + ); } } diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/ResetRequestTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/ResetRequestTest.java index 3d092f0fdc9..7d41d60ab3d 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/ResetRequestTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/ResetRequestTest.java @@ -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 diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SettingsServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SettingsServiceTest.java index d0b84d18afa..c3c1f9c051e 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SettingsServiceTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/setting/SettingsServiceTest.java @@ -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(); }