aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-alm-client
diff options
context:
space:
mode:
Diffstat (limited to 'server/sonar-alm-client')
-rw-r--r--server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevOpsHttpClient.java26
-rw-r--r--server/sonar-alm-client/src/main/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClient.java5
-rw-r--r--server/sonar-alm-client/src/test/java/org/sonar/alm/client/azure/AzureDevOpsHttpClientTest.java52
-rw-r--r--server/sonar-alm-client/src/test/java/org/sonar/alm/client/bitbucketserver/BitbucketServerRestClientTest.java40
4 files changed, 115 insertions, 8 deletions
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevOpsHttpClient.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevOpsHttpClient.java
index c4f2f4f0c57..36cb08dea3b 100644
--- a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevOpsHttpClient.java
+++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevOpsHttpClient.java
@@ -23,13 +23,11 @@ import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
-
import java.io.IOException;
import java.net.HttpURLConnection;
import java.nio.charset.StandardCharsets;
import java.util.function.Function;
import javax.annotation.Nullable;
-
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
@@ -61,13 +59,18 @@ public class AzureDevOpsHttpClient {
.build();
}
+ public void checkPAT(String serverUrl, String token) {
+ String url = String.format("%s/_apis/projects", getTrimmedUrl(serverUrl));
+ LOG.debug(String.format("check pat : [%s]", url));
+ doGet(token, url);
+ }
+
public GsonAzureProjectList getProjects(String serverUrl, String token) {
String url = String.format("%s/_apis/projects", getTrimmedUrl(serverUrl));
LOG.debug(String.format("get projects : [%s]", url));
return doGet(token, url, r -> buildGson().fromJson(r.body().charStream(), GsonAzureProjectList.class));
}
-
public GsonAzureRepoList getRepos(String serverUrl, String token, @Nullable String projectName) {
String url;
if (projectName != null && !projectName.isEmpty()) {
@@ -85,12 +88,25 @@ public class AzureDevOpsHttpClient {
return doGet(token, url, r -> buildGson().fromJson(r.body().charStream(), GsonAzureRepo.class));
}
- protected <G> G doGet(String token, String url, Function<Response, G> handler) {
+ private void doGet(String token, String url) {
+ Request request = prepareRequestWithToken(token, GET, url, null);
+ doCall(request);
+ }
+
+ protected void doCall(Request request) {
+ try (Response response = client.newCall(request).execute()) {
+ checkResponseIsSuccessful(response);
+ } catch (IOException e) {
+ throw new IllegalArgumentException(UNABLE_TO_CONTACT_AZURE_SERVER, e);
+ }
+ }
+
+ protected <G> G doGet(String token, String url, Function<Response, G> handler) {
Request request = prepareRequestWithToken(token, GET, url, null);
return doCall(request, handler);
}
- protected <G> G doCall(Request request, Function<Response, G> handler) {
+ protected <G> G doCall(Request request, Function<Response, G> handler) {
try (Response response = client.newCall(request).execute()) {
checkResponseIsSuccessful(response);
return handler.apply(response);
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
index 758db6e799f..7a3ba3b6303 100644
--- 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
@@ -77,6 +77,11 @@ public class BitbucketServerRestClient {
return doGet(token, url, r -> buildGson().fromJson(r.body().charStream(), Repository.class));
}
+ public RepositoryList getRecentRepo(String serverUrl, String token) {
+ HttpUrl url = buildUrl(serverUrl, "/rest/api/1.0/profile/recent/repos");
+ return doGet(token, url, r -> buildGson().fromJson(r.body().charStream(), RepositoryList.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));
diff --git a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/azure/AzureDevOpsHttpClientTest.java b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/azure/AzureDevOpsHttpClientTest.java
index c296f79b5af..40d3407943f 100644
--- a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/azure/AzureDevOpsHttpClientTest.java
+++ b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/azure/AzureDevOpsHttpClientTest.java
@@ -19,6 +19,9 @@
*/
package org.sonar.alm.client.azure;
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+import javax.annotation.Nullable;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
@@ -34,9 +37,6 @@ import org.sonar.api.utils.log.LoggerLevel;
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 javax.annotation.Nullable;
-import java.io.IOException;
-import java.util.concurrent.TimeUnit;
public class AzureDevOpsHttpClientTest {
public static final String UNABLE_TO_CONTACT_AZURE = "Unable to contact Azure DevOps server, got an unexpected response";
@@ -61,6 +61,52 @@ public class AzureDevOpsHttpClientTest {
}
@Test
+ public void check_pat() throws InterruptedException {
+ enqueueResponse(200, " { \"count\": 1,\n" +
+ " \"value\": [\n" +
+ " {\n" +
+ " \"id\": \"3311cd05-3f00-4a5e-b47f-df94a9982b6e\",\n" +
+ " \"name\": \"Project\",\n" +
+ " \"description\": \"Project Description\",\n" +
+ " \"url\": \"https://ado.sonarqube.com/DefaultCollection/_apis/projects/3311cd05-3f00-4a5e-b47f-df94a9982b6e\",\n" +
+ " \"state\": \"wellFormed\",\n" +
+ " \"revision\": 63,\n" +
+ " \"visibility\": \"private\"\n" +
+ " }]}");
+
+ underTest.checkPAT(server.url("").toString(), "token");
+
+ RecordedRequest request = server.takeRequest(10, TimeUnit.SECONDS);
+ String azureDevOpsUrlCall = request.getRequestUrl().toString();
+ assertThat(azureDevOpsUrlCall).isEqualTo(server.url("") + "_apis/projects");
+ assertThat(request.getMethod()).isEqualTo("GET");
+
+ assertThat(logTester.logs()).hasSize(1);
+ assertThat(logTester.logs(LoggerLevel.DEBUG))
+ .contains("check pat : [" + server.url("").toString() + "_apis/projects]");
+ }
+
+ @Test
+ public void check_invalid_pat() {
+ enqueueResponse(401);
+
+ String serverUrl = server.url("").toString();
+ assertThatThrownBy(() -> underTest.checkPAT(serverUrl, "invalid-token"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Invalid personal access token");
+ }
+
+ @Test
+ public void check_pat_with_server_error() {
+ enqueueResponse(500);
+
+ String serverUrl = server.url("").toString();
+ assertThatThrownBy(() -> underTest.checkPAT(serverUrl, "token"))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage("Unable to contact Azure DevOps server");
+ }
+
+ @Test
public void get_projects() throws InterruptedException {
enqueueResponse(200, " { \"count\": 2,\n" +
" \"value\": [\n" +
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
index aac841beb37..a4cafcf1941 100644
--- 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
@@ -88,6 +88,46 @@ public class BitbucketServerRestClientTest {
}
@Test
+ public void get_recent_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.getRecentRepo(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")