From 8ef77cdbd0b222513bdaaa6bebbc42d145b5b35d Mon Sep 17 00:00:00 2001
From: Sergiusz Bazanski <q3k@q3k.org>
Date: Sun, 28 Jul 2019 20:03:03 +0200
Subject: [PATCH] ci: implement lint & Annoyatron

We want to run a linter on CI submission.

We also want to display nice error messages to the user. Thus,
annoyatron is born.

The flow is as follows:
 - a MR is submitted and triggers the 'lint' pipeline
 - annoyatron gets pinged over https that an MR requires attention
 - annoyatron starts running and waits until the MR's pipeline run
   succeeds or fails
 - the lint jobs either succeeds or fails
 - annoyatron notices the pipeline passed, and inspects its results to
   see if the lint pipeline failed or not.
 - annoyatron posts a comment, if necessary
---
 .gitlab-ci.yml             | 16 ++++++++++++++++
 docker/lint-env/Dockerfile | 15 +++++++++++++++
 docker/lint-env/push.sh    | 17 +++++++++++++++++
 tools/code-style.sh        |  3 +--
 4 files changed, 49 insertions(+), 2 deletions(-)
 create mode 100644 docker/lint-env/Dockerfile
 create mode 100755 docker/lint-env/push.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 7e7dd356..4b893897 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -8,6 +8,22 @@ build:
         - ninja -C build/
         - arm-none-eabi-size build/bootloader/bootloader.elf build/epicardium/epicardium.elf build/pycardium/pycardium.elf
 
+lint:
+    stage: test
+    image: "registry.k0.hswaw.net/q3k/card10-lint-env:20190728-233522Z-ded74f0-dirty"
+    script:
+        # Annoyatron is maintained by q3k. Its job is to serve MR comments that are friendlier than just a failing pipeline.
+        #  source code: https://git.card10.badge.events.ccc.de/q3k/annoyatron/
+        #  prod deployment: https://gerrit.hackerspace.pl/plugins/gitiles/hscloud/+/refs/heads/master/personal/q3k/annoyatron/
+        # If this starts failing for any reason, just remove this curl ping.
+        - curl --fail https://annoyatron-prod.q3k.org/ping/mr?mr=${CI_MERGE_REQUEST_IID}
+        - git remote rm card10 || true # old gitlab runners might have this remote.
+        - git fetch https://git.card10.badge.events.ccc.de/card10/firmware.git master:card10/master
+        - git diff --name-only $(git merge-base --fork-point card10/master)...HEAD | xargs tools/code-style.sh
+        - git diff --exit-code
+    only:
+        - merge_requests
+
 pages:
     stage: deploy
     # maintaned by q3k, build using docker/deploy-env
diff --git a/docker/lint-env/Dockerfile b/docker/lint-env/Dockerfile
new file mode 100644
index 00000000..0085b57c
--- /dev/null
+++ b/docker/lint-env/Dockerfile
@@ -0,0 +1,15 @@
+FROM ubuntu:bionic
+
+RUN set -e -x ;\
+    export DEBIAN_FRONTEND=noninteractive ;\
+    apt-get update -y ;\
+    apt-get install -y \
+        curl \
+        ca-certificates \
+        clang-format \
+        git \
+        python3 \
+        python3-pip ;\
+    python3 -m pip install black ;\
+    rm -rf /var/lib/apt/lists
+
diff --git a/docker/lint-env/push.sh b/docker/lint-env/push.sh
new file mode 100755
index 00000000..08cc6092
--- /dev/null
+++ b/docker/lint-env/push.sh
@@ -0,0 +1,17 @@
+#!/usr/bin/env bash
+
+# Build and push the deploy env Docker container to the Gitlab container registry.
+
+set -e
+
+function rev() {
+    ( cd $1; git describe --always --match "v[0-9].*" --dirty )
+}
+
+IMAGE=registry.k0.hswaw.net/q3k/card10-lint-env
+TAG=$(TZ=UTC date +%Y%m%d-%H%M%SZ)-$(rev .)
+
+docker build -t $IMAGE:$TAG .
+docker push $IMAGE:$TAG
+
+echo "Pushed $IMAGE:$TAG"
diff --git a/tools/code-style.sh b/tools/code-style.sh
index b6943dbd..295b77b9 100755
--- a/tools/code-style.sh
+++ b/tools/code-style.sh
@@ -56,6 +56,5 @@ for source_file in "$@"; do
         black -q "$source_file"
     else
         echo " - Ignoring $source_file" >&2
-        continue
-    fi
+	fi
 done
-- 
GitLab