]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14371 Move Bitbucket Server http client to CE
authorJacek <jacek.poreda@sonarsource.com>
Tue, 26 Jan 2021 14:45:01 +0000 (15:45 +0100)
committersonartech <sonartech@sonarsource.com>
Thu, 4 Feb 2021 20:07:07 +0000 (20:07 +0000)
16 files changed:
server/sonar-alm-client/build.gradle
server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java [new file with mode: 0644]
server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/Project.java [new file with mode: 0644]
server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/ProjectList.java [new file with mode: 0644]
server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/Repository.java [new file with mode: 0644]
server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/RepositoryList.java [new file with mode: 0644]
server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/package-info.java [new file with mode: 0644]
server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/AlmIntegrationsWSModule.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectAction.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/ListBitbucketServerProjectsAction.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/SearchBitbucketServerReposAction.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/ImportBitbucketServerProjectActionTest.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/ListBitbucketServerProjectsActionTest.java [new file with mode: 0644]
server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/SearchBitbucketServerReposActionTest.java [new file with mode: 0644]
server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java

index cdcae780e98de957f43484108adc0c2ca406272a..afe80fc3278e35ed91518aa15889347524e5d50c 100644 (file)
@@ -8,6 +8,7 @@ dependencies {
     compile 'com.squareup.okhttp3:okhttp'
 
     testCompile project(':sonar-plugin-api-impl')
+
     testCompile 'junit:junit'
     testCompile 'com.tngtech.java:junit-dataprovider'
     testCompile 'org.assertj:assertj-core'
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java
new file mode 100644 (file)
index 0000000..758db6e
--- /dev/null
@@ -0,0 +1,175 @@
+/*
+ * 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.bitbucketserver;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.JsonParseException;
+import com.google.gson.JsonSyntaxException;
+import com.google.gson.annotations.SerializedName;
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.util.Optional;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import javax.annotation.Nullable;
+import okhttp3.HttpUrl;
+import okhttp3.MediaType;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okhttp3.RequestBody;
+import okhttp3.Response;
+import okhttp3.ResponseBody;
+import org.sonar.alm.client.TimeoutConfiguration;
+import org.sonar.api.server.ServerSide;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
+import org.sonarqube.ws.client.OkHttpClientBuilder;
+
+import static java.lang.String.format;
+import static java.util.Locale.ENGLISH;
+import static org.sonar.api.internal.apachecommons.lang.StringUtils.removeEnd;
+
+@ServerSide
+public class BitbucketServerRestClient {
+
+  private static final Logger LOG = Loggers.get(BitbucketServerRestClient.class);
+  private static final String GET = "GET";
+  protected static final String UNABLE_TO_CONTACT_BITBUCKET_SERVER = "Unable to contact Bitbucket server";
+
+  protected final OkHttpClient client;
+
+  public BitbucketServerRestClient(TimeoutConfiguration timeoutConfiguration) {
+    OkHttpClientBuilder okHttpClientBuilder = new OkHttpClientBuilder();
+    client = okHttpClientBuilder
+      .setConnectTimeoutMs(timeoutConfiguration.getConnectTimeout())
+      .setReadTimeoutMs(timeoutConfiguration.getReadTimeout())
+      .build();
+  }
+
+  public RepositoryList getRepos(String serverUrl, String token, @Nullable String project, @Nullable String repo) {
+    String projectOrEmpty = Optional.ofNullable(project).orElse("");
+    String repoOrEmpty = Optional.ofNullable(repo).orElse("");
+    HttpUrl url = buildUrl(serverUrl, format("/rest/api/1.0/repos?projectname=%s&name=%s", projectOrEmpty, repoOrEmpty));
+    return doGet(token, url, r -> buildGson().fromJson(r.body().charStream(), RepositoryList.class));
+  }
+
+  public Repository getRepo(String serverUrl, String token, String project, String repoSlug) {
+    HttpUrl url = buildUrl(serverUrl, format("/rest/api/1.0/projects/%s/repos/%s", project, repoSlug));
+    return doGet(token, url, r -> buildGson().fromJson(r.body().charStream(), Repository.class));
+  }
+
+  public ProjectList getProjects(String serverUrl, String token) {
+    HttpUrl url = buildUrl(serverUrl, "/rest/api/1.0/projects");
+    return doGet(token, url, r -> buildGson().fromJson(r.body().charStream(), ProjectList.class));
+  }
+
+  protected static HttpUrl buildUrl(@Nullable String serverUrl, String relativeUrl) {
+    if (serverUrl == null || !(serverUrl.toLowerCase(ENGLISH).startsWith("http://") || serverUrl.toLowerCase(ENGLISH).startsWith("https://"))) {
+      throw new IllegalArgumentException("url must start with http:// or https://");
+    }
+    return HttpUrl.parse(removeEnd(serverUrl, "/") + relativeUrl);
+  }
+
+  protected <G> G doGet(String token, HttpUrl url, Function<Response, G> handler) {
+    Request request = prepareRequestWithBearerToken(token, GET, url, null);
+    return doCall(request, handler);
+  }
+
+  protected static Request prepareRequestWithBearerToken(String token, String method, HttpUrl url, @Nullable RequestBody body) {
+    return new Request.Builder()
+      .method(method, body)
+      .url(url)
+      .addHeader("Authorization", "Bearer " + token)
+      .addHeader("x-atlassian-token", "no-check")
+      .build();
+  }
+
+  protected <G> G doCall(Request request, Function<Response, G> handler) {
+    try (Response response = client.newCall(request).execute()) {
+      handleError(response);
+      return handler.apply(response);
+    } catch (JsonSyntaxException e) {
+      throw new IllegalArgumentException(UNABLE_TO_CONTACT_BITBUCKET_SERVER + ", got an unexpected response", e);
+    } catch (IOException e) {
+      throw new IllegalArgumentException(UNABLE_TO_CONTACT_BITBUCKET_SERVER, e);
+    }
+  }
+
+  protected static void handleError(Response response) throws IOException {
+    if (!response.isSuccessful()) {
+      String errorMessage = getErrorMessage(response.body());
+      LOG.debug(UNABLE_TO_CONTACT_BITBUCKET_SERVER + ": {} {}", response.code(), errorMessage);
+      if (response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) {
+        throw new IllegalArgumentException("Invalid personal access token");
+      }
+      throw new IllegalArgumentException(UNABLE_TO_CONTACT_BITBUCKET_SERVER);
+    }
+  }
+
+  protected static boolean equals(@Nullable MediaType first, @Nullable MediaType second) {
+    String s1 = first == null ? null : first.toString().toLowerCase(ENGLISH).replace(" ", "");
+    String s2 = second == null ? null : second.toString().toLowerCase(ENGLISH).replace(" ", "");
+    return s1 != null && s2 != null && s1.equals(s2);
+  }
+
+  protected static String getErrorMessage(ResponseBody body) throws IOException {
+    if (equals(MediaType.parse("application/json;charset=utf-8"), body.contentType())) {
+      try {
+        return Stream.of(buildGson().fromJson(body.charStream(), Errors.class).errorData)
+          .map(e -> e.exceptionName + " " + e.message)
+          .collect(Collectors.joining("\n"));
+      } catch (JsonParseException e) {
+        return body.string();
+      }
+    }
+    return body.string();
+  }
+
+  protected static Gson buildGson() {
+    return new GsonBuilder()
+      .create();
+  }
+
+  protected static class Errors {
+
+    @SerializedName("errors")
+    public Error[] errorData;
+
+    public Errors() {
+      // http://stackoverflow.com/a/18645370/229031
+    }
+
+    public static class Error {
+      @SerializedName("message")
+      public String message;
+
+      @SerializedName("exceptionName")
+      public String exceptionName;
+
+      public Error() {
+        // http://stackoverflow.com/a/18645370/229031
+      }
+    }
+
+  }
+
+}
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/Project.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/Project.java
new file mode 100644 (file)
index 0000000..0764393
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * 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.bitbucketserver;
+
+import com.google.gson.annotations.SerializedName;
+
+public class Project {
+
+  @SerializedName("key")
+  private String key;
+
+  @SerializedName("name")
+  private String name;
+
+  @SerializedName("id")
+  private long id;
+
+  public Project() {
+    // http://stackoverflow.com/a/18645370/229031
+  }
+
+  public Project(String key, String name, long id) {
+    this.key = key;
+    this.name = name;
+    this.id = id;
+  }
+
+  public String getKey() {
+    return key;
+  }
+
+  public Project setKey(String key) {
+    this.key = key;
+    return this;
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  public Project setName(String name) {
+    this.name = name;
+    return this;
+  }
+
+  public long getId() {
+    return id;
+  }
+
+  public Project setId(long id) {
+    this.id = id;
+    return this;
+  }
+
+  @Override
+  public String toString() {
+    return "{" +
+      "key='" + key + '\'' +
+      ", name='" + name + '\'' +
+      ", id=" + id +
+      '}';
+  }
+}
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/ProjectList.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/ProjectList.java
new file mode 100644 (file)
index 0000000..5cd7e44
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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.bitbucketserver;
+
+import com.google.gson.annotations.SerializedName;
+import java.util.ArrayList;
+import java.util.List;
+
+public class ProjectList {
+
+  @SerializedName("values")
+  private List<Project> values;
+
+  public ProjectList() {
+    // http://stackoverflow.com/a/18645370/229031
+    this(new ArrayList<>());
+  }
+
+  public ProjectList(List<Project> values) {
+    this.values = values;
+  }
+
+  public List<Project> getValues() {
+    return values;
+  }
+
+  public ProjectList setValues(List<Project> values) {
+    this.values = values;
+    return this;
+  }
+
+  @Override
+  public String toString() {
+    return "{" +
+      "values=" + values +
+      '}';
+  }
+}
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/Repository.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/Repository.java
new file mode 100644 (file)
index 0000000..5b13c42
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * 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.bitbucketserver;
+
+import com.google.gson.annotations.SerializedName;
+
+public class Repository {
+
+  @SerializedName("slug")
+  private String slug;
+
+  @SerializedName("name")
+  private String name;
+
+  @SerializedName("id")
+  private long id;
+
+  @SerializedName("project")
+  private Project project;
+
+  public Repository() {
+    // http://stackoverflow.com/a/18645370/229031
+  }
+
+  public Repository(String slug, String name, long id, Project project) {
+    this.slug = slug;
+    this.name = name;
+    this.id = id;
+    this.project = project;
+  }
+
+  public String getSlug() {
+    return slug;
+  }
+
+  public Repository setSlug(String slug) {
+    this.slug = slug;
+    return this;
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  public Repository setName(String name) {
+    this.name = name;
+    return this;
+  }
+
+  public long getId() {
+    return id;
+  }
+
+  public Repository setId(long id) {
+    this.id = id;
+    return this;
+  }
+
+  public Project getProject() {
+    return project;
+  }
+
+  public Repository setProject(Project project) {
+    this.project = project;
+    return this;
+  }
+
+  @Override
+  public String toString() {
+    return "{" +
+      "slug='" + slug + '\'' +
+      ", name='" + name + '\'' +
+      ", id=" + id +
+      ", project=" + project +
+      '}';
+  }
+}
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/RepositoryList.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/RepositoryList.java
new file mode 100644 (file)
index 0000000..e7125f0
--- /dev/null
@@ -0,0 +1,74 @@
+/*
+ * 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.bitbucketserver;
+
+import com.google.gson.Gson;
+import com.google.gson.annotations.SerializedName;
+import java.util.ArrayList;
+import java.util.List;
+
+public class RepositoryList {
+
+  @SerializedName("isLastPage")
+  private boolean isLastPage;
+
+  @SerializedName("values")
+  private List<Repository> values;
+
+  public RepositoryList() {
+    // http://stackoverflow.com/a/18645370/229031
+    this(false, new ArrayList<>());
+  }
+
+  public RepositoryList(boolean isLastPage, List<Repository> values) {
+    this.isLastPage = isLastPage;
+    this.values = values;
+  }
+
+  static RepositoryList parse(String json) {
+    return new Gson().fromJson(json, RepositoryList.class);
+  }
+
+  public boolean isLastPage() {
+    return isLastPage;
+  }
+
+  public RepositoryList setLastPage(boolean lastPage) {
+    isLastPage = lastPage;
+    return this;
+  }
+
+  public List<Repository> getValues() {
+    return values;
+  }
+
+  public RepositoryList setValues(List<Repository> values) {
+    this.values = values;
+    return this;
+  }
+
+  @Override
+  public String toString() {
+    return "{" +
+      "isLastPage=" + isLastPage +
+      ", values=" + values +
+      '}';
+  }
+}
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/package-info.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/package-info.java
new file mode 100644 (file)
index 0000000..a265ee7
--- /dev/null
@@ -0,0 +1,23 @@
+/*
+ * 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.
+ */
+@ParametersAreNonnullByDefault
+package org.sonar.alm.client.bitbucketserver;
+
+import javax.annotation.ParametersAreNonnullByDefault;
diff --git a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java
new file mode 100644 (file)
index 0000000..aac841b
--- /dev/null
@@ -0,0 +1,205 @@
+/*
+ * 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.bitbucketserver;
+
+import java.io.IOException;
+import okhttp3.mockwebserver.MockResponse;
+import okhttp3.mockwebserver.MockWebServer;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.sonar.alm.client.ConstantTimeoutConfiguration;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.assertj.core.api.Assertions.tuple;
+
+public class BitbucketServerRestClientTest {
+  private final MockWebServer server = new MockWebServer();
+  private BitbucketServerRestClient underTest;
+
+  @Before
+  public void prepare() throws IOException {
+    server.start();
+
+    underTest = new BitbucketServerRestClient(new ConstantTimeoutConfiguration(500));
+  }
+
+  @After
+  public void stopServer() throws IOException {
+    server.shutdown();
+  }
+
+  @Test
+  public void get_repos() {
+    server.enqueue(new MockResponse()
+      .setHeader("Content-Type", "application/json;charset=UTF-8")
+      .setBody("{\n" +
+        "  \"isLastPage\": true,\n" +
+        "  \"values\": [\n" +
+        "    {\n" +
+        "      \"slug\": \"banana\",\n" +
+        "      \"id\": 2,\n" +
+        "      \"name\": \"banana\",\n" +
+        "      \"project\": {\n" +
+        "        \"key\": \"HOY\",\n" +
+        "        \"id\": 2,\n" +
+        "        \"name\": \"hoy\"\n" +
+        "      }\n" +
+        "    },\n" +
+        "    {\n" +
+        "      \"slug\": \"potato\",\n" +
+        "      \"id\": 1,\n" +
+        "      \"name\": \"potato\",\n" +
+        "      \"project\": {\n" +
+        "        \"key\": \"HEY\",\n" +
+        "        \"id\": 1,\n" +
+        "        \"name\": \"hey\"\n" +
+        "      }\n" +
+        "    }\n" +
+        "  ]\n" +
+        "}"));
+
+    RepositoryList gsonBBSRepoList = underTest.getRepos(server.url("/").toString(), "token", "", "");
+    assertThat(gsonBBSRepoList.isLastPage()).isTrue();
+    assertThat(gsonBBSRepoList.getValues()).hasSize(2);
+    assertThat(gsonBBSRepoList.getValues()).extracting(Repository::getId, Repository::getName, Repository::getSlug,
+      g -> g.getProject().getId(), g -> g.getProject().getKey(), g -> g.getProject().getName())
+      .containsExactlyInAnyOrder(
+        tuple(2L, "banana", "banana", 2L, "HOY", "hoy"),
+        tuple(1L, "potato", "potato", 1L, "HEY", "hey"));
+  }
+
+  @Test
+  public void get_repo() {
+    server.enqueue(new MockResponse()
+      .setHeader("Content-Type", "application/json;charset=UTF-8")
+      .setBody(
+        "    {" +
+          "      \"slug\": \"banana-slug\"," +
+          "      \"id\": 2,\n" +
+          "      \"name\": \"banana\"," +
+          "      \"project\": {\n" +
+          "        \"key\": \"HOY\"," +
+          "        \"id\": 3,\n" +
+          "        \"name\": \"hoy\"" +
+          "      }" +
+          "    }"));
+
+    Repository repository = underTest.getRepo(server.url("/").toString(), "token", "", "");
+    assertThat(repository.getId()).isEqualTo(2L);
+    assertThat(repository.getName()).isEqualTo("banana");
+    assertThat(repository.getSlug()).isEqualTo("banana-slug");
+    assertThat(repository.getProject())
+      .extracting(Project::getId, Project::getKey, Project::getName)
+      .contains(3L, "HOY", "hoy");
+  }
+
+  @Test
+  public void get_projects() {
+    server.enqueue(new MockResponse()
+        .setHeader("Content-Type", "application/json;charset=UTF-8")
+        .setBody("{\n" +
+            "  \"isLastPage\": true,\n" +
+            "  \"values\": [\n" +
+            "    {\n" +
+            "      \"key\": \"HEY\",\n" +
+            "      \"id\": 1,\n" +
+            "      \"name\": \"hey\"\n" +
+            "    },\n" +
+            "    {\n" +
+            "      \"key\": \"HOY\",\n" +
+            "      \"id\": 2,\n" +
+            "      \"name\": \"hoy\"\n" +
+            "    }\n" +
+            "  ]\n" +
+            "}"));
+
+    final ProjectList gsonBBSProjectList = underTest.getProjects(server.url("/").toString(), "token");
+    assertThat(gsonBBSProjectList.getValues()).hasSize(2);
+    assertThat(gsonBBSProjectList.getValues()).extracting(Project::getId, Project::getKey, Project::getName)
+        .containsExactlyInAnyOrder(
+            tuple(1L, "HEY", "hey"),
+            tuple(2L, "HOY", "hoy"));
+  }
+
+  @Test
+  public void invalid_url() {
+    assertThatThrownBy(() -> BitbucketServerRestClient.buildUrl("file://wrong-url", ""))
+      .isInstanceOf(IllegalArgumentException.class)
+      .hasMessage("url must start with http:// or https://");
+  }
+
+  @Test
+  public void malformed_json() {
+    server.enqueue(new MockResponse()
+      .setHeader("Content-Type", "application/json;charset=UTF-8")
+      .setBody(
+        "I'm malformed JSON"));
+
+    String serverUrl = server.url("/").toString();
+    assertThatThrownBy(() -> underTest.getRepo(serverUrl, "token", "", ""))
+      .isInstanceOf(IllegalArgumentException.class)
+      .hasMessage("Unable to contact Bitbucket server, got an unexpected response");
+  }
+
+  @Test
+  public void error_handling() {
+    server.enqueue(new MockResponse()
+        .setHeader("Content-Type", "application/json;charset=UTF-8")
+        .setResponseCode(400)
+        .setBody("{\n" +
+            "  \"errors\": [\n" +
+            "    {\n" +
+            "      \"context\": null,\n" +
+            "      \"message\": \"Bad message\",\n" +
+            "      \"exceptionName\": \"com.atlassian.bitbucket.auth.BadException\"\n" +
+            "    }\n" +
+            "  ]\n" +
+            "}"));
+
+    String serverUrl = server.url("/").toString();
+    assertThatThrownBy(() -> underTest.getRepo(serverUrl, "token", "", ""))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Unable to contact Bitbucket server");
+  }
+
+  @Test
+  public void unauthorized_error() {
+    server.enqueue(new MockResponse()
+        .setHeader("Content-Type", "application/json;charset=UTF-8")
+        .setResponseCode(401)
+        .setBody("{\n" +
+            "  \"errors\": [\n" +
+            "    {\n" +
+            "      \"context\": null,\n" +
+            "      \"message\": \"Bad message\",\n" +
+            "      \"exceptionName\": \"com.atlassian.bitbucket.auth.BadException\"\n" +
+            "    }\n" +
+            "  ]\n" +
+            "}"));
+
+    String serverUrl = server.url("/").toString();
+    assertThatThrownBy(() -> underTest.getRepo(serverUrl, "token", "", ""))
+        .isInstanceOf(IllegalArgumentException.class)
+        .hasMessage("Invalid personal access token");
+  }
+
+}
index 5bcfb2009c4d166145c03e381cf13cef5556f6ca..65b65cac982d1702a1556bc9ef99c03d47e5806a 100644 (file)
@@ -20,6 +20,9 @@
 package org.sonar.server.almintegration.ws;
 
 import org.sonar.core.platform.Module;
+import org.sonar.server.almintegration.ws.bitbucketserver.ImportBitbucketServerProjectAction;
+import org.sonar.server.almintegration.ws.bitbucketserver.ListBitbucketServerProjectsAction;
+import org.sonar.server.almintegration.ws.bitbucketserver.SearchBitbucketServerReposAction;
 import org.sonar.server.almintegration.ws.gitlab.ImportGitLabProjectAction;
 import org.sonar.server.almintegration.ws.gitlab.SearchGitlabReposAction;
 
@@ -27,6 +30,9 @@ public class AlmIntegrationsWSModule extends Module {
   @Override
   protected void configureModule() {
     add(
+      ImportBitbucketServerProjectAction.class,
+      ListBitbucketServerProjectsAction.class,
+      SearchBitbucketServerReposAction.class,
       ImportGitLabProjectAction.class,
       SearchGitlabReposAction.class,
       AlmIntegrationsWs.class);
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
new file mode 100644 (file)
index 0000000..748230a
--- /dev/null
@@ -0,0 +1,149 @@
+/*
+ * 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.server.almintegration.ws.bitbucketserver;
+
+import java.util.Optional;
+import org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient;
+import org.sonar.alm.client.bitbucketserver.Repository;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.alm.pat.AlmPatDto;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.db.alm.setting.ProjectAlmSettingDto;
+import org.sonar.db.component.ComponentDto;
+import org.sonar.server.almintegration.ws.AlmIntegrationsWsAction;
+import org.sonar.server.almintegration.ws.ImportHelper;
+import org.sonar.server.component.ComponentUpdater;
+import org.sonar.server.project.ProjectDefaultVisibility;
+import org.sonar.server.user.UserSession;
+import org.sonarqube.ws.Projects;
+
+import static java.util.Objects.requireNonNull;
+import static org.sonar.api.resources.Qualifiers.PROJECT;
+import static org.sonar.server.almintegration.ws.ImportHelper.PARAM_ALM_SETTING;
+import static org.sonar.server.almintegration.ws.ImportHelper.toCreateResponse;
+import static org.sonar.server.component.NewComponent.newComponentBuilder;
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
+
+public class ImportBitbucketServerProjectAction implements AlmIntegrationsWsAction {
+
+  private static final String PARAM_PROJECT_KEY = "projectKey";
+  private static final String PARAM_REPO_SLUG = "repositorySlug";
+
+  private final DbClient dbClient;
+  private final UserSession userSession;
+  private final BitbucketServerRestClient bitbucketServerRestClient;
+  private final ProjectDefaultVisibility projectDefaultVisibility;
+  private final ComponentUpdater componentUpdater;
+  private final ImportHelper importHelper;
+
+  public ImportBitbucketServerProjectAction(DbClient dbClient, UserSession userSession, BitbucketServerRestClient bitbucketServerRestClient,
+    ProjectDefaultVisibility projectDefaultVisibility, ComponentUpdater componentUpdater,
+    ImportHelper importHelper) {
+    this.dbClient = dbClient;
+    this.userSession = userSession;
+    this.bitbucketServerRestClient = bitbucketServerRestClient;
+    this.projectDefaultVisibility = projectDefaultVisibility;
+    this.componentUpdater = componentUpdater;
+    this.importHelper = importHelper;
+  }
+
+  @Override
+  public void define(WebService.NewController context) {
+    WebService.NewAction action = context.createAction("import_bitbucketserver_project")
+      .setDescription("Create a SonarQube project with the information from the provided BitbucketServer project.<br/>" +
+        "Autoconfigure pull request decoration mechanism.<br/>" +
+        "Requires the 'Create Projects' permission")
+      .setPost(true)
+      .setInternal(true)
+      .setSince("8.2")
+      .setHandler(this);
+
+    action.createParam(PARAM_ALM_SETTING)
+      .setRequired(true)
+      .setMaximumLength(200)
+      .setDescription("ALM setting key");
+
+    action.createParam(PARAM_PROJECT_KEY)
+      .setRequired(true)
+      .setMaximumLength(200)
+      .setDescription("BitbucketServer project key");
+
+    action.createParam(PARAM_REPO_SLUG)
+      .setRequired(true)
+      .setMaximumLength(200)
+      .setDescription("BitbucketServer repository slug");
+  }
+
+  @Override
+  public void handle(Request request, Response response) {
+    Projects.CreateWsResponse createResponse = doHandle(request);
+    writeProtobuf(createResponse, request, response);
+  }
+
+  private Projects.CreateWsResponse doHandle(Request request) {
+    importHelper.checkProvisionProjectPermission();
+    AlmSettingDto almSettingDto = importHelper.getAlmSetting(request);
+    String userUuid = importHelper.getUserUuid();
+    try (DbSession dbSession = dbClient.openSession(false)) {
+
+      Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
+      String pat = almPatDto.map(AlmPatDto::getPersonalAccessToken)
+        .orElseThrow(() -> new IllegalArgumentException(String.format("personal access token for '%s' is missing", almSettingDto.getKey())));
+
+      String projectKey = request.mandatoryParam(PARAM_PROJECT_KEY);
+      String repoSlug = request.mandatoryParam(PARAM_REPO_SLUG);
+
+      String url = requireNonNull(almSettingDto.getUrl(), "ALM url cannot be null");
+      Repository repo = bitbucketServerRestClient.getRepo(url, pat, projectKey, repoSlug);
+
+      ComponentDto componentDto = createProject(dbSession, repo);
+      populatePRSetting(dbSession, repo, componentDto, almSettingDto);
+
+      return toCreateResponse(componentDto);
+    }
+  }
+
+  private ComponentDto createProject(DbSession dbSession, Repository repo) {
+    boolean visibility = projectDefaultVisibility.get(dbSession).isPrivate();
+    return componentUpdater.create(dbSession, newComponentBuilder()
+      .setKey(repo.getProject().getKey() + "_" + repo.getSlug())
+      .setName(repo.getName())
+      .setPrivate(visibility)
+      .setQualifier(PROJECT)
+      .build(),
+      userSession.isLoggedIn() ? userSession.getUuid() : null);
+  }
+
+  private void populatePRSetting(DbSession dbSession, Repository repo, ComponentDto componentDto, AlmSettingDto almSettingDto) {
+    ProjectAlmSettingDto projectAlmSettingDto = new ProjectAlmSettingDto()
+      .setAlmSettingUuid(almSettingDto.getUuid())
+      .setAlmRepo(repo.getProject().getKey())
+      .setAlmSlug(repo.getSlug())
+      .setProjectUuid(componentDto.uuid())
+      .setMonorepo(false);
+    dbClient.projectAlmSettingDao().insertOrUpdate(dbSession, projectAlmSettingDto);
+    dbSession.commit();
+  }
+
+}
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/ListBitbucketServerProjectsAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/ListBitbucketServerProjectsAction.java
new file mode 100644 (file)
index 0000000..f2fe002
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * 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.server.almintegration.ws.bitbucketserver;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient;
+import org.sonar.alm.client.bitbucketserver.Project;
+import org.sonar.alm.client.bitbucketserver.ProjectList;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.alm.pat.AlmPatDto;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.server.almintegration.ws.AlmIntegrationsWsAction;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.user.UserSession;
+import org.sonarqube.ws.AlmIntegrations.AlmProject;
+import org.sonarqube.ws.AlmIntegrations.ListBitbucketserverProjectsWsResponse;
+
+import static java.util.Objects.requireNonNull;
+import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
+
+public class ListBitbucketServerProjectsAction implements AlmIntegrationsWsAction {
+
+  private static final String PARAM_ALM_SETTING = "almSetting";
+
+  private final DbClient dbClient;
+  private final UserSession userSession;
+  private final BitbucketServerRestClient bitbucketServerRestClient;
+
+  public ListBitbucketServerProjectsAction(DbClient dbClient, UserSession userSession, BitbucketServerRestClient bitbucketServerRestClient) {
+    this.dbClient = dbClient;
+    this.userSession = userSession;
+    this.bitbucketServerRestClient = bitbucketServerRestClient;
+  }
+
+  @Override
+  public void define(WebService.NewController context) {
+    WebService.NewAction action = context.createAction("list_bitbucketserver_projects")
+      .setDescription("List the Bitbucket Server projects<br/>" +
+        "Requires the 'Create Projects' permission")
+      .setPost(false)
+      .setSince("8.2")
+      .setHandler(this);
+
+    action.createParam(PARAM_ALM_SETTING)
+      .setRequired(true)
+      .setMaximumLength(200)
+      .setDescription("ALM setting key");
+  }
+
+  @Override
+  public void handle(Request request, Response response) {
+    ListBitbucketserverProjectsWsResponse wsResponse = doHandle(request);
+    writeProtobuf(wsResponse, request, response);
+  }
+
+  private ListBitbucketserverProjectsWsResponse doHandle(Request request) {
+
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
+
+      String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
+      String userUuid = requireNonNull(userSession.getUuid(), "User UUID is not null");
+      AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, almSettingKey)
+        .orElseThrow(() -> new NotFoundException(String.format("ALM Setting '%s' not found", almSettingKey)));
+      Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
+      String pat = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
+
+      String url = requireNonNull(almSettingDto.getUrl(), "URL cannot be null");
+      ProjectList projectList = bitbucketServerRestClient.getProjects(url, pat);
+
+      List<AlmProject> values = projectList.getValues().stream().map(ListBitbucketServerProjectsAction::toAlmProject).collect(Collectors.toList());
+      ListBitbucketserverProjectsWsResponse.Builder builder = ListBitbucketserverProjectsWsResponse.newBuilder()
+        .addAllProjects(values);
+      return builder.build();
+    }
+  }
+
+  private static AlmProject toAlmProject(Project project) {
+    return AlmProject.newBuilder()
+      .setKey(project.getKey())
+      .setName(project.getName())
+      .build();
+  }
+
+}
diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/SearchBitbucketServerReposAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/almintegration/ws/bitbucketserver/SearchBitbucketServerReposAction.java
new file mode 100644 (file)
index 0000000..aa009e1
--- /dev/null
@@ -0,0 +1,183 @@
+/*
+ * 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.server.almintegration.ws.bitbucketserver;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.function.BinaryOperator;
+import java.util.function.Function;
+import org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient;
+import org.sonar.alm.client.bitbucketserver.Repository;
+import org.sonar.alm.client.bitbucketserver.RepositoryList;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.log.Logger;
+import org.sonar.api.utils.log.Loggers;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.alm.pat.AlmPatDto;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.db.alm.setting.ProjectAlmSettingDao;
+import org.sonar.db.alm.setting.ProjectAlmSettingDto;
+import org.sonar.db.project.ProjectDao;
+import org.sonar.db.project.ProjectDto;
+import org.sonar.server.almintegration.ws.AlmIntegrationsWsAction;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.user.UserSession;
+import org.sonarqube.ws.AlmIntegrations.BBSRepo;
+import org.sonarqube.ws.AlmIntegrations.SearchBitbucketserverReposWsResponse;
+
+import static java.util.Objects.requireNonNull;
+import static java.util.stream.Collectors.toList;
+import static java.util.stream.Collectors.toMap;
+import static java.util.stream.Collectors.toSet;
+import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
+
+public class SearchBitbucketServerReposAction implements AlmIntegrationsWsAction {
+
+  private static final Logger LOG = Loggers.get(SearchBitbucketServerReposAction.class);
+
+  private static final String PARAM_ALM_SETTING = "almSetting";
+  private static final String PARAM_REPO_NAME = "repositoryName";
+  private static final String PARAM_PROJECT_NAME = "projectName";
+
+  private final DbClient dbClient;
+  private final UserSession userSession;
+  private final BitbucketServerRestClient bitbucketServerRestClient;
+  private final ProjectAlmSettingDao projectAlmSettingDao;
+  private final ProjectDao projectDao;
+
+  public SearchBitbucketServerReposAction(DbClient dbClient, UserSession userSession,
+    BitbucketServerRestClient bitbucketServerRestClient, ProjectAlmSettingDao projectAlmSettingDao, ProjectDao projectDao) {
+    this.dbClient = dbClient;
+    this.userSession = userSession;
+    this.bitbucketServerRestClient = bitbucketServerRestClient;
+    this.projectAlmSettingDao = projectAlmSettingDao;
+    this.projectDao = projectDao;
+  }
+
+  @Override
+  public void define(WebService.NewController context) {
+    WebService.NewAction action = context.createAction("search_bitbucketserver_repos")
+      .setDescription("Search the Bitbucket Server repositories with REPO_ADMIN access<br/>" +
+        "Requires the 'Create Projects' permission")
+      .setPost(false)
+      .setSince("8.2")
+      .setHandler(this);
+
+    action.createParam(PARAM_ALM_SETTING)
+      .setRequired(true)
+      .setMaximumLength(200)
+      .setDescription("ALM setting key");
+    action.createParam(PARAM_PROJECT_NAME)
+      .setRequired(false)
+      .setMaximumLength(200)
+      .setDescription("Project name filter");
+    action.createParam(PARAM_REPO_NAME)
+      .setRequired(false)
+      .setMaximumLength(200)
+      .setDescription("Repository name filter");
+  }
+
+  @Override
+  public void handle(Request request, Response response) {
+    SearchBitbucketserverReposWsResponse wsResponse = doHandle(request);
+    writeProtobuf(wsResponse, request, response);
+  }
+
+  private SearchBitbucketserverReposWsResponse doHandle(Request request) {
+
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      userSession.checkLoggedIn().checkPermission(PROVISION_PROJECTS);
+
+      String almSettingKey = request.mandatoryParam(PARAM_ALM_SETTING);
+      String userUuid = requireNonNull(userSession.getUuid(), "User UUID cannot be null");
+      AlmSettingDto almSettingDto = dbClient.almSettingDao().selectByKey(dbSession, almSettingKey)
+        .orElseThrow(() -> new NotFoundException(String.format("ALM Setting '%s' not found", almSettingKey)));
+      Optional<AlmPatDto> almPatDto = dbClient.almPatDao().selectByUserAndAlmSetting(dbSession, userUuid, almSettingDto);
+
+      String projectKey = request.param(PARAM_PROJECT_NAME);
+      String repoName = request.param(PARAM_REPO_NAME);
+      String pat = almPatDto.map(AlmPatDto::getPersonalAccessToken).orElseThrow(() -> new IllegalArgumentException("No personal access token found"));
+      String url = requireNonNull(almSettingDto.getUrl(), "ALM url cannot be null");
+      RepositoryList gsonBBSRepoList = bitbucketServerRestClient.getRepos(url, pat, projectKey, repoName);
+
+      LOG.info(gsonBBSRepoList.toString());
+
+      Map<String, String> sqProjectsKeyByBBSKey = getSqProjectsKeyByBBSKey(dbSession, almSettingDto, gsonBBSRepoList);
+      List<BBSRepo> bbsRepos = gsonBBSRepoList.getValues().stream().map(gsonBBSRepo -> toBBSRepo(gsonBBSRepo, sqProjectsKeyByBBSKey))
+        .collect(toList());
+
+      SearchBitbucketserverReposWsResponse.Builder builder = SearchBitbucketserverReposWsResponse.newBuilder()
+        .setIsLastPage(gsonBBSRepoList.isLastPage())
+        .addAllRepositories(bbsRepos);
+      return builder.build();
+    }
+  }
+
+  private Map<String, String> getSqProjectsKeyByBBSKey(DbSession dbSession, AlmSettingDto almSettingDto, RepositoryList gsonBBSRepoList) {
+    Set<String> slugs = gsonBBSRepoList.getValues().stream().map(Repository::getSlug).collect(toSet());
+
+    List<ProjectAlmSettingDto> projectAlmSettingDtos = projectAlmSettingDao.selectByAlmSettingAndSlugs(dbSession, almSettingDto, slugs);
+    // As the previous request return bbs only filtered by slug, we need to do an additional filtering on bitbucketServer projectKey + slug
+    Set<String> bbsProjectsAndRepos = gsonBBSRepoList.getValues().stream().map(SearchBitbucketServerReposAction::customKey).collect(toSet());
+    Map<String, ProjectAlmSettingDto> filteredProjectsByUuid = projectAlmSettingDtos.stream()
+      .filter(p -> bbsProjectsAndRepos.contains(customKey(p)))
+      .collect(toMap(ProjectAlmSettingDto::getProjectUuid, Function.identity()));
+
+    Set<String> projectUuids = filteredProjectsByUuid.values().stream().map(ProjectAlmSettingDto::getProjectUuid).collect(toSet());
+    return projectDao.selectByUuids(dbSession, projectUuids).stream()
+      .collect(toMap(p -> customKey(filteredProjectsByUuid.get(p.getUuid())), ProjectDto::getKey, resolveNameCollisionOperatorByNaturalOrder()));
+  }
+
+  private static BBSRepo toBBSRepo(Repository gsonBBSRepo, Map<String, String> sqProjectsKeyByBBSKey) {
+    BBSRepo.Builder builder = BBSRepo.newBuilder()
+      .setSlug(gsonBBSRepo.getSlug())
+      .setId(gsonBBSRepo.getId())
+      .setName(gsonBBSRepo.getName())
+      .setProjectKey(gsonBBSRepo.getProject().getKey());
+
+    String sqProjectKey = sqProjectsKeyByBBSKey.get(customKey(gsonBBSRepo));
+    if (sqProjectKey != null) {
+      builder.setSqProjectKey(sqProjectKey);
+    }
+
+    return builder.build();
+  }
+
+  private static String customKey(ProjectAlmSettingDto project) {
+    return project.getAlmRepo() + "/" + project.getAlmSlug();
+  }
+
+  private static String customKey(Repository gsonBBSRepo) {
+    return gsonBBSRepo.getProject().getKey() + "/" + gsonBBSRepo.getSlug();
+  }
+
+  private static BinaryOperator<String> resolveNameCollisionOperatorByNaturalOrder() {
+    return (a, b) -> new TreeSet<>(Arrays.asList(a, b)).first();
+  }
+
+}
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
new file mode 100644 (file)
index 0000000..811f79f
--- /dev/null
@@ -0,0 +1,240 @@
+/*
+ * 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.server.almintegration.ws.bitbucketserver;
+
+import java.util.Optional;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient;
+import org.sonar.alm.client.bitbucketserver.Project;
+import org.sonar.alm.client.bitbucketserver.Repository;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.api.utils.System2;
+import org.sonar.core.i18n.I18n;
+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.project.ProjectDto;
+import org.sonar.db.user.UserDto;
+import org.sonar.server.almintegration.ws.ImportHelper;
+import org.sonar.server.component.ComponentUpdater;
+import org.sonar.server.es.TestProjectIndexers;
+import org.sonar.server.exceptions.BadRequestException;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.exceptions.UnauthorizedException;
+import org.sonar.server.favorite.FavoriteUpdater;
+import org.sonar.server.permission.PermissionTemplateService;
+import org.sonar.server.project.ProjectDefaultVisibility;
+import org.sonar.server.project.Visibility;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.WsActionTester;
+import org.sonarqube.ws.Projects;
+
+import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
+import static org.apache.commons.lang.math.JVMRandom.nextLong;
+import static org.assertj.core.api.Assertions.assertThat;
+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;
+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;
+
+public class ImportBitbucketServerProjectActionTest {
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+  @Rule
+  public UserSessionRule userSession = UserSessionRule.standalone();
+  @Rule
+  public DbTester db = DbTester.create();
+
+  private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class);
+  private final BitbucketServerRestClient bitbucketServerRestClient = mock(BitbucketServerRestClient.class);
+
+  private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), mock(I18n.class), System2.INSTANCE,
+    mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestProjectIndexers(), new SequenceUuidFactory());
+
+  private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession);
+  private final WsActionTester ws = new WsActionTester(new ImportBitbucketServerProjectAction(db.getDbClient(), userSession,
+    bitbucketServerRestClient, projectDefaultVisibility, componentUpdater, importHelper));
+
+  @Before
+  public void before() {
+    when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE);
+  }
+
+  @Test
+  public void import_project() {
+    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();
+    Repository repo = getGsonBBSRepo(project);
+    when(bitbucketServerRestClient.getRepo(any(), any(), any(), any())).thenReturn(repo);
+
+    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();
+    assertThat(result.getKey()).isEqualTo(project.getKey() + "_" + repo.getSlug());
+    assertThat(result.getName()).isEqualTo(repo.getName());
+
+    Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
+    assertThat(projectDto).isPresent();
+    assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
+  }
+
+  @Test
+  public void fail_project_already_exist() {
+    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();
+    Repository repo = getGsonBBSRepo(project);
+    String projectKey = project.getKey() + "_" + repo.getSlug();
+    db.components().insertPublicProject(p -> p.setDbKey(projectKey));
+
+    expectedException.expect(BadRequestException.class);
+    expectedException.expectMessage("Could not create null, key already exists: " + projectKey);
+
+    when(bitbucketServerRestClient.getRepo(any(), any(), any(), any())).thenReturn(repo);
+    ws.newRequest()
+      .setParam("almSetting", almSetting.getKey())
+      .setParam("projectKey", "projectKey")
+      .setParam("repositorySlug", "repo-slug")
+      .execute();
+  }
+
+  @Test
+  public void fail_when_not_logged_in() {
+    expectedException.expect(UnauthorizedException.class);
+
+    ws.newRequest()
+      .setParam("almSetting", "sdgfdshfjztutz")
+      .setParam("projectKey", "projectKey")
+      .setParam("repositorySlug", "repo-slug")
+      .execute();
+  }
+
+  @Test
+  public void fail_when_missing_project_creator_permission() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).addPermission(SCAN);
+
+    expectedException.expect(ForbiddenException.class);
+    expectedException.expectMessage("Insufficient privileges");
+
+    ws.newRequest()
+      .setParam("almSetting", "sdgfdshfjztutz")
+      .setParam("projectKey", "projectKey")
+      .setParam("repositorySlug", "repo-slug")
+      .execute();
+  }
+
+  @Test
+  public void check_pat_is_missing() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
+    AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("personal access token for '" + almSetting.getKey() + "' is missing");
+
+    ws.newRequest()
+      .setParam("almSetting", almSetting.getKey())
+      .execute();
+  }
+
+  @Test
+  public void fail_check_alm_setting_not_found() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
+    AlmPatDto almPatDto = newAlmPatDto();
+    db.getDbClient().almPatDao().insert(db.getSession(), almPatDto);
+
+    expectedException.expect(NotFoundException.class);
+    expectedException.expectMessage("ALM Setting 'testKey' not found");
+
+    ws.newRequest()
+      .setParam("almSetting", "testKey")
+      .execute();
+  }
+
+  @Test
+  public void fail_when_no_creation_project_permission() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user);
+
+    expectedException.expect(ForbiddenException.class);
+    expectedException.expectMessage("Insufficient privileges");
+
+    ws.newRequest()
+      .setParam("almSetting", "anyvalue")
+      .execute();
+  }
+
+  @Test
+  public void definition() {
+    WebService.Action def = ws.getDef();
+
+    assertThat(def.since()).isEqualTo("8.2");
+    assertThat(def.isPost()).isTrue();
+    assertThat(def.params())
+      .extracting(WebService.Param::key, WebService.Param::isRequired)
+      .containsExactlyInAnyOrder(
+        tuple("almSetting", true),
+        tuple("repositorySlug", true),
+        tuple("projectKey", true));
+  }
+
+  private Repository getGsonBBSRepo(Project project) {
+    Repository bbsResult = new Repository();
+    bbsResult.setProject(project);
+    bbsResult.setSlug(randomAlphanumeric(5));
+    bbsResult.setName(randomAlphanumeric(5));
+    bbsResult.setId(nextLong(100));
+    return bbsResult;
+  }
+
+  private Project getGsonBBSProject() {
+    return new Project()
+      .setKey(randomAlphanumeric(5))
+      .setId(nextLong(100))
+      .setName(randomAlphanumeric(5));
+  }
+
+}
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/ListBitbucketServerProjectsActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/ListBitbucketServerProjectsActionTest.java
new file mode 100644 (file)
index 0000000..050ec5a
--- /dev/null
@@ -0,0 +1,159 @@
+/*
+ * 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.server.almintegration.ws.bitbucketserver;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient;
+import org.sonar.alm.client.bitbucketserver.Project;
+import org.sonar.alm.client.bitbucketserver.ProjectList;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbTester;
+import org.sonar.db.alm.pat.AlmPatDto;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.db.user.UserDto;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.exceptions.UnauthorizedException;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestRequest;
+import org.sonar.server.ws.WsActionTester;
+import org.sonarqube.ws.AlmIntegrations;
+import org.sonarqube.ws.AlmIntegrations.ListBitbucketserverProjectsWsResponse;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.assertj.core.api.Assertions.tuple;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+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;
+
+public class ListBitbucketServerProjectsActionTest {
+
+  @Rule
+  public UserSessionRule userSession = UserSessionRule.standalone();
+  @Rule
+  public DbTester db = DbTester.create();
+
+  private final BitbucketServerRestClient bitbucketServerRestClient = mock(BitbucketServerRestClient.class);
+  private final WsActionTester ws = new WsActionTester(new ListBitbucketServerProjectsAction(db.getDbClient(), userSession, bitbucketServerRestClient));
+
+  @Before
+  public void before() {
+    ProjectList projectList = new ProjectList();
+    List<Project> values = new ArrayList<>();
+    Project project = new Project();
+    project.setId(1);
+    project.setKey("key");
+    project.setName("name");
+    values.add(project);
+    projectList.setValues(values);
+    when(bitbucketServerRestClient.getProjects(anyString(), anyString())).thenReturn(projectList);
+
+  }
+
+  @Test
+  public void list_projects() {
+    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());
+    });
+
+    ListBitbucketserverProjectsWsResponse response = ws.newRequest()
+      .setParam("almSetting", almSetting.getKey())
+      .executeProtobuf(ListBitbucketserverProjectsWsResponse.class);
+
+    assertThat(response.getProjectsCount()).isEqualTo(1);
+    assertThat(response.getProjectsList())
+      .extracting(AlmIntegrations.AlmProject::getKey, AlmIntegrations.AlmProject::getName)
+      .containsExactly(tuple("key", "name"));
+  }
+
+  @Test
+  public void check_pat_is_missing() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
+    AlmSettingDto almSetting = db.almSettings().insertBitbucketAlmSetting();
+
+    TestRequest request = ws.newRequest()
+      .setParam("almSetting", almSetting.getKey());
+
+    assertThatThrownBy(request::execute)
+      .isInstanceOf(IllegalArgumentException.class)
+      .hasMessage("No personal access token found");
+  }
+
+  @Test
+  public void fail_check_alm_setting_not_found() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
+    AlmPatDto almPatDto = newAlmPatDto();
+    db.getDbClient().almPatDao().insert(db.getSession(), almPatDto);
+
+    TestRequest request = ws.newRequest()
+      .setParam("almSetting", "testKey");
+
+    assertThatThrownBy(request::execute)
+      .isInstanceOf(NotFoundException.class)
+      .hasMessage("ALM Setting 'testKey' not found");
+  }
+
+  @Test
+  public void fail_when_not_logged_in() {
+    TestRequest request = ws.newRequest()
+      .setParam("almSetting", "anyvalue");
+
+    assertThatThrownBy(request::execute)
+      .isInstanceOf(UnauthorizedException.class);
+  }
+
+  @Test
+  public void fail_when_no_creation_project_permission() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user);
+
+    TestRequest request = ws.newRequest()
+      .setParam("almSetting", "anyvalue");
+
+    assertThatThrownBy(request::execute)
+      .isInstanceOf(ForbiddenException.class)
+      .hasMessage("Insufficient privileges");
+  }
+
+  @Test
+  public void definition() {
+    WebService.Action def = ws.getDef();
+
+    assertThat(def.since()).isEqualTo("8.2");
+    assertThat(def.isPost()).isFalse();
+    assertThat(def.params())
+      .extracting(WebService.Param::key, WebService.Param::isRequired)
+      .containsExactlyInAnyOrder(tuple("almSetting", true));
+  }
+
+}
diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/SearchBitbucketServerReposActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/almintegration/ws/bitbucketserver/SearchBitbucketServerReposActionTest.java
new file mode 100644 (file)
index 0000000..d0c3907
--- /dev/null
@@ -0,0 +1,275 @@
+/*
+ * 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.server.almintegration.ws.bitbucketserver;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient;
+import org.sonar.alm.client.bitbucketserver.Project;
+import org.sonar.alm.client.bitbucketserver.Repository;
+import org.sonar.alm.client.bitbucketserver.RepositoryList;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.db.DbTester;
+import org.sonar.db.alm.pat.AlmPatDto;
+import org.sonar.db.alm.setting.AlmSettingDto;
+import org.sonar.db.alm.setting.ProjectAlmSettingDao;
+import org.sonar.db.project.ProjectDao;
+import org.sonar.db.project.ProjectDto;
+import org.sonar.db.user.UserDto;
+import org.sonar.server.exceptions.ForbiddenException;
+import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.exceptions.UnauthorizedException;
+import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.WsActionTester;
+import org.sonarqube.ws.AlmIntegrations;
+import org.sonarqube.ws.AlmIntegrations.SearchBitbucketserverReposWsResponse;
+
+import static org.assertj.core.api.Assertions.assertThat;
+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;
+import static org.sonar.db.alm.integration.pat.AlmPatsTesting.newAlmPatDto;
+import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
+
+public class SearchBitbucketServerReposActionTest {
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+  @Rule
+  public UserSessionRule userSession = UserSessionRule.standalone();
+  @Rule
+  public DbTester db = DbTester.create();
+
+  private final BitbucketServerRestClient bitbucketServerRestClient = mock(BitbucketServerRestClient.class);
+  private final ProjectAlmSettingDao projectAlmSettingDao = db.getDbClient().projectAlmSettingDao();
+  private final ProjectDao projectDao = db.getDbClient().projectDao();
+  private final WsActionTester ws = new WsActionTester(
+    new SearchBitbucketServerReposAction(db.getDbClient(), userSession, bitbucketServerRestClient, projectAlmSettingDao, projectDao));
+
+  @Before
+  public void before() {
+    RepositoryList gsonBBSRepoList = new RepositoryList();
+    gsonBBSRepoList.setLastPage(true);
+    List<Repository> values = new ArrayList<>();
+    values.add(getGsonBBSRepo1());
+    values.add(getGsonBBSRepo2());
+    gsonBBSRepoList.setValues(values);
+    when(bitbucketServerRestClient.getRepos(any(), any(), any(), any())).thenReturn(gsonBBSRepoList);
+  }
+
+  @Test
+  public void list_repos() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
+    AlmSettingDto almSetting = db.almSettings().insertBitbucketAlmSetting();
+    db.almPats().insert(dto -> {
+      dto.setAlmSettingUuid(almSetting.getUuid());
+      dto.setUserUuid(user.getUuid());
+    });
+    ProjectDto projectDto = db.components().insertPrivateProjectDto();
+    db.almSettings().insertBitbucketProjectAlmSetting(almSetting, projectDto, s -> s.setAlmRepo("projectKey2"), s -> s.setAlmSlug("repo-slug-2"));
+
+    AlmIntegrations.SearchBitbucketserverReposWsResponse response = ws.newRequest()
+      .setParam("almSetting", almSetting.getKey())
+      .executeProtobuf(SearchBitbucketserverReposWsResponse.class);
+
+    assertThat(response.getIsLastPage()).isTrue();
+    assertThat(response.getRepositoriesList())
+      .extracting(AlmIntegrations.BBSRepo::getId, AlmIntegrations.BBSRepo::getName, AlmIntegrations.BBSRepo::getSlug, AlmIntegrations.BBSRepo::hasSqProjectKey,
+        AlmIntegrations.BBSRepo::getSqProjectKey, AlmIntegrations.BBSRepo::getProjectKey)
+      .containsExactlyInAnyOrder(
+        tuple(1L, "repoName1", "repo-slug-1", false, "", "projectKey1"),
+        tuple(3L, "repoName2", "repo-slug-2", true, projectDto.getKey(), "projectKey2"));
+  }
+
+  @Test
+  public void return_empty_list_when_no_bbs_repo() {
+    RepositoryList gsonBBSRepoList = new RepositoryList();
+    gsonBBSRepoList.setLastPage(true);
+    gsonBBSRepoList.setValues(new ArrayList<>());
+    when(bitbucketServerRestClient.getRepos(any(), any(), any(), any())).thenReturn(gsonBBSRepoList);
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
+    AlmSettingDto almSetting = db.almSettings().insertBitbucketAlmSetting();
+    db.almPats().insert(dto -> {
+      dto.setAlmSettingUuid(almSetting.getUuid());
+      dto.setUserUuid(user.getUuid());
+    });
+    ProjectDto projectDto = db.components().insertPrivateProjectDto();
+    db.almSettings().insertBitbucketProjectAlmSetting(almSetting, projectDto, s -> s.setAlmRepo("projectKey2"), s -> s.setAlmSlug("repo-slug-2"));
+
+    AlmIntegrations.SearchBitbucketserverReposWsResponse response = ws.newRequest()
+      .setParam("almSetting", almSetting.getKey())
+      .executeProtobuf(SearchBitbucketserverReposWsResponse.class);
+
+    assertThat(response.getIsLastPage()).isTrue();
+    assertThat(response.getRepositoriesList()).isEmpty();
+  }
+
+  @Test
+  public void already_imported_detection_does_not_get_confused_by_same_slug_in_different_projects() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
+    AlmSettingDto almSetting = db.almSettings().insertBitbucketAlmSetting();
+    db.almPats().insert(dto -> {
+      dto.setAlmSettingUuid(almSetting.getUuid());
+      dto.setUserUuid(user.getUuid());
+    });
+    ProjectDto projectDto = db.components().insertPrivateProjectDto();
+    db.almSettings().insertBitbucketProjectAlmSetting(almSetting, projectDto, s -> s.setAlmRepo("projectKey2"), s -> s.setAlmSlug("repo-slug-2"));
+    db.almSettings().insertBitbucketProjectAlmSetting(almSetting, db.components().insertPrivateProjectDto(), s -> s.setAlmRepo("projectKey2"), s -> s.setAlmSlug("repo-slug-2"));
+
+    AlmIntegrations.SearchBitbucketserverReposWsResponse response = ws.newRequest()
+      .setParam("almSetting", almSetting.getKey())
+      .executeProtobuf(SearchBitbucketserverReposWsResponse.class);
+
+    assertThat(response.getIsLastPage()).isTrue();
+    assertThat(response.getRepositoriesList())
+      .extracting(AlmIntegrations.BBSRepo::getId, AlmIntegrations.BBSRepo::getName, AlmIntegrations.BBSRepo::getSlug, AlmIntegrations.BBSRepo::getSqProjectKey,
+        AlmIntegrations.BBSRepo::getProjectKey)
+      .containsExactlyInAnyOrder(
+        tuple(1L, "repoName1", "repo-slug-1", "", "projectKey1"),
+        tuple(3L, "repoName2", "repo-slug-2", projectDto.getKey(), "projectKey2"));
+  }
+
+  @Test
+  public void use_projectKey_to_disambiguate_when_multiple_projects_are_binded_on_one_bitbucketserver_repo() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
+    AlmSettingDto almSetting = db.almSettings().insertBitbucketAlmSetting();
+    db.almPats().insert(dto -> {
+      dto.setAlmSettingUuid(almSetting.getUuid());
+      dto.setUserUuid(user.getUuid());
+    });
+    ProjectDto project1 = db.components().insertPrivateProjectDto(p -> p.setDbKey("B"));
+    ProjectDto project2 = db.components().insertPrivateProjectDto(p -> p.setDbKey("A"));
+    db.almSettings().insertBitbucketProjectAlmSetting(almSetting, project1, s -> s.setAlmRepo("projectKey2"), s -> s.setAlmSlug("repo-slug-2"));
+    db.almSettings().insertBitbucketProjectAlmSetting(almSetting, project2, s -> s.setAlmRepo("projectKey2"), s -> s.setAlmSlug("repo-slug-2"));
+
+    AlmIntegrations.SearchBitbucketserverReposWsResponse response = ws.newRequest()
+      .setParam("almSetting", almSetting.getKey())
+      .executeProtobuf(SearchBitbucketserverReposWsResponse.class);
+
+    assertThat(response.getIsLastPage()).isTrue();
+    assertThat(response.getRepositoriesList())
+      .extracting(AlmIntegrations.BBSRepo::getId, AlmIntegrations.BBSRepo::getName, AlmIntegrations.BBSRepo::getSlug, AlmIntegrations.BBSRepo::getSqProjectKey,
+        AlmIntegrations.BBSRepo::getProjectKey)
+      .containsExactlyInAnyOrder(
+        tuple(1L, "repoName1", "repo-slug-1", "", "projectKey1"),
+        tuple(3L, "repoName2", "repo-slug-2", "A", "projectKey2"));
+  }
+
+  @Test
+  public void check_pat_is_missing() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
+    AlmSettingDto almSetting = db.almSettings().insertGitHubAlmSetting();
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("No personal access token found");
+
+    ws.newRequest()
+      .setParam("almSetting", almSetting.getKey())
+      .execute();
+  }
+
+  @Test
+  public void fail_check_pat_alm_setting_not_found() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user).addPermission(PROVISION_PROJECTS);
+    AlmPatDto almPatDto = newAlmPatDto();
+    db.getDbClient().almPatDao().insert(db.getSession(), almPatDto);
+
+    expectedException.expect(NotFoundException.class);
+    expectedException.expectMessage("ALM Setting 'testKey' not found");
+
+    ws.newRequest()
+      .setParam("almSetting", "testKey")
+      .execute();
+  }
+
+  @Test
+  public void fail_when_not_logged_in() {
+    expectedException.expect(UnauthorizedException.class);
+
+    ws.newRequest()
+      .setParam("almSetting", "anyvalue")
+      .execute();
+  }
+
+  @Test
+  public void fail_when_no_creation_project_permission() {
+    UserDto user = db.users().insertUser();
+    userSession.logIn(user);
+
+    expectedException.expect(ForbiddenException.class);
+    expectedException.expectMessage("Insufficient privileges");
+
+    ws.newRequest()
+      .setParam("almSetting", "anyvalue")
+      .execute();
+  }
+
+  @Test
+  public void definition() {
+    WebService.Action def = ws.getDef();
+
+    assertThat(def.since()).isEqualTo("8.2");
+    assertThat(def.isPost()).isFalse();
+    assertThat(def.params())
+      .extracting(WebService.Param::key, WebService.Param::isRequired)
+      .containsExactlyInAnyOrder(
+        tuple("almSetting", true),
+        tuple("projectName", false),
+        tuple("repositoryName", false));
+  }
+
+  private Repository getGsonBBSRepo1() {
+    Repository gsonBBSRepo1 = new Repository();
+    gsonBBSRepo1.setId(1L);
+    gsonBBSRepo1.setName("repoName1");
+    Project project1 = new Project();
+    project1.setName("projectName1");
+    project1.setKey("projectKey1");
+    project1.setId(2L);
+    gsonBBSRepo1.setProject(project1);
+    gsonBBSRepo1.setSlug("repo-slug-1");
+    return gsonBBSRepo1;
+  }
+
+  private Repository getGsonBBSRepo2() {
+    Repository gsonBBSRepo = new Repository();
+    gsonBBSRepo.setId(3L);
+    gsonBBSRepo.setName("repoName2");
+    Project project = new Project();
+    project.setName("projectName2");
+    project.setKey("projectKey2");
+    project.setId(4L);
+    gsonBBSRepo.setProject(project);
+    gsonBBSRepo.setSlug("repo-slug-2");
+    return gsonBBSRepo;
+  }
+
+}
index bed805ac5af6588e7f839997a54d5c40c1dab584..d6761042cc37050045ae53423ca60f6cd130cd7b 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.platform.platformlevel;
 
 import java.util.List;
 import org.sonar.alm.client.TimeoutConfigurationImpl;
+import org.sonar.alm.client.bitbucketserver.BitbucketServerRestClient;
 import org.sonar.alm.client.gitlab.GitlabHttpClient;
 import org.sonar.api.profiles.AnnotationProfileParser;
 import org.sonar.api.profiles.XMLProfileParser;
@@ -491,6 +492,7 @@ public class PlatformLevel4 extends PlatformLevel {
       // ALM integrations
       TimeoutConfigurationImpl.class,
       ImportHelper.class,
+      BitbucketServerRestClient.class,
       GitlabHttpClient.class,
       AlmIntegrationsWSModule.class,