简体   繁体   English

如何仅在 Gitlab 中更改的文件上运行 pylint?

[英]How to run pylint only on changed files in Gitlab?

I'm trying to run pylint only on changed python files, but my build keeps failing.我试图仅在已更改的 python 文件上运行 pylint,但我的构建一直失败。 I have extracted the changed files through git diff and saved them in a variable, but when I inject the variable into the pylint call, it fails.我已经通过 git diff 提取了更改的文件并将它们保存在一个变量中,但是当我将变量注入 pylint 调用时,它失败了。 It works fine with a hardcoded filename however.但是,它适用于硬编码文件名。 Here is my yaml:这是我的 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.]*\)\/.*/\1/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 permits to do that. Darker允许这样做。 It supports black, isort, mypy, pylint, and flake8 in february 2021. 2021年2月支持black、isort、mypy、pylint和flake8。

This utility reformats and checks Python source code files in a Git repository.此实用程序重新格式化并检查 Git 存储库中的 Python 源代码文件。 However, it only applies reformatting and reports errors in regions which have changed in the Git working tree since the last commit.但是,它仅适用于重新格式化并报告自上次提交以来 Git 工作树中已更改的区域中的错误。

Found a way to do it.找到了一种方法来做到这一点。 I the GitLab variables to get changed files in the merge request and input that into the pylint command我使用 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 $?

This is what you can do这是你可以做的

.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"

While qathulu answer is adequate, it does not work if no python files are changed.虽然 qathulu 答案已经足够,但如果没有更改 python 文件,它就不起作用。 The below prints all the changed files, then use grep to find the changed python files.下面打印所有变化的文件,然后使用grep找到变化的python文件。

Unfortunately there is an error on gitlab, where assigning empty output from grep ends the pipeline.不幸的是,在 gitlab 上有一个错误,从 grep 分配空的 output 结束了管道。 In order to fix that, grep is not assigned to variable until we are sure at least one python file is changed.为了解决这个问题,在我们确定至少有一个 python 文件被更改之前,不会将 grep 分配给变量。

- 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}
- tmp_files=$(git diff --name-only ${CI_COMMIT_SHA} origin/${CI_MERGE_REQUEST_TARGET_BRANCH_NAME})
- echo "Changed files are $tmp_files"
- if [ -z "$(echo "$tmp_files" | grep "\.py")" ]; then exit 0; else echo "Python files are found"; fi
- tmp_pfiles=$(echo "$tmp_files" | grep "\.py")
- echo "Python files are $tmp_pfiles"
- mkdir ./pylint
- pylint --output-format=text $tmp_pfiles | tee ./pylint/pylint.log || pylint-exit $?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM