}
@Test
- public void fail_when_alm_setting_not_bitbucket_server_nor_gitlab() {
+ public void set_new_github_pat() {
UserDto user = db.users().insertUser();
AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
userSession.logIn(user).addPermission(PROVISION_PROJECTS);
- assertThatThrownBy(() -> {
- ws.newRequest()
- .setParam("almSetting", almSetting.getKey())
- .setParam("pat", "12345678987654321")
- .execute();
- })
- .isInstanceOf(IllegalArgumentException.class)
- .hasMessage("Only Azure DevOps, Bitbucket Server, GitLab and Bitbucket Cloud Settings are supported.");
+ ws.newRequest()
+ .setParam("almSetting", almSetting.getKey())
+ .setParam("pat", "12345678987654321")
+ .execute();
+
+ Optional<AlmPatDto> actualAlmPat = db.getDbClient().almPatDao().selectByUserAndAlmSetting(db.getSession(), user.getUuid(), almSetting);
+ assertThat(actualAlmPat).isPresent();
+ assertThat(actualAlmPat.get().getPersonalAccessToken()).isEqualTo("12345678987654321");
+ assertThat(actualAlmPat.get().getUserUuid()).isEqualTo(user.getUuid());
+ assertThat(actualAlmPat.get().getAlmSettingUuid()).isEqualTo(almSetting.getUuid());
}
@Test
package org.sonar.server.almintegration.ws;
import com.google.common.base.Strings;
-import java.util.Arrays;
import java.util.Optional;
import org.sonar.api.server.ws.Change;
import org.sonar.api.server.ws.Request;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
-import static org.sonar.db.alm.setting.ALM.AZURE_DEVOPS;
-import static org.sonar.db.alm.setting.ALM.BITBUCKET;
import static org.sonar.db.alm.setting.ALM.BITBUCKET_CLOUD;
-import static org.sonar.db.alm.setting.ALM.GITLAB;
import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
public class SetPatAction implements AlmIntegrationsWsAction {
public void define(WebService.NewController context) {
WebService.NewAction action = context.createAction("set_pat")
.setDescription("Set a Personal Access Token for the given DevOps Platform setting<br/>" +
- "Only valid for Azure DevOps, Bitbucket Server, GitLab and Bitbucket Cloud Setting<br/>" +
"Requires the 'Create Projects' permission")
.setPost(true)
.setSince("8.2")
.setHandler(this)
- .setChangelog(new Change("9.0", "Bitbucket Cloud support and optional Username parameter were added"));
+ .setChangelog(
+ new Change("9.0", "Bitbucket Cloud support and optional Username parameter were added"),
+ new Change("10.3", "Allow setting Personal Access Tokens for all DevOps platforms"));
action.createParam(PARAM_ALM_SETTING)
.setRequired(true)
AlmSettingDto almSetting = dbClient.almSettingDao().selectByKey(dbSession, almSettingKey)
.orElseThrow(() -> new NotFoundException(format("DevOps Platform Setting '%s' not found", almSettingKey)));
- Preconditions.checkArgument(Arrays.asList(AZURE_DEVOPS, BITBUCKET, GITLAB, BITBUCKET_CLOUD)
- .contains(almSetting.getAlm()), "Only Azure DevOps, Bitbucket Server, GitLab and Bitbucket Cloud Settings are supported.");
-
- if(almSetting.getAlm().equals(BITBUCKET_CLOUD)) {
+ if (almSetting.getAlm().equals(BITBUCKET_CLOUD)) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(username), "Username cannot be null for Bitbucket Cloud");
}