Analysis of Merge Requests in GitLab Using PVS-Studio For

Analysis of Merge Requests in GitLab Using PVS-Studio For

Like GitLab and don't like bugs? Want to improve the quality of your source code? Let's see how to configure PVS-Studio C# analyzer for checking merge requests.

Do you like GitLab and don't like bugs? Do you want to improve the quality of your source code? Then you've come to the right place. Today we will tell you how to configure the PVS-Studio C# analyzer for checking merge requests. Enjoy the reading and have a nice unicorn mood.

PVS-Studio is a tool designed to detect errors and potential vulnerabilities in the source code of programs, written in C, C++, C#, and Java. Works in 64-bit systems on Windows, Linux, and macOS. Can analyze the code meant for 32-bit, 64-bit, and embedded ARM platforms.

By the way, we've released PVS-Studio 7.08, which was full of new sapid features. For example:

  • C# analyzer under Linux and macOS;
  • plugin for Rider;
  • new mode for checking a list of files.

Mode of Checking a List of Files

Previously, to check certain files, one had to pass .xml to the analyzer with a list of files. But since this is not very convenient, we have added the ability to pass .txt, which makes life much simpler.

To check certain files, specify the --sourceFiles (-f) flag and pass .txt with the list of files. It looks like this:

C#

1

pvs-studio-dotnet -t path/to/solution.sln -f fileList.txt -o project.json

If you are interested in configuring checks of commits or pull requests, you can also do this using this mode. The difference will be in getting a list of files for analysis and will depend on which systems you are using.

Principle of Checking Merge Requests

The main point of checking is to make sure that problems detected by the analyzer do not make it into the master branch when merging. We also don't want to analyze the entire project every time. Moreover, when merging branches, we have a list of changed files. Therefore, I suggest adding a merge request check.

This is how a merge request looks like before introducing a static analyzer:

merge request In other words, all errors in the changes branch will get to the master branch. Since we wouldn't like this, we add the analysis, and now the scheme looks as follows:

errors in changes branch We analyze changes2 and, if there are no errors, we accept the merge request, otherwise reject it.

By the way, if you are interested in analyzing commits and pull requests for C/C++, you are welcome to read about it here.

GitLab

GitLab is an open-source DevOps lifecycle web tool that provides a code repository management system for Git with its wiki, bug tracking system, CI/CD pipeline, and other features.

Before you start implementing the merge request analysis, you need to register and upload your project. If you do not know how to do this, then I suggest an article by my colleague.

Note. One of the possible ways to configure the environment is described below. The point is to show the steps for configuring the environment needed for analyzing and running the analyzer. In your case, it may be better to separate the stages of environment preparation (adding repositories, installing the analyzer), and analysis. For example, preparing Docker instances with the necessary environment and their usage, or some other method.

To get a better understanding of what is going to happen next, I suggest taking a look at the following scheme:

scheme example The analyzer needs .NET Core SDK 3 for proper operation from which the necessary dependencies for the analyzer will be installed. Adding Microsoft repositories for various Linux distributions is described in the relevant document.

To install PVS-Studio via the package manager, you will also need to add PVS-Studio repositories. Adding repositories for various distributions is described in more detail in the relevant section of the documentation.

The analyzer needs a license key to operate. You can get a trial license on the analyzer download page.

Note. Please note that the described operating mode (merge requests analysis) requires an Enterprise license. Therefore, if you would like to try this mode of operation, don't forget to specify that you need an Enterprise license in the "Message" field.

If a merge request occurs, we only need to analyze the list of changed files, otherwise, we analyze all files. After the analysis, we need to convert the logs to the format we need.

Now, with the algorithm in front of your eyes, you can proceed to writing the script. To do this, we need to change the .gitlab-ci.yml file or, if there is no such file, create one. To create it, click on the name of your project -> Set up CI/CD.

changing the .gitlab-ci.yml file Now we are ready to write the script. Let's first write the code that will install the analyzer and enter the license:

C#

1

before_script:

2

  - apt-get update && apt-get -y install wget gnupg 

3

4

  - apt-get -y install git

5

  - wget https://packages.microsoft.com/config/debian/10/

6

packages-microsoft-prod.deb -O packages-microsoft-prod.deb

7

  - dpkg -i packages-microsoft-prod.deb

8

  - apt-get update

9

  - apt-get install apt-transport-https

10

  - apt-get update

11

12

  - wget -q -O - https://files.viva64.com/etc/pubkey.txt | apt-key add -

13

  - wget -O /etc/apt/sources.list.d/viva64.list

14

https://files.viva64.com/etc/viva64.list

15

  - apt-get update

16

  - apt-get -y install pvs-studio-dotnet

17

18

  - pvs-studio-analyzer credentials $PVS_NAME $PVS_KEY

19

  - dotnet restore "$CI_PROJECT_DIR"/Test/Test.sln

Since installation and activation must occur before all other scripts, we use a special before_script label. Let me be clear on this fragment.

Preparation for the analyzer installation:

C#

1

  - wget https://packages.microsoft.com/config/debian/10/

2

packages-microsoft-prod.deb -O packages-microsoft-prod.deb

3

  - dpkg -i packages-microsoft-prod.deb

4

  - apt-get update

5

  - apt-get install apt-transport-https

6

  - apt-get update

Adding PVS-Studio repositories and the analyzer:

C#

1

  - wget -q -O - https://files.viva64.com/etc/pubkey.txt | apt-key add -

2

  - wget -O /etc/apt/sources.list.d/viva64.list

3

https://files.viva64.com/etc/viva64.list

4

  - apt-get update

5

  - apt-get -y install pvs-studio-dotnet

devops devsecops csharp static analysis gitlab static analysis tools static analyzer

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Static Code Analysis: What It Is? How to Use It?

Static code analysis is a method of debugging by examining source code before a program is run. It's done by analyzing a set of code against a set (or multiple sets) of coding rules. Static code analysis and static analysis are often used interchangeably, along with source code analysis.

10 Best Tools for DevOps You’ve Never Heard About - DZone DevOps

Looking for DevOps tools? See these 10 great tools for DevOps. You won't find such tools anywhere else. Free plans included, no BS.

Automating Security in DevOps: Top 15 Tools

Cybersecurity is a big concern for many companies. With data breaches happening more and more as attacks increase in sophistication, teams are looking at all of the options they have to prevent them.

50+ Useful DevOps Tools

The article comprises both very well established tools for those who are new to the DevOps methodology. DevOps has come to mean many things to each individual who uses the term as DevOps is not a singularly defined standard, software, or process but more of a culture.

How to Extend your DevOps Strategy For Success in the Cloud?

DevOps and Cloud computing are joined at the hip, now that fact is well appreciated by the organizations that engaged in SaaS cloud and developed applications in the Cloud. During the COVID crisis period, most of the organizations have started using cloud computing services and implementing a cloud-first strategy to establish their remote operations. Similarly, the extended DevOps strategy will make the development process more agile with automated test cases.