]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14871 Add Azure Devops project binding validation
authorJacek <jacek.poreda@sonarsource.com>
Wed, 2 Jun 2021 07:29:46 +0000 (09:29 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 10 Jun 2021 20:03:27 +0000 (20:03 +0000)
server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevOpsHttpClient.java
server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevopsServerException.java [new file with mode: 0644]

index 082e8247e7272daa35ab28a8ff6fc282c07c8ce7..07d79a7b5e7bc28376f16e52b722018689aba5c2 100644 (file)
@@ -74,6 +74,13 @@ public class AzureDevOpsHttpClient {
     return doGet(token, url, r -> buildGson().fromJson(r.body().charStream(), GsonAzureProjectList.class));
   }
 
+  public GsonAzureProject getProject(String serverUrl, String token, String projectName) {
+    String url = String.format("%s/_apis/projects/%s?%s", getTrimmedUrl(serverUrl), projectName, API_VERSION_3);
+    LOG.debug(String.format("get project : [%s]", url));
+    return doGet(token, url, r -> buildGson().fromJson(r.body().charStream(), GsonAzureProject.class));
+
+  }
+
   public GsonAzureRepoList getRepos(String serverUrl, String token, @Nullable String projectName) {
     String url;
     if (projectName != null && !projectName.isEmpty()) {
@@ -132,13 +139,13 @@ public class AzureDevOpsHttpClient {
     if (!response.isSuccessful()) {
       LOG.debug(UNABLE_TO_CONTACT_AZURE_SERVER + ": {} {}", response.request().url().toString(), response.code());
       if (response.code() == HttpURLConnection.HTTP_UNAUTHORIZED) {
-        throw new IllegalArgumentException("Invalid personal access token");
+        throw new AzureDevopsServerException(response.code(), "Invalid personal access token");
       }
       ResponseBody responseBody = response.body();
       String body = responseBody == null ? "" : responseBody.string();
       String errorMessage = generateErrorMessage(body, UNABLE_TO_CONTACT_AZURE_SERVER);
       LOG.info(String.format("Azure API call to [%s] failed with %s http code. Azure response content : [%s]", response.request().url().toString(), response.code(), body));
-      throw new IllegalArgumentException(errorMessage);
+      throw new AzureDevopsServerException(response.code(), errorMessage);
     }
   }
 
diff --git a/server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevopsServerException.java b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevopsServerException.java
new file mode 100644 (file)
index 0000000..7a29842
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * 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.azure;
+
+public class AzureDevopsServerException extends IllegalArgumentException {
+  private final int httpCode;
+
+  public AzureDevopsServerException(int httpCode, String clientErrorMessage) {
+    super(clientErrorMessage);
+    this.httpCode = httpCode;
+  }
+
+  public int getHttpCode() {
+    return httpCode;
+  }
+}