3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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 org.apache.commons.io.IOUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.sonar.api.utils.MessageException;
34 import org.sonar.scanner.bootstrap.ScannerWsClient;
35 import org.sonar.scanner.util.ScannerUtils;
36 import org.sonarqube.ws.WsBatch;
37 import org.sonarqube.ws.WsBatch.WsProjectResponse;
38 import org.sonarqube.ws.WsBatch.WsProjectResponse.FileDataByPath;
39 import org.sonarqube.ws.WsBatch.WsProjectResponse.Settings;
40 import org.sonarqube.ws.client.GetRequest;
41 import org.sonarqube.ws.client.HttpException;
42 import org.sonarqube.ws.client.WsResponse;
44 public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoader {
45 private static final Logger LOG = LoggerFactory.getLogger(DefaultProjectRepositoriesLoader.class);
46 private static final String BATCH_PROJECT_URL = "/batch/project.protobuf";
47 private ScannerWsClient wsClient;
49 public DefaultProjectRepositoriesLoader(ScannerWsClient wsClient) {
50 this.wsClient = wsClient;
54 public ProjectRepositories load(String projectKey, boolean issuesMode) {
55 GetRequest request = new GetRequest(getUrl(projectKey, issuesMode));
56 try (WsResponse response = wsClient.call(request)) {
57 InputStream is = response.contentStream();
58 return processStream(is, projectKey);
59 } catch (RuntimeException e) {
64 LOG.debug("Project repository not available - continuing without it", e);
65 return new ProjectRepositories();
69 private static String getUrl(String projectKey, boolean issuesMode) {
70 StringBuilder builder = new StringBuilder();
72 builder.append(BATCH_PROJECT_URL)
73 .append("?key=").append(ScannerUtils.encodeForUrl(projectKey));
75 builder.append("&issues_mode=true");
77 return builder.toString();
80 private static boolean shouldThrow(Exception e) {
81 for (Throwable t : Throwables.getCausalChain(e)) {
82 if (t instanceof HttpException) {
83 HttpException http = (HttpException) t;
84 return http.code() != HttpURLConnection.HTTP_NOT_FOUND;
86 if (t instanceof MessageException) {
94 private static ProjectRepositories processStream(InputStream is, String projectKey) {
96 WsProjectResponse response = WsProjectResponse.parseFrom(is);
98 Table<String, String, FileData> fileDataTable = HashBasedTable.create();
99 Table<String, String, String> settings = HashBasedTable.create();
101 Map<String, Settings> settingsByModule = response.getSettingsByModule();
102 for (Map.Entry<String, Settings> e1 : settingsByModule.entrySet()) {
103 for (Map.Entry<String, String> e2 : e1.getValue().getSettings().entrySet()) {
104 settings.put(e1.getKey(), e2.getKey(), e2.getValue());
108 Map<String, FileDataByPath> fileDataByModuleAndPath = response.getFileDataByModuleAndPath();
109 for (Map.Entry<String, FileDataByPath> e1 : fileDataByModuleAndPath.entrySet()) {
110 for (Map.Entry<String, WsBatch.WsProjectResponse.FileData> e2 : e1.getValue().getFileDataByPath().entrySet()) {
111 FileData fd = new FileData(e2.getValue().getHash(), e2.getValue().getRevision());
112 fileDataTable.put(e1.getKey(), e2.getKey(), fd);
116 return new ProjectRepositories(settings, fileDataTable, new Date(response.getLastAnalysisDate()));
117 } catch (IOException e) {
118 throw new IllegalStateException("Couldn't load project repository for " + projectKey, e);
120 IOUtils.closeQuietly(is);