Commit 142589f2 authored by Gaëtan Montury's avatar Gaëtan Montury Committed by Montury Gaëtan
Browse files

feat(Ruff): add `ruff-format` job for code formatting

parent 0c1fc133
Loading
Loading
Loading
Loading
+11 −2
Original line number Diff line number Diff line
@@ -298,9 +298,8 @@ This job **disabled by default** and runs [Ruff](https://docs.astral.sh/ruff/) o
| ---------------- | ----------------------------------------------------------------------- | ----------------- |
| `ruff-enabled` / `RUFF_ENABLED` | Set to `true` to enable ruff job                  | _none_ (disabled) |
| `ruff-args` / `RUFF_ARGS` | Additional [Ruff Linter CLI options](https://docs.astral.sh/ruff/configuration/#full-command-line-interface)     | _none_           |
| `ruff-ext-exclude` / `RUFF_EXT_EXCLUDE` | Define [extend-exclude](https://docs.astral.sh/ruff/settings/#extend-exclude) files                             | _.venv,.cache_   |

:warning: Ruff can replace isort, Black, Bandit, Pylint and much more. [More info](https://github.com/astral-sh/ruff/blob/main/docs/faq.md#which-tools-does-ruff-replace).
:warning: Ruff can replace isort, Bandit, Pylint and much more. [More info](https://github.com/astral-sh/ruff/blob/main/docs/faq.md#which-tools-does-ruff-replace).

In addition to logs in the console, this job produces the following reports, kept for one week:

@@ -309,6 +308,16 @@ In addition to logs in the console, this job produces the following reports, kep
| `$PYTHON_PROJECT_DIR/reports/py-ruff.gitlab.json` | [GitLab](https://docs.astral.sh/ruff/settings/#output-format) | [GitLab integration](https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html#artifactsreportscodequality) |
| `$PYTHON_PROJECT_DIR/reports/py-ruff.native.json` | [JSON](https://docs.astral.sh/ruff/settings/#output-format) | [SonarQube integration](https://docs.sonarqube.org/latest/analysis/external-issues/)<br/>_This report is generated only if SonarQube template is detected_ |

### `py-ruff-format` job

This job **disabled by default** and runs [Ruff](https://docs.astral.sh/ruff/) on the repo. It is bound to the build stage.

| Input / Variable | Description                                                             | Default value     |
| ---------------- | ----------------------------------------------------------------------- | ----------------- |
| `ruff-format-enabled` / `RUFF_FORMAT_ENABLED` | Set to `true` to enable ruff job                  | _none_ (disabled) |

:warning: Ruff can replace Black and much more. [More info](https://github.com/astral-sh/ruff/blob/main/docs/faq.md#which-tools-does-ruff-replace).

#### `py-mypy` job

This job is **disabled by default** and performs code analysis based on [mypy](https://mypy.readthedocs.io/en/stable/).
+8 −5
Original line number Diff line number Diff line
@@ -263,12 +263,15 @@
          "name": "RUFF_ARGS",
          "description": "Additional [Ruff Linter CLI options](https://docs.astral.sh/ruff/configuration/#full-command-line-interface)",
          "advanced": true
        }
      ]
    },
    {
          "name": "RUFF_EXT_EXCLUDE",
          "description": "Define [extend-exclude](https://docs.astral.sh/ruff/settings/#extend-exclude) files",
          "advanced": true
        }
      "id": "ruff-format",
      "name": "Ruff Format",
      "description": "An extremely fast Python linter and code formatter, written in Rust. [Ruff](https://docs.astral.sh/ruff/)",
      "enable_with": "RUFF_FORMAT_ENABLED",
      "variables": [
      ]
    },
    {
+27 −8
Original line number Diff line number Diff line
@@ -166,9 +166,10 @@ spec:
    ruff-args:
      description: Additional [Ruff Linter CLI options](https://docs.astral.sh/ruff/configuration/#full-command-line-interface)
      default: ""
    ruff-ext-exclude:
      description: Define [extend-exclude](https://docs.astral.sh/ruff/settings/#extend-exclude) files
      default: ""
    ruff-format-enabled:
      description: Enable Ruff
      type: boolean
      default: false
    mypy-enabled:
      description: Enable mypy
      type: boolean
@@ -291,7 +292,7 @@ variables:
  PYTHON_ISORT_ENABLED: $[[ inputs.isort-enabled ]]
  RUFF_ENABLED: $[[ inputs.ruff-enabled ]]
  RUFF_ARGS: $[[ inputs.ruff-args ]]
  RUFF_EXT_EXCLUDE: $[[ inputs.ruff-ext-exclude ]]
  RUFF_FORMAT_ENABLED: $[[ inputs.ruff-format-enabled ]]
  MYPY_ENABLED: $[[ inputs.mypy-enabled ]]
  MYPY_ARGS: $[[ inputs.mypy-args ]]
  MYPY_FILES: $[[ inputs.mypy-files ]]
@@ -1063,8 +1064,8 @@ py-ruff:
  script:
    - mkdir -p -m 777 reports
    - |
      if [[  ${BANDIT_ENABLED} == "true" || ${PYLINT_ENABLED} == "true" || ${PYTHON_ISORT_ENABLED} == "true" || ${PYTHON_BLACK_ENABLED} == "true" ]]; then
        log_warn "Ruff can replace isort, Black, Bandit, Pylint"
      if [[ ${BANDIT_ENABLED} == "true" || ${PYLINT_ENABLED} == "true" || ${PYTHON_ISORT_ENABLED} == "true" ]]; then
        log_warn "Ruff can replace isort, Bandit, Pylint"
      fi
    # Ruff is self dependent tool (written in Rust), it can be installed without project dependencies (_pip and _run don't look required here)
    - pip install ${PIP_OPTS} ruff
@@ -1072,10 +1073,10 @@ py-ruff:
    - |
      if [[ "$SONAR_HOST_URL" ]]
      then
        ruff check . ${RUFF_ARGS} ${RUFF_EXCLUDE:---extend-exclude .venv,.cache} --exit-zero --output-format json --output-file reports/py-ruff.native.json
        ruff check . ${RUFF_ARGS} --extend-exclude .venv,.cache --exit-zero --output-format json --output-file reports/py-ruff.native.json
      fi
    # then GitLab and grouped/console formats
    - ruff check . ${RUFF_ARGS} ${RUFF_EXCLUDE:---extend-exclude .venv,.cache} --output-format gitlab --output-file reports/py-ruff.gitlab.json || ruff check . ${RUFF_ARGS} ${RUFF_EXCLUDE:---extend-exclude .venv,.cache} --output-format grouped
    - ruff check . ${RUFF_ARGS} --extend-exclude .venv,.cache --output-format gitlab --output-file reports/py-ruff.gitlab.json || ruff check . ${RUFF_ARGS} --extend-exclude .venv,.cache --output-format grouped
  artifacts:
    name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
    expire_in: 1 day
@@ -1090,6 +1091,24 @@ py-ruff:
      when: never
    - !reference [.test-policy, rules]

py-ruff-format:
  extends: .python-base
  stage: build
  script:
    - mkdir -p -m 777 reports
    - |
      if [[ ${PYTHON_BLACK_ENABLED} == "true" ]]; then
        log_warn "Ruff can replace Black"
      fi
    # Ruff is self dependent tool (written in Rust), it can be installed without project dependencies (_pip and _run don't look required here)
    - pip install ${PIP_OPTS} ruff
    - ruff format --check . --exclude .venv,.cache
  rules:
    # exclude if $RUFF_FORMAT_ENABLED not set
    - if: '$RUFF_FORMAT_ENABLED != "true"'
      when: never
    - !reference [.test-policy, rules]

py-mypy:
  extends: .python-base
  stage: build