diff options
author | Zipeng WU <zipeng.wu@sonarsource.com> | 2021-05-11 22:11:39 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2021-05-21 20:03:36 +0000 |
commit | 97baa10facb3a4e6295fd7dfe93359ebd578c60f (patch) | |
tree | ff378b5065356d43d1e86ca2cb0e453ce485a9df /server/sonar-alm-client/src | |
parent | 5333756be234c4bb50c9edf3c4b2cab5e0483559 (diff) | |
download | sonarqube-97baa10facb3a4e6295fd7dfe93359ebd578c60f.tar.gz sonarqube-97baa10facb3a4e6295fd7dfe93359ebd578c60f.zip |
SONAR-14802 Search bitbucket cloud repository for onboarding
Diffstat (limited to 'server/sonar-alm-client/src')
5 files changed, 258 insertions, 0 deletions
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/BitbucketCloudRestClient.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/BitbucketCloudRestClient.java index 34c7ba2572c..1627f75869c 100644 --- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/BitbucketCloudRestClient.java +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/BitbucketCloudRestClient.java @@ -134,6 +134,12 @@ public class BitbucketCloudRestClient { } } + public RepositoryList searchRepos(String encodedCredentials, String workspace, @Nullable String repoName, Integer page, Integer pageSize) { + String filterQuery = String.format("q=name~\"%s\"", repoName != null ? repoName : ""); + HttpUrl url = buildUrl(String.format("/repositories/%s?%s&page=%s&pagelen=%s", workspace, filterQuery, page, pageSize)); + return doGetWithBasicAuth(encodedCredentials, url, r -> buildGson().fromJson(r.body().charStream(), RepositoryList.class)); + } + public String createAccessToken(String clientId, String clientSecret) { Request request = createAccessTokenRequest(clientId, clientSecret); return doCall(request, r -> buildGson().fromJson(r.body().charStream(), Token.class)).getAccessToken(); diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/Project.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/Project.java new file mode 100644 index 00000000000..9e32c6774b4 --- /dev/null +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/Project.java @@ -0,0 +1,57 @@ +/* + * 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 Project { + + @SerializedName("key") + private String key; + + @SerializedName("name") + private String name; + + @SerializedName("uuid") + private String uuid; + + public Project() { + // http://stackoverflow.com/a/18645370/229031 + } + + public Project(String uuid, String key, String name) { + this.uuid = uuid; + this.key = key; + this.name = name; + } + + public String getKey() { + return key; + } + + public String getName() { + return name; + } + + public String getUuid() { + return uuid; + } + +} 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 new file mode 100644 index 00000000000..534a4773aff --- /dev/null +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/Repository.java @@ -0,0 +1,65 @@ +/* + * 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 Repository { + + @SerializedName("slug") + private String slug; + + @SerializedName("name") + private String name; + + @SerializedName("uuid") + private String uuid; + + @SerializedName("project") + private Project project; + + public Repository() { + // http://stackoverflow.com/a/18645370/229031 + } + + public Repository(String uuid, String slug, String name, Project project) { + this.uuid = uuid; + this.slug = slug; + this.name = name; + this.project = project; + } + + public String getSlug() { + return slug; + } + + public String getName() { + return name; + } + + public String getUuid() { + return uuid; + } + + public Project getProject() { + return project; + } + +} diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/RepositoryList.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/RepositoryList.java new file mode 100644 index 00000000000..73cc74d6c9c --- /dev/null +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucket/bitbucketcloud/RepositoryList.java @@ -0,0 +1,66 @@ +/* + * 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; +import java.util.List; + +public class RepositoryList { + + @SerializedName("next") + private String next; + + @SerializedName("page") + private Integer page; + + @SerializedName("pagelen") + private Integer pagelen; + + @SerializedName("values") + private List<Repository> values; + + public RepositoryList() { + // http://stackoverflow.com/a/18645370/229031 + } + + public RepositoryList(String next, List<Repository> values, Integer page, Integer pagelen) { + this.next = next; + this.values = values; + this.page = page; + this.pagelen = pagelen; + } + + public String getNext() { + return next; + } + + public List<Repository> getValues() { + return values; + } + + public Integer getPage() { + return page; + } + + public Integer getPagelen() { + return pagelen; + } + +} 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 387da13f89c..7a6d675a7c5 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 @@ -19,6 +19,7 @@ */ package org.sonar.alm.client.bitbucket.bitbucketcloud; +import com.google.gson.Gson; import java.io.IOException; import okhttp3.Call; import okhttp3.OkHttpClient; @@ -34,9 +35,11 @@ import org.junit.Before; import org.junit.Test; import org.sonarqube.ws.client.OkHttpClientBuilder; +import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.assertj.core.api.Assertions.tuple; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -59,6 +62,67 @@ public class BitbucketCloudRestClientTest { } @Test + public void get_repos() { + server.enqueue(new MockResponse() + .setHeader("Content-Type", "application/json;charset=UTF-8") + .setBody("{\n" + + " \"values\": [\n" + + " {\n" + + " \"slug\": \"banana\",\n" + + " \"uuid\": \"BANANA-UUID\",\n" + + " \"name\": \"banana\",\n" + + " \"project\": {\n" + + " \"key\": \"HOY\",\n" + + " \"uuid\": \"BANANA-PROJECT-UUID\",\n" + + " \"name\": \"hoy\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"slug\": \"potato\",\n" + + " \"uuid\": \"POTATO-UUID\",\n" + + " \"name\": \"potato\",\n" + + " \"project\": {\n" + + " \"key\": \"HEY\",\n" + + " \"uuid\": \"POTATO-PROJECT-UUID\",\n" + + " \"name\": \"hey\"\n" + + " }\n" + + " }\n" + + " ]\n" + + "}")); + + RepositoryList repositoryList = underTest.searchRepos("user:apppwd", "", null, 1, 100); + assertThat(repositoryList.getNext()).isNull(); + assertThat(repositoryList.getValues()) + .hasSize(2) + .extracting(Repository::getUuid, Repository::getName, Repository::getSlug, + g -> g.getProject().getUuid(), g -> g.getProject().getKey(), g -> g.getProject().getName()) + .containsExactlyInAnyOrder( + tuple("BANANA-UUID", "banana", "banana", "BANANA-PROJECT-UUID", "HOY", "hoy"), + tuple("POTATO-UUID", "potato", "potato", "POTATO-PROJECT-UUID", "HEY", "hey")); + } + + @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); + RepositoryList repos = new RepositoryList(null, asList(repository), 1, 100); + server.enqueue(new MockResponse() + .setHeader("Content-Type", "application/json;charset=UTF-8") + .setBody(new Gson().toJson(repos))); + + RepositoryList repositoryList = underTest.searchRepos("user:apppwd", "", null, 1, 100); + assertThat(repositoryList.getNext()).isNull(); + assertThat(repositoryList.getPage()).isEqualTo(1); + assertThat(repositoryList.getPagelen()).isEqualTo(100); + assertThat(repositoryList.getValues()) + .hasSize(1) + .extracting(Repository::getUuid, Repository::getName, Repository::getSlug, + g -> g.getProject().getUuid(), g -> g.getProject().getKey(), g -> g.getProject().getName()) + .containsExactlyInAnyOrder( + tuple("REPO-UUID-ONE", "repoName", "repo-slug", "PROJECT-UUID-ONE", "projectKey", "projectName")); + } + + @Test public void failIfUnauthorized() { server.enqueue(new MockResponse().setResponseCode(401).setBody("Unauthorized")); |