From 2cf35e5bd9752e86cd34457e4bb58548e036d709 Mon Sep 17 00:00:00 2001 From: Matteo Mara Date: Thu, 21 Dec 2023 14:24:42 +0100 Subject: [PATCH] SONAR-21195 Fetch publishAllFiles value from the languages registered by the plugins --- .../mediumtest/ScannerMediumTester.java | 6 +- .../mediumtest/FakeLanguagesLoader.java | 24 +----- .../mediumtest/FakeLanguagesProvider.java | 75 +++++++++++++++++++ .../scanner/scan/filesystem/FileIndexer.java | 16 +++- 4 files changed, 96 insertions(+), 25 deletions(-) create mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java diff --git a/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java b/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java index 861f1b1d207..41d49b205e0 100644 --- a/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java +++ b/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java @@ -107,6 +107,7 @@ public class ScannerMediumTester extends ExternalResource { private final FakeSonarRuntime sonarRuntime = new FakeSonarRuntime(); private final CeTaskReportDataHolder reportMetadataHolder = new CeTaskReportDataHolderExt(); private final FakeLanguagesLoader languagesLoader = new FakeLanguagesLoader(); + private final FakeLanguagesProvider languagesProvider = new FakeLanguagesProvider(); private LogOutput logOutput = null; private static void createWorkingDirs() throws IOException { @@ -287,10 +288,12 @@ public class ScannerMediumTester extends ExternalResource { public void addLanguage(String key, String name, String... suffixes) { languagesLoader.addLanguage(key, name, suffixes, new String[0]); + languagesProvider.addLanguage(key, name, true); } public void addLanguage(String key, String name, boolean publishAllFiles, String... suffixes) { - languagesLoader.addLanguage(key, name, suffixes, new String[0], publishAllFiles); + languagesLoader.addLanguage(key, name, suffixes, new String[0]); + languagesProvider.addLanguage(key, name, publishAllFiles); } public static class AnalysisBuilder { @@ -325,6 +328,7 @@ public class ScannerMediumTester extends ExternalResource { tester.sonarRuntime, tester.reportMetadataHolder, tester.languagesLoader, + tester.languagesProvider, result); if (tester.logOutput != null) { builder.setLogOutput(tester.logOutput); diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java index d7b481a1113..f2e2f48d556 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesLoader.java @@ -33,12 +33,12 @@ public class FakeLanguagesLoader implements LanguagesLoader { private final Map languageMap = new HashMap<>(); public FakeLanguagesLoader() { - languageMap.put("xoo", new Language(new FakeLanguage("xoo", "xoo", new String[] { ".xoo" }, new String[0], true))); + languageMap.put("xoo", new Language(new SupportedLanguageDto("xoo", "xoo", new String[] { ".xoo" }, new String[0]))); } public FakeLanguagesLoader(Languages languages) { for (org.sonar.api.resources.Language language : languages.all()) { - languageMap.put(language.getKey(), new Language(new FakeLanguage(language.getKey(), language.getName(), language.getFileSuffixes(), language.filenamePatterns(), true))); + languageMap.put(language.getKey(), new Language(new SupportedLanguageDto(language.getKey(), language.getName(), language.getFileSuffixes(), language.filenamePatterns()))); } } @Override @@ -47,25 +47,7 @@ public class FakeLanguagesLoader implements LanguagesLoader { } public void addLanguage(String key, String name, String[] suffixes, String[] filenamePatterns) { - languageMap.put(key, new Language(new FakeLanguage(key, name, suffixes, filenamePatterns, true))); + languageMap.put(key, new Language(new SupportedLanguageDto(key, name, suffixes, filenamePatterns))); } - public void addLanguage(String key, String name, String[] suffixes, String[] filenamePatterns, boolean publishAllFiles) { - languageMap.put(key, new Language(new FakeLanguage(key, name, suffixes, filenamePatterns, publishAllFiles))); - } - - private static class FakeLanguage extends SupportedLanguageDto { - - private final boolean publishAllFiles; - - public FakeLanguage(String key, String name, String[] fileSuffixes, String[] filenamePatterns, boolean publishAllFiles) { - super(key, name, fileSuffixes, filenamePatterns); - this.publishAllFiles = publishAllFiles; - } - - @Override - public boolean publishAllFiles() { - return publishAllFiles; - } - } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java new file mode 100644 index 00000000000..60097466670 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/mediumtest/FakeLanguagesProvider.java @@ -0,0 +1,75 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.mediumtest; + +import javax.annotation.Priority; +import org.sonar.api.resources.Language; +import org.sonar.api.resources.Languages; +import org.springframework.context.annotation.Bean; + +@Priority(1) +public class FakeLanguagesProvider { + + private Languages languages = new Languages(); + + @Bean("Languages") + public Languages provide() { + return this.languages; + } + + public void addLanguage(String key, String name, boolean publishAllFiles) { + this.languages.add(new FakeLanguage(key, name, publishAllFiles)); + } + + private static class FakeLanguage implements Language { + + private final String name; + private final String key; + private final boolean publishAllFiles; + + public FakeLanguage(String key, String name, boolean publishAllFiles) { + this.name = name; + this.key = key; + this.publishAllFiles = publishAllFiles; + } + + @Override + public String getKey() { + return this.key; + } + + @Override + public String getName() { + return this.name; + } + + @Override + public String[] getFileSuffixes() { + return new String[0]; + } + + @Override + public boolean publishAllFiles() { + return this.publishAllFiles; + } + } + + +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java index 30687416d14..9b84a719520 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/FileIndexer.java @@ -32,6 +32,7 @@ import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.fs.internal.DefaultInputModule; import org.sonar.api.batch.fs.internal.DefaultInputProject; import org.sonar.api.batch.fs.internal.SensorStrategy; +import org.sonar.api.resources.Languages; import org.sonar.api.utils.MessageException; import org.sonar.scanner.issue.ignore.scanner.IssueExclusionsLoader; import org.sonar.scanner.repository.language.Language; @@ -59,14 +60,15 @@ public class FileIndexer { private final LanguageDetection langDetection; private final StatusDetection statusDetection; private final ScmChangedFiles scmChangedFiles; - private final ModuleRelativePathWarner moduleRelativePathWarner; private final InputFileFilterRepository inputFileFilterRepository; + private final Languages languages; public FileIndexer(DefaultInputProject project, ScannerComponentIdGenerator scannerComponentIdGenerator, InputComponentStore componentStore, ProjectCoverageAndDuplicationExclusions projectCoverageAndDuplicationExclusions, IssueExclusionsLoader issueExclusionsLoader, MetadataGenerator metadataGenerator, SensorStrategy sensorStrategy, LanguageDetection languageDetection, ScanProperties properties, - ScmChangedFiles scmChangedFiles, StatusDetection statusDetection, ModuleRelativePathWarner moduleRelativePathWarner, InputFileFilterRepository inputFileFilterRepository) { + ScmChangedFiles scmChangedFiles, StatusDetection statusDetection, ModuleRelativePathWarner moduleRelativePathWarner, + InputFileFilterRepository inputFileFilterRepository, Languages languages) { this.project = project; this.scannerComponentIdGenerator = scannerComponentIdGenerator; this.componentStore = componentStore; @@ -80,6 +82,7 @@ public class FileIndexer { this.statusDetection = statusDetection; this.moduleRelativePathWarner = moduleRelativePathWarner; this.inputFileFilterRepository = inputFileFilterRepository; + this.languages = languages; } void indexFile(DefaultInputModule module, ModuleCoverageAndDuplicationExclusions moduleCoverageAndDuplicationExclusions, Path sourceFile, @@ -104,7 +107,7 @@ public class FileIndexer { DefaultInputFile inputFile = new DefaultInputFile(indexedFile, f -> metadataGenerator.setMetadata(module.key(), f, module.getEncoding()), f -> f.setStatus(statusDetection.findStatusFromScm(f))); - if (language != null && language.isPublishAllFiles()) { + if (language != null && isPublishAllFiles(language.key())) { inputFile.setPublished(true); } if (!accept(inputFile)) { @@ -126,6 +129,13 @@ public class FileIndexer { progressReport.message(count + " " + pluralizeFiles(count) + " indexed... (last one was " + inputFile.getProjectRelativePath() + ")"); } + private boolean isPublishAllFiles(String languageKey) { + if (languages.get(languageKey) != null) { + return languages.get(languageKey).publishAllFiles(); + } + return false; + } + private void checkIfAlreadyIndexed(DefaultInputFile inputFile) { if (componentStore.inputFile(inputFile.getProjectRelativePath()) != null) { throw MessageException.of("File " + inputFile + " can't be indexed twice. Please check that inclusion/exclusion patterns produce " -- 2.39.5