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

Merge branch 'black' into 'master'

feat: add black and isort jobs

See merge request to-be-continuous/python!98
parents 507fb740 e8e068ba
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ This project implements a GitLab CI/CD template to build, test and analyse your ...@@ -4,7 +4,7 @@ This project implements a GitLab CI/CD template to build, test and analyse your
## Usage ## Usage
This template can be used both as a [CI/CD component](https://docs.gitlab.com/ee/ci/components/#use-a-component-in-a-cicd-configuration) This template can be used both as a [CI/CD component](https://docs.gitlab.com/ee/ci/components/#use-a-component-in-a-cicd-configuration)
or using the legacy [`include:project`](https://docs.gitlab.com/ee/ci/yaml/index.html#includeproject) syntax. or using the legacy [`include:project`](https://docs.gitlab.com/ee/ci/yaml/index.html#includeproject) syntax.
### Use as a CI/CD component ### Use as a CI/CD component
...@@ -266,6 +266,23 @@ In addition to logs in the console, this job produces the following reports, kep ...@@ -266,6 +266,23 @@ In addition to logs in the console, this job produces the following reports, kep
| -------------- | ---------------------------------------------------------------------------- | ----------------- | | -------------- | ---------------------------------------------------------------------------- | ----------------- |
| `$PYTHON_PROJECT_DIR/reports/py-sbom.cyclonedx.json` | [CycloneDX JSON](https://cyclonedx.org/docs/latest/json/) | [Security & Compliance integration](https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html#artifactsreportscyclonedx) | | `$PYTHON_PROJECT_DIR/reports/py-sbom.cyclonedx.json` | [CycloneDX JSON](https://cyclonedx.org/docs/latest/json/) | [Security & Compliance integration](https://docs.gitlab.com/ee/ci/yaml/artifacts_reports.html#artifactsreportscyclonedx) |
### `py-black` job
This job **disabled by default** and runs [black](https://black.readthedocs.io) on the repo. It is bound to the build stage.
| Input / Variable | Description | Default value |
| ---------------- | ----------------------------------------------------------------------- | ----------------- |
| `black-enabled` / `PYTHON_BLACK_ENABLED` | Set to `true` to enable black job | _none_ (disabled) |
### `py-isort` job
This job **disabled by default** and runs [isort](https://pycqa.github.io/isort/) on the repo. It is bound to the build stage.
| Input / Variable | Description | Default value |
| ---------------- | ----------------------------------------------------------------------- | ----------------- |
| `isort-enabled` / `PYTHON_ISORT_ENABLED` | Set to `true` to enable isort job | _none_ (disabled) |
### SonarQube analysis ### SonarQube analysis
If you're using the SonarQube template to analyse your Python code, here is a sample `sonar-project.properties` file: If you're using the SonarQube template to analyse your Python code, here is a sample `sonar-project.properties` file:
...@@ -413,7 +430,6 @@ variables: ...@@ -413,7 +430,6 @@ variables:
PIP_EXTRA_INDEX_URL: "${CI_SERVER_PROTOCOL}://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_HOST}:${CI_SERVER_PORT}/api/v4/groups/<group-id>/-/packages/pypi/simple" PIP_EXTRA_INDEX_URL: "${CI_SERVER_PROTOCOL}://gitlab-ci-token:${CI_JOB_TOKEN}@${CI_SERVER_HOST}:${CI_SERVER_PORT}/api/v4/groups/<group-id>/-/packages/pypi/simple"
``` ```
## Variants ## Variants
The Python template can be used in conjunction with template variants to cover specific cases. The Python template can be used in conjunction with template variants to cover specific cases.
......
...@@ -240,6 +240,18 @@ ...@@ -240,6 +240,18 @@
"default": "$CI_JOB_TOKEN" "default": "$CI_JOB_TOKEN"
} }
] ]
},
{
"id": "black",
"name": "black",
"description": "Code formatting based on [black](https://black.readthedocs.io)",
"enable_with": "PYTHON_BLACK_ENABLED"
},
{
"id": "isort",
"name": "isort",
"description": "Check imports order with [isort](https://pycqa.github.io/isort)",
"enable_with": "PYTHON_ISORT_ENABLED"
} }
], ],
"variants": [ "variants": [
......
...@@ -147,6 +147,14 @@ spec: ...@@ -147,6 +147,14 @@ spec:
_defaults to [GitLab project's packages repository](https://docs.gitlab.com/ee/user/packages/pypi_repository/)_ _defaults to [GitLab project's packages repository](https://docs.gitlab.com/ee/user/packages/pypi_repository/)_
default: ${CI_SERVER_URL}/api/v4/projects/${CI_PROJECT_ID}/packages/pypi default: ${CI_SERVER_URL}/api/v4/projects/${CI_PROJECT_ID}/packages/pypi
black-enabled:
description: Enable black
type: boolean
default: false
isort-enabled:
description: Enable isort
type: boolean
default: false
--- ---
# default workflow rules: Merge Request pipelines # default workflow rules: Merge Request pipelines
workflow: workflow:
...@@ -260,6 +268,9 @@ variables: ...@@ -260,6 +268,9 @@ variables:
PYTHON_SBOM_DISABLED: $[[ inputs.sbom-disabled ]] PYTHON_SBOM_DISABLED: $[[ inputs.sbom-disabled ]]
PYTHON_RELEASE_ENABLED: $[[ inputs.release-enabled ]] PYTHON_RELEASE_ENABLED: $[[ inputs.release-enabled ]]
PYTHON_BLACK_ENABLED: $[[ inputs.black-enabled ]]
PYTHON_ISORT_ENABLED: $[[ inputs.isort-enabled ]]
.python-scripts: &python-scripts | .python-scripts: &python-scripts |
# BEGSCRIPT # BEGSCRIPT
...@@ -860,6 +871,32 @@ py-compile: ...@@ -860,6 +871,32 @@ py-compile:
when: never when: never
- !reference [.test-policy, rules] - !reference [.test-policy, rules]
py-black:
extends: .python-base
stage: build
script:
- install_requirements
- _pip install black
- _run black . --check
rules:
# exclude if $PYTHON_BLACK_ENABLED not set
- if: '$PYTHON_BLACK_ENABLED != "true"'
when: never
- !reference [.test-policy, rules]
py-isort:
extends: .python-base
stage: build
script:
- install_requirements
- _pip install isort
- _run isort . --check-only
rules:
# exclude if $PYTHON_ISORT_ENABLED not set
- if: '$PYTHON_ISORT_ENABLED != "true"'
when: never
- !reference [.test-policy, rules]
############################################################################################### ###############################################################################################
# test stage # # test stage #
############################################################################################### ###############################################################################################
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment