Skip to content
Snippets Groups Projects
Commit 9c84bd58 authored by Pierre Smeyers's avatar Pierre Smeyers
Browse files

Merge branch 'feat/ruff-format' into 'master'

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

Closes #86

See merge request to-be-continuous/python!124
parents 0c1fc133 142589f2
No related branches found
No related tags found
No related merge requests found
...@@ -298,9 +298,8 @@ This job **disabled by default** and runs [Ruff](https://docs.astral.sh/ruff/) o ...@@ -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-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-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: 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 ...@@ -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.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_ | | `$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 #### `py-mypy` job
This job is **disabled by default** and performs code analysis based on [mypy](https://mypy.readthedocs.io/en/stable/). This job is **disabled by default** and performs code analysis based on [mypy](https://mypy.readthedocs.io/en/stable/).
......
...@@ -263,12 +263,15 @@ ...@@ -263,12 +263,15 @@
"name": "RUFF_ARGS", "name": "RUFF_ARGS",
"description": "Additional [Ruff Linter CLI options](https://docs.astral.sh/ruff/configuration/#full-command-line-interface)", "description": "Additional [Ruff Linter CLI options](https://docs.astral.sh/ruff/configuration/#full-command-line-interface)",
"advanced": true "advanced": true
}
]
}, },
{ {
"name": "RUFF_EXT_EXCLUDE", "id": "ruff-format",
"description": "Define [extend-exclude](https://docs.astral.sh/ruff/settings/#extend-exclude) files", "name": "Ruff Format",
"advanced": true "description": "An extremely fast Python linter and code formatter, written in Rust. [Ruff](https://docs.astral.sh/ruff/)",
} "enable_with": "RUFF_FORMAT_ENABLED",
"variables": [
] ]
}, },
{ {
......
...@@ -166,9 +166,10 @@ spec: ...@@ -166,9 +166,10 @@ spec:
ruff-args: ruff-args:
description: Additional [Ruff Linter CLI options](https://docs.astral.sh/ruff/configuration/#full-command-line-interface) description: Additional [Ruff Linter CLI options](https://docs.astral.sh/ruff/configuration/#full-command-line-interface)
default: "" default: ""
ruff-ext-exclude: ruff-format-enabled:
description: Define [extend-exclude](https://docs.astral.sh/ruff/settings/#extend-exclude) files description: Enable Ruff
default: "" type: boolean
default: false
mypy-enabled: mypy-enabled:
description: Enable mypy description: Enable mypy
type: boolean type: boolean
...@@ -291,7 +292,7 @@ variables: ...@@ -291,7 +292,7 @@ variables:
PYTHON_ISORT_ENABLED: $[[ inputs.isort-enabled ]] PYTHON_ISORT_ENABLED: $[[ inputs.isort-enabled ]]
RUFF_ENABLED: $[[ inputs.ruff-enabled ]] RUFF_ENABLED: $[[ inputs.ruff-enabled ]]
RUFF_ARGS: $[[ inputs.ruff-args ]] 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_ENABLED: $[[ inputs.mypy-enabled ]]
MYPY_ARGS: $[[ inputs.mypy-args ]] MYPY_ARGS: $[[ inputs.mypy-args ]]
MYPY_FILES: $[[ inputs.mypy-files ]] MYPY_FILES: $[[ inputs.mypy-files ]]
...@@ -1063,8 +1064,8 @@ py-ruff: ...@@ -1063,8 +1064,8 @@ py-ruff:
script: script:
- mkdir -p -m 777 reports - mkdir -p -m 777 reports
- | - |
if [[ ${BANDIT_ENABLED} == "true" || ${PYLINT_ENABLED} == "true" || ${PYTHON_ISORT_ENABLED} == "true" || ${PYTHON_BLACK_ENABLED} == "true" ]]; then if [[ ${BANDIT_ENABLED} == "true" || ${PYLINT_ENABLED} == "true" || ${PYTHON_ISORT_ENABLED} == "true" ]]; then
log_warn "Ruff can replace isort, Black, Bandit, Pylint" log_warn "Ruff can replace isort, Bandit, Pylint"
fi fi
# Ruff is self dependent tool (written in Rust), it can be installed without project dependencies (_pip and _run don't look required here) # 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 - pip install ${PIP_OPTS} ruff
...@@ -1072,10 +1073,10 @@ py-ruff: ...@@ -1072,10 +1073,10 @@ py-ruff:
- | - |
if [[ "$SONAR_HOST_URL" ]] if [[ "$SONAR_HOST_URL" ]]
then 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 fi
# then GitLab and grouped/console formats # 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: artifacts:
name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG" name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
expire_in: 1 day expire_in: 1 day
...@@ -1090,6 +1091,24 @@ py-ruff: ...@@ -1090,6 +1091,24 @@ py-ruff:
when: never when: never
- !reference [.test-policy, rules] - !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: py-mypy:
extends: .python-base extends: .python-base
stage: build stage: build
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment