diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2018-02-01 15:30:48 +0100 |
---|---|---|
committer | Teryk Bellahsene <teryk@users.noreply.github.com> | 2018-03-13 14:05:36 +0100 |
commit | 751e4000e40a4af66b80767d632b1bef64dc5647 (patch) | |
tree | 0bf9735c29f6bae50ec10c11d8fbf7e50e909722 /sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch | |
parent | 23e4b51cde3ed0dc76ca1d73aa6cc31715dd25d4 (diff) | |
download | sonarqube-751e4000e40a4af66b80767d632b1bef64dc5647.tar.gz sonarqube-751e4000e40a4af66b80767d632b1bef64dc5647.zip |
MMF-1134 Make pull request a 1st class citizen
SONAR-10366 Add pull request object to api/project_branches/list and api/ce/activity
SONAR-10365 Analyze pull requests as 1st class citizen
SONAR-10366 SONAR-10367 Create WS api/projet_pull_requests/list and delete
SONAR-10383 Add Pull Request information when listing CE tasks
SONAR-10365 Add key type in PROJECT_BRANCHES (#3063)
SONAR-10371 Add pullRequest parameter in the Web API (#3076)
* ComponentFinder searches by branch or pull request
* Add pullRequest parameter to WS api/issues/* WS
* Add pullRequest parameter to api/settings/* WS
* Add pullRequest parameter to api/badges/* WS
* Add pullRequest parameter to api/components/* WS
* Add pullRequest parameter to api/sources/* WS
SONAR-10368 Copy issue states from pull request after it's merged
SONAR-10373 Send notifications for events on issues of a pull request
SONAR-10365 Add pull_request_binary column in project_branches (#3073)
SONAR-10433 Store pull request in projects table
SONAR-10371 Add pullRequest field in the Web API
SONAR-10365 Analyze pull requests as 1st class citizen
BRANCH-45 Expose issue resolution for PR decoration
BRANCH-49 Basic support of pull request analysis on pull request branch
BRANCH-47 Fail when user tries to analyze a pull request and the plugin is not available
SONAR-10366 update pull request decorated links to project and issues
SONAR-10365 Use pull request id as key instead of branch name
SONAR-10454 Update embedded Git 1.4 and SVN 1.7
SONAR-10365 rename sonar.pullrequest.id to sonar.pullrequest.key
SONAR-10383 api/navigation/component returns the pull request key
Diffstat (limited to 'sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch')
10 files changed, 222 insertions, 17 deletions
diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfiguration.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfiguration.java index 4ddcff98abd..695c3c6d62d 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfiguration.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchConfiguration.java @@ -36,8 +36,8 @@ public interface BranchConfiguration { */ BranchType branchType(); - default boolean isShortLivingBranch() { - return branchType() == BranchType.SHORT; + default boolean isShortOrPullRequest() { + return branchType() == BranchType.PULL_REQUEST || branchType() == BranchType.SHORT; } /** @@ -54,7 +54,19 @@ public interface BranchConfiguration { /** * The name of the base branch to determine project repository and changed files. + * + * Note: this is important for the scanner during the analysis of long living branches. + * For short living branches, branchBase is always the same as branchTarget. + * For long living branches, branchBase is the target in case of first analysis, + * otherwise it's the branch itself. */ @CheckForNull String branchBase(); + + /** + * The key of the pull request. + * + * @throws IllegalStateException if this branch configuration is not a pull request. + */ + String pullRequestKey(); } 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 24b57650ee9..5b845195d05 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 @@ -27,5 +27,5 @@ import org.sonar.api.batch.ScannerSide; @ScannerSide @InstantiationStrategy(InstantiationStrategy.PER_BATCH) public interface BranchConfigurationLoader { - BranchConfiguration load(Map<String, String> localSettings, Supplier<Map<String, String>> remoteSettingsSupplier, ProjectBranches branches); + BranchConfiguration load(Map<String, String> localSettings, Supplier<Map<String, String>> remoteSettingsSupplier, ProjectBranches branches, ProjectPullRequests pullRequests); } 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 08caee68ca2..276ba411c28 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 @@ -39,14 +39,14 @@ public class BranchConfigurationProvider extends ProviderAdapter { private BranchConfiguration branchConfiguration = null; public BranchConfiguration provide(@Nullable BranchConfigurationLoader loader, GlobalConfiguration globalConfiguration, ProjectKey projectKey, - SettingsLoader settingsLoader, ProjectBranches branches) { + SettingsLoader settingsLoader, ProjectBranches branches, ProjectPullRequests pullRequests) { if (branchConfiguration == null) { if (loader == null) { branchConfiguration = new DefaultBranchConfiguration(); } else { Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); Supplier<Map<String, String>> settingsSupplier = createSettingsSupplier(globalConfiguration, projectKey, settingsLoader); - branchConfiguration = loader.load(globalConfiguration.getProperties(), settingsSupplier, branches); + branchConfiguration = loader.load(globalConfiguration.getProperties(), settingsSupplier, branches, pullRequests); profiler.stopInfo(); } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchType.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchType.java index b90f633ded0..e8143ce4480 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchType.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/BranchType.java @@ -20,5 +20,5 @@ package org.sonar.scanner.scan.branch; public enum BranchType { - SHORT, LONG + SHORT, LONG, PULL_REQUEST } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/DefaultBranchConfiguration.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/DefaultBranchConfiguration.java index 045c3e3ccd4..2e048e6c689 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/DefaultBranchConfiguration.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/DefaultBranchConfiguration.java @@ -46,4 +46,9 @@ public class DefaultBranchConfiguration implements BranchConfiguration { public String branchBase() { return null; } + + @Override + public String pullRequestKey() { + throw new IllegalStateException("Only a branch of type PULL_REQUEST can have a pull request id."); + } } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranchesProvider.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranchesProvider.java index 2a59148dbd0..043360fb64f 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranchesProvider.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectBranchesProvider.java @@ -26,8 +26,6 @@ import org.sonar.api.batch.bootstrap.ProjectKey; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; import org.sonar.api.utils.log.Profiler; -import org.sonar.core.config.ScannerProperties; -import org.sonar.scanner.bootstrap.GlobalConfiguration; public class ProjectBranchesProvider extends ProviderAdapter { @@ -36,16 +34,19 @@ public class ProjectBranchesProvider extends ProviderAdapter { private ProjectBranches branches = null; - public ProjectBranches provide(@Nullable ProjectBranchesLoader loader, ProjectKey projectKey, GlobalConfiguration settings) { - if (branches == null) { - if (loader == null || !settings.get(ScannerProperties.BRANCH_NAME).isPresent()) { - branches = new ProjectBranches(Collections.emptyList()); - } else { - Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); - branches = loader.load(projectKey.get()); - profiler.stopInfo(); - } + public ProjectBranches provide(@Nullable ProjectBranchesLoader loader, ProjectKey projectKey) { + if (branches != null) { + return branches; } + + if (loader == null) { + branches = new ProjectBranches(Collections.emptyList()); + return branches; + } + + Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); + branches = loader.load(projectKey.get()); + profiler.stopInfo(); return branches; } } 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 new file mode 100644 index 00000000000..a9f4c2c3d07 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectPullRequests.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.scan.branch; + +import java.util.List; +import java.util.Map; +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 final Map<String, PullRequestInfo> pullRequestsById; + + public ProjectPullRequests(List<PullRequestInfo> pullRequestsById) { + this.pullRequestsById = pullRequestsById.stream().collect(Collectors.toMap(PullRequestInfo::getBranch, Function.identity())); + } + + @CheckForNull + public PullRequestInfo get(String branch) { + return pullRequestsById.get(branch); + } + + public boolean isEmpty() { + return pullRequestsById.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 new file mode 100644 index 00000000000..525afd56f38 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectPullRequestsLoader.java @@ -0,0 +1,34 @@ +/* + * 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.scan.branch; + +import org.sonar.api.batch.InstantiationStrategy; +import org.sonar.api.batch.ScannerSide; + +@ScannerSide +@InstantiationStrategy(InstantiationStrategy.PER_BATCH) +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 new file mode 100644 index 00000000000..15023c8c2fc --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/ProjectPullRequestsProvider.java @@ -0,0 +1,51 @@ +/* + * 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.scan.branch; + +import java.util.Collections; +import org.picocontainer.injectors.ProviderAdapter; +import org.sonar.api.batch.bootstrap.ProjectKey; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.api.utils.log.Profiler; + +public class ProjectPullRequestsProvider extends ProviderAdapter { + + private static final Logger LOG = Loggers.get(ProjectPullRequestsProvider.class); + private static final String LOG_MSG = "Load project pull requests"; + + private ProjectPullRequests pullRequests = null; + + public ProjectPullRequests provide(@org.picocontainer.annotations.Nullable ProjectPullRequestsLoader loader, ProjectKey projectKey) { + if (pullRequests != null) { + return pullRequests; + } + + if (loader == null) { + pullRequests = new ProjectPullRequests(Collections.emptyList()); + return pullRequests; + } + + Profiler profiler = Profiler.create(LOG).startInfo(LOG_MSG); + pullRequests = loader.load(projectKey.get()); + 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 new file mode 100644 index 00000000000..6ccc3656fb5 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/branch/PullRequestInfo.java @@ -0,0 +1,53 @@ +/* + * 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.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 id; + private final String branch; + private final String base; + + public PullRequestInfo(String id, String branch, @Nullable String base) { + this.id = id; + this.branch = branch; + this.base = base; + } + + public String getId() { + return id; + } + + public String getBranch() { + return branch; + } + + @CheckForNull + public String getBase() { + return base; + } +} |