5

SAP CPQ : How to Write Cleaner, Safer Code with SonarQube & Docker

 1 year ago
source link: https://blogs.sap.com/2023/04/08/sap-cpq-how-to-write-cleaner-safer-code-with-sonarqube-docker/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
April 8, 2023 3 minute read

SAP CPQ : How to Write Cleaner, Safer Code with SonarQube & Docker

Introduction

SonarQube is a tool that helps you catch bugs and vulnerabilities in your SAP CPQ app written Ironpython scripting. Working together with PYLint and Unit tests, it provides a great code quality scan.

On this blog, I will show you how to set up SonarQube and run locally over a Ironpython scripts folder project. Then, we will improve SonarQube analysis by adding PYLint reports.

download-3.png

What is SonarQube?

SonarQube is a popular continuous inspection tool for code quality and code security that aims to help development teams ship better software. It functions as an automatic code review tool with support for more than 30 programming languages.

SonarQube easily interfaces with CI pipelines and DevOps builds to make code inspection swift and efficient for engineers. It is also self-managed, satisfying the need for developers to ship quality and maintainable code at a fast pace.

Installing SonarQube on Docker

Getting SonarQube on Docker simply involves grabbing the image from Docker Hub. If you use a Linux machine, you’ll need to set the recommended base configurations using the commands provided by Docker under “Docker Host Requirements”.

2023-04-08_21-53-28.png

Next, launch the Docker daemon in a separate terminal. On the terminal, run the below command to start a server:

docker run -d --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 sonarqube:latest

You can access the SonarQube instance with the host IP address and the specified port (localhost:9000, in our example).  http://localhost:9000/

0-8uQPpXvk__IB0CQI.png

When the SonarQube portal homepage appears, go ahead and log in; use the default username and password (“admin”). Next, you’ll be asked to update your password:

Select the “Manually” option. (If you want setup SonarQube with GitHub or another platform then select that option)

How-to-Setup-the-SonarQube-on-Local-Machine08.png

Enter the “display name” and “key” and click “Set Up”.

9-5.png

Now select the “Locally” option. Because we are going to setup in our local machine.

2023-04-08_22-03-44.png

Enter the token name and click the “Generate” button. You will get the sonar token. 

2023-04-08_22-04-51.png

Save that token and Click “Continue” and Choose your project language.

2023-04-08_22-05-31.png

Download the Scanner zip file from the link and Extract it.

https://docs.sonarqube.org/latest/analyzing-source-code/scanners/sonarscanner/

Copy all folders and paste them somewhere and add the “bin” directory path under the PATH environment variable

2023-04-08_21-47-06.png

Let’s Run your code Anaysis

Running a SonarQube analysis is now very simple. You just need to execute the following commands in your project’s root folder. The command runs a sonar check for your whole project.

sonar-scanner.bat -D"sonar.projectKey=test-key" -D"sonar.sources=." -D"sonar.host.url=http://localhost:9000" -D"sonar.login=<sonar-token-here>"
// Here replace <sonar-token-here> above generated token
sonar-scanner.bat -D"sonar.projectKey=test-key" -D"sonar.sources=<file-or-folder-path-here>" -D"sonar.host.url=http://localhost:9000" -D"sonar.login=<sonar-token-here>"
2023-04-08_22-12-20.png
2023-04-08_22-13-52.png

After the above command runs successfully, you can check the results on your SonarQube project page http://localhost:9000/

Sonar report will automatically infer the project name from your code. i.e., the final report will not have test-key as the project name but your actual project name.

2023-04-08_22-22-54.png

Code Smell Issues

2023-04-08_22-23-25.png

SAP CPQ Script suggests from Code Smell to make some changes.

2023-04-08_22-20-25.png

Final Thoughts

SonarQube is a great tool for checking the quality of code and also supports more than 25 languages. I hope you have liked it and know about SonarQube and how to setup it in a local machine.

Note

We can discuss about CI configuration on another time. The main goal here was to run and use SonarQube locally.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK