You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultQualityProfileLoader.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.repository;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.ArrayList;
  24. import java.util.LinkedHashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.function.BinaryOperator;
  28. import java.util.function.Supplier;
  29. import org.sonar.api.utils.MessageException;
  30. import org.sonar.scanner.http.DefaultScannerWsClient;
  31. import org.sonarqube.ws.Qualityprofiles.SearchWsResponse;
  32. import org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile;
  33. import org.sonarqube.ws.client.GetRequest;
  34. import org.sonarqube.ws.client.HttpException;
  35. import static java.util.function.Function.identity;
  36. import static java.util.stream.Collectors.toMap;
  37. import static org.sonar.api.impl.utils.ScannerUtils.encodeForUrl;
  38. public class DefaultQualityProfileLoader implements QualityProfileLoader {
  39. private static final String WS_URL = "/api/qualityprofiles/search.protobuf";
  40. private final DefaultScannerWsClient wsClient;
  41. public DefaultQualityProfileLoader(DefaultScannerWsClient wsClient) {
  42. this.wsClient = wsClient;
  43. }
  44. private List<QualityProfile> loadDefault() {
  45. StringBuilder url = new StringBuilder(WS_URL + "?defaults=true");
  46. return handleErrors(url, () -> "Failed to load the default quality profiles", false);
  47. }
  48. @Override
  49. public List<QualityProfile> load(String projectKey) {
  50. StringBuilder url = new StringBuilder(WS_URL + "?project=").append(encodeForUrl(projectKey));
  51. return handleErrors(url, () -> String.format("Failed to load the quality profiles of project '%s'", projectKey), true);
  52. }
  53. private List<QualityProfile> handleErrors(StringBuilder url, Supplier<String> errorMsg, boolean tryLoadDefault) {
  54. try {
  55. return doLoad(url);
  56. } catch (HttpException e) {
  57. if (e.code() == 404) {
  58. if (tryLoadDefault) {
  59. return loadDefault();
  60. } else {
  61. throw MessageException.of(errorMsg.get() + ": " + DefaultScannerWsClient.createErrorMessage(e));
  62. }
  63. }
  64. throw new IllegalStateException(errorMsg.get() + ": " + DefaultScannerWsClient.createErrorMessage(e));
  65. } catch (MessageException e) {
  66. throw e;
  67. } catch (Exception e) {
  68. throw new IllegalStateException(errorMsg.get(), e);
  69. }
  70. }
  71. private List<QualityProfile> doLoad(StringBuilder url) throws IOException {
  72. Map<String, QualityProfile> result = call(url.toString());
  73. if (result.isEmpty()) {
  74. throw MessageException.of("No quality profiles have been found, you probably don't have any language plugin installed.");
  75. }
  76. return new ArrayList<>(result.values());
  77. }
  78. private Map<String, QualityProfile> call(String url) throws IOException {
  79. GetRequest getRequest = new GetRequest(url);
  80. try (InputStream is = wsClient.call(getRequest).contentStream()) {
  81. SearchWsResponse profiles = SearchWsResponse.parseFrom(is);
  82. List<QualityProfile> profilesList = profiles.getProfilesList();
  83. return profilesList.stream().collect(toMap(QualityProfile::getLanguage, identity(), throwingMerger(), LinkedHashMap::new));
  84. }
  85. }
  86. private static <T> BinaryOperator<T> throwingMerger() {
  87. return (u, v) -> {
  88. throw new IllegalStateException(String.format("Duplicate key %s", u));
  89. };
  90. }
  91. }