]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15233 - Handle tildas on BBS Onboarding
authorBelen Pruvost <belen.pruvost@sonarsource.com>
Wed, 15 Sep 2021 10:29:42 +0000 (12:29 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 15 Sep 2021 20:03:23 +0000 (20:03 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectAction.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionTest.java

index f00c0c22728ced60c4bada74ce3bcf38620bdffb..0170d07a6f1a1ee17974bfd0b0e9bbb2c800cf9d 100644 (file)
@@ -132,7 +132,7 @@ public class ImportBitbucketServerProjectAction implements AlmIntegrationsWsActi
       return toCreateResponse(componentDto);
     }
   }
-  
+
   private String getDefaultBranchName(String pat, String projectKey, String repoSlug, String url) {
     BranchesList branches = bitbucketServerRestClient.getBranches(url, pat, projectKey, repoSlug);
     Optional<Branch> defaultBranch = branches.findDefaultBranch();
@@ -142,7 +142,7 @@ public class ImportBitbucketServerProjectAction implements AlmIntegrationsWsActi
   private ComponentDto createProject(DbSession dbSession, Repository repo, @Nullable String defaultBranchName) {
     boolean visibility = projectDefaultVisibility.get(dbSession).isPrivate();
     NewComponent newProject = newComponentBuilder()
-      .setKey(repo.getProject().getKey() + "_" + repo.getSlug())
+      .setKey(getProjectKey(repo))
       .setName(repo.getName())
       .setPrivate(visibility)
       .setQualifier(PROJECT)
@@ -150,7 +150,8 @@ public class ImportBitbucketServerProjectAction implements AlmIntegrationsWsActi
     String userUuid = userSession.isLoggedIn() ? userSession.getUuid() : null;
     String userLogin = userSession.isLoggedIn() ? userSession.getLogin() : null;
 
-    return componentUpdater.createWithoutCommit(dbSession, newProject, userUuid, userLogin, defaultBranchName, p -> {});
+    return componentUpdater.createWithoutCommit(dbSession, newProject, userUuid, userLogin, defaultBranchName, p -> {
+    });
   }
 
   private void populatePRSetting(DbSession dbSession, Repository repo, ComponentDto componentDto, AlmSettingDto almSettingDto) {
@@ -164,4 +165,9 @@ public class ImportBitbucketServerProjectAction implements AlmIntegrationsWsActi
       componentDto.name(), componentDto.getKey());
   }
 
+  private static String getProjectKey(Repository repo) {
+    String key = repo.getProject().getKey() + "_" + repo.getSlug();
+    return key.replace("~", "");
+  }
+
 }
index d3cdd223bb68a5778142715ad9394d5e0d68a10d..c3daabb2714401e0e8bc295c135ac38bfaad7077 100644 (file)
@@ -64,6 +64,7 @@ import static org.mockito.Mockito.when;
 import static org.sonar.db.alm.integration.pat.AlmPatsTesting.newAlmPatDto;
 import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
 import static org.sonar.db.permission.GlobalPermission.SCAN;
+
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
@@ -131,6 +132,34 @@ public class ImportBitbucketServerProjectActionTest {
     assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
   }
 
+  @Test
+  public void import_project_with_tilda() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
+    AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
+    db.almPats().insert(dto -> {
+      dto.setAlmSettingUuid(almSetting.getUuid());
+      dto.setUserUuid(user.getUuid());
+    });
+    Project project = getGsonBBSProject();
+    project.setKey("~" + project.getKey());
+    Repository repo = getGsonBBSRepo(project);
+    when(bitbucketServerRestClient.getRepo(any(), any(), any(), any())).thenReturn(repo);
+    when(bitbucketServerRestClient.getBranches(any(), any(), any(), any())).thenReturn(defaultBranchesList);
+
+    Projects.CreateWsResponse response = ws.newRequest()
+      .setParam("almSetting", almSetting.getKey())
+      .setParam("projectKey", "~projectKey")
+      .setParam("repositorySlug", "repo-slug")
+      .executeProtobuf(Projects.CreateWsResponse.class);
+
+    Projects.CreateWsResponse.Project result = response.getProject();
+
+    String key = project.getKey() + "_" + repo.getSlug();
+    assertThat(result.getKey()).isNotEqualTo(key);
+    assertThat(result.getKey()).isEqualTo(key.substring(1));
+  }
+
   @Test
   public void fail_project_already_exist() {
     UserDto user = db.users().insertUser();
@@ -150,7 +179,7 @@ public class ImportBitbucketServerProjectActionTest {
 
     when(bitbucketServerRestClient.getRepo(any(), any(), any(), any())).thenReturn(repo);
     when(bitbucketServerRestClient.getBranches(any(), any(), any(), any())).thenReturn(defaultBranchesList);
-    
+
     ws.newRequest()
       .setParam("almSetting", almSetting.getKey())
       .setParam("projectKey", "projectKey")