Update sonar-scanner.properties with version and name from package.json

Lately, I added SonarQube to our build pipeline. SonarQube is a code analysis tool which lets you easily scan your projects for smells and bugs. We mainly use it for Java and NodeJS code. The scanning is straightforward and can, in the simplest case, be started from the developer’s machine by using sonar-scanner. The sonar-scanner checks the current directory for a sonar-scanner.properties-File. In our case, an example properties file looks like this:

# must be unique in a given SonarQube instance
sonar.projectKey=some-unique-project-key
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=this-will-be-taken-from-package-json-soon
sonar.projectVersion=this-will-be-taken-from-package-json-soon
 
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set. 
sonar.sources=.
sonar.tests=tests
sonar.language=js
sonar.exclusions=tests/**, node_modules/**, .*/**, coverage/**

# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

To simply update the version and package, we start with the script from my other post Keep your swagger.yaml version in sync with package.json and extend it a bit. The result looks like so:

Although this looks a bit overwhelming, the real magic only happens in these few lines.

Put the contents of the gist in a file called updateSonarProps.sh in your project directory, next to the package.json and sonar-scanner.properties and run it via:

sh updateSonarProps.sh

Bonus: to start the scan even easier, I added the following script to the package.json as well:

  "scripts": {
    ...
    "sonar": "sh updateSonarProps.sh && sonar-scanner"
  },

A post on how we integrated that all into our Jenkins will follow soon 🙂

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert