@@ -25,6 +25,7 @@ import org.sonar.db.alm.setting.AlmSettingDto; | |||
import static java.util.Arrays.stream; | |||
import static org.sonar.db.almsettings.AlmSettingsTesting.newAzureAlmSettingDto; | |||
import static org.sonar.db.almsettings.AlmSettingsTesting.newBitbucketAlmSettingDto; | |||
import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubAlmSettingDto; | |||
public class AlmSettingsDbTester { | |||
@@ -37,22 +38,24 @@ public class AlmSettingsDbTester { | |||
@SafeVarargs | |||
public final AlmSettingDto insertGitHubAlmSetting(Consumer<AlmSettingDto>... populators) { | |||
AlmSettingDto dto = newGithubAlmSettingDto(); | |||
stream(populators).forEach(p -> p.accept(dto)); | |||
db.getDbClient().almSettingDao().insert(db.getSession(), dto); | |||
db.commit(); | |||
return dto; | |||
return insert(newGithubAlmSettingDto(), populators); | |||
} | |||
@SafeVarargs | |||
public final AlmSettingDto insertAzureAlmSetting(Consumer<AlmSettingDto>... populators) { | |||
AlmSettingDto dto = newAzureAlmSettingDto(); | |||
return insert(newAzureAlmSettingDto(), populators); | |||
} | |||
@SafeVarargs | |||
public final AlmSettingDto insertBitbucketAlmSetting(Consumer<AlmSettingDto>... populators) { | |||
return insert(newBitbucketAlmSettingDto(), populators); | |||
} | |||
private AlmSettingDto insert(AlmSettingDto dto, Consumer<AlmSettingDto>[] populators) { | |||
stream(populators).forEach(p -> p.accept(dto)); | |||
db.getDbClient().almSettingDao().insert(db.getSession(), dto); | |||
db.commit(); | |||
return dto; | |||
} | |||
} |
@@ -41,4 +41,12 @@ public class AlmSettingsTesting { | |||
.setPersonalAccessToken(randomAlphanumeric(2000)) | |||
.setAlm(ALM.AZURE_DEVOPS); | |||
} | |||
public static AlmSettingDto newBitbucketAlmSettingDto() { | |||
return new AlmSettingDto() | |||
.setKey(randomAlphanumeric(40)) | |||
.setUrl(randomAlphanumeric(2000)) | |||
.setPersonalAccessToken(randomAlphanumeric(2000)) | |||
.setAlm(ALM.BITBUCKET); | |||
} | |||
} |
@@ -31,6 +31,8 @@ public class AlmSettingsWsModule extends Module { | |||
UpdateGitHubAction.class, | |||
DeleteAction.class, | |||
CreateAzureAction.class, | |||
UpdateAzureAction.class); | |||
UpdateAzureAction.class, | |||
CreateBitBucketAction.class, | |||
UpdateBitbucketAction.class); | |||
} | |||
} |
@@ -0,0 +1,96 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2019 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.server.almsettings; | |||
import org.sonar.api.server.ws.Request; | |||
import org.sonar.api.server.ws.Response; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.alm.setting.AlmSettingDto; | |||
import org.sonar.server.user.UserSession; | |||
import static java.lang.String.format; | |||
import static org.sonar.db.alm.setting.ALM.AZURE_DEVOPS; | |||
import static org.sonar.db.alm.setting.ALM.BITBUCKET; | |||
public class CreateBitBucketAction implements AlmSettingsWsAction { | |||
private static final String PARAM_KEY = "key"; | |||
private static final String PARAM_URL = "url"; | |||
private static final String PARAM_PERSONAL_ACCESS_TOKEN = "personalAccessToken"; | |||
private final DbClient dbClient; | |||
private UserSession userSession; | |||
public CreateBitBucketAction(DbClient dbClient, UserSession userSession) { | |||
this.dbClient = dbClient; | |||
this.userSession = userSession; | |||
} | |||
@Override | |||
public void define(WebService.NewController context) { | |||
WebService.NewAction action = context.createAction("create_bitbucket") | |||
.setDescription("Create Bitbucket ALM instance Setting. <br/>" + | |||
"Requires the 'Administer System' permission") | |||
.setPost(true) | |||
.setSince("8.1") | |||
.setHandler(this); | |||
action.createParam(PARAM_KEY) | |||
.setRequired(true) | |||
.setMaximumLength(40) | |||
.setDescription("Unique key of the Bitbucket instance setting"); | |||
action.createParam(PARAM_URL) | |||
.setRequired(true) | |||
.setMaximumLength(2000) | |||
.setDescription("Azure API URL"); | |||
action.createParam(PARAM_PERSONAL_ACCESS_TOKEN) | |||
.setRequired(true) | |||
.setMaximumLength(2000) | |||
.setDescription("Bitbucket personal access token"); | |||
} | |||
@Override | |||
public void handle(Request request, Response response) throws Exception { | |||
userSession.checkIsSystemAdministrator(); | |||
doHandle(request); | |||
response.noContent(); | |||
} | |||
private void doHandle(Request request) { | |||
String key = request.mandatoryParam(PARAM_KEY); | |||
String url = request.mandatoryParam(PARAM_URL); | |||
String pat = request.mandatoryParam(PARAM_PERSONAL_ACCESS_TOKEN); | |||
try (DbSession dbSession = dbClient.openSession(false)) { | |||
dbClient.almSettingDao().selectByKey(dbSession, key) | |||
.ifPresent(almSetting -> { | |||
throw new IllegalArgumentException(format("An ALM setting with key '%s' already exist", almSetting.getKey())); | |||
}); | |||
dbClient.almSettingDao().insert(dbSession, new AlmSettingDto() | |||
.setAlm(BITBUCKET) | |||
.setKey(key) | |||
.setUrl(url) | |||
.setPersonalAccessToken(pat)); | |||
dbSession.commit(); | |||
} | |||
} | |||
} |
@@ -47,7 +47,7 @@ public class DeleteAction implements AlmSettingsWsAction { | |||
public void define(WebService.NewController context) { | |||
WebService.NewAction action = context | |||
.createAction("delete") | |||
.setDescription("Delete ALM Settings.<br/>" + | |||
.setDescription("Delete an ALM Settings.<br/>" + | |||
"Requires the 'Administer System' permission") | |||
.setSince("8.1") | |||
.setPost(true) |
@@ -30,13 +30,13 @@ import org.sonar.db.DbSession; | |||
import org.sonar.db.alm.setting.ALM; | |||
import org.sonar.db.alm.setting.AlmSettingDto; | |||
import org.sonar.server.user.UserSession; | |||
import org.sonarqube.ws.AlmSettings; | |||
import org.sonarqube.ws.AlmSettings.AlmSettingGithub; | |||
import static java.util.Collections.emptyList; | |||
import static java.util.Objects.requireNonNull; | |||
import static org.sonar.server.ws.WsUtils.writeProtobuf; | |||
import static org.sonarqube.ws.AlmSettings.*; | |||
import static org.sonarqube.ws.AlmSettings.AlmSettingAzure; | |||
import static org.sonarqube.ws.AlmSettings.AlmSettingBitbucket; | |||
import static org.sonarqube.ws.AlmSettings.ListDefinitionsWsResponse; | |||
public class ListDefinitionsAction implements AlmSettingsWsAction { | |||
@@ -49,24 +49,6 @@ public class ListDefinitionsAction implements AlmSettingsWsAction { | |||
this.userSession = userSession; | |||
} | |||
private static AlmSettingGithub toGitHub(AlmSettingDto settingDto) { | |||
return AlmSettingGithub | |||
.newBuilder() | |||
.setKey(settingDto.getKey()) | |||
.setUrl(requireNonNull(settingDto.getUrl(), "URL cannot be null for GitHub ALM setting")) | |||
.setAppId(requireNonNull(settingDto.getAppId(), "App ID cannot be null for GitHub ALM setting")) | |||
.setPrivateKey(requireNonNull(settingDto.getPrivateKey(), "Private Key cannot be null for GitHub ALM setting")) | |||
.build(); | |||
} | |||
private static AlmSettingAzure toAzure(AlmSettingDto settingDto) { | |||
return AlmSettingAzure | |||
.newBuilder() | |||
.setKey(settingDto.getKey()) | |||
.setPersonalAccessToken(requireNonNull(settingDto.getPersonalAccessToken(), "Personal Access Token cannot be null for Azure ALM setting")) | |||
.build(); | |||
} | |||
@Override | |||
public void define(WebService.NewController context) { | |||
context.createAction("list_definitions") | |||
@@ -92,10 +74,40 @@ public class ListDefinitionsAction implements AlmSettingsWsAction { | |||
.map(ListDefinitionsAction::toGitHub).collect(Collectors.toList()); | |||
List<AlmSettingAzure> azureSettings = settingsByAlm.getOrDefault(ALM.AZURE_DEVOPS, emptyList()).stream() | |||
.map(ListDefinitionsAction::toAzure).collect(Collectors.toList()); | |||
List<AlmSettingBitbucket> bitbucketSettings = settingsByAlm.getOrDefault(ALM.BITBUCKET, emptyList()).stream() | |||
.map(ListDefinitionsAction::toBitbucket).collect(Collectors.toList()); | |||
return ListDefinitionsWsResponse.newBuilder() | |||
.addAllGithub(githubSettings) | |||
.addAllAzure(azureSettings) | |||
.addAllBitbucket(bitbucketSettings) | |||
.build(); | |||
} | |||
} | |||
private static AlmSettingGithub toGitHub(AlmSettingDto settingDto) { | |||
return AlmSettingGithub | |||
.newBuilder() | |||
.setKey(settingDto.getKey()) | |||
.setUrl(requireNonNull(settingDto.getUrl(), "URL cannot be null for GitHub ALM setting")) | |||
.setAppId(requireNonNull(settingDto.getAppId(), "App ID cannot be null for GitHub ALM setting")) | |||
.setPrivateKey(requireNonNull(settingDto.getPrivateKey(), "Private Key cannot be null for GitHub ALM setting")) | |||
.build(); | |||
} | |||
private static AlmSettingAzure toAzure(AlmSettingDto settingDto) { | |||
return AlmSettingAzure | |||
.newBuilder() | |||
.setKey(settingDto.getKey()) | |||
.setPersonalAccessToken(requireNonNull(settingDto.getPersonalAccessToken(), "Personal Access Token cannot be null for Azure ALM setting")) | |||
.build(); | |||
} | |||
private static AlmSettingBitbucket toBitbucket(AlmSettingDto settingDto) { | |||
return AlmSettingBitbucket | |||
.newBuilder() | |||
.setKey(settingDto.getKey()) | |||
.setUrl(requireNonNull(settingDto.getUrl(), "URL cannot be null for Bitbucket ALM setting")) | |||
.setPersonalAccessToken(requireNonNull(settingDto.getPersonalAccessToken(), "Personal Access Token cannot be null for Bitbucket ALM setting")) | |||
.build(); | |||
} | |||
} |
@@ -0,0 +1,107 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2019 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.server.almsettings; | |||
import org.sonar.api.server.ws.Request; | |||
import org.sonar.api.server.ws.Response; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.db.DbClient; | |||
import org.sonar.db.DbSession; | |||
import org.sonar.db.alm.setting.AlmSettingDto; | |||
import org.sonar.server.exceptions.NotFoundException; | |||
import org.sonar.server.user.UserSession; | |||
import static java.lang.String.format; | |||
import static org.apache.commons.lang.StringUtils.isNotBlank; | |||
public class UpdateBitbucketAction implements AlmSettingsWsAction { | |||
private static final String PARAM_KEY = "key"; | |||
private static final String PARAM_NEW_KEY = "newKey"; | |||
private static final String PARAM_URL = "url"; | |||
private static final String PARAM_PERSONAL_ACCESS_TOKEN = "personalAccessToken"; | |||
private final DbClient dbClient; | |||
private UserSession userSession; | |||
public UpdateBitbucketAction(DbClient dbClient, UserSession userSession) { | |||
this.dbClient = dbClient; | |||
this.userSession = userSession; | |||
} | |||
@Override | |||
public void define(WebService.NewController context) { | |||
WebService.NewAction action = context.createAction("update_bitbucket") | |||
.setDescription("Update Bitbucket ALM instance Setting. <br/>" + | |||
"Requires the 'Administer System' permission") | |||
.setPost(true) | |||
.setSince("8.1") | |||
.setHandler(this); | |||
action.createParam(PARAM_KEY) | |||
.setRequired(true) | |||
.setMaximumLength(40) | |||
.setDescription("Unique key of the Bitbucket instance setting"); | |||
action.createParam(PARAM_NEW_KEY) | |||
.setRequired(false) | |||
.setMaximumLength(40) | |||
.setDescription("Optional new value for an unique key of the Bitbucket instance setting"); | |||
action.createParam(PARAM_URL) | |||
.setRequired(true) | |||
.setMaximumLength(2000) | |||
.setDescription("Bitbucket API URL"); | |||
action.createParam(PARAM_PERSONAL_ACCESS_TOKEN) | |||
.setRequired(true) | |||
.setMaximumLength(2000) | |||
.setDescription("Bitbucket personal access token"); | |||
} | |||
@Override | |||
public void handle(Request request, Response response) throws Exception { | |||
userSession.checkIsSystemAdministrator(); | |||
doHandle(request); | |||
response.noContent(); | |||
} | |||
private void doHandle(Request request) { | |||
String key = request.mandatoryParam(PARAM_KEY); | |||
String newKey = request.param(PARAM_NEW_KEY); | |||
String url = request.mandatoryParam(PARAM_URL); | |||
String pat = request.mandatoryParam(PARAM_PERSONAL_ACCESS_TOKEN); | |||
try (DbSession dbSession = dbClient.openSession(false)) { | |||
AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, key) | |||
.orElseThrow(() -> new NotFoundException(format("No ALM setting with key '%s' has been found", key))); | |||
if (isNotBlank(newKey) && !newKey.equals(key)) { | |||
dbClient.almSettingDao().selectByKey(dbSession, newKey) | |||
.ifPresent(almSetting -> { | |||
throw new IllegalArgumentException(format("ALM setting with key '%s' already exists", almSetting.getKey())); | |||
}); | |||
} | |||
dbClient.almSettingDao().update(dbSession, almSettingDto | |||
.setKey(isNotBlank(newKey) ? newKey : key) | |||
.setUrl(url) | |||
.setPersonalAccessToken(pat)); | |||
dbSession.commit(); | |||
} | |||
} | |||
} |
@@ -32,7 +32,7 @@ public class AlmSettingsWsModuleTest { | |||
public void verify_count_of_added_components() { | |||
ComponentContainer container = new ComponentContainer(); | |||
new AlmSettingsWsModule().configure(container); | |||
assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 7); | |||
assertThat(container.size()).isEqualTo(COMPONENTS_IN_EMPTY_COMPONENT_CONTAINER + 8); | |||
} | |||
} |
@@ -0,0 +1,103 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2019 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.server.almsettings; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.rules.ExpectedException; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.db.alm.setting.AlmSettingDto; | |||
import org.sonar.db.user.UserDto; | |||
import org.sonar.server.exceptions.ForbiddenException; | |||
import org.sonar.server.tester.UserSessionRule; | |||
import org.sonar.server.ws.WsActionTester; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.assertj.core.groups.Tuple.tuple; | |||
public class CreateBitbucketActionTest { | |||
@Rule | |||
public ExpectedException expectedException = ExpectedException.none(); | |||
@Rule | |||
public UserSessionRule userSession = UserSessionRule.standalone(); | |||
@Rule | |||
public DbTester db = DbTester.create(); | |||
private WsActionTester ws = new WsActionTester(new CreateBitBucketAction(db.getDbClient(), userSession)); | |||
@Test | |||
public void create() { | |||
UserDto user = db.users().insertUser(); | |||
userSession.logIn(user).setSystemAdministrator(); | |||
ws.newRequest() | |||
.setParam("key", "Bitbucket Server - Dev Team") | |||
.setParam("url", "https://bitbucket.enterprise.com") | |||
.setParam("personalAccessToken", "98765432100") | |||
.execute(); | |||
assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession())) | |||
.extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getPersonalAccessToken) | |||
.containsOnly(tuple("Bitbucket Server - Dev Team", "https://bitbucket.enterprise.com", "98765432100")); | |||
} | |||
@Test | |||
public void fail_when_key_is_already_used() { | |||
UserDto user = db.users().insertUser(); | |||
userSession.logIn(user).setSystemAdministrator(); | |||
AlmSettingDto bitbucketAlmSetting = db.almSettings().insertBitbucketAlmSetting(); | |||
expectedException.expect(IllegalArgumentException.class); | |||
expectedException.expectMessage(String.format("An ALM setting with key '%s' already exist", bitbucketAlmSetting.getKey())); | |||
ws.newRequest() | |||
.setParam("key", bitbucketAlmSetting.getKey()) | |||
.setParam("url", "https://bitbucket.enterprise.com") | |||
.setParam("personalAccessToken", "98765432100") | |||
.execute(); | |||
} | |||
@Test | |||
public void fail_when_missing_administer_system_permission() { | |||
UserDto user = db.users().insertUser(); | |||
userSession.logIn(user); | |||
expectedException.expect(ForbiddenException.class); | |||
ws.newRequest() | |||
.setParam("key", "Bitbucket Server - Dev Team") | |||
.setParam("url", "https://bitbucket.enterprise.com") | |||
.setParam("personalAccessToken", "98765432100") | |||
.execute(); | |||
} | |||
@Test | |||
public void definition() { | |||
WebService.Action def = ws.getDef(); | |||
assertThat(def.since()).isEqualTo("8.1"); | |||
assertThat(def.isPost()).isTrue(); | |||
assertThat(def.params()) | |||
.extracting(WebService.Param::key, WebService.Param::isRequired) | |||
.containsExactlyInAnyOrder(tuple("key", true), tuple("url", true), tuple("personalAccessToken", true)); | |||
} | |||
} |
@@ -53,9 +53,7 @@ public class UpdateAzureActionTest { | |||
UserDto user = db.users().insertUser(); | |||
userSession.logIn(user).setSystemAdministrator(); | |||
AlmSettingDto almSettingDto = newAzureAlmSettingDto(); | |||
db.getDbClient().almSettingDao().insert(db.getSession(), almSettingDto); | |||
db.commit(); | |||
AlmSettingDto almSettingDto = db.almSettings().insertAzureAlmSetting(); | |||
ws.newRequest() | |||
.setParam("key", almSettingDto.getKey()) | |||
@@ -71,9 +69,7 @@ public class UpdateAzureActionTest { | |||
UserDto user = db.users().insertUser(); | |||
userSession.logIn(user).setSystemAdministrator(); | |||
AlmSettingDto almSettingDto = newAzureAlmSettingDto(); | |||
db.getDbClient().almSettingDao().insert(db.getSession(), almSettingDto); | |||
db.commit(); | |||
AlmSettingDto almSettingDto = db.almSettings().insertAzureAlmSetting(); | |||
ws.newRequest() | |||
.setParam("key", almSettingDto.getKey()) | |||
@@ -120,7 +116,7 @@ public class UpdateAzureActionTest { | |||
public void fail_when_missing_administer_system_permission() { | |||
UserDto user = db.users().insertUser(); | |||
userSession.logIn(user); | |||
AlmSettingDto almSettingDto = db.almSettings().insertGitHubAlmSetting(); | |||
AlmSettingDto almSettingDto = db.almSettings().insertAzureAlmSetting(); | |||
expectedException.expect(ForbiddenException.class); | |||
@@ -0,0 +1,146 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2019 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonar.server.almsettings; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
import org.junit.rules.ExpectedException; | |||
import org.sonar.api.server.ws.WebService; | |||
import org.sonar.db.DbTester; | |||
import org.sonar.db.alm.setting.AlmSettingDto; | |||
import org.sonar.db.user.UserDto; | |||
import org.sonar.server.exceptions.ForbiddenException; | |||
import org.sonar.server.exceptions.NotFoundException; | |||
import org.sonar.server.tester.UserSessionRule; | |||
import org.sonar.server.ws.WsActionTester; | |||
import static java.lang.String.format; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.assertj.core.groups.Tuple.tuple; | |||
import static org.sonar.db.almsettings.AlmSettingsTesting.newBitbucketAlmSettingDto; | |||
public class UpdateBitbucketActionTest { | |||
@Rule | |||
public ExpectedException expectedException = ExpectedException.none(); | |||
@Rule | |||
public UserSessionRule userSession = UserSessionRule.standalone(); | |||
@Rule | |||
public DbTester db = DbTester.create(); | |||
private WsActionTester ws = new WsActionTester(new UpdateBitbucketAction(db.getDbClient(), userSession)); | |||
@Test | |||
public void update() { | |||
UserDto user = db.users().insertUser(); | |||
userSession.logIn(user).setSystemAdministrator(); | |||
AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting(); | |||
ws.newRequest() | |||
.setParam("key", almSettingDto.getKey()) | |||
.setParam("url", "https://bitbucket.enterprise-unicorn.com") | |||
.setParam("personalAccessToken", "10987654321") | |||
.execute(); | |||
assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession())) | |||
.extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getPersonalAccessToken) | |||
.containsOnly(tuple(almSettingDto.getKey(), "https://bitbucket.enterprise-unicorn.com", "10987654321")); | |||
} | |||
@Test | |||
public void update_with_new_key() { | |||
UserDto user = db.users().insertUser(); | |||
userSession.logIn(user).setSystemAdministrator(); | |||
AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting(); | |||
ws.newRequest() | |||
.setParam("key", almSettingDto.getKey()) | |||
.setParam("newKey", "Bitbucket Server - Infra Team") | |||
.setParam("url", "https://bitbucket.enterprise-unicorn.com") | |||
.setParam("personalAccessToken", "0123456789") | |||
.execute(); | |||
assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession())) | |||
.extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getPersonalAccessToken) | |||
.containsOnly(tuple("Bitbucket Server - Infra Team", "https://bitbucket.enterprise-unicorn.com", "0123456789")); | |||
} | |||
@Test | |||
public void fail_when_key_does_not_match_existing_alm_setting() { | |||
UserDto user = db.users().insertUser(); | |||
userSession.logIn(user).setSystemAdministrator(); | |||
expectedException.expect(NotFoundException.class); | |||
expectedException.expectMessage("No ALM setting with key 'unknown' has been found"); | |||
ws.newRequest() | |||
.setParam("key", "unknown") | |||
.setParam("url", "https://bitbucket.enterprise-unicorn.com") | |||
.setParam("personalAccessToken", "0123456789") | |||
.execute(); | |||
} | |||
@Test | |||
public void fail_when_new_key_matches_existing_alm_setting() { | |||
UserDto user = db.users().insertUser(); | |||
userSession.logIn(user).setSystemAdministrator(); | |||
AlmSettingDto almSetting1 = db.almSettings().insertBitbucketAlmSetting(); | |||
AlmSettingDto almSetting2 = db.almSettings().insertBitbucketAlmSetting(); | |||
expectedException.expect(IllegalArgumentException.class); | |||
expectedException.expectMessage(format("ALM setting with key '%s' already exists", almSetting2.getKey())); | |||
ws.newRequest() | |||
.setParam("key", almSetting1.getKey()) | |||
.setParam("newKey", almSetting2.getKey()) | |||
.setParam("url", "https://bitbucket.enterprise-unicorn.com") | |||
.setParam("personalAccessToken", "0123456789") | |||
.execute(); | |||
} | |||
@Test | |||
public void fail_when_missing_administer_system_permission() { | |||
UserDto user = db.users().insertUser(); | |||
userSession.logIn(user); | |||
AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting(); | |||
expectedException.expect(ForbiddenException.class); | |||
ws.newRequest() | |||
.setParam("key", almSettingDto.getKey()) | |||
.setParam("newKey", "Bitbucket Server - Infra Team") | |||
.setParam("url", "https://bitbucket.enterprise-unicorn.com") | |||
.setParam("personalAccessToken", "0123456789") | |||
.execute(); | |||
} | |||
@Test | |||
public void definition() { | |||
WebService.Action def = ws.getDef(); | |||
assertThat(def.since()).isEqualTo("8.1"); | |||
assertThat(def.isPost()).isTrue(); | |||
assertThat(def.params()) | |||
.extracting(WebService.Param::key, WebService.Param::isRequired) | |||
.containsExactlyInAnyOrder(tuple("key", true), tuple("newKey", false), tuple("url", true), tuple("personalAccessToken", true)); | |||
} | |||
} |
@@ -124,4 +124,35 @@ public class AlmSettingsService extends BaseService { | |||
.setMediaType(MediaTypes.JSON)).content(); | |||
} | |||
/** | |||
* | |||
* This is a POST request. | |||
* @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/alm_settings/create_bitbucket">Further information about this action online (including a response example)</a> | |||
* @since 8.1 | |||
*/ | |||
public void createBitbucket(CreateBitbucketRequest request) { | |||
call( | |||
new PostRequest(path("create_bitbucket")) | |||
.setParam("key", request.getKey()) | |||
.setParam("url", request.getUrl()) | |||
.setParam("personalAccessToken", request.getPersonalAccessToken()) | |||
.setMediaType(MediaTypes.JSON)).content(); | |||
} | |||
/** | |||
* | |||
* This is a POST request. | |||
* @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/alm_settings/update_bitbucket">Further information about this action online (including a response example)</a> | |||
* @since 8.1 | |||
*/ | |||
public void updateBitbucket(UpdateBitbucketRequest request) { | |||
call( | |||
new PostRequest(path("update_bitbucket")) | |||
.setParam("key", request.getKey()) | |||
.setParam("newKey", request.getNewKey()) | |||
.setParam("url", request.getUrl()) | |||
.setParam("personalAccessToken", request.getPersonalAccessToken()) | |||
.setMediaType(MediaTypes.JSON)).content(); | |||
} | |||
} |
@@ -0,0 +1,71 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2019 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonarqube.ws.client.almsettings; | |||
import javax.annotation.Generated; | |||
/** | |||
* This is a POST request. | |||
* @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/alm_settings/create_bitbucket">Further information about this action online (including a response example)</a> | |||
* @since 8.1 | |||
*/ | |||
@Generated("sonar-ws-generator") | |||
public class CreateBitbucketRequest { | |||
private String key; | |||
private String url; | |||
private String personalAccessToken; | |||
/** | |||
* This is a mandatory parameter. | |||
*/ | |||
public CreateBitbucketRequest setKey(String key) { | |||
this.key = key; | |||
return this; | |||
} | |||
public String getKey() { | |||
return key; | |||
} | |||
/** | |||
* This is a mandatory parameter. | |||
*/ | |||
public CreateBitbucketRequest setPersonalAccessToken(String personalAccessToken) { | |||
this.personalAccessToken = personalAccessToken; | |||
return this; | |||
} | |||
public String getPersonalAccessToken() { | |||
return personalAccessToken; | |||
} | |||
/** | |||
* This is a mandatory parameter. | |||
*/ | |||
public CreateBitbucketRequest setUrl(String url) { | |||
this.url = url; | |||
return this; | |||
} | |||
public String getUrl() { | |||
return url; | |||
} | |||
} |
@@ -0,0 +1,84 @@ | |||
/* | |||
* SonarQube | |||
* Copyright (C) 2009-2019 SonarSource SA | |||
* mailto:info AT sonarsource DOT com | |||
* | |||
* This program is free software; you can redistribute it and/or | |||
* modify it under the terms of the GNU Lesser General Public | |||
* License as published by the Free Software Foundation; either | |||
* version 3 of the License, or (at your option) any later version. | |||
* | |||
* This program is distributed in the hope that it will be useful, | |||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
* Lesser General Public License for more details. | |||
* | |||
* You should have received a copy of the GNU Lesser General Public License | |||
* along with this program; if not, write to the Free Software Foundation, | |||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |||
*/ | |||
package org.sonarqube.ws.client.almsettings; | |||
import javax.annotation.Generated; | |||
/** | |||
* This is a POST request. | |||
* @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/alm_settings/update_bitbucket">Further information about this action online (including a response example)</a> | |||
* @since 8.1 | |||
*/ | |||
@Generated("sonar-ws-generator") | |||
public class UpdateBitbucketRequest { | |||
private String key; | |||
private String newKey; | |||
private String personalAccessToken; | |||
private String url; | |||
public String getKey() { | |||
return key; | |||
} | |||
/** | |||
* This is a mandatory parameter. | |||
*/ | |||
public UpdateBitbucketRequest setKey(String key) { | |||
this.key = key; | |||
return this; | |||
} | |||
public String getNewKey() { | |||
return newKey; | |||
} | |||
/** | |||
*/ | |||
public UpdateBitbucketRequest setNewKey(String newKey) { | |||
this.newKey = newKey; | |||
return this; | |||
} | |||
public String getPersonalAccessToken() { | |||
return personalAccessToken; | |||
} | |||
/** | |||
* This is a mandatory parameter. | |||
*/ | |||
public UpdateBitbucketRequest setPersonalAccessToken(String personalAccessToken) { | |||
this.personalAccessToken = personalAccessToken; | |||
return this; | |||
} | |||
public String getUrl() { | |||
return url; | |||
} | |||
/** | |||
* This is a mandatory parameter. | |||
*/ | |||
public UpdateBitbucketRequest setUrl(String url) { | |||
this.url = url; | |||
return this; | |||
} | |||
} |
@@ -28,6 +28,7 @@ option optimize_for = SPEED; | |||
message ListDefinitionsWsResponse { | |||
repeated AlmSettingGithub github = 1; | |||
repeated AlmSettingAzure azure = 2; | |||
repeated AlmSettingBitbucket bitbucket = 3; | |||
} | |||
message AlmSettingGithub { | |||
@@ -42,3 +43,8 @@ message AlmSettingAzure { | |||
optional string personalAccessToken = 2; | |||
} | |||
message AlmSettingBitbucket { | |||
optional string key = 1; | |||
optional string url = 2; | |||
optional string personalAccessToken = 3; | |||
} |