You've already forked sap-jenkins-library
mirror of
https://github.com/SAP/jenkins-library.git
synced 2025-11-06 09:09:19 +02:00
feat(sonar): make step orchestrator-agnostic (#2874)
* Make sonarExecuteScan orchestrator-agnostic * Increase coverage + support empty or false env vars * Use cleared env for unit tests * Refactor to standalone package * Fix review findings * Fix review findings * Fix unit test * Add logging * Refactor * Add to codeowners 😎 * Apply suggestions from code review * Remove unreachable code * no message * fix typos Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
This commit is contained in:
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/SAP/jenkins-library/pkg/command"
|
||||
piperhttp "github.com/SAP/jenkins-library/pkg/http"
|
||||
"github.com/SAP/jenkins-library/pkg/log"
|
||||
"github.com/SAP/jenkins-library/pkg/orchestrator"
|
||||
FileUtils "github.com/SAP/jenkins-library/pkg/piperutils"
|
||||
SliceUtils "github.com/SAP/jenkins-library/pkg/piperutils"
|
||||
StepResults "github.com/SAP/jenkins-library/pkg/piperutils"
|
||||
@@ -105,6 +106,9 @@ func sonarExecuteScan(config sonarExecuteScanOptions, _ *telemetry.CustomData, i
|
||||
}
|
||||
|
||||
func runSonar(config sonarExecuteScanOptions, client piperhttp.Downloader, runner command.ExecRunner, apiClient SonarUtils.Sender, influx *sonarExecuteScanInflux) error {
|
||||
// Set config based on orchestrator-specific environment variables
|
||||
detectParametersFromCI(&config)
|
||||
|
||||
if len(config.ServerURL) > 0 {
|
||||
sonar.addEnvironment("SONAR_HOST_URL=" + config.ServerURL)
|
||||
}
|
||||
@@ -411,3 +415,35 @@ func getTempDir() string {
|
||||
}
|
||||
return tmpFolder
|
||||
}
|
||||
|
||||
// Fetches parameters from environment variables and updates the options accordingly (only if not already set)
|
||||
func detectParametersFromCI(options *sonarExecuteScanOptions) {
|
||||
provider, err := orchestrator.NewOrchestratorSpecificConfigProvider()
|
||||
if err != nil {
|
||||
log.Entry().WithError(err).Warning("Cannot infer config from CI environment")
|
||||
return
|
||||
}
|
||||
|
||||
if provider.IsPullRequest() {
|
||||
config := provider.GetPullRequestConfig()
|
||||
if len(options.ChangeBranch) == 0 {
|
||||
log.Entry().Info("Inferring parameter changeBranch from environment: " + config.Branch)
|
||||
options.ChangeBranch = config.Branch
|
||||
}
|
||||
if len(options.ChangeTarget) == 0 {
|
||||
log.Entry().Info("Inferring parameter changeTarget from environment: " + config.Base)
|
||||
options.ChangeTarget = config.Base
|
||||
}
|
||||
if len(options.ChangeID) == 0 {
|
||||
log.Entry().Info("Inferring parameter changeId from environment: " + config.Key)
|
||||
options.ChangeID = config.Key
|
||||
}
|
||||
} else {
|
||||
config := provider.GetBranchBuildConfig()
|
||||
|
||||
if options.InferBranchName && len(options.BranchName) == 0 {
|
||||
log.Entry().Info("Inferring parameter branchName from environment: " + config.Branch)
|
||||
options.BranchName = config.Branch
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -186,11 +186,11 @@ func addSonarExecuteScanFlags(cmd *cobra.Command, stepConfig *sonarExecuteScanOp
|
||||
cmd.Flags().BoolVar(&stepConfig.InferJavaBinaries, "inferJavaBinaries", false, "Find the location of generated Java class files in all modules and pass the option `sonar.java.binaries to the sonar tool.")
|
||||
cmd.Flags().BoolVar(&stepConfig.InferJavaLibraries, "inferJavaLibraries", false, "If the parameter `m2Path` is configured for the step `mavenExecute` in the general section of the configuration, pass it as option `sonar.java.libraries` to the sonar tool.")
|
||||
cmd.Flags().StringSliceVar(&stepConfig.Options, "options", []string{}, "A list of options which are passed to the sonar-scanner.")
|
||||
cmd.Flags().StringVar(&stepConfig.BranchName, "branchName", os.Getenv("PIPER_branchName"), "Non-Pull-Request only: Name of the SonarQube branch that should be used to report findings to.")
|
||||
cmd.Flags().BoolVar(&stepConfig.InferBranchName, "inferBranchName", false, "Jenkins only: Whether to infer the `branchName` parameter automatically based on the `BRANCH_NAME` environment variable in non-productive runs of the pipeline.")
|
||||
cmd.Flags().StringVar(&stepConfig.ChangeID, "changeId", os.Getenv("PIPER_changeId"), "Pull-Request only: The id of the pull-request.")
|
||||
cmd.Flags().StringVar(&stepConfig.ChangeBranch, "changeBranch", os.Getenv("PIPER_changeBranch"), "Pull-Request only: The name of the pull-request branch.")
|
||||
cmd.Flags().StringVar(&stepConfig.ChangeTarget, "changeTarget", os.Getenv("PIPER_changeTarget"), "Pull-Request only: The name of the base branch.")
|
||||
cmd.Flags().StringVar(&stepConfig.BranchName, "branchName", os.Getenv("PIPER_branchName"), "Non-Pull-Request only: Name of the SonarQube branch that should be used to report findings to. Automatically inferred from environment variables on supported orchestrators if `inferBranchName` is set to true.")
|
||||
cmd.Flags().BoolVar(&stepConfig.InferBranchName, "inferBranchName", false, "Whether to infer the `branchName` parameter automatically based on the orchestrator-specific environment variable in runs of the pipeline.")
|
||||
cmd.Flags().StringVar(&stepConfig.ChangeID, "changeId", os.Getenv("PIPER_changeId"), "Pull-Request only: The id of the pull-request. Automatically inferred from environment variables on supported orchestrators.")
|
||||
cmd.Flags().StringVar(&stepConfig.ChangeBranch, "changeBranch", os.Getenv("PIPER_changeBranch"), "Pull-Request only: The name of the pull-request branch. Automatically inferred from environment variables on supported orchestrators.")
|
||||
cmd.Flags().StringVar(&stepConfig.ChangeTarget, "changeTarget", os.Getenv("PIPER_changeTarget"), "Pull-Request only: The name of the base branch. Automatically inferred from environment variables on supported orchestrators.")
|
||||
cmd.Flags().StringVar(&stepConfig.PullRequestProvider, "pullRequestProvider", `GitHub`, "Pull-Request only: The scm provider.")
|
||||
cmd.Flags().StringVar(&stepConfig.Owner, "owner", os.Getenv("PIPER_owner"), "Pull-Request only: The owner of the scm repository.")
|
||||
cmd.Flags().StringVar(&stepConfig.Repository, "repository", os.Getenv("PIPER_repository"), "Pull-Request only: The scm repository.")
|
||||
|
||||
Reference in New Issue
Block a user