[Background]
I have a Python project with config CI on Gitlab. It has a lint stage which will check code quality of all source code when push event or merge request event is triggered. It will take time to check all source code with merge request. And it will not focus on code quality of merge request. It should check code quality on commit change files.
pylint:
stage: lint
before_script:
- mkdir -p public/badges public/lint
- echo undefined > public/badges/$CI_JOB_NAME.score
- pip install pylint-gitlab
script:
- pip install -r requirements.txt
- pylint --rcfile=.pylintrc --exit-zero --output-format=text $(find -type f -name "*.py" ! -path "**/.venv/**") | tee /tmp/pylint.txt
- sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*/\1/p' /tmp/pylint.txt > public/badges/$CI_JOB_NAME.score
- pylint --exit-zero --output-format=pylint_gitlab.GitlabCodeClimateReporter $(find -type f -name "*.py" ! -path "**/.venv/**") > codeclimate.json
- pylint --exit-zero --output-format=pylint_gitlab.GitlabPagesHtmlReporter $(find -type f -name "*.py" ! -path "**/.venv/**") > public/lint/index.html
after_script:
- anybadge --overwrite --label $CI_JOB_NAME --value=$(cat public/badges/$CI_JOB_NAME.score) --file=public/badges/$CI_JOB_NAME.svg 4=red 6=orange 8=yellow 10=green
- |
echo "Your score is: $(cat public/badges/$CI_JOB_NAME.score)"
artifacts:
paths:
- public
reports:
codequality: codeclimate.json
when: always
only:
- develop
- merge_requests
tags:
- docker
[Solution]
Here is a solution to resolve my problems.
I will create another config pylint for merge request event. It just check code quality on commit changed files compare with merge request target branch.
pylint_merge_request:
stage: lint
before_script:
- pip install pylint-gitlab
script:
- echo CI_COMMIT_SHA=${CI_COMMIT_SHA}
- echo CI_MERGE_REQUEST_TARGET_BRANCH_NAME=${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}
- git fetch origin ${CI_MERGE_REQUEST_TARGET_BRANCH_NAME}
- FILES=$(git diff --name-only ${CI_COMMIT_SHA} origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME} | grep '\.py'$)
- echo "Changed files are $FILES"
- pip install -r requirements.txt
- pylint --rcfile=.pylintrc --output-format=text $FILES | tee /tmp/pylint.txt
only:
- merge_requests
tags:
- docker
I hope it is helpful for everyone. You can use it for other programing languages on Gitlab.
Top comments (0)