From c0971c7f44823c4ce442ead28f2e9c0021769dc4 Mon Sep 17 00:00:00 2001 From: Rogan Date: Sat, 8 Feb 2020 02:04:04 +0800 Subject: [PATCH] fix: support sonar-project.Properties --- .gitignore | 2 +- DOCS.md | 38 ++++++++++++++++++++++---------------- main.go | 11 +++++++++-- plugin.go | 37 ++++++++++++++++++++++--------------- 4 files changed, 54 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index f21e9b3..4ab6684 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,4 @@ .history .vscode .scannerwork - +.idea diff --git a/DOCS.md b/DOCS.md index 884c4b3..744bdd6 100644 --- a/DOCS.md +++ b/DOCS.md @@ -13,33 +13,34 @@ This plugin can scan your code quality and post the analysis report to your Sona The below pipeline configuration demonstrates simple usage: ```yaml -steps +steps: - name: code-analysis image: aosapps/drone-sonar-plugin settings: - sonar_host: - from_secret: sonar_host - sonar_token: - from_secret: sonar_token + sonar_host: + from_secret: sonar_host + sonar_token: + from_secret: sonar_token ``` Customized parameters could be specified: ```diff - steps + steps: - name: code-analysis image: aosapps/drone-sonar-plugin settings: - sonar_host: - from_secret: sonar_host - sonar_token: - from_secret: sonar_token -+ ver: 1.0 -+ timeout: 20 -+ sources: . -+ level: DEBUG -+ showProfiling: true -+ exclusions: **/static/**/*,**/dist/**/*.js + sonar_host: + from_secret: sonar_host + sonar_token: + from_secret: sonar_token ++ ver: 1.0 ++ timeout: 20 ++ sources: . ++ level: DEBUG ++ showProfiling: true ++ exclusions: **/static/**/*,**/dist/**/*.js ++ usingProperties: false ``` # Secret Reference @@ -62,6 +63,11 @@ Safety first, the host and token are stored in Drone Secrets. * `showProfiling`: Display logs to see where the analyzer spends time. Default value `false` * `branchAnalysis`: Pass currently analysed branch to SonarQube. (Must not be active for initial scan!) Default value `false` + +* `usingProperties`: Using the `sonar-project.properties` file in root directory as sonar parameters. (Not include `sonar_host` and +`sonar_token`.) Default value `false` + + # Notes * projectKey: `DRONE_REPO` diff --git a/main.go b/main.go index 32b3b57..d23168f 100755 --- a/main.go +++ b/main.go @@ -87,6 +87,11 @@ func main() { Usage: "execute branchAnalysis", EnvVar: "PLUGIN_BRANCHANALYSIS", }, + cli.BoolFlag{ + Name: "usingProperties", + Usage: "using sonar-project.properties", + EnvVar: "PLUGIN_USINGPROPERTIES", + }, } app.Run(os.Args) @@ -107,8 +112,10 @@ func run(c *cli.Context) { Inclusions: c.String("inclusions"), Exclusions: c.String("exclusions"), Level: c.String("level"), - showProfiling: c.String("showProfiling"), - branchAnalysis: c.Bool("branchAnalysis"), + ShowProfiling: c.String("showProfiling"), + BranchAnalysis: c.Bool("branchAnalysis"), + UsingProperties: c.Bool("usingProperties"), + }, } diff --git a/plugin.go b/plugin.go index 30338be..2b2cb32 100755 --- a/plugin.go +++ b/plugin.go @@ -20,8 +20,9 @@ type ( Inclusions string Exclusions string Level string - showProfiling string - branchAnalysis bool + ShowProfiling string + BranchAnalysis bool + UsingProperties bool } Plugin struct { Config Config @@ -30,23 +31,29 @@ type ( func (p Plugin) Exec() error { args := []string{ - "-Dsonar.projectKey=" + strings.Replace(p.Config.Key, "/", ":", -1), - "-Dsonar.projectName=" + p.Config.Name, "-Dsonar.host.url=" + p.Config.Host, "-Dsonar.login=" + p.Config.Token, - - "-Dsonar.projectVersion=" + p.Config.Version, - "-Dsonar.sources=" + p.Config.Sources, - "-Dsonar.ws.timeout=" + p.Config.Timeout, - "-Dsonar.inclusions=" + p.Config.Inclusions, - "-Dsonar.exclusions=" + p.Config.Exclusions, - "-Dsonar.log.level=" + p.Config.Level, - "-Dsonar.showProfiling=" + p.Config.showProfiling, - "-Dsonar.scm.provider=git", } - if p.Config.branchAnalysis { - args = append(args, "-Dsonar.branch.name="+p.Config.Branch) + if !p.Config.UsingProperties { + argsParameter := []string{ + "-Dsonar.projectKey=" + strings.Replace(p.Config.Key, "/", ":", -1), + "-Dsonar.projectName=" + p.Config.Name, + "-Dsonar.projectVersion=" + p.Config.Version, + "-Dsonar.sources=" + p.Config.Sources, + "-Dsonar.ws.timeout=" + p.Config.Timeout, + "-Dsonar.inclusions=" + p.Config.Inclusions, + "-Dsonar.exclusions=" + p.Config.Exclusions, + "-Dsonar.log.level=" + p.Config.Level, + "-Dsonar.showProfiling=" + p.Config.ShowProfiling, + "-Dsonar.scm.provider=git", + } + args = append(args, argsParameter) + } + + + if p.Config.BranchAnalysis { + args = append(args, "-Dsonar.branch.name=" + p.Config.Branch) } cmd := exec.Command("sonar-scanner", args...)