2019-09-04 21:01:54 +05:30
|
|
|
---
|
|
|
|
type: reference, howto
|
2019-10-12 21:52:04 +05:30
|
|
|
disqus_identifier: 'https://docs.gitlab.com/ee/user/project/merge_requests/code_quality_diff.html'
|
2019-09-04 21:01:54 +05:30
|
|
|
---
|
|
|
|
|
2019-09-30 21:07:59 +05:30
|
|
|
# Code Quality **(STARTER)**
|
2019-07-31 22:56:46 +05:30
|
|
|
|
|
|
|
> [Introduced](https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/1984)
|
|
|
|
in [GitLab Starter](https://about.gitlab.com/pricing/) 9.3.
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
With the help of [GitLab CI/CD](../../../ci/README.md), you can analyze your
|
2019-07-31 22:56:46 +05:30
|
|
|
source code quality using GitLab Code Quality.
|
2019-10-12 21:52:04 +05:30
|
|
|
|
|
|
|
Code Quality:
|
|
|
|
|
|
|
|
- Uses [Code Climate Engines](https://codeclimate.com), which are
|
|
|
|
free and open source. Code Quality doesn't require a Code Climate
|
|
|
|
subscription.
|
|
|
|
- Runs in [pipelines](../../../ci/pipelines.md) using an Docker image built in
|
|
|
|
[GitLab Code
|
|
|
|
Quality](https://gitlab.com/gitlab-org/security-products/codequality) project.
|
|
|
|
- Can make use of a [template](#template-and-examples).
|
|
|
|
- Is available with [Auto
|
|
|
|
DevOps](../../../topics/autodevops/index.md#auto-code-quality-starter).
|
2019-07-31 22:56:46 +05:30
|
|
|
|
|
|
|
Going a step further, GitLab can show the Code Quality report right
|
|
|
|
in the merge request widget area:
|
|
|
|
|
|
|
|
![Code Quality Widget](img/code_quality.gif)
|
|
|
|
|
|
|
|
## Use cases
|
|
|
|
|
|
|
|
For instance, consider the following workflow:
|
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
1. Your backend team member starts a new implementation for making a certain
|
|
|
|
feature in your app faster.
|
|
|
|
1. With Code Quality reports, they analyze how their implementation is impacting
|
|
|
|
the code quality.
|
|
|
|
1. The metrics show that their code degrade the quality in 10 points.
|
|
|
|
1. You ask a co-worker to help them with this modification.
|
|
|
|
1. They both work on the changes until Code Quality report displays no
|
|
|
|
degradations, only improvements.
|
|
|
|
1. You approve the merge request and authorize its deployment to staging.
|
|
|
|
1. Once verified, their changes are deployed to production.
|
|
|
|
|
|
|
|
## Template and examples
|
|
|
|
|
|
|
|
For most GitLab instances, the supplied template is the preferred method of
|
|
|
|
implementing Code Quality. See
|
|
|
|
[Analyze your project's Code Quality](../../../ci/examples/code_quality.md) for:
|
|
|
|
|
|
|
|
- Information on the builtin GitLab Code Quality template.
|
|
|
|
- Examples of manual GitLab configuration for earlier GitLab versions.
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
## Configuring jobs using variables
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
The Code Quality job supports environment variables that users can set to
|
|
|
|
configure job execution at runtime.
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
For a list of available environment variables, see
|
|
|
|
[Environment variables](https://gitlab.com/gitlab-org/security-products/codequality/blob/master/README.md#environment-variables).
|
|
|
|
|
|
|
|
## Implementing a custom tool
|
|
|
|
|
|
|
|
It's possible to have a custom tool provide Code Quality reports in GitLab. To
|
|
|
|
do this:
|
|
|
|
|
|
|
|
1. Define a job in your `.gitlab-ci.yml` file that generates the
|
|
|
|
[Code Quality report
|
|
|
|
artifact](../../../ci/yaml/README.md#artifactsreportscodequality-starter).
|
|
|
|
1. Configure your tool to generate the Code Quality report artifact as a JSON
|
|
|
|
file that implements subset of the [Code Climate
|
|
|
|
spec](https://github.com/codeclimate/spec/blob/master/SPEC.md#data-types).
|
|
|
|
|
|
|
|
The Code Quality report artifact JSON file must contain an array of objects
|
|
|
|
with the following properties:
|
2019-07-31 22:56:46 +05:30
|
|
|
|
|
|
|
| Name | Description |
|
|
|
|
| ---------------------- | -------------------------------------------------------------------------------------- |
|
|
|
|
| `description` | A description of the code quality violation. |
|
|
|
|
| `fingerprint` | A unique fingerprint to identify the code quality violation. For example, an MD5 hash. |
|
|
|
|
| `location.path` | The relative path to the file containing the code quality violation. |
|
|
|
|
| `location.lines.begin` | The line on which the code quality violation occurred. |
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
```json
|
|
|
|
[
|
|
|
|
{
|
|
|
|
"description": "'unused' is assigned a value but never used.",
|
|
|
|
"fingerprint": "7815696ecbf1c96e6894b779456d330e",
|
|
|
|
"location": {
|
|
|
|
"path": "lib/index.js",
|
|
|
|
"lines": {
|
|
|
|
"begin": 42
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
```
|
|
|
|
|
|
|
|
NOTE: **Note:**
|
2019-10-12 21:52:04 +05:30
|
|
|
Although the Code Climate spec supports more properties, those are ignored by
|
|
|
|
GitLab.
|
|
|
|
|
|
|
|
## Code Quality reports
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
Once the Code Quality job has completed, GitLab:
|
2019-07-31 22:56:46 +05:30
|
|
|
|
2019-10-12 21:52:04 +05:30
|
|
|
- Checks the generated report.
|
|
|
|
- Compares the metrics between the source and target branches.
|
|
|
|
- Shows the information right on the merge request.
|
2019-07-31 22:56:46 +05:30
|
|
|
|
|
|
|
If multiple jobs in a pipeline generate a code quality artifact, only the artifact from
|
|
|
|
the last created job (the job with the largest job ID) is used. To avoid confusion,
|
|
|
|
configure only one job to generate a code quality artifact.
|
|
|
|
|
|
|
|
If the Code Quality report doesn't have anything to compare to, no information
|
|
|
|
will be displayed in the merge request area. That is the case when you add the
|
|
|
|
Code Quality job in your `.gitlab-ci.yml` for the very first time.
|
|
|
|
Consecutive merge requests will have something to compare to and the Code Quality
|
|
|
|
report will be shown properly.
|
2019-09-04 21:01:54 +05:30
|
|
|
|
|
|
|
<!-- ## Troubleshooting
|
|
|
|
|
|
|
|
Include any troubleshooting steps that you can foresee. If you know beforehand what issues
|
|
|
|
one might have when setting this up, or when something is changed, or on upgrading, it's
|
|
|
|
important to describe those, too. Think of things that may go wrong and include them here.
|
|
|
|
This is important to minimize requests for support, and to avoid doc comments with
|
|
|
|
questions that you know someone might ask.
|
|
|
|
|
|
|
|
Each scenario can be a third-level heading, e.g. `### Getting error message X`.
|
|
|
|
If you have none to add when creating a doc, leave this section in place
|
|
|
|
but commented out to help encourage others to add to it in the future. -->
|