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;
.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
return ResetRequest.builder()
.setKeys(request.paramAsStrings(PARAM_KEYS))
.setComponent(request.param(PARAM_COMPONENT))
+ .setBranch(request.param(PARAM_BRANCH))
.build();
}
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) {
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;
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();
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
.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);
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) {
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() {
return component;
}
+ @CheckForNull
+ public String getBranch() {
+ return branch;
+ }
+
public static Builder builder() {
return new Builder();
}
public static class Builder {
private List<String> keys;
private String component;
+ private String branch;
private Builder() {
// enforce factory method use
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);
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())
+ );
}
}
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
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();
}