From e8e068ba46130a99ba0c99005d2690b8640a75aa Mon Sep 17 00:00:00 2001
From: Lars Bilke <lars.bilke@ufz.de>
Date: Wed, 21 Feb 2024 11:11:31 +0100
Subject: [PATCH] feat: add isort job

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

diff --git a/README.md b/README.md
index 8e7250f..3d633b3 100644
--- a/README.md
+++ b/README.md
@@ -274,6 +274,15 @@ This job **disabled by default** and runs [black](https://black.readthedocs.io)
 | ---------------- | ----------------------------------------------------------------------- | ----------------- |
 | `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
 
 If you're using the SonarQube template to analyse your Python code, here is a sample `sonar-project.properties` file:
diff --git a/kicker.json b/kicker.json
index 54b83d4..8ec4b45 100644
--- a/kicker.json
+++ b/kicker.json
@@ -246,6 +246,12 @@
       "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": [
diff --git a/templates/gitlab-ci-python.yml b/templates/gitlab-ci-python.yml
index c938018..680bd42 100644
--- a/templates/gitlab-ci-python.yml
+++ b/templates/gitlab-ci-python.yml
@@ -151,6 +151,10 @@ spec:
       description: Enable black
       type: boolean
       default: false
+    isort-enabled:
+      description: Enable isort
+      type: boolean
+      default: false
 ---
 # default workflow rules: Merge Request pipelines
 workflow:
@@ -265,6 +269,7 @@ variables:
   PYTHON_RELEASE_ENABLED: $[[ inputs.release-enabled ]]
 
   PYTHON_BLACK_ENABLED: $[[ inputs.black-enabled ]]
+  PYTHON_ISORT_ENABLED: $[[ inputs.isort-enabled ]]
 
 
 .python-scripts: &python-scripts |
@@ -879,6 +884,19 @@ py-black:
       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                                             #
 ###############################################################################################
-- 
GitLab