-
-
Notifications
You must be signed in to change notification settings - Fork 520
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#523: Add auto-detection for all CIs in Sonarqube commercial editions
The plugin previously only provided support for auto-detecting and configuring the scanner properties for a Pull Request in Azure Devops and a Merge Request or Branch in Gitlab CI. The Sonarqube documentation also stated that Bitbucket Pipelines, Github Actions, CodeMagic, Jenkins Branch API, and Cirrus CI could also be used to auto-discover Pull Request or Branch information although the plugin did not provide these. This change adds support for detecting these additional CIs based on the various environment variables they provide, and to auto-configure Pull Request or Branch parameters in the scanner when a suitable build job is detected. Includes the general clean-up of the creation of Branch and Pull Request configuration to force fail-fast behaviour where target branches are not provided or can't be matched against known branches, to ensure the correct reference branch is selected for Pull Request analysis, and to force an error to be displayed if a user mixes Pull Rrequest and Branch parameters in their launch properties.
- Loading branch information
Showing
20 changed files
with
1,277 additions
and
337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/main/java/com/github/mc1arke/sonarqube/plugin/scanner/BranchAutoConfigurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright (C) 2021 Michael Clarke | ||
* | ||
* 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 com.github.mc1arke.sonarqube.plugin.scanner; | ||
|
||
import org.sonar.api.scanner.ScannerSide; | ||
import org.sonar.api.utils.System2; | ||
import org.sonar.scanner.scan.branch.BranchConfiguration; | ||
import org.sonar.scanner.scan.branch.ProjectBranches; | ||
|
||
import java.util.Optional; | ||
|
||
@ScannerSide | ||
public interface BranchAutoConfigurer { | ||
|
||
Optional<BranchConfiguration> detectConfiguration(System2 system, ProjectBranches projectBranches); | ||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/github/mc1arke/sonarqube/plugin/scanner/BranchConfigurationFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright (C) 2021 Michael Clarke | ||
* | ||
* 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 com.github.mc1arke.sonarqube.plugin.scanner; | ||
|
||
import org.sonar.api.scanner.ScannerSide; | ||
import org.sonar.api.utils.MessageException; | ||
import org.sonar.scanner.scan.branch.BranchConfiguration; | ||
import org.sonar.scanner.scan.branch.BranchInfo; | ||
import org.sonar.scanner.scan.branch.BranchType; | ||
import org.sonar.scanner.scan.branch.ProjectBranches; | ||
|
||
import java.util.Optional; | ||
|
||
@ScannerSide | ||
public class BranchConfigurationFactory { | ||
|
||
public BranchConfiguration createBranchConfiguration(String branchName, ProjectBranches branches) { | ||
if (branches.isEmpty()) { | ||
return new CommunityBranchConfiguration(branchName, BranchType.BRANCH, null, null, null); | ||
} | ||
|
||
String targetBranchName = branches.get(branchName) == null ? branches.defaultBranchName() : branchName; | ||
return new CommunityBranchConfiguration(branchName, BranchType.BRANCH, targetBranchName, null, null); | ||
} | ||
|
||
public BranchConfiguration createPullRequestConfiguration(String pullRequestKey, String pullRequestBranch, String pullRequestBase, ProjectBranches branches) { | ||
if (branches.isEmpty()) { | ||
return new CommunityBranchConfiguration(pullRequestBranch, BranchType.PULL_REQUEST, null, pullRequestBase, pullRequestKey); | ||
} | ||
|
||
String referenceBranch = branches.get(pullRequestBase) == null ? branches.defaultBranchName() : findReferenceBranch(pullRequestBase, branches); | ||
return new CommunityBranchConfiguration(pullRequestBranch, BranchType.PULL_REQUEST, referenceBranch, pullRequestBase, pullRequestKey); | ||
|
||
} | ||
|
||
private static String findReferenceBranch(String targetBranch, ProjectBranches branches) { | ||
BranchInfo target = Optional.ofNullable(branches.get(targetBranch)) | ||
.orElseThrow(() -> MessageException.of("No branch exists in Sonarqube with the name " + targetBranch)); | ||
|
||
if (target.type() == BranchType.BRANCH) { | ||
return targetBranch; | ||
} | ||
|
||
String targetBranchTarget = target.branchTargetName(); | ||
if (targetBranchTarget == null) { | ||
throw MessageException.of(String.format("The branch '%s' of type %s does not have a target", target.name(), target.type())); | ||
} | ||
|
||
return findReferenceBranch(targetBranchTarget, branches); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
.../github/mc1arke/sonarqube/plugin/scanner/autoconfiguration/AzureDevopsAutoConfigurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (C) 2021 Michael Clarke | ||
* | ||
* 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 com.github.mc1arke.sonarqube.plugin.scanner.autoconfiguration; | ||
|
||
import com.github.mc1arke.sonarqube.plugin.scanner.BranchAutoConfigurer; | ||
import com.github.mc1arke.sonarqube.plugin.scanner.BranchConfigurationFactory; | ||
import org.sonar.api.utils.System2; | ||
import org.sonar.scanner.scan.branch.BranchConfiguration; | ||
import org.sonar.scanner.scan.branch.ProjectBranches; | ||
|
||
import java.util.Optional; | ||
|
||
public class AzureDevopsAutoConfigurer implements BranchAutoConfigurer { | ||
|
||
private final BranchConfigurationFactory branchConfigurationFactory; | ||
|
||
public AzureDevopsAutoConfigurer(BranchConfigurationFactory branchConfigurationFactory) { | ||
this.branchConfigurationFactory = branchConfigurationFactory; | ||
} | ||
|
||
@Override | ||
public Optional<BranchConfiguration> detectConfiguration(System2 system2, ProjectBranches projectBranches) { | ||
if (!Boolean.parseBoolean(system2.envVariable("TF_BUILD"))) { | ||
return Optional.empty(); | ||
} | ||
|
||
String key = system2.envVariable("SYSTEM_PULLREQUEST_PULLREQUESTID"); | ||
if (key != null) { | ||
String sourceBranch = system2.envVariable("SYSTEM_PULLREQUEST_SOURCEBRANCH"); | ||
String targetBranch = system2.envVariable("SYSTEM_PULLREQUEST_TARGETBRANCH"); | ||
|
||
return Optional.of(branchConfigurationFactory.createPullRequestConfiguration(key, sourceBranch, targetBranch, projectBranches)); | ||
} | ||
|
||
return Optional.empty(); | ||
} | ||
} |
Oops, something went wrong.