[polari/gnome-3-38] ci/check-commit-log: Generate JUnit report



commit 492eaf1452524097a5c8c20b7333cb51c7caa9f3
Author: Jonas Ådahl <jadahl gmail com>
Date:   Fri Nov 6 18:02:23 2020 +0100

    ci/check-commit-log: Generate JUnit report
    
    This means the merge request will see the commit log review issues
    causing the pipeline to fail without having to dig through CI log files.
    
    https://gitlab.gnome.org/GNOME/polari/-/merge_requests/170

 .gitlab-ci.yml                 |  6 ++++++
 .gitlab-ci/check-commit-log.sh | 47 ++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 51 insertions(+), 2 deletions(-)
---
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index b5153f12..d34fe1b2 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -22,6 +22,12 @@ check_commit_log:
         - ./.gitlab-ci/check-commit-log.sh
     only:
         - merge_requests
+    artifacts:
+        expire_in: 1 week
+        paths:
+            - commit-message-junit-report.xml
+        reports:
+            junit: commit-message-junit-report.xml
 
 eslint:
     image: registry.gitlab.gnome.org/gnome/gnome-shell/fedora/33:2020-11-17.0
diff --git a/.gitlab-ci/check-commit-log.sh b/.gitlab-ci/check-commit-log.sh
index 8d0e2ee0..4462cfcb 100755
--- a/.gitlab-ci/check-commit-log.sh
+++ b/.gitlab-ci/check-commit-log.sh
@@ -16,6 +16,42 @@ if [ -z "$commits" ]; then
   exit 1
 fi
 
+JUNIT_REPORT_TESTS_FILE=$(mktemp)
+
+function append_failed_test_case() {
+  test_name="$1"
+  commit="$2"
+  test_message="$3"
+  commit_short=${commit:0:8}
+
+  echo "<testcase name=\"$test_name: $commit_short\"><failure message=\"$commit_short: 
$test_message\"/></testcase>" >> $JUNIT_REPORT_TESTS_FILE
+  echo &>2 "Commit check failed: $commit_short: $test_message"
+}
+
+function append_passed_test_case() {
+  test_name="$1"
+  commit="$2"
+  commit_short=${commit:0:8}
+  echo "<testcase name=\"$test_name: $commit_short\"></testcase>" >> $JUNIT_REPORT_TESTS_FILE
+}
+
+function generate_junit_report() {
+  junit_report_file="$1"
+  num_tests=$(cat "$JUNIT_REPORT_TESTS_FILE" | wc -l)
+  num_failures=$(grep '<failure ' "$JUNIT_REPORT_TESTS_FILE" | wc -l )
+
+  echo Generating JUnit report \"$(pwd)/$junit_report_file\" with $num_tests tests and $num_failures 
failures.
+
+  cat > $junit_report_file << __EOF__
+<?xml version="1.0" encoding="utf-8"?>
+<testsuites tests="$num_tests" errors="0" failures="$num_failures">
+<testsuite name="commit-review" tests="$num_tests" errors="0" failures="$num_failures" skipped="0">
+$(< $JUNIT_REPORT_TESTS_FILE)
+</testsuite>
+</testsuites>
+__EOF__
+}
+
 function commit_message_has_url() {
   commit=$1
   commit_message=$(git show -s --format='format:%b' $commit)
@@ -25,7 +61,14 @@ function commit_message_has_url() {
 
 for commit in $commits; do
   if ! commit_message_has_url $commit; then
-    echo "Missing merge request or issue URL on commit $(echo $commit | cut -c -8)"
-    exit 1
+    append_failed_test_case missing_url $commit \
+      "Commit message must contain a link to an issue or merge request"
+  else
+    append_passed_test_case missing_url $commit
   fi
 done
+
+generate_junit_report commit-message-junit-report.xml
+
+! grep -q '<failure' commit-message-junit-report.xml
+exit $?


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