如何 运行 仅对 Gitlab 中更改的文件进行 pylint?

How to run pylint only on changed files in Gitlab?

我试图 运行 pylint 仅在更改的 python 文件上,但我的构建一直失败。我已经通过 git diff 提取了更改的文件并将它们保存在一个变量中,但是当我将变量注入 pylint 调用时,它失败了。但是,它适用于硬编码文件名。这是我的 yaml:

pylint:
stage: test
  before_script:
    - pip install pylint pylint-exit anybadge
  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"
      - pylint --output-format=text $(find -type f -name "$FILES" ! -path "**/.venv/**") | tee ./pylint/pylint.log || pylint-exit $?
      - PYLINT_SCORE=$(sed -n 's/^Your code has been rated at \([-0-9.]*\)\/.*//p' ./pylint/pylint.log)
      - anybadge --label=Pylint --file=pylint/pylint.svg --value=$PYLINT_SCORE 2=red 4=orange 8=yellow 10=green
      - echo "Pylint score is $PYLINT_SCORE"
  artifacts:
    paths:
      - ./pylint/
    when: always
  only:
      refs:
          - merge_requests
      changes:
          - "**/*.py"

Darker 允许这样做。 2021年2月支持black、isort、mypy、pylint、flake8

This utility reformats and checks Python source code files in a Git repository. However, it only applies reformatting and reports errors in regions which have changed in the Git working tree since the last commit.

找到方法了。我使用 GitLab 变量在合并请求中获取更改的文件并将其输入到 pylint 命令中

 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"
      - mkdir ./pylint
      - pylint --output-format=text $FILES | tee ./pylint/pylint.log || pylint-exit $?

这就是你能做的

.gitlab-ci.yml

stages:
  - Lint

Lint:
  stage: Lint
  allow_failure: true
  script:
  - chmod +x lint.sh
  - ./lint.sh

lint.sh

#! /bin/sh

pip install pycodestyle
current_branch="$CI_BUILD_REF_NAME" 
echo $current_branch
all_changed_files=$(git diff --name-only origin/master origin/$current_branch)
echo "Checking changes!"
for each_file in $all_changed_files
do
# Checks each newly added file change with pycodestyle
pycodestyle $each_file
error_count=$(pycodestyle $each_file --count | wc -l)
if [ $error_count -ge 1 ]; then
    exit 1
fi
if [ $error_count -eq 0 ]; then
    exit 0
fi
done
echo "Completed checking"