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.

DefaultMetricsRepositoryLoader.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.Reader;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import org.sonar.api.measures.Metric;
  26. import org.sonar.api.measures.Metric.ValueType;
  27. import org.sonar.scanner.http.DefaultScannerWsClient;
  28. import org.sonar.scanner.protocol.GsonHelper;
  29. import org.sonarqube.ws.client.GetRequest;
  30. public class DefaultMetricsRepositoryLoader implements MetricsRepositoryLoader {
  31. private static final String METRICS_SEARCH_URL = "/api/metrics/search?ps=500&p=";
  32. private DefaultScannerWsClient wsClient;
  33. public DefaultMetricsRepositoryLoader(DefaultScannerWsClient wsClient) {
  34. this.wsClient = wsClient;
  35. }
  36. @Override
  37. public MetricsRepository load() {
  38. List<Metric> metrics = new ArrayList<>();
  39. try {
  40. loadFromPaginatedWs(metrics);
  41. } catch (Exception e) {
  42. throw new IllegalStateException("Unable to load metrics", e);
  43. }
  44. return new MetricsRepository(metrics);
  45. }
  46. private void loadFromPaginatedWs(List<Metric> metrics) throws IOException {
  47. int page = 1;
  48. WsMetricsResponse response;
  49. do {
  50. GetRequest getRequest = new GetRequest(METRICS_SEARCH_URL + page);
  51. try (Reader reader = wsClient.call(getRequest).contentReader()) {
  52. response = GsonHelper.create().fromJson(reader, WsMetricsResponse.class);
  53. for (WsMetric metric : response.metrics) {
  54. metrics.add(new Metric.Builder(metric.getKey(), metric.getName(), ValueType.valueOf(metric.getType()))
  55. .create()
  56. .setDirection(metric.getDirection())
  57. .setQualitative(metric.isQualitative())
  58. .setUserManaged(false)
  59. .setDescription(metric.getDescription())
  60. .setUuid(metric.getUuid()));
  61. }
  62. }
  63. page++;
  64. } while (response.getP() < (response.getTotal() / response.getPs() + 1));
  65. }
  66. private static class WsMetric {
  67. private String uuid;
  68. private String key;
  69. private String type;
  70. private String name;
  71. private String description;
  72. private int direction;
  73. private boolean qualitative;
  74. public String getUuid() {
  75. return uuid;
  76. }
  77. public String getKey() {
  78. return key;
  79. }
  80. public String getType() {
  81. return type;
  82. }
  83. public String getName() {
  84. return name;
  85. }
  86. public String getDescription() {
  87. return description;
  88. }
  89. public int getDirection() {
  90. return direction;
  91. }
  92. public boolean isQualitative() {
  93. return qualitative;
  94. }
  95. }
  96. private static class WsMetricsResponse {
  97. private List<WsMetric> metrics = new ArrayList<>();
  98. private int total;
  99. private int p;
  100. private int ps;
  101. public WsMetricsResponse() {
  102. // http://stackoverflow.com/a/18645370/229031
  103. }
  104. public int getTotal() {
  105. return total;
  106. }
  107. public int getP() {
  108. return p;
  109. }
  110. public int getPs() {
  111. return ps;
  112. }
  113. }
  114. }