Commit 58c00d54 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

Merge branch 'feat/configurable-pylint-severity-threshold' into 'master'

feat(pylint): add configurable pylint severity threshold

See merge request to-be-continuous/python!157
parents dd5d4d96 ad0b894a
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -147,6 +147,7 @@ It is bound to the `build` stage, and uses the following variables:
| `pylint-enabled` / `PYLINT_ENABLED` | Set to `true` to enable the `pylint` job                  | _none_ (disabled) |
| `pylint-args` / `PYLINT_ARGS` | Additional [pylint CLI options](http://pylint.pycqa.org/en/latest/user_guide/run.html#command-line-options) |  _none_           |
| `pylint-files` / `PYLINT_FILES` | Files or directories to analyse   | _none_ (by default analyses all found python source files) |
| `pylint-severity` / `PYLINT_SEVERITY` | The minimum [pylint message level](https://pylint.readthedocs.io/en/latest/user_guide/messages/#messages-categories) that will fail the job (lower severity findings will appear as a non-failing warning) -  (one of: `fatal`, `error`, `warning`, `convention`, `refactor`, `any`) | `any` |

In addition to a textual report in the console, this job produces the following reports, kept for one day:

+7 −0
Original line number Diff line number Diff line
@@ -81,6 +81,13 @@
          "name": "PYLINT_FILES",
          "description": "Files or directories to analyse",
          "advanced": true
        },
        {
          "name": "PYLINT_SEVERITY",
          "description": "The minimum [pylint message level](https://pylint.readthedocs.io/en/latest/user_guide/messages/#messages-categories) that will fail the job (lower severity findings will appear as a non-failing warning)",
          "type": "enum",
          "values": ["fatal", "error", "warning", "convention", "refactor", "any"],
          "default": "any"
        }
      ]
    },
+39 −1
Original line number Diff line number Diff line
@@ -70,6 +70,16 @@ spec:
    pylint-files:
      description: Files or directories to analyse
      default: ''
    pylint-severity:
      description: The minimum [pylint message level](https://pylint.readthedocs.io/en/latest/user_guide/messages/#messages-categories) that will fail the job (lower severity findings will appear as a non-failing warning)
      options:
      - fatal
      - error
      - warning
      - convention
      - refactor
      - any
      default: any
    unittest-enabled:
      description: Enable unittest
      type: boolean
@@ -290,6 +300,7 @@ variables:
  PYLINT_ENABLED: $[[ inputs.pylint-enabled ]]
  PYLINT_ARGS: $[[ inputs.pylint-args ]]
  PYLINT_FILES: $[[ inputs.pylint-files ]]
  PYLINT_SEVERITY: $[[ inputs.pylint-severity ]]
  UNITTEST_ENABLED: $[[ inputs.unittest-enabled ]]
  UNITTEST_ARGS: $[[ inputs.unittest-args ]]
  PYTEST_ENABLED: $[[ inputs.pytest-enabled ]]
@@ -1160,7 +1171,31 @@ py-lint:
    - install_requirements
    - _pip install pylint_gitlab # codeclimate reports
    # run pylint and generate reports all at once
    - _run pylint --output-format=colorized,pylint_gitlab.GitlabCodeClimateReporter:reports/py-lint.codeclimate.json,parseable:reports/py-lint.parseable.txt ${PYLINT_ARGS} ${PYLINT_FILES:-$(find -type f -name "*.py" -not -path "./.cache/*" -not -path "./.venv/*")}
    - _run pylint --output-format=colorized,pylint_gitlab.GitlabCodeClimateReporter:reports/py-lint.codeclimate.json,parseable:reports/py-lint.parseable.txt ${PYLINT_ARGS} ${PYLINT_FILES:-$(find -type f -name "*.py" -not -path "./.cache/*" -not -path "./.venv/*")} || exit_code=$?
    # check pylint exit code against $PYLINT_SEVERITY
    - |
      if [[ "${exit_code:-0}" == "0" ]]
      then
        exit 0
      fi
      case "$PYLINT_SEVERITY" in
        fatal) mask=1;;
        error) mask=3;;
        warning) mask=7;;
        convention) mask=15;;
        refactor) mask=31;;
        *) mask=63;;
      esac
      if [[ "$((exit_code & mask))" == "0" ]]
      then
        # severity threshold not met: warn
        log_warn "Issues found with severity below the threshold \\e[33;1m${PYLINT_SEVERITY}\\e[0m (original exit code $exit_code)"
        exit 128
      else
        # severity threshold met: fail
        log_error "Issues detected with severity meeting or exceeding threshold \\e[33;1m${PYLINT_SEVERITY}\\e[0m  (original exit code $exit_code)"
        exit 1
      fi
  artifacts:
    name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
    expire_in: 1 day
@@ -1169,6 +1204,9 @@ py-lint:
      codequality: $PYTHON_PROJECT_DIR/reports/py-lint.codeclimate.json
    paths:
      - "$PYTHON_PROJECT_DIR/reports/py-lint.*"
  allow_failure:
    exit_codes:
      - 128 # when severity threshold not reached
  rules:
    # exclude if $PYLINT_ENABLED not set
    - if: '$PYLINT_ENABLED != "true"'