Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

DefaultActiveRulesLoader.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.rule;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.HashMap;
  24. import java.util.LinkedList;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.stream.Collectors;
  28. import org.apache.commons.io.IOUtils;
  29. import org.sonar.api.batch.rule.LoadedActiveRule;
  30. import org.sonar.api.impl.utils.ScannerUtils;
  31. import org.sonar.api.rule.RuleKey;
  32. import org.sonar.api.utils.DateUtils;
  33. import org.sonar.scanner.bootstrap.ScannerWsClient;
  34. import org.sonarqube.ws.Rules;
  35. import org.sonarqube.ws.Rules.Active;
  36. import org.sonarqube.ws.Rules.Active.Param;
  37. import org.sonarqube.ws.Rules.ActiveList;
  38. import org.sonarqube.ws.Rules.Rule;
  39. import org.sonarqube.ws.Rules.SearchResponse;
  40. import org.sonarqube.ws.client.GetRequest;
  41. public class DefaultActiveRulesLoader implements ActiveRulesLoader {
  42. private static final String RULES_SEARCH_URL = "/api/rules/search.protobuf?" +
  43. "f=repo,name,severity,lang,internalKey,templateKey,params,actives,createdAt,updatedAt,deprecatedKeys&activation=true";
  44. private final ScannerWsClient wsClient;
  45. public DefaultActiveRulesLoader(ScannerWsClient wsClient) {
  46. this.wsClient = wsClient;
  47. }
  48. @Override
  49. public List<LoadedActiveRule> load(String qualityProfileKey) {
  50. List<LoadedActiveRule> ruleList = new LinkedList<>();
  51. int page = 1;
  52. int pageSize = 500;
  53. long loaded = 0;
  54. while (true) {
  55. GetRequest getRequest = new GetRequest(getUrl(qualityProfileKey, page, pageSize));
  56. SearchResponse response = loadFromStream(wsClient.call(getRequest).contentStream());
  57. List<LoadedActiveRule> pageRules = readPage(response);
  58. ruleList.addAll(pageRules);
  59. loaded += response.getPs();
  60. if (response.getTotal() <= loaded) {
  61. break;
  62. }
  63. page++;
  64. }
  65. return ruleList;
  66. }
  67. private static String getUrl(String qualityProfileKey, int page, int pageSize) {
  68. StringBuilder builder = new StringBuilder(1024);
  69. builder.append(RULES_SEARCH_URL);
  70. builder.append("&qprofile=").append(ScannerUtils.encodeForUrl(qualityProfileKey));
  71. builder.append("&ps=").append(pageSize);
  72. builder.append("&p=").append(page);
  73. return builder.toString();
  74. }
  75. private static SearchResponse loadFromStream(InputStream is) {
  76. try {
  77. return SearchResponse.parseFrom(is);
  78. } catch (IOException e) {
  79. throw new IllegalStateException("Failed to load quality profiles", e);
  80. } finally {
  81. IOUtils.closeQuietly(is);
  82. }
  83. }
  84. private static List<LoadedActiveRule> readPage(SearchResponse response) {
  85. List<LoadedActiveRule> loadedRules = new LinkedList<>();
  86. List<Rule> rulesList = response.getRulesList();
  87. Map<String, ActiveList> actives = response.getActives().getActives();
  88. for (Rule r : rulesList) {
  89. ActiveList activeList = actives.get(r.getKey());
  90. Active active = activeList.getActiveList(0);
  91. LoadedActiveRule loadedRule = new LoadedActiveRule();
  92. loadedRule.setRuleKey(RuleKey.parse(r.getKey()));
  93. loadedRule.setName(r.getName());
  94. loadedRule.setSeverity(active.getSeverity());
  95. loadedRule.setCreatedAt(DateUtils.dateToLong(DateUtils.parseDateTime(active.getCreatedAt())));
  96. loadedRule.setUpdatedAt(DateUtils.dateToLong(DateUtils.parseDateTime(active.getUpdatedAt())));
  97. loadedRule.setLanguage(r.getLang());
  98. loadedRule.setInternalKey(r.getInternalKey());
  99. if (r.hasTemplateKey()) {
  100. RuleKey templateRuleKey = RuleKey.parse(r.getTemplateKey());
  101. loadedRule.setTemplateRuleKey(templateRuleKey.rule());
  102. }
  103. Map<String, String> params = new HashMap<>();
  104. for (Rules.Rule.Param param : r.getParams().getParamsList()) {
  105. params.put(param.getKey(), param.getDefaultValue());
  106. }
  107. // overrides defaultValue if the key is the same
  108. for (Param param : active.getParamsList()) {
  109. params.put(param.getKey(), param.getValue());
  110. }
  111. loadedRule.setParams(params);
  112. loadedRule.setDeprecatedKeys(r.getDeprecatedKeys().getDeprecatedKeyList()
  113. .stream()
  114. .map(RuleKey::parse)
  115. .collect(Collectors.toSet()));
  116. loadedRules.add(loadedRule);
  117. }
  118. return loadedRules;
  119. }
  120. }