fix: support sonar-project.Properties

This commit is contained in:
Rogan 2020-02-08 02:04:04 +08:00
parent 15157105c5
commit c0971c7f44
4 changed files with 54 additions and 34 deletions

2
.gitignore vendored
View File

@ -14,4 +14,4 @@
.history .history
.vscode .vscode
.scannerwork .scannerwork
.idea

38
DOCS.md
View File

@ -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: The below pipeline configuration demonstrates simple usage:
```yaml ```yaml
steps steps:
- name: code-analysis - name: code-analysis
image: aosapps/drone-sonar-plugin image: aosapps/drone-sonar-plugin
settings: settings:
sonar_host: sonar_host:
from_secret: sonar_host from_secret: sonar_host
sonar_token: sonar_token:
from_secret: sonar_token from_secret: sonar_token
``` ```
Customized parameters could be specified: Customized parameters could be specified:
```diff ```diff
steps steps:
- name: code-analysis - name: code-analysis
image: aosapps/drone-sonar-plugin image: aosapps/drone-sonar-plugin
settings: settings:
sonar_host: sonar_host:
from_secret: sonar_host from_secret: sonar_host
sonar_token: sonar_token:
from_secret: sonar_token from_secret: sonar_token
+ ver: 1.0 + ver: 1.0
+ timeout: 20 + timeout: 20
+ sources: . + sources: .
+ level: DEBUG + level: DEBUG
+ showProfiling: true + showProfiling: true
+ exclusions: **/static/**/*,**/dist/**/*.js + exclusions: **/static/**/*,**/dist/**/*.js
+ usingProperties: false
``` ```
# Secret Reference # 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` * `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` * `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 # Notes
* projectKey: `DRONE_REPO` * projectKey: `DRONE_REPO`

11
main.go
View File

@ -87,6 +87,11 @@ func main() {
Usage: "execute branchAnalysis", Usage: "execute branchAnalysis",
EnvVar: "PLUGIN_BRANCHANALYSIS", EnvVar: "PLUGIN_BRANCHANALYSIS",
}, },
cli.BoolFlag{
Name: "usingProperties",
Usage: "using sonar-project.properties",
EnvVar: "PLUGIN_USINGPROPERTIES",
},
} }
app.Run(os.Args) app.Run(os.Args)
@ -107,8 +112,10 @@ func run(c *cli.Context) {
Inclusions: c.String("inclusions"), Inclusions: c.String("inclusions"),
Exclusions: c.String("exclusions"), Exclusions: c.String("exclusions"),
Level: c.String("level"), Level: c.String("level"),
showProfiling: c.String("showProfiling"), ShowProfiling: c.String("showProfiling"),
branchAnalysis: c.Bool("branchAnalysis"), BranchAnalysis: c.Bool("branchAnalysis"),
UsingProperties: c.Bool("usingProperties"),
}, },
} }

View File

@ -20,8 +20,9 @@ type (
Inclusions string Inclusions string
Exclusions string Exclusions string
Level string Level string
showProfiling string ShowProfiling string
branchAnalysis bool BranchAnalysis bool
UsingProperties bool
} }
Plugin struct { Plugin struct {
Config Config Config Config
@ -30,23 +31,29 @@ type (
func (p Plugin) Exec() error { func (p Plugin) Exec() error {
args := []string{ args := []string{
"-Dsonar.projectKey=" + strings.Replace(p.Config.Key, "/", ":", -1),
"-Dsonar.projectName=" + p.Config.Name,
"-Dsonar.host.url=" + p.Config.Host, "-Dsonar.host.url=" + p.Config.Host,
"-Dsonar.login=" + p.Config.Token, "-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 { if !p.Config.UsingProperties {
args = append(args, "-Dsonar.branch.name="+p.Config.Branch) 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...) cmd := exec.Command("sonar-scanner", args...)