From 271bc37b16625c393d47503a235a626645d77641 Mon Sep 17 00:00:00 2001 From: Duarte Meneses Date: Fri, 2 Dec 2022 17:08:38 -0600 Subject: [PATCH] SONAR-17722 Scanner doesn't need to download list of PRs --- .../scan/SpringProjectScanContainer.java | 2 - .../branch/BranchConfigurationLoader.java | 2 +- .../branch/BranchConfigurationProvider.java | 5 +- .../scan/branch/ProjectPullRequests.java | 50 ------------ .../branch/ProjectPullRequestsLoader.java | 32 -------- .../branch/ProjectPullRequestsProvider.java | 46 ----------- .../scanner/scan/branch/PullRequestInfo.java | 59 -------------- .../mediumtest/ScannerMediumTester.java | 7 +- .../BranchConfigurationProviderTest.java | 7 +- .../ProjectPullRequestsProviderTest.java | 61 --------------- .../scan/branch/ProjectPullRequestsTest.java | 77 ------------------- 11 files changed, 7 insertions(+), 341 deletions(-) delete mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectPullRequests.java delete mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectPullRequestsLoader.java delete mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectPullRequestsProvider.java delete mode 100644 sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/PullRequestInfo.java delete mode 100644 sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectPullRequestsProviderTest.java delete mode 100644 sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectPullRequestsTest.java diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringProjectScanContainer.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringProjectScanContainer.java index 38198a08e42..cd20aa1f1e3 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringProjectScanContainer.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/SpringProjectScanContainer.java @@ -110,7 +110,6 @@ import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.branch.BranchConfigurationProvider; import org.sonar.scanner.scan.branch.BranchType; import org.sonar.scanner.scan.branch.ProjectBranchesProvider; -import org.sonar.scanner.scan.branch.ProjectPullRequestsProvider; import org.sonar.scanner.scan.filesystem.DefaultProjectFileSystem; import org.sonar.scanner.scan.filesystem.FileIndexer; import org.sonar.scanner.scan.filesystem.InputComponentStore; @@ -171,7 +170,6 @@ public class SpringProjectScanContainer extends SpringComponentContainer { new RulesProvider(), new BranchConfigurationProvider(), new ProjectBranchesProvider(), - new ProjectPullRequestsProvider(), ProjectRepositoriesProvider.class, new ProjectServerSettingsProvider(), AnalysisCacheEnabled.class, diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfigurationLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfigurationLoader.java index 4bd974ec68d..d0c341e84bd 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfigurationLoader.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfigurationLoader.java @@ -24,5 +24,5 @@ import org.sonar.api.scanner.ScannerSide; @ScannerSide public interface BranchConfigurationLoader { - BranchConfiguration load(Map projectSettings, ProjectBranches branches, ProjectPullRequests pullRequests); + BranchConfiguration load(Map projectSettings, ProjectBranches branches); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfigurationProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfigurationProvider.java index 87287fadd29..02da2514ff6 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfigurationProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfigurationProvider.java @@ -32,13 +32,12 @@ public class BranchConfigurationProvider { private static final String LOG_MSG = "Load branch configuration"; @Bean("BranchConfiguration") - public BranchConfiguration provide(@Nullable BranchConfigurationLoader loader, ProjectConfiguration projectConfiguration, - ProjectBranches branches, ProjectPullRequests pullRequests) { + public BranchConfiguration provide(@Nullable BranchConfigurationLoader loader, ProjectConfiguration projectConfiguration, ProjectBranches branches) { if (loader == null) { return new DefaultBranchConfiguration(); } else { Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - BranchConfiguration branchConfiguration = loader.load(projectConfiguration.getProperties(), branches, pullRequests); + BranchConfiguration branchConfiguration = loader.load(projectConfiguration.getProperties(), branches); profiler.stopInfo(); return branchConfiguration; } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectPullRequests.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectPullRequests.java deleted file mode 100644 index e79b22f1624..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectPullRequests.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.scan.branch; - -import java.util.List; -import java.util.Map; -import java.util.function.BinaryOperator; -import java.util.function.Function; -import java.util.stream.Collectors; -import javax.annotation.CheckForNull; -import javax.annotation.concurrent.Immutable; - -/** - * Container class for information about the pull requests of a project. - */ -@Immutable -public class ProjectPullRequests { - private static final BinaryOperator PICK_MOST_RECENT_ANALYSIS = (a, b) -> a.getAnalysisDate() < b.getAnalysisDate() ? b : a; - private final Map pullRequestsByBranchName; - - public ProjectPullRequests(List pullRequestInfos) { - this.pullRequestsByBranchName = pullRequestInfos.stream().collect(Collectors.toMap(PullRequestInfo::getBranch, Function.identity(), PICK_MOST_RECENT_ANALYSIS)); - } - - @CheckForNull - public PullRequestInfo get(String branch) { - return pullRequestsByBranchName.get(branch); - } - - public boolean isEmpty() { - return pullRequestsByBranchName.isEmpty(); - } -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectPullRequestsLoader.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectPullRequestsLoader.java deleted file mode 100644 index 164a8d714b3..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectPullRequestsLoader.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.scan.branch; - -import org.sonar.api.scanner.ScannerSide; - -@ScannerSide -public interface ProjectPullRequestsLoader { - - /** - * Load the pull requests of a project. - */ - ProjectPullRequests load(String projectKey); - -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectPullRequestsProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectPullRequestsProvider.java deleted file mode 100644 index 0b2ebde0fa5..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectPullRequestsProvider.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.scan.branch; - -import java.util.Collections; -import javax.annotation.Nullable; -import org.sonar.api.utils.log.Logger; -import org.sonar.api.utils.log.Loggers; -import org.sonar.api.utils.log.Profiler; -import org.sonar.scanner.bootstrap.ScannerProperties; -import org.springframework.context.annotation.Bean; - -public class ProjectPullRequestsProvider { - - private static final Logger LOG = Loggers.get(ProjectPullRequestsProvider.class); - private static final String LOG_MSG = "Load project pull requests"; - - @Bean("ProjectPullRequests") - public ProjectPullRequests provide(@Nullable ProjectPullRequestsLoader loader, ScannerProperties scannerProperties) { - if (loader == null) { - return new ProjectPullRequests(Collections.emptyList()); - } - - Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - ProjectPullRequests pullRequests = loader.load(scannerProperties.getProjectKey()); - profiler.stopInfo(); - return pullRequests; - } -} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/PullRequestInfo.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/PullRequestInfo.java deleted file mode 100644 index b30742cf2fc..00000000000 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/PullRequestInfo.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.scan.branch; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; - -/** - * Container class for information about a pull request. - */ -@Immutable -public class PullRequestInfo { - private final String key; - private final String branch; - private final String base; - private final long analysisDate; - - public PullRequestInfo(String key, String branch, @Nullable String base, long analysisDate) { - this.key = key; - this.branch = branch; - this.base = base; - this.analysisDate = analysisDate; - } - - public String getKey() { - return key; - } - - public String getBranch() { - return branch; - } - - @CheckForNull - public String getBase() { - return base; - } - - public long getAnalysisDate() { - return analysisDate; - } -} diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java index e694c35a702..f9774c09fe6 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/mediumtest/ScannerMediumTester.java @@ -46,7 +46,6 @@ import org.sonar.api.SonarProduct; import org.sonar.api.SonarQubeSide; import org.sonar.api.SonarRuntime; import org.sonar.api.batch.rule.LoadedActiveRule; -import org.sonar.api.batch.sensor.cache.ReadCache; import org.sonar.api.impl.server.RulesDefinitionContext; import org.sonar.api.measures.CoreMetrics; import org.sonar.api.measures.Metric; @@ -60,8 +59,6 @@ import org.sonar.batch.bootstrapper.EnvironmentInformation; import org.sonar.batch.bootstrapper.LogOutput; import org.sonar.scanner.bootstrap.GlobalAnalysisMode; import org.sonar.scanner.cache.AnalysisCacheLoader; -import org.sonar.scanner.cache.AnalysisCacheMemoryStorage; -import org.sonar.scanner.cache.ReadCacheImpl; import org.sonar.scanner.protocol.internal.ScannerInternal; import org.sonar.scanner.report.CeTaskReportDataHolder; import org.sonar.scanner.repository.FileData; @@ -81,11 +78,9 @@ import org.sonar.scanner.scan.branch.BranchConfiguration; import org.sonar.scanner.scan.branch.BranchConfigurationLoader; import org.sonar.scanner.scan.branch.BranchType; import org.sonar.scanner.scan.branch.ProjectBranches; -import org.sonar.scanner.scan.branch.ProjectPullRequests; import org.sonarqube.ws.NewCodePeriods; import org.sonarqube.ws.Qualityprofiles.SearchWsResponse.QualityProfile; import org.sonarqube.ws.Rules.ListResponse.Rule; -import org.springframework.context.annotation.Bean; import static java.util.Collections.emptySet; @@ -502,7 +497,7 @@ public class ScannerMediumTester extends ExternalResource { @Priority(1) private class FakeBranchConfigurationLoader implements BranchConfigurationLoader { @Override - public BranchConfiguration load(Map projectSettings, ProjectBranches branches, ProjectPullRequests pullRequests) { + public BranchConfiguration load(Map projectSettings, ProjectBranches branches) { return branchConfiguration; } } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/BranchConfigurationProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/BranchConfigurationProviderTest.java index 9ce2b0e1bd8..3ae90563b91 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/BranchConfigurationProviderTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/BranchConfigurationProviderTest.java @@ -39,7 +39,6 @@ public class BranchConfigurationProviderTest { private BranchConfigurationLoader loader = mock(BranchConfigurationLoader.class); private BranchConfiguration config = mock(BranchConfiguration.class); private ProjectBranches branches = mock(ProjectBranches.class); - private ProjectPullRequests pullRequests = mock(ProjectPullRequests.class); private ProjectReactor reactor = mock(ProjectReactor.class); private Map projectSettings = new HashMap<>(); private ProjectDefinition root = mock(ProjectDefinition.class); @@ -53,16 +52,16 @@ public class BranchConfigurationProviderTest { @Test public void should_use_loader() { - when(loader.load(eq(projectSettings), eq(branches), eq(pullRequests))).thenReturn(config); + when(loader.load(eq(projectSettings), eq(branches))).thenReturn(config); - BranchConfiguration result = provider.provide(loader, projectConfiguration, branches, pullRequests); + BranchConfiguration result = provider.provide(loader, projectConfiguration, branches); assertThat(result).isSameAs(config); } @Test public void should_return_default_if_no_loader() { - BranchConfiguration result = provider.provide(null, projectConfiguration, branches, pullRequests); + BranchConfiguration result = provider.provide(null, projectConfiguration, branches); assertThat(result.targetBranchName()).isNull(); assertThat(result.branchType()).isEqualTo(BranchType.BRANCH); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectPullRequestsProviderTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectPullRequestsProviderTest.java deleted file mode 100644 index 5749445200b..00000000000 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectPullRequestsProviderTest.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.scan.branch; - -import org.junit.Before; -import org.junit.Test; -import org.sonar.scanner.bootstrap.ScannerProperties; - -import static java.util.Collections.emptyList; -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -public class ProjectPullRequestsProviderTest { - private ProjectPullRequestsProvider provider = new ProjectPullRequestsProvider(); - private ProjectPullRequestsLoader mockLoader; - private ProjectPullRequests pullRequests; - private ScannerProperties scannerProperties; - - @Before - public void setUp() { - mockLoader = mock(ProjectPullRequestsLoader.class); - pullRequests = new ProjectPullRequests(emptyList()); - scannerProperties = mock(ScannerProperties.class); - } - - @Test - public void should_use_loader() { - when(scannerProperties.getProjectKey()).thenReturn("key"); - when(mockLoader.load("key")).thenReturn(pullRequests); - - ProjectPullRequests result = provider.provide(mockLoader, scannerProperties); - - assertThat(result).isSameAs(pullRequests); - } - - @Test - public void should_return_default_if_no_loader() { - when(scannerProperties.getProjectKey()).thenReturn("project"); - ProjectPullRequests result = provider.provide(null, scannerProperties); - - assertThat(result.isEmpty()).isTrue(); - } -} diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectPullRequestsTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectPullRequestsTest.java deleted file mode 100644 index 92b464176fa..00000000000 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/branch/ProjectPullRequestsTest.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2022 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.scan.branch; - -import java.util.Arrays; -import java.util.concurrent.atomic.AtomicInteger; -import org.junit.*; - -import static org.assertj.core.api.Assertions.assertThat; - -public class ProjectPullRequestsTest { - - private static AtomicInteger counter = new AtomicInteger(); - - @Test - public void should_pick_the_latest_branch_by_analysis_date() { - String branchName = "dummyBranch"; - long date = 1000; - - PullRequestInfo pr1 = newPullRequestInfo(branchName, date - 2); - PullRequestInfo pr2 = newPullRequestInfo(branchName, date - 1); - PullRequestInfo latest = newPullRequestInfo(branchName, date); - - PullRequestInfo pullRequestInfo = getPullRequestInfo(branchName, pr1, pr2, latest); - assertThat(pullRequestInfo.getKey()).isEqualTo(latest.getKey()); - assertThat(pullRequestInfo.getAnalysisDate()).isEqualTo(date); - } - - @Test - public void should_not_crash_when_cannot_pick_a_unique_branch() { - String branchName = "dummyBranch"; - long date = 1000; - - PullRequestInfo pr1 = newPullRequestInfo(branchName, date); - PullRequestInfo pr2 = newPullRequestInfo(branchName, date); - - PullRequestInfo pullRequestInfo = getPullRequestInfo(branchName, pr1, pr2); - assertThat(pullRequestInfo.getAnalysisDate()).isEqualTo(date); - } - - @Test - public void should_get_correct_branch() { - long date = 1000; - - PullRequestInfo foo = newPullRequestInfo("foo", date); - PullRequestInfo bar = newPullRequestInfo("bar", date); - - assertThat(getPullRequestInfo("foo", foo, bar).getBranch()).isEqualTo("foo"); - assertThat(getPullRequestInfo("bar", foo, bar).getBranch()).isEqualTo("bar"); - } - - private PullRequestInfo newPullRequestInfo(String branchName, long date) { - return new PullRequestInfo("pr" + counter.incrementAndGet(), branchName, null, date); - } - - private PullRequestInfo getPullRequestInfo(String branchName, PullRequestInfo ...prs) { - return new ProjectPullRequests(Arrays.asList(prs)).get(branchName); - } - -} -- 2.39.5