import org.sonar.server.ws.KeyExamples;
import org.sonarqube.ws.client.setting.SetRequest;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_SET;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_ID;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_KEY;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_KEY;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_VALUE;
+
public class SetAction implements SettingsWsAction {
private final DbClient dbClient;
private final ComponentFinder componentFinder;
@Override
public void define(WebService.NewController context) {
- WebService.NewAction action = context.createAction("set")
+ WebService.NewAction action = context.createAction(ACTION_SET)
.setDescription("Update a setting value.<br>" +
"Either '%s' or '%s' can be provided, not both.<br> " +
"Requires one of the following permissions: " +
"<ul>" +
"<li>'Administer System'</li>" +
"<li>'Administer' rights on the specified component</li>" +
- "</ul>", "componentId", "componentKey")
+ "</ul>", PARAM_COMPONENT_ID, PARAM_COMPONENT_KEY)
.setSince("6.1")
.setPost(true)
.setHandler(this);
- action.createParam("key")
+ action.createParam(PARAM_KEY)
.setDescription("Setting key")
.setExampleValue("sonar.links.scm")
.setRequired(true);
- action.createParam("value")
+ action.createParam(PARAM_VALUE)
.setDescription("Setting value. To reset a value, please use the reset web service.")
.setExampleValue("git@github.com:SonarSource/sonarqube.git")
.setRequired(true);
- action.createParam("componentId")
+ action.createParam(PARAM_COMPONENT_ID)
.setDescription("Component id")
.setExampleValue(Uuids.UUID_EXAMPLE_01);
- action.createParam("componentKey")
+ action.createParam(PARAM_COMPONENT_KEY)
.setDescription("Component key")
.setExampleValue(KeyExamples.KEY_PROJECT_EXAMPLE_001);
}
private static SetRequest toWsRequest(Request request) {
return SetRequest.builder()
- .setKey(request.mandatoryParam("key"))
- .setValue(request.mandatoryParam("value"))
- .setComponentId(request.param("componentId"))
- .setComponentKey(request.param("componentKey"))
+ .setKey(request.mandatoryParam(PARAM_KEY))
+ .setValue(request.mandatoryParam(PARAM_VALUE))
+ .setComponentId(request.param(PARAM_COMPONENT_ID))
+ .setComponentKey(request.param(PARAM_COMPONENT_KEY))
.build();
}
import org.sonarqube.ws.Settings.ValuesWsResponse;
import org.sonarqube.ws.client.BaseService;
import org.sonarqube.ws.client.GetRequest;
+import org.sonarqube.ws.client.PostRequest;
import org.sonarqube.ws.client.WsConnector;
import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_COMPONENT_ID;
import static org.sonarqube.ws.client.measure.MeasuresWsParameters.PARAM_COMPONENT_KEY;
import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_LIST_DEFINITIONS;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_SET;
import static org.sonarqube.ws.client.setting.SettingsWsParameters.ACTION_VALUES;
import static org.sonarqube.ws.client.setting.SettingsWsParameters.CONTROLLER_SETTINGS;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_KEY;
import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_KEYS;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_VALUE;
public class SettingsService extends BaseService {
public SettingsService(WsConnector wsConnector) {
return call(getRequest, ValuesWsResponse.parser());
}
+ public void set(SetRequest request) {
+ call(new PostRequest(path(ACTION_SET))
+ .setParam(PARAM_KEY, request.getKey())
+ .setParam(PARAM_VALUE, request.getValue())
+ .setParam(PARAM_COMPONENT_ID, request.getComponentId())
+ .setParam(PARAM_COMPONENT_KEY, request.getComponentKey()));
+ }
+
}
public static final String ACTION_LIST_DEFINITIONS = "list_definitions";
public static final String ACTION_VALUES = "values";
+ public static final String ACTION_SET = "set";
public static final String PARAM_COMPONENT_ID = "componentId";
public static final String PARAM_COMPONENT_KEY = "componentKey";
public static final String PARAM_KEYS = "keys";
+ public static final String PARAM_KEY = "key";
+ public static final String PARAM_VALUE = "value";
private SettingsWsParameters() {
// Only static stuff
assertThat(result.getKey()).isEqualTo("my.key");
assertThat(result.getValue()).isEqualTo("my value");
+ assertThat(result.getComponentKey()).isNull();
+ assertThat(result.getComponentId()).isNull();
+ }
+
+ @Test
+ public void create_request_with_component_id() {
+ SetRequest result = underTest.setKey("my.key").setValue("my value").setComponentId("projectId").build();
+
+ assertThat(result.getKey()).isEqualTo("my.key");
+ assertThat(result.getValue()).isEqualTo("my value");
+ assertThat(result.getComponentId()).isEqualTo("projectId");
+ assertThat(result.getComponentKey()).isNull();
+ }
+
+ @Test
+ public void create_request_with_component_key() {
+ SetRequest result = underTest.setKey("my.key").setValue("my value").setComponentKey("projectKey").build();
+
+ assertThat(result.getKey()).isEqualTo("my.key");
+ assertThat(result.getValue()).isEqualTo("my value");
+ assertThat(result.getComponentId()).isNull();
+ assertThat(result.getComponentKey()).isEqualTo("projectKey");
}
@Test
.setValue(null)
.build();
}
+
}
import org.sonarqube.ws.Settings.ListDefinitionsWsResponse;
import org.sonarqube.ws.Settings.ValuesWsResponse;
import org.sonarqube.ws.client.GetRequest;
+import org.sonarqube.ws.client.PostRequest;
import org.sonarqube.ws.client.ServiceTester;
import org.sonarqube.ws.client.WsConnector;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_COMPONENT_KEY;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_KEY;
import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_KEYS;
+import static org.sonarqube.ws.client.setting.SettingsWsParameters.PARAM_VALUE;
public class SettingsServiceTest {
.andNoOtherParam();
}
+ @Test
+ public void set() {
+ underTest.set(SetRequest.builder()
+ .setKey("sonar.debt")
+ .setValue("8h")
+ .setComponentKey("KEY")
+ .build());
+ PostRequest request = serviceTester.getPostRequest();
+
+ serviceTester.assertThat(request)
+ .hasParam(PARAM_KEY, "sonar.debt")
+ .hasParam(PARAM_VALUE, "8h")
+ .hasParam(PARAM_COMPONENT_KEY, "KEY")
+ .andNoOtherParam();
+ }
+
}