code_buddy
code_buddy

Reputation: 61

GitLab Code-Quality to run only on changed files in Merge Request

Do we have any option in GitLab code_quality to run only changed files in source repository? Right now, code_quality is analysing the entire project, and the artefacts it produces are enormous. We want to gradually make them better. Here is my .git.ci.yaml configuration

stages:
    test
include:
    template: Code-Quality.gitlab-ci.yml
code_quality: 
    rules: - if: $CI_PIPELINE_SOURCE == "merge_request_event" 
    variables: 
        REPORT_FORMAT: html 
    services:
    artifacts: 
        paths: [gl-code-quality-report.html]
    stage: test 
    tags: - ode-quality

I am currently referring GitLab official documentation code_quality and I have tried various way using Script, When, except options. But none worked out to analyse only changed files. I also saw an open issue on this.

Can anyone please let me know if we have any way to run code_quality analysis only for modified files ?

Upvotes: 4

Views: 878

Answers (1)

Mr.Teapot
Mr.Teapot

Reputation: 141

Maybe this is not the best available solution, but it works for me. Here is a fragment of the Code Quality pipeline definition which allows to get only changed files between the source branch and the target branch:

...
  before_script:
    - git fetch origin $CI_MERGE_REQUEST_TARGET_BRANCH_NAME
    - export CHANGED_FILES=$(git --no-pager diff --name-only origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME..$CI_COMMIT_BRANCH | grep "**/*\.py$")
  script:
    - if [[ -z "$CHANGED_FILES" ]]; then exit 0; fi
    - prospector -X -o json $CHANGED_FILES | tee gl-code-quality-report.json
...

What is going on in this configuration fragment:

In before_script we need to checkout the target branch because GitLab by default performs shallow clone because of that we need to run git fetch origin $CI_MERGE_REQUEST_TARGET_BRANCH_NAME.

Then we can make a diff between the target branch and the source branch using git --no-pager diff --name-only origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME..$CI_COMMIT_BRANCH | grep "**/*\.py$" which return all files changed in the source branch comparing to the target branch.

Option --no-pager forces git to skip pager usage, diff option --name-only allows us to get only file paths. $CI_MERGE_REQUEST_TARGET_BRANCH_NAME and $CI_COMMIT_BRANCH are pre-defined variables which GitLab gives you access in pipeline. The ending grep is in this case used to only get Python files because this pipeline is from a Python project. The result of this command is saved in the variable CHANGED_FILES.

Then in the script section, we check if we have any changes files that need to be checked using condition if [[ -z "$CHANGED_FILES" ]]; then exit 0; fi

The last line is the execution of the prospector tool(project-related). In your case, it will be probably a different tool. The tee command allows you to get the output of the prospector on stdout and save it to the file gl-code-quality-report.json.

Upvotes: 0

Related Questions