]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20636 Allow setting PATs for all ALMs.
authorWojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com>
Mon, 2 Oct 2023 14:43:49 +0000 (16:43 +0200)
committersonartech <sonartech@sonarsource.com>
Tue, 3 Oct 2023 20:02:47 +0000 (20:02 +0000)
server/sonar-webserver-webapi/src/it/java/org/sonar/server/almintegration/ws/SetPatActionIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/SetPatAction.java

index 2c685028cf7136f940a41365582309341ea0289f..c90746b6c963673f5ba25ef7ce6fdb21404f7a0e 100644 (file)
@@ -175,19 +175,21 @@ public class SetPatActionIT {
   }
 
   @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
index 4e44533f3d871326f116bc3b2dabdbf775584d7d..0743d3e82200f556f51f3fc8d45bf8f60a42ccc9 100644 (file)
@@ -20,7 +20,6 @@
 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;
@@ -36,10 +35,7 @@ import org.sonar.server.user.UserSession;
 
 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 {
@@ -60,12 +56,13 @@ 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)
@@ -98,10 +95,7 @@ public class SetPatAction implements AlmIntegrationsWsAction {
       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");
       }