diff options
author | Michal Duda <michalno1@gmail.com> | 2018-11-22 18:09:44 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2019-01-16 09:43:02 +0100 |
commit | cf1c8d76184de334921362e9abd7dd64d56e5f16 (patch) | |
tree | 8daacd2319f8f93c5a0858638c040551b38ae386 /sonar-scanner-engine/src/main/java/org | |
parent | e4c0b858c7e6807801c6481d995ca11682c58fad (diff) | |
download | sonarqube-cf1c8d76184de334921362e9abd7dd64d56e5f16.tar.gz sonarqube-cf1c8d76184de334921362e9abd7dd64d56e5f16.zip |
SONAR-11507 WS /batch/project new file structure
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org')
6 files changed, 133 insertions, 48 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java index 043264de8b3..271b84bceee 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/DefaultProjectRepositoriesLoader.java @@ -20,12 +20,11 @@ package org.sonar.scanner.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.HashMap; import java.util.Map; import javax.annotation.Nullable; import org.apache.commons.io.IOUtils; @@ -34,9 +33,7 @@ import org.slf4j.LoggerFactory; import org.sonar.api.utils.MessageException; import org.sonar.scanner.bootstrap.ScannerWsClient; import org.sonar.scanner.util.ScannerUtils; -import org.sonarqube.ws.Batch; import org.sonarqube.ws.Batch.WsProjectResponse; -import org.sonarqube.ws.Batch.WsProjectResponse.FileDataByPath; import org.sonarqube.ws.client.GetRequest; import org.sonarqube.ws.client.HttpException; import org.sonarqube.ws.client.WsResponse; @@ -62,7 +59,7 @@ public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoad } LOG.debug("Project repository not available - continuing without it"); - return new ProjectRepositories(); + return new SingleProjectRepository(); } } @@ -97,22 +94,32 @@ public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoad private static ProjectRepositories processStream(InputStream is, String projectKey) { try { WsProjectResponse response = WsProjectResponse.parseFrom(is); - - Table<String, String, FileData> fileDataTable = HashBasedTable.create(); - - Map<String, FileDataByPath> fileDataByModuleAndPath = response.getFileDataByModuleAndPath(); - for (Map.Entry<String, FileDataByPath> e1 : fileDataByModuleAndPath.entrySet()) { - for (Map.Entry<String, Batch.WsProjectResponse.FileData> e2 : e1.getValue().getFileDataByPath().entrySet()) { - FileData fd = new FileData(e2.getValue().getHash(), e2.getValue().getRevision()); - fileDataTable.put(e1.getKey(), e2.getKey(), fd); - } + if (response.getFileDataByModuleAndPathCount() == 0) { + return new SingleProjectRepository(constructFileDataMap(response.getFileDataByPathMap()), new Date(response.getLastAnalysisDate())); + } else { + final Map<String, SingleProjectRepository> repositoriesPerModule = new HashMap<>(); + response.getFileDataByModuleAndPathMap().keySet().forEach(moduleKey -> { + WsProjectResponse.FileDataByPath filePaths = response.getFileDataByModuleAndPathMap().get(moduleKey); + repositoriesPerModule.put(moduleKey, new SingleProjectRepository( + constructFileDataMap(filePaths.getFileDataByPathMap()), new Date(response.getLastAnalysisDate()))); + }); + return new MultiModuleProjectRepository(repositoriesPerModule, new Date(response.getLastAnalysisDate())); } - return new ProjectRepositories(fileDataTable, new Date(response.getLastAnalysisDate())); } catch (IOException e) { throw new IllegalStateException("Couldn't load project repository for " + projectKey, e); } finally { IOUtils.closeQuietly(is); } } + + private static Map<String, FileData> constructFileDataMap(Map<String, WsProjectResponse.FileData> content) { + Map<String, FileData> fileDataMap = new HashMap<>(); + content.forEach((key, value) -> { + FileData fd = new FileData(value.getHash(), value.getRevision()); + fileDataMap.put(key, fd); + }); + + return fileDataMap; + } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/MultiModuleProjectRepository.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/MultiModuleProjectRepository.java new file mode 100644 index 00000000000..6876d638547 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/MultiModuleProjectRepository.java @@ -0,0 +1,45 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info 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.scanner.repository; + +import com.google.common.collect.ImmutableMap; +import java.util.Date; +import java.util.Map; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import javax.annotation.concurrent.Immutable; + +@Immutable +public class MultiModuleProjectRepository extends ProjectRepositories { + + private Map<String, SingleProjectRepository> repositoriesPerModule; + + public MultiModuleProjectRepository(Map<String, SingleProjectRepository> repositoriesPerModule, @Nullable Date lastAnalysisDate) { + super(lastAnalysisDate, true); + this.repositoriesPerModule = ImmutableMap.copyOf(repositoriesPerModule); + } + + @CheckForNull + public FileData fileData(String moduleKeyWithBranch, String path) { + SingleProjectRepository repository = repositoriesPerModule.get(moduleKeyWithBranch); + return repository == null ? null : repository.fileData(path); + } + +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositories.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositories.java index bed6c1c2c34..e4cce89c392 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositories.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/ProjectRepositories.java @@ -19,48 +19,33 @@ */ package org.sonar.scanner.repository; -import com.google.common.collect.ImmutableTable; -import com.google.common.collect.Table; import java.util.Date; -import java.util.Map; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; +import org.sonar.api.batch.fs.internal.DefaultInputFile; @Immutable -public class ProjectRepositories { - private final ImmutableTable<String, String, FileData> fileDataByModuleAndPath; +public abstract class ProjectRepositories { private final Date lastAnalysisDate; private final boolean exists; - public ProjectRepositories() { - this.exists = false; - this.fileDataByModuleAndPath = new ImmutableTable.Builder<String, String, FileData>().build(); - this.lastAnalysisDate = null; - } - - public ProjectRepositories(Table<String, String, FileData> fileDataByModuleAndPath, - @Nullable Date lastAnalysisDate) { - this.fileDataByModuleAndPath = ImmutableTable.copyOf(fileDataByModuleAndPath); + public ProjectRepositories(@Nullable Date lastAnalysisDate, boolean exists) { this.lastAnalysisDate = lastAnalysisDate; - this.exists = true; + this.exists = exists; } public boolean exists() { return exists; } - public Map<String, FileData> fileDataByPath(String moduleKey) { - return fileDataByModuleAndPath.row(moduleKey); - } - - public Table<String, String, FileData> fileDataByModuleAndPath() { - return fileDataByModuleAndPath; - } - @CheckForNull - public FileData fileData(String projectKeyWithBranch, String path) { - return fileDataByModuleAndPath.get(projectKeyWithBranch, path); + public FileData fileData(String moduleKeyWithBranch, DefaultInputFile inputFile) { + if (this instanceof SingleProjectRepository) { + return ((SingleProjectRepository) this).fileData(inputFile.getProjectRelativePath()); + } else { + return ((MultiModuleProjectRepository) this).fileData(moduleKeyWithBranch, inputFile.getModuleRelativePath()); + } } @CheckForNull diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/SingleProjectRepository.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/SingleProjectRepository.java new file mode 100644 index 00000000000..0d9637cf2e3 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/SingleProjectRepository.java @@ -0,0 +1,49 @@ +/* + * SonarQube + * Copyright (C) 2009-2018 SonarSource SA + * mailto:info 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.scanner.repository; + +import com.google.common.collect.ImmutableMap; +import java.util.Date; +import java.util.Map; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import javax.annotation.concurrent.Immutable; + +@Immutable +public class SingleProjectRepository extends ProjectRepositories { + + private final ImmutableMap<String, FileData> fileDataByPath; + + public SingleProjectRepository() { + super(null, false); + this.fileDataByPath = ImmutableMap.<String, FileData>builder().build(); + } + + public SingleProjectRepository(Map<String, FileData> fileDataByPath, + @Nullable Date lastAnalysisDate) { + super(lastAnalysisDate, true); + this.fileDataByPath = ImmutableMap.copyOf(fileDataByPath); + } + + @CheckForNull + public FileData fileData(String path) { + return fileDataByPath.get(path); + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/StatusDetection.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/StatusDetection.java index 2554bf6b2fe..dedb65043c7 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/StatusDetection.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/StatusDetection.java @@ -34,7 +34,6 @@ import static org.sonar.api.batch.fs.InputFile.Status.SAME; @Immutable public class StatusDetection { - private final ProjectRepositories projectRepositories; private final ScmChangedFiles scmChangedFiles; @@ -43,8 +42,8 @@ public class StatusDetection { this.scmChangedFiles = scmChangedFiles; } - InputFile.Status status(String projectKeyWithBranch, DefaultInputFile inputFile, String hash) { - FileData fileDataPerPath = projectRepositories.fileData(projectKeyWithBranch, inputFile.relativePath()); + InputFile.Status status(String moduleKeyWithBranch, DefaultInputFile inputFile, String hash) { + FileData fileDataPerPath = projectRepositories.fileData(moduleKeyWithBranch, inputFile); if (fileDataPerPath == null) { return checkChanged(ADDED, inputFile); } @@ -64,8 +63,8 @@ public class StatusDetection { * @return null if it was not possible to get the status without calculating metadata */ @CheckForNull - public InputFile.Status getStatusWithoutMetadata(String projectKeyWithBranch, DefaultInputFile inputFile) { - FileData fileDataPerPath = projectRepositories.fileData(projectKeyWithBranch, inputFile.relativePath()); + public InputFile.Status getStatusWithoutMetadata(String moduleKeyWithBranch, DefaultInputFile inputFile) { + FileData fileDataPerPath = projectRepositories.fileData(moduleKeyWithBranch, inputFile); if (fileDataPerPath == null) { return checkChanged(ADDED, inputFile); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java index 78184f1dfb1..424f3ee1459 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scm/ScmPublisher.java @@ -27,8 +27,8 @@ import org.sonar.api.batch.InstantiationStrategy; import org.sonar.api.batch.ScannerSide; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputFile.Status; -import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.AbstractProjectOrModule; +import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.scm.ScmProvider; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; @@ -57,7 +57,7 @@ public final class ScmPublisher { private final BranchConfiguration branchConfiguration; public ScmPublisher(AbstractProjectOrModule inputModule, ScmConfiguration configuration, ProjectRepositories projectRepositories, - ModuleInputComponentStore componentStore, DefaultModuleFileSystem fs, ReportPublisher reportPublisher, BranchConfiguration branchConfiguration) { + ModuleInputComponentStore componentStore, DefaultModuleFileSystem fs, ReportPublisher reportPublisher, BranchConfiguration branchConfiguration) { this.inputModule = inputModule; this.configuration = configuration; this.projectRepositories = projectRepositories; @@ -108,7 +108,7 @@ public final class ScmPublisher { addIfNotEmpty(filesToBlame, f); } else if (!branchConfiguration.isShortOrPullRequest()) { // File status is SAME so that mean fileData exists - FileData fileData = projectRepositories.fileData(inputModule.definition().getKeyWithBranch(), inputFile.getModuleRelativePath()); + FileData fileData = projectRepositories.fileData(inputModule.definition().getKeyWithBranch(), inputFile); if (StringUtils.isEmpty(fileData.revision())) { addIfNotEmpty(filesToBlame, f); } else { |