All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.batch.repository.DefaultProjectRepositoriesLoader Maven / Gradle / Ivy

There is a newer version: 25.1.0.102122
Show newest version
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.batch.repository;

import com.google.common.base.Throwables;

import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.Date;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.utils.MessageException;
import org.sonar.batch.bootstrap.BatchWsClient;
import org.sonar.batch.util.BatchUtils;
import org.sonarqube.ws.WsBatch.WsProjectResponse;
import org.sonarqube.ws.WsBatch.WsProjectResponse.FileDataByPath;
import org.sonarqube.ws.WsBatch.WsProjectResponse.Settings;
import org.sonarqube.ws.client.GetRequest;
import org.sonarqube.ws.client.HttpException;

public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoader {
  private static final Logger LOG = LoggerFactory.getLogger(DefaultProjectRepositoriesLoader.class);
  private static final String BATCH_PROJECT_URL = "/batch/project.protobuf";
  private BatchWsClient wsClient;

  public DefaultProjectRepositoriesLoader(BatchWsClient wsClient) {
    this.wsClient = wsClient;
  }

  @Override
  public ProjectRepositories load(String projectKey, boolean issuesMode) {
    try {
      GetRequest request = new GetRequest(getUrl(projectKey, issuesMode));
      InputStream is = wsClient.call(request).contentStream();
      return processStream(is, projectKey);
    } catch (RuntimeException e) {
      if (shouldThrow(e)) {
        throw e;
      }

      LOG.debug("Project repository not available - continuing without it", e);
      return new ProjectRepositories();
    }
  }

  private static String getUrl(String projectKey, boolean issuesMode) {
    StringBuilder builder = new StringBuilder();

    builder.append(BATCH_PROJECT_URL)
      .append("?key=").append(BatchUtils.encodeForUrl(projectKey));
    if (issuesMode) {
      builder.append("&issues_mode=true");
    }
    return builder.toString();
  }

  private static boolean shouldThrow(Exception e) {
    for (Throwable t : Throwables.getCausalChain(e)) {
      if (t instanceof HttpException) {
        HttpException http = (HttpException) t;
        return http.code() != HttpURLConnection.HTTP_NOT_FOUND;
      }
      if (t instanceof MessageException) {
        return true;
      }
    }

    return false;
  }

  private static ProjectRepositories processStream(InputStream is, String projectKey) {
    try {
      WsProjectResponse response = WsProjectResponse.parseFrom(is);

      Table fileDataTable = HashBasedTable.create();
      Table settings = HashBasedTable.create();

      Map settingsByModule = response.getSettingsByModule();
      for (Map.Entry e1 : settingsByModule.entrySet()) {
        for (Map.Entry e2 : e1.getValue().getSettings().entrySet()) {
          settings.put(e1.getKey(), e2.getKey(), e2.getValue());
        }
      }

      Map fileDataByModuleAndPath = response.getFileDataByModuleAndPath();
      for (Map.Entry e1 : fileDataByModuleAndPath.entrySet()) {
        for (Map.Entry e2 : e1.getValue().getFileDataByPath().entrySet()) {
          FileData fd = new FileData(e2.getValue().getHash(), e2.getValue().getRevision());
          fileDataTable.put(e1.getKey(), e2.getKey(), fd);
        }
      }

      return new ProjectRepositories(settings, fileDataTable, new Date(response.getLastAnalysisDate()));
    } catch (IOException e) {
      throw new IllegalStateException("Couldn't load project repository for " + projectKey, e);
    } finally {
      IOUtils.closeQuietly(is);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy