From 054b0403499164be5f046ff694e6f8ef94a487ca Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Tue, 6 Feb 2024 14:18:27 +0000
Subject: [PATCH] feat: add black job

Signed-off-by: Lars Bilke <lars.bilke@ufz.de>
---
 README.md                      | 11 +++++++++--
 kicker.json                    |  6 ++++++
 templates/gitlab-ci-python.yml | 19 +++++++++++++++++++
 3 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index 115ea64..8e7250f 100644
--- a/README.md
+++ b/README.md
@@ -4,7 +4,7 @@ This project implements a GitLab CI/CD template to build, test and analyse your
 
 ## 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.
 
 ### Use as a CI/CD component
@@ -266,6 +266,14 @@ 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) |
 
+### `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) |
+
 ### SonarQube analysis
 
 If you're using the SonarQube template to analyse your Python code, here is a sample `sonar-project.properties` file:
@@ -413,7 +421,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"
 ```
 
-
 ## Variants
 
 The Python template can be used in conjunction with template variants to cover specific cases.
diff --git a/kicker.json b/kicker.json
index 95cade1..54b83d4 100644
--- a/kicker.json
+++ b/kicker.json
@@ -240,6 +240,12 @@
           "default": "$CI_JOB_TOKEN"
         }
       ]
+    },
+    {
+      "id": "black",
+      "name": "black",
+      "description": "Code formatting based on [black](https://black.readthedocs.io)",
+      "enable_with": "PYTHON_BLACK_ENABLED"
     }
   ],
   "variants": [
diff --git a/templates/gitlab-ci-python.yml b/templates/gitlab-ci-python.yml
index d802dda..c938018 100644
--- a/templates/gitlab-ci-python.yml
+++ b/templates/gitlab-ci-python.yml
@@ -147,6 +147,10 @@ spec:
 
         _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
+    black-enabled:
+      description: Enable black
+      type: boolean
+      default: false
 ---
 # default workflow rules: Merge Request pipelines
 workflow:
@@ -260,6 +264,8 @@ variables:
   PYTHON_SBOM_DISABLED: $[[ inputs.sbom-disabled ]]
   PYTHON_RELEASE_ENABLED: $[[ inputs.release-enabled ]]
 
+  PYTHON_BLACK_ENABLED: $[[ inputs.black-enabled ]]
+
 
 .python-scripts: &python-scripts |
   # BEGSCRIPT
@@ -860,6 +866,19 @@ py-compile:
       when: never
     - !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]
+
 ###############################################################################################
 #                                      test stage                                             #
 ###############################################################################################
-- 
GitLab