diff options
author | Zipeng WU <zipeng.wu@sonarsource.com> | 2021-05-17 22:57:03 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-05-21 20:03:36 +0000 |
commit | 382b8bac9c477bd08b1c9949c80583da5e3de256 (patch) | |
tree | 8772e9f81f2ed37417ecae92f18e22bb1ef2708b /server | |
parent | cb16d8a222d99630826a64ba891dc93b77187bfc (diff) | |
download | sonarqube-382b8bac9c477bd08b1c9949c80583da5e3de256.tar.gz sonarqube-382b8bac9c477bd08b1c9949c80583da5e3de256.zip |
SONAR-14806 Enable main branch detection for bitbucket cloud onboarding
Diffstat (limited to 'server')
6 files changed, 105 insertions, 7 deletions
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/MainBranch.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/MainBranch.java new file mode 100644 index 00000000000..144d49f881f --- /dev/null +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/MainBranch.java @@ -0,0 +1,48 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 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.alm.client.bitbucket.bitbucketcloud; + +import com.google.gson.annotations.SerializedName; + +public class MainBranch { + @SerializedName("type") + private String type; + + @SerializedName("name") + private String name; + + public MainBranch() { + // http://stackoverflow.com/a/18645370/229031 + } + + public MainBranch(String type, String name) { + this.type = type; + this.name = name; + } + + public String getType() { + return type; + } + + public String getName() { + return name; + } + +} diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/Repository.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/Repository.java index 534a4773aff..b8c100ffea4 100644 --- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/Repository.java +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/Repository.java @@ -35,15 +35,19 @@ public class Repository { @SerializedName("project") private Project project; + @SerializedName("mainbranch") + private MainBranch mainBranch; + public Repository() { // http://stackoverflow.com/a/18645370/229031 } - public Repository(String uuid, String slug, String name, Project project) { + public Repository(String uuid, String slug, String name, Project project, MainBranch mainBranch) { this.uuid = uuid; this.slug = slug; this.name = name; this.project = project; + this.mainBranch = mainBranch; } public String getSlug() { @@ -62,4 +66,7 @@ public class Repository { return project; } + public MainBranch getMainBranch() { + return mainBranch; + } } diff --git a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucket/bitbucketcloud/BitbucketCloudRestClientTest.java b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucket/bitbucketcloud/BitbucketCloudRestClientTest.java index 7a6d675a7c5..6268a0da493 100644 --- a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucket/bitbucketcloud/BitbucketCloudRestClientTest.java +++ b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucket/bitbucketcloud/BitbucketCloudRestClientTest.java @@ -102,9 +102,42 @@ public class BitbucketCloudRestClientTest { } @Test + public void get_repo() { + server.enqueue(new MockResponse() + .setHeader("Content-Type", "application/json;charset=UTF-8") + .setBody( + " {\n" + + " \"slug\": \"banana\",\n" + + " \"uuid\": \"BANANA-UUID\",\n" + + " \"name\": \"banana\",\n" + + " \"mainbranch\": {\n" + + " \"type\": \"branch\",\n" + + " \"name\": \"develop\"\n" + + " },"+ + " \"project\": {\n" + + " \"key\": \"HOY\",\n" + + " \"uuid\": \"BANANA-PROJECT-UUID\",\n" + + " \"name\": \"hoy\"\n" + + " }\n" + + " }")); + + Repository repository = underTest.getRepo("user:apppwd", "workspace", "rep"); + assertThat(repository.getUuid()).isEqualTo("BANANA-UUID"); + assertThat(repository.getName()).isEqualTo("banana"); + assertThat(repository.getSlug()).isEqualTo("banana"); + assertThat(repository.getProject()) + .extracting(Project::getUuid, Project::getKey, Project::getName) + .contains("BANANA-PROJECT-UUID", "HOY", "hoy"); + assertThat(repository.getMainBranch()) + .extracting(MainBranch::getType, MainBranch::getName) + .contains("branch", "develop"); + } + + @Test public void bbc_object_serialization_deserialization() { Project project = new Project("PROJECT-UUID-ONE", "projectKey", "projectName"); - Repository repository = new Repository("REPO-UUID-ONE", "repo-slug", "repoName", project); + MainBranch mainBranch = new MainBranch("branch", "develop"); + Repository repository = new Repository("REPO-UUID-ONE", "repo-slug", "repoName", project, mainBranch); RepositoryList repos = new RepositoryList(null, asList(repository), 1, 100); server.enqueue(new MockResponse() .setHeader("Content-Type", "application/json;charset=UTF-8") @@ -117,9 +150,12 @@ public class BitbucketCloudRestClientTest { assertThat(repositoryList.getValues()) .hasSize(1) .extracting(Repository::getUuid, Repository::getName, Repository::getSlug, - g -> g.getProject().getUuid(), g -> g.getProject().getKey(), g -> g.getProject().getName()) + g -> g.getProject().getUuid(), g -> g.getProject().getKey(), g -> g.getProject().getName(), + g -> g.getMainBranch().getType(), g -> g.getMainBranch().getName()) .containsExactlyInAnyOrder( - tuple("REPO-UUID-ONE", "repoName", "repo-slug", "PROJECT-UUID-ONE", "projectKey", "projectName")); + tuple("REPO-UUID-ONE", "repoName", "repo-slug", + "PROJECT-UUID-ONE", "projectKey", "projectName", + "branch", "develop")); } @Test diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoAction.java index eb79ccc3661..063d53fd2fa 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoAction.java @@ -112,7 +112,7 @@ public class ImportBitbucketCloudRepoAction implements AlmIntegrationsWsAction { Repository repo = bitbucketCloudRestClient.getRepo(pat, workspace, repoSlug); - ComponentDto componentDto = createProject(dbSession, workspace, repo, null); + ComponentDto componentDto = createProject(dbSession, workspace, repo, repo.getMainBranch().getName()); populatePRSetting(dbSession, repo, componentDto, almSettingDto); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionTest.java index a2c526827b8..b13cdfed22f 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketcloud/ImportBitbucketCloudRepoActionTest.java @@ -24,6 +24,7 @@ import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.sonar.alm.client.bitbucket.bitbucketcloud.BitbucketCloudRestClient; +import org.sonar.alm.client.bitbucket.bitbucketcloud.MainBranch; import org.sonar.alm.client.bitbucket.bitbucketcloud.Project; import org.sonar.alm.client.bitbucket.bitbucketcloud.Repository; import org.sonar.api.server.ws.WebService; @@ -33,6 +34,7 @@ import org.sonar.core.util.SequenceUuidFactory; import org.sonar.db.DbTester; import org.sonar.db.alm.pat.AlmPatDto; import org.sonar.db.alm.setting.AlmSettingDto; +import org.sonar.db.component.BranchDto; import org.sonar.db.project.ProjectDto; import org.sonar.db.user.UserDto; import org.sonar.server.almintegration.ws.ImportHelper; @@ -106,6 +108,10 @@ public class ImportBitbucketCloudRepoActionTest { Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey()); assertThat(projectDto).isPresent(); assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent(); + + Optional<BranchDto> branchDto = db.getDbClient().branchDao().selectByBranchKey(db.getSession(), projectDto.get().getUuid(), "develop"); + assertThat(branchDto).isPresent(); + assertThat(branchDto.get().isMain()).isTrue(); } @Test @@ -217,7 +223,8 @@ public class ImportBitbucketCloudRepoActionTest { private Repository getGsonBBCRepo() { Project project1 = new Project("PROJECT-UUID-ONE", "projectKey1", "projectName1"); - Repository repo1 = new Repository("REPO-UUID-ONE", "repo-slug-1", "repoName1", project1); + MainBranch mainBranch = new MainBranch("branch", "develop"); + Repository repo1 = new Repository("REPO-UUID-ONE", "repo-slug-1", "repoName1", project1, mainBranch); return repo1; } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketcloud/SearchBitbucketCloudReposActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketcloud/SearchBitbucketCloudReposActionTest.java index be24d49f87a..4f740e6c43a 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketcloud/SearchBitbucketCloudReposActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketcloud/SearchBitbucketCloudReposActionTest.java @@ -175,7 +175,7 @@ public class SearchBitbucketCloudReposActionTest { private Repository getBBCRepo1() { Project project1 = new Project("PROJECT-UUID-ONE", "projectKey1", "projectName1"); - return new Repository("REPO-UUID-ONE", "repo-slug-1", "repoName1", project1); + return new Repository("REPO-UUID-ONE", "repo-slug-1", "repoName1", project1, null); } } |