[gnome-shell] ci: Add eslint job to review stage



commit 3b5d13a0b262eb6559b26c6a7f4cb1f870a2c172
Author: Florian Müllner <fmuellner gnome org>
Date:   Sun Jun 30 13:05:04 2019 +0200

    ci: Add eslint job to review stage
    
    We have now reduced the number of eslint errors enough to add it to
    the CI pipeline. There are still plenty of errors left though, so we
    cannot simply run eslint and fail on any errors. So instead, run it
    through a fancy script that:
    
     - generates an eslint report using the "regular" configuration
     - generates an eslint report using the "legacy" configuration
     - creates a combined report with errors common to both configurations
    
    When the pipeline is running for a branch or tag, the final report is
    printed out and the job succeeds (we know there are errors left);
    when the pipelne is running for a merge request, we fail if any errors
    are reported for the lines modified/added by the MR.
    
    https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/627

 .gitlab-ci.yml           | 11 ++++++
 .gitlab-ci/run-eslint.sh | 94 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 105 insertions(+)
---
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 7625b8711..281d75c97 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -38,6 +38,17 @@ js_check:
             - ${JS_LOG}
         when: on_failure
 
+eslint:
+    image: registry.gitlab.gnome.org/gnome/gnome-shell/extension-ci:v1
+    stage: review
+    script:
+        - ./.gitlab-ci/run-eslint.sh
+    <<: *only_default
+    artifacts:
+        paths:
+            - reports
+        when: always
+
 build:
     image: registry.gitlab.gnome.org/gnome/mutter/master:v2
     stage: build
diff --git a/.gitlab-ci/run-eslint.sh b/.gitlab-ci/run-eslint.sh
new file mode 100755
index 000000000..4732f986f
--- /dev/null
+++ b/.gitlab-ci/run-eslint.sh
@@ -0,0 +1,94 @@
+#!/usr/bin/bash
+
+OUTPUT_REGULAR=reports/lint-regular-report.txt
+OUTPUT_LEGACY=reports/lint-legacy-report.txt
+OUTPUT_FINAL=reports/lint-common-report.txt
+
+OUTPUT_MR=reports/lint-mr-report.txt
+
+LINE_CHANGES=changed-lines.txt
+
+is_empty() {
+  (! grep -q . $1)
+}
+
+run_eslint() {
+  ARGS_LEGACY='--config lint/eslintrc-legacy.json'
+
+  local extra_args=ARGS_$1
+  local output=OUTPUT_$1
+  eslint -f unix ${!extra_args} -o ${!output} js
+}
+
+list_commit_range_additions() {
+  # Turn raw context-less git-diff into a list of
+  # filename:lineno pairs of new (+) lines
+  git diff -U0 "$@" -- js |
+  awk '
+    BEGIN { file=""; }
+    /^+++ b/ { file=substr($0,7); }
+    /^@@ / {
+        len = split($3,a,",")
+        start=a[1]
+        count=(len > 1) ? a[2] : 1
+
+        for (line=start; line<start+count; line++)
+            printf "%s/%s:%d:\n",ENVIRON["PWD"],file,line;
+    }'
+}
+
+copy_matched_lines() {
+  local source=$1
+  local matches=$2
+  local target=$3
+
+  echo -n > $target
+  for l in $(<$matches); do
+    grep $l $source >> $target
+  done
+}
+
+create_common() {
+  # comm requires sorted input;
+  # we also strip the error message to make the following a "common" error:
+  # regular:
+  #  file.js:42:23 Indentation of 55, expected 42
+  # legacy:
+  #  file.js:42:23 Indentation of 55, extected 24
+  prepare() {
+    sed 's: .*::' $1 | sort
+  }
+
+  comm -12 <(prepare $OUTPUT_REGULAR) <(prepare $OUTPUT_LEGACY) >$OUTPUT_FINAL.tmp
+
+  # Now add back the stripped error messages
+  copy_matched_lines $OUTPUT_REGULAR $OUTPUT_FINAL.tmp $OUTPUT_FINAL
+  rm $OUTPUT_FINAL.tmp
+}
+
+if [ "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" ]; then
+  commit_range=origin/$CI_MERGE_REQUEST_TARGET_BRANCH_NAME...$CI_COMMIT_SHA
+  list_commit_range_additions $commit_range > $LINE_CHANGES
+
+  # Don't bother with running lint when no JS changed
+  if is_empty $LINE_CHANGES; then
+    exit 0
+  fi
+fi
+
+echo Generating lint report using regular configuration
+run_eslint REGULAR
+echo Generating lint report using legacy configuration
+run_eslint LEGACY
+echo Done.
+create_common
+
+# Just show the report and succeed when not testing a MR
+if [ -z "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" ]; then
+  cat $OUTPUT_FINAL
+  exit 0
+fi
+
+copy_matched_lines $OUTPUT_FINAL $LINE_CHANGES $OUTPUT_MR
+cat $OUTPUT_MR
+is_empty $OUTPUT_MR


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]