Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DefaultProjectRepositoriesLoader.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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 com.google.common.base.Throwables;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.net.HttpURLConnection;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import javax.annotation.Nullable;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import org.sonar.scanner.bootstrap.ScannerWsClient;
  31. import org.sonar.scanner.util.ScannerUtils;
  32. import org.sonarqube.ws.Batch.WsProjectResponse;
  33. import org.sonarqube.ws.client.GetRequest;
  34. import org.sonarqube.ws.client.HttpException;
  35. import org.sonarqube.ws.client.WsResponse;
  36. public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoader {
  37. private static final Logger LOG = LoggerFactory.getLogger(DefaultProjectRepositoriesLoader.class);
  38. private static final String BATCH_PROJECT_URL = "/batch/project.protobuf";
  39. private final ScannerWsClient wsClient;
  40. public DefaultProjectRepositoriesLoader(ScannerWsClient wsClient) {
  41. this.wsClient = wsClient;
  42. }
  43. @Override
  44. public ProjectRepositories load(String projectKey, @Nullable String branchBase) {
  45. GetRequest request = new GetRequest(getUrl(projectKey, branchBase));
  46. try (WsResponse response = wsClient.call(request)) {
  47. try (InputStream is = response.contentStream()) {
  48. return processStream(is);
  49. } catch (IOException e) {
  50. throw new IllegalStateException("Couldn't load project repository for " + projectKey, e);
  51. }
  52. } catch (RuntimeException e) {
  53. if (shouldThrow(e)) {
  54. throw e;
  55. }
  56. LOG.debug("Project repository not available - continuing without it");
  57. return new SingleProjectRepository();
  58. }
  59. }
  60. private static String getUrl(String projectKey, @Nullable String branchBase) {
  61. StringBuilder builder = new StringBuilder();
  62. builder.append(BATCH_PROJECT_URL)
  63. .append("?key=").append(ScannerUtils.encodeForUrl(projectKey));
  64. if (branchBase != null) {
  65. builder.append("&branch=").append(branchBase);
  66. }
  67. return builder.toString();
  68. }
  69. private static boolean shouldThrow(Exception e) {
  70. for (Throwable t : Throwables.getCausalChain(e)) {
  71. if (t instanceof HttpException && ((HttpException) t).code() == HttpURLConnection.HTTP_NOT_FOUND) {
  72. return false;
  73. }
  74. }
  75. return true;
  76. }
  77. private static ProjectRepositories processStream(InputStream is) throws IOException {
  78. WsProjectResponse response = WsProjectResponse.parseFrom(is);
  79. if (response.getFileDataByModuleAndPathCount() == 0) {
  80. return new SingleProjectRepository(constructFileDataMap(response.getFileDataByPathMap()));
  81. } else {
  82. final Map<String, SingleProjectRepository> repositoriesPerModule = new HashMap<>();
  83. response.getFileDataByModuleAndPathMap().keySet().forEach(moduleKey -> {
  84. WsProjectResponse.FileDataByPath filePaths = response.getFileDataByModuleAndPathMap().get(moduleKey);
  85. repositoriesPerModule.put(moduleKey, new SingleProjectRepository(
  86. constructFileDataMap(filePaths.getFileDataByPathMap())));
  87. });
  88. return new MultiModuleProjectRepository(repositoriesPerModule);
  89. }
  90. }
  91. private static Map<String, FileData> constructFileDataMap(Map<String, WsProjectResponse.FileData> content) {
  92. Map<String, FileData> fileDataMap = new HashMap<>();
  93. content.forEach((key, value) -> {
  94. FileData fd = new FileData(value.getHash(), value.getRevision());
  95. fileDataMap.put(key, fd);
  96. });
  97. return fileDataMap;
  98. }
  99. }