3 * Copyright (C) 2009-2018 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.scanner.repository;
22 import com.google.common.base.Throwables;
23 import com.google.common.collect.HashBasedTable;
24 import com.google.common.collect.Table;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.net.HttpURLConnection;
28 import java.util.Date;
30 import javax.annotation.Nullable;
31 import org.apache.commons.io.IOUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.sonar.api.utils.MessageException;
35 import org.sonar.scanner.bootstrap.ScannerWsClient;
36 import org.sonar.scanner.util.ScannerUtils;
37 import org.sonarqube.ws.WsBatch;
38 import org.sonarqube.ws.WsBatch.WsProjectResponse;
39 import org.sonarqube.ws.WsBatch.WsProjectResponse.FileDataByPath;
40 import org.sonarqube.ws.WsBatch.WsProjectResponse.Settings;
41 import org.sonarqube.ws.client.GetRequest;
42 import org.sonarqube.ws.client.HttpException;
43 import org.sonarqube.ws.client.WsResponse;
45 public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoader {
46 private static final Logger LOG = LoggerFactory.getLogger(DefaultProjectRepositoriesLoader.class);
47 private static final String BATCH_PROJECT_URL = "/batch/project.protobuf";
48 private ScannerWsClient wsClient;
50 public DefaultProjectRepositoriesLoader(ScannerWsClient wsClient) {
51 this.wsClient = wsClient;
55 public ProjectRepositories load(String projectKey, boolean issuesMode, @Nullable String branchBase) {
56 GetRequest request = new GetRequest(getUrl(projectKey, issuesMode, branchBase));
57 try (WsResponse response = wsClient.call(request)) {
58 InputStream is = response.contentStream();
59 return processStream(is, projectKey);
60 } catch (RuntimeException e) {
65 LOG.debug("Project repository not available - continuing without it");
66 return new ProjectRepositories();
70 private static String getUrl(String projectKey, boolean issuesMode, @Nullable String branchBase) {
71 StringBuilder builder = new StringBuilder();
73 builder.append(BATCH_PROJECT_URL)
74 .append("?key=").append(ScannerUtils.encodeForUrl(projectKey));
76 builder.append("&issues_mode=true");
78 if (branchBase != null) {
79 builder.append("&branch=").append(branchBase);
81 return builder.toString();
84 private static boolean shouldThrow(Exception e) {
85 for (Throwable t : Throwables.getCausalChain(e)) {
86 if (t instanceof HttpException) {
87 HttpException http = (HttpException) t;
88 return http.code() != HttpURLConnection.HTTP_NOT_FOUND;
90 if (t instanceof MessageException) {
98 private static ProjectRepositories processStream(InputStream is, String projectKey) {
100 WsProjectResponse response = WsProjectResponse.parseFrom(is);
102 Table<String, String, FileData> fileDataTable = HashBasedTable.create();
103 Table<String, String, String> settings = HashBasedTable.create();
105 Map<String, Settings> settingsByModule = response.getSettingsByModule();
106 for (Map.Entry<String, Settings> e1 : settingsByModule.entrySet()) {
107 for (Map.Entry<String, String> e2 : e1.getValue().getSettings().entrySet()) {
108 settings.put(e1.getKey(), e2.getKey(), e2.getValue());
112 Map<String, FileDataByPath> fileDataByModuleAndPath = response.getFileDataByModuleAndPath();
113 for (Map.Entry<String, FileDataByPath> e1 : fileDataByModuleAndPath.entrySet()) {
114 for (Map.Entry<String, WsBatch.WsProjectResponse.FileData> e2 : e1.getValue().getFileDataByPath().entrySet()) {
115 FileData fd = new FileData(e2.getValue().getHash(), e2.getValue().getRevision());
116 fileDataTable.put(e1.getKey(), e2.getKey(), fd);
120 return new ProjectRepositories(settings, fileDataTable, new Date(response.getLastAnalysisDate()));
121 } catch (IOException e) {
122 throw new IllegalStateException("Couldn't load project repository for " + projectKey, e);
124 IOUtils.closeQuietly(is);