From c5b8d3670e5433ddf76b61f86084677e6cda24b6 Mon Sep 17 00:00:00 2001
From: Robert Rosca <robert.rosca@xfel.eu>
Date: Mon, 8 Feb 2021 10:50:37 +0100
Subject: [PATCH] Update CI to work with pre-commit

Check stage tries to find the commit hash being merged into

Uses that hash to get  a list of changed files

Passes this list of files to pre-commit so that checks only run on
modified files
---
 .gitlab-ci.yml | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 687b779fc..e25f8e359 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -1,11 +1,21 @@
-isort:
-  stage: test
+stages:
+  - check
+  - test
+
+checks:
+  stage: check
+  only: [merge_requests]
   script:
-    - python3 -m pip install --user isort==5.6.4
-    - isort --diff **/*.py && isort -c **/*.py
+    - export PATH=/home/gitlab-runner/.local/bin:$PATH
+    - export CI_MERGE_REQUEST_TARGET_BRANCH_SHA=$(git ls-remote origin $CI_MERGE_REQUEST_TARGET_BRANCH_NAME | cut -d$'\t' -f1)
+    - export FILES=$(git diff $CI_COMMIT_SHA  $CI_MERGE_REQUEST_TARGET_BRANCH_SHA --name-only | tr '\n' ' ')
+    - echo "Running pre-commit on diff from  $CI_COMMIT_SHA to $CI_MERGE_REQUEST_TARGET_BRANCH_SHA ($CI_MERGE_REQUEST_TARGET_BRANCH_NAME)"
+    - python3 -m pip install --user -r requirements.txt
+    - echo $FILES | xargs pre-commit run --color=always --files
 
 pytest:
   stage: test
+  only: [merge_requests]
   script:
     - python3 -m pip install --user -r requirements.txt
     - python3 -m pip install --user pytest
-- 
GitLab