1
1

More than 3 years have passed since last update.

GoプロジェクトにSonarScannerをかけてみる

Last updated at Posted at 2021-02-10

What's?

SonarQubeでは、Goも解析対象となっているようなので試してみようかなと。

SonarQubeでGoを使う

SonarQubeのGoに関するドキュメントは、こちら。

Go

SonarScannerを使うようです。

SonarScannerは64ビットのWindows、macOS、Linuxで動作し、Go自体のインストールはカバレッジを取得しなければ不要のようです。

  • SonarQube Scanner should run on a x86-64 Windows, macOS or Linux 64bits machine
  • You need the Go installation on the scan machine only if you want to import coverage data

Goに対する解析ルールは、こちら。

Go static code analysis

サードパーティ製の解析ツールとしては、GoVet、GoLint、GoMetaLinter、golangci-lintをサポートしており、これらのツールで検出したissueを取り込めるようです。

Importing Third-Party Issues

カバレッジも送信することができます。

Test Coverage & Execution

今回、SonarQubeを使ってGoプロジェクトを解析してみます。

環境

今回のSonarQubeサーバーのバージョンは、こちらです。

SonarQube ID information
Server ID: 641C58CB-AXdrQxhRQD8BFDvb7Vf0
Version: 8.6.1.40680
Date: 2021-02-10

アクセスする際のURLは、http://172.24.24.94:9000とします。

SonarScannerをインストールする

SonarScannerのインストール先の環境は、こちらです。CentOS 8 Streamです。

$ cat /etc/redhat-release 
CentOS Stream release 8


$ uname -srvmpio
Linux 4.18.0-277.el8.x86_64 #1 SMP Wed Feb 3 20:35:19 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

ドキュメントに従って、SonarScannerをインストールします。

SonarScanner

zipファイルをダウンロードして展開。

$ curl -sLO https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.5.0.2216-linux.zip
$ unzip sonar-scanner-cli-4.5.0.2216-linux.zip

PATHを通しておきます。これで、インストール自体は完了です。

$ PATH=`pwd`/sonar-scanner-4.5.0.2216-linux/bin:$PATH

今回のSonarScannerのバージョンは、こちら。Javaで動いているみたいですね。

$ sonar-scanner --version
INFO: Scanner configuration file: /path/to/sonar-scanner-4.5.0.2216-linux/conf/sonar-scanner.properties
INFO: Project root configuration file: NONE
INFO: SonarScanner 4.5.0.2216
INFO: Java 11.0.3 AdoptOpenJDK (64-bit)
INFO: Linux 4.18.0-277.el8.x86_64 amd64

デフォルトの設定ファイルの内容。全部コメントアウトされていますが。

sonar-scanner-4.5.0.2216-linux/conf/sonar-scanner.properties

#Configure here general information about the environment, such as SonarQube server connection details for example
#No information about specific project should appear here

#----- Default SonarQube server
#sonar.host.url=http://localhost:9000

#----- Default source code encoding
#sonar.sourceEncoding=UTF-8

SonarScannerのディレクトリ内。JREを同梱しているようです。

$ ll ~/sonar-scanner-4.5.0.2216-linux
total 0
drwxr-xr-x. 2 xxxxx xxxxx 54 10月  2 12:43 bin
drwxr-xr-x. 2 xxxxx xxxxx 38 10月  2 12:42 conf
drwxr-xr-x. 6 xxxxx xxxxx 68 10月  2 12:43 jre
drwxr-xr-x. 2 xxxxx xxxxx 46 10月  2 12:43 lib

設定ファイルは、SonarScannerのconfディレクトリ内にあるものを修正してもいいのですが、プロジェクトごとにも指定できるようなので、今回はそのようにしましょう。

SonarScanner / Alternatives to sonar-project.properties

作成した、SonarScannerの設定ファイルはこちら。

my-sonar-scanner.properties

sonar.projectKey=my-go-project

sonar.host.url=http://172.24.24.94:9000

sonar.login=[トークン]

プロジェクト名は、my-go-projectとしました。sonar.host.urlはSonarQubeサーバーへの接続URLを、sonar.loginには認証に使うトークンを指定してください。

SonarScannerの実行にあたり、Goプロジェクト内に移動。

$ find -type f | grep -E '\.(go|sum|mod)$'
./main.go

〜省略〜

./go.mod
./go.sum

-Dproject.settingsオプションで、先程作成したSonarScannerの設定ファイルを指定して、SonarScannerを実行します。

$ sonar-scanner -Dproject.settings=my-sonar-scanner.properties

実行時のログ。

$ sonar-scanner -Dproject.settings=my-sonar-scanner.properties 
INFO: Scanner configuration file: /path/to/sonar-scanner-4.5.0.2216-linux/conf/sonar-scanner.properties
INFO: Project root configuration file: /path/to/my-go-project/my-sonar-scanner.properties
INFO: SonarScanner 4.5.0.2216
INFO: Java 11.0.3 AdoptOpenJDK (64-bit)
INFO: Linux 4.18.0-277.el8.x86_64 amd64
INFO: User cache: $HOME/.sonar/cache
INFO: Scanner configuration file: /path/to/sonar-scanner-4.5.0.2216-linux/conf/sonar-scanner.properties
INFO: Project root configuration file: /path/to/my-go-project/my-sonar-scanner.properties
INFO: Analyzing on SonarQube server 8.6.1
INFO: Default locale: "ja_JP", source code encoding: "UTF-8" (analysis is platform dependent)
INFO: Load global settings
INFO: Load global settings (done) | time=66ms
INFO: Server id: 641C58CB-AXdrQxhRQD8BFDvb7Vf0
INFO: User cache: $HOME/.sonar/cache
INFO: Load/download plugins
INFO: Load plugins index
INFO: Load plugins index (done) | time=40ms
INFO: Load/download plugins (done) | time=86ms
INFO: Process project properties
INFO: Process project properties (done) | time=0ms
INFO: Execute project builders
INFO: Execute project builders (done) | time=1ms
INFO: Project key: my-go-project
INFO: Base dir: /path/to/my-go-project
INFO: Working dir: /path/to/my-go-project/.scannerwork
INFO: Load project settings for component key: 'my-go-project'
INFO: Load quality profiles
INFO: Load quality profiles (done) | time=40ms
INFO: Load active rules
INFO: Load active rules (done) | time=634ms
INFO: Indexing files...
INFO: Project configuration:
INFO: 41 files indexed
INFO: 4 files ignored because of scm ignore settings
INFO: Quality profile for go: Sonar way
INFO: ------------- Run sensors on module my-go-project
INFO: Load metrics repository
INFO: Load metrics repository (done) | time=18ms
INFO: Sensor CSS Rules [cssfamily]
INFO: No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
INFO: Sensor CSS Rules [cssfamily] (done) | time=1ms
INFO: Sensor Code Quality and Security for Go [go]
INFO: 27 source files to be analyzed
INFO: Load project repositories
INFO: Load project repositories (done) | time=4ms
INFO: Sensor Code Quality and Security for Go [go] (done) | time=583ms
INFO: Sensor JaCoCo XML Report Importer [jacoco]
INFO: 'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
INFO: No report imported, no coverage information will be imported by JaCoCo XML Report Importer
INFO: Sensor JaCoCo XML Report Importer [jacoco] (done) | time=3ms
INFO: Sensor C# Properties [csharp]
INFO: Sensor C# Properties [csharp] (done) | time=0ms
INFO: Sensor JavaXmlSensor [java]
INFO: Sensor JavaXmlSensor [java] (done) | time=1ms
INFO: Sensor HTML [web]
INFO: Sensor HTML [web] (done) | time=2ms
INFO: Sensor VB.NET Properties [vbnet]
INFO: Sensor VB.NET Properties [vbnet] (done) | time=0ms
INFO: 27/27 source files have been analyzed
INFO: ------------- Run sensors on project
INFO: Sensor Zero Coverage Sensor
INFO: Sensor Zero Coverage Sensor (done) | time=13ms
INFO: SCM Publisher SCM provider for this project is: git
INFO: SCM Publisher 27 source files to be analyzed
INFO: SCM Publisher 27/27 source files have been analyzed (done) | time=190ms
INFO: CPD Executor 4 files had no CPD blocks
INFO: CPD Executor Calculating CPD for 23 files
INFO: CPD Executor CPD calculation finished (done) | time=17ms
INFO: Analysis report generated in 53ms, dir size=190 KB
INFO: Analysis report compressed in 38ms, zip size=75 KB
INFO: Analysis report uploaded in 136ms
INFO: ANALYSIS SUCCESSFUL, you can browse http://172.24.24.94:9000/dashboard?id=my-go-project
INFO: Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
INFO: More about the report processing at http://172.24.24.94:9000/api/ce/task?id=AXeKSMSZQD8BFDvb7aW1
INFO: Analysis total time: 2.987 s
INFO: ------------------------------------------------------------------------
INFO: EXECUTION SUCCESS
INFO: ------------------------------------------------------------------------
INFO: Total time: 3.661s
INFO: Final Memory: 12M/44M
INFO: ------------------------------------------------------------------------

カレントディレクトリを対象に解析を行っているようです。

INFO: Base dir: /path/to/my-go-project
INFO: Working dir: /path/to/my-go-project/.scannerwork

完了したら、SonarQubeサーバー側で結果を見てみます。

こんな感じで、解析結果やソースコードが登録されました。

※強い権限を持ったユーザーで実行したからか、送信先のSonarQubeプロジェクトは勝手に作られました…

image.png

image.png

image.png

デフォルトで有効なGoのルール(Sonar way)は、25個のようです。

image.png

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1