]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14628 ADO use version-specific API endpoints
authorBelen Pruvost <belen.pruvost@sonarsource.com>
Mon, 29 Mar 2021 12:24:12 +0000 (14:24 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 29 Mar 2021 20:03:43 +0000 (20:03 +0000)
server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevOpsHttpClient.java
server/sonar-alm-client/src/test/java/org/sonar/alm/client/azure/AzureDevOpsHttpClientTest.java

index 36cb08dea3b1bd013204dd1085a5685dfd909eab..082e8247e7272daa35ab28a8ff6fc282c07c8ce7 100644 (file)
@@ -45,8 +45,11 @@ import static org.sonar.api.internal.apachecommons.lang.StringUtils.substringBef
 
 @ServerSide
 public class AzureDevOpsHttpClient {
+
   private static final Logger LOG = Loggers.get(AzureDevOpsHttpClient.class);
 
+  public static final String API_VERSION_3 = "api-version=3.0";
+
   protected static final String GET = "GET";
   protected static final String UNABLE_TO_CONTACT_AZURE_SERVER = "Unable to contact Azure DevOps server";
 
@@ -60,13 +63,13 @@ public class AzureDevOpsHttpClient {
   }
 
   public void checkPAT(String serverUrl, String token) {
-    String url = String.format("%s/_apis/projects", getTrimmedUrl(serverUrl));
+    String url = String.format("%s/_apis/projects?%s", getTrimmedUrl(serverUrl), API_VERSION_3);
     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));
+    String url = String.format("%s/_apis/projects?%s", getTrimmedUrl(serverUrl), API_VERSION_3);
     LOG.debug(String.format("get projects : [%s]", url));
     return doGet(token, url, r -> buildGson().fromJson(r.body().charStream(), GsonAzureProjectList.class));
   }
@@ -74,16 +77,16 @@ public class AzureDevOpsHttpClient {
   public GsonAzureRepoList getRepos(String serverUrl, String token, @Nullable String projectName) {
     String url;
     if (projectName != null && !projectName.isEmpty()) {
-      url = String.format("%s/%s/_apis/git/repositories", getTrimmedUrl(serverUrl), projectName);
+      url = String.format("%s/%s/_apis/git/repositories?%s", getTrimmedUrl(serverUrl), projectName, API_VERSION_3);
     } else {
-      url = String.format("%s/_apis/git/repositories", getTrimmedUrl(serverUrl));
+      url = String.format("%s/_apis/git/repositories?%s", getTrimmedUrl(serverUrl), API_VERSION_3);
     }
     LOG.debug(String.format("get repos : [%s]", url));
     return doGet(token, url, r -> buildGson().fromJson(r.body().charStream(), GsonAzureRepoList.class));
   }
 
   public GsonAzureRepo getRepo(String serverUrl, String token, String projectName, String repositoryName) {
-    String url = String.format("%s/%s/_apis/git/repositories/%s", getTrimmedUrl(serverUrl), projectName, repositoryName);
+    String url = String.format("%s/%s/_apis/git/repositories/%s?%s", getTrimmedUrl(serverUrl), projectName, repositoryName, API_VERSION_3);
     LOG.debug(String.format("get repo : [%s]", url));
     return doGet(token, url, r -> buildGson().fromJson(r.body().charStream(), GsonAzureRepo.class));
   }
index da1ea680661b92afdae29bafdda1b6702e7584d9..bfb9c22b0be91e6fcc75b16738dfdd31711184b2 100644 (file)
@@ -78,12 +78,12 @@ public class AzureDevOpsHttpClientTest {
 
     RecordedRequest request = server.takeRequest(10, TimeUnit.SECONDS);
     String azureDevOpsUrlCall = request.getRequestUrl().toString();
-    assertThat(azureDevOpsUrlCall).isEqualTo(server.url("") + "_apis/projects");
+    assertThat(azureDevOpsUrlCall).isEqualTo(server.url("") + "_apis/projects?api-version=3.0");
     assertThat(request.getMethod()).isEqualTo("GET");
 
     assertThat(logTester.logs()).hasSize(1);
     assertThat(logTester.logs(LoggerLevel.DEBUG))
-      .contains("check pat : [" + server.url("").toString() + "_apis/projects]");
+      .contains("check pat : [" + server.url("").toString() + "_apis/projects?api-version=3.0]");
   }
 
   @Test
@@ -132,12 +132,12 @@ public class AzureDevOpsHttpClientTest {
 
     RecordedRequest request = server.takeRequest(10, TimeUnit.SECONDS);
     String azureDevOpsUrlCall = request.getRequestUrl().toString();
-    assertThat(azureDevOpsUrlCall).isEqualTo(server.url("") + "_apis/projects");
+    assertThat(azureDevOpsUrlCall).isEqualTo(server.url("") + "_apis/projects?api-version=3.0");
     assertThat(request.getMethod()).isEqualTo("GET");
 
     assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1);
     assertThat(logTester.logs(LoggerLevel.DEBUG))
-      .contains("get projects : [" + server.url("").toString() + "_apis/projects]");
+      .contains("get projects : [" + server.url("").toString() + "_apis/projects?api-version=3.0]");
     assertThat(projects.getValues()).hasSize(2);
     assertThat(projects.getValues())
       .extracting(GsonAzureProject::getName, GsonAzureProject::getDescription)
@@ -198,12 +198,12 @@ public class AzureDevOpsHttpClientTest {
 
     RecordedRequest request = server.takeRequest(10, TimeUnit.SECONDS);
     String azureDevOpsUrlCall = request.getRequestUrl().toString();
-    assertThat(azureDevOpsUrlCall).isEqualTo(server.url("") + "projectName/_apis/git/repositories");
+    assertThat(azureDevOpsUrlCall).isEqualTo(server.url("") + "projectName/_apis/git/repositories?api-version=3.0");
     assertThat(request.getMethod()).isEqualTo("GET");
 
     assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1);
     assertThat(logTester.logs(LoggerLevel.DEBUG))
-      .contains("get repos : [" + server.url("").toString() + "projectName/_apis/git/repositories]");
+      .contains("get repos : [" + server.url("").toString() + "projectName/_apis/git/repositories?api-version=3.0]");
     assertThat(repos.getValues()).hasSize(1);
     assertThat(repos.getValues())
       .extracting(GsonAzureRepo::getName, GsonAzureRepo::getUrl, r -> r.getProject().getName())
@@ -218,7 +218,7 @@ public class AzureDevOpsHttpClientTest {
 
     RecordedRequest request = server.takeRequest(10, TimeUnit.SECONDS);
     String azureDevOpsUrlCall = request.getRequestUrl().toString();
-    assertThat(azureDevOpsUrlCall).isEqualTo(server.url("") + "_apis/git/repositories");
+    assertThat(azureDevOpsUrlCall).isEqualTo(server.url("") + "_apis/git/repositories?api-version=3.0");
     assertThat(request.getMethod()).isEqualTo("GET");
     assertThat(repos.getValues()).isEmpty();
   }
@@ -251,12 +251,12 @@ public class AzureDevOpsHttpClientTest {
 
     RecordedRequest request = server.takeRequest(10, TimeUnit.SECONDS);
     String azureDevOpsUrlCall = request.getRequestUrl().toString();
-    assertThat(azureDevOpsUrlCall).isEqualTo(server.url("") + "Project-Name/_apis/git/repositories/Repo-Name-1");
+    assertThat(azureDevOpsUrlCall).isEqualTo(server.url("") + "Project-Name/_apis/git/repositories/Repo-Name-1?api-version=3.0");
     assertThat(request.getMethod()).isEqualTo("GET");
 
     assertThat(logTester.logs(LoggerLevel.DEBUG)).hasSize(1);
     assertThat(logTester.logs(LoggerLevel.DEBUG))
-      .contains("get repo : [" + server.url("").toString() + "Project-Name/_apis/git/repositories/Repo-Name-1]");
+      .contains("get repo : [" + server.url("").toString() + "Project-Name/_apis/git/repositories/Repo-Name-1?api-version=3.0]");
     assertThat(repo.getId()).isEqualTo("Repo-Id-1");
     assertThat(repo.getName()).isEqualTo("Repo-Name-1");
     assertThat(repo.getUrl()).isEqualTo("https://ado.sonarqube.com/DefaultCollection/Repo-Id-1");