From 1417465dc1947354f332dbd0faa58ac95802edbb Mon Sep 17 00:00:00 2001 From: Jacek Date: Wed, 2 Jun 2021 09:29:46 +0200 Subject: [PATCH] SONAR-14871 Add Azure Devops project binding validation --- .../client/azure/AzureDevOpsHttpClient.java | 11 +++++-- .../azure/AzureDevopsServerException.java | 33 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevopsServerException.java 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 082e8247e72..07d79a7b5e7 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 @@ -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 index 00000000000..7a298423278 --- /dev/null +++ b/server/sonar-alm-client/src/main/java/org/sonar/alm/client/azure/AzureDevopsServerException.java @@ -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; + } +} -- 2.39.5