From 5671fa7f35616d949305167230f2e6b3c9e7bac3 Mon Sep 17 00:00:00 2001 From: Belen Pruvost Date: Wed, 15 Sep 2021 12:29:42 +0200 Subject: [PATCH] SONAR-15233 - Handle tildas on BBS Onboarding --- .../ImportBitbucketServerProjectAction.java | 12 +++++-- ...mportBitbucketServerProjectActionTest.java | 31 ++++++++++++++++++- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectAction.java index f00c0c22728..0170d07a6f1 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectAction.java @@ -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 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("~", ""); + } + } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionTest.java index d3cdd223bb6..c3daabb2714 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionTest.java @@ -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") -- 2.39.5