From fca03273ec05ada1763987f837a80dc589a2ff26 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Wed, 6 Mar 2024 18:33:57 +1100 Subject: [PATCH] chore(ci): add annotations to GH on Go test failures --- .github/workflows/ci.yml | 19 +++++-------------- .gitignore | 1 + bin/.gotestsum-1.11.0.pkg | 1 + bin/gotestsum | 1 + scripts/go-test-annotate | 23 +++++++++++++++++++++++ scripts/to-annotation | 10 ++++++++++ 6 files changed, 41 insertions(+), 14 deletions(-) create mode 120000 bin/.gotestsum-1.11.0.pkg create mode 120000 bin/gotestsum create mode 100755 scripts/go-test-annotate create mode 100755 scripts/to-annotation diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 46524b5a89..6df567d486 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,7 +33,7 @@ jobs: - name: Docker Compose run: docker compose up -d --wait - name: Test - run: go test ./... + run: go-test-annotate sql: name: SQL runs-on: ubuntu-latest @@ -61,11 +61,11 @@ jobs: - name: Build Cache uses: ./.github/actions/build-cache - name: golangci-lint - run: golangci-lint run + run: golangci-lint run --out-format github-actions ./... - name: go-check-sumtype - run: go-check-sumtype ./... + run: go-check-sumtype ./... | to-annotation - name: actionlint - run: actionlint --oneline + run: actionlint --oneline | to-annotation # Too annoying to disable individual warnings # - name: staticcheck # run: staticcheck ./... @@ -82,16 +82,7 @@ jobs: - name: Init Hermit uses: cashapp/activate-hermit@v1 - name: Proto Breaking Change Check - run: | - buf breaking --against 'https://github.com/TBD54566975/ftl.git#branch=main' | while read -r line; do - # Extract the file path, line number, and column number from the warning message - file_path=$(echo "$line" | cut -d':' -f1) - line_number=$(echo "$line" | cut -d':' -f2) - column_number=$(echo "$line" | cut -d':' -f3) - - # Output the warning message in the format expected by GitHub Actions - echo "::error file=$file_path,line=$line_number,col=$column_number::$line" - done + run: buf breaking --against 'https://github.com/TBD54566975/ftl.git#branch=main' | to-annotation console: name: Console runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index 3fffcf6724..edfdc1ebc3 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ testdata/**/go.work.sum **/_ftl buildengine/.gitignore go.work* +junit*.xml \ No newline at end of file diff --git a/bin/.gotestsum-1.11.0.pkg b/bin/.gotestsum-1.11.0.pkg new file mode 120000 index 0000000000..383f4511d4 --- /dev/null +++ b/bin/.gotestsum-1.11.0.pkg @@ -0,0 +1 @@ +hermit \ No newline at end of file diff --git a/bin/gotestsum b/bin/gotestsum new file mode 120000 index 0000000000..1471d7ff99 --- /dev/null +++ b/bin/gotestsum @@ -0,0 +1 @@ +.gotestsum-1.11.0.pkg \ No newline at end of file diff --git a/scripts/go-test-annotate b/scripts/go-test-annotate new file mode 100755 index 0000000000..fb04c8ff69 --- /dev/null +++ b/scripts/go-test-annotate @@ -0,0 +1,23 @@ +#!/bin/bash +set -euo pipefail + +# This is convoluted because Go's -json output is basically just a JSON struct +# per line of output, and gotestsum doesn't include structured line output in +# the junit.xml file. So we parse the XML, extracting the failure messages and +# then match lines containing lines that look like error messages. + +fail=0 +if ! gotestsum --format-hide-empty-pkg --junitfile junit-go.xml --junitfile-hide-empty-pkg -- -fullpath ./...; then + fail=1 +fi + +dir="$(pwd)" +yq -oj '.. | select(has("failure")) | .failure["+content"]' junit-go.xml | while read -r failure; do + line="$(echo "$failure" | jq -r | grep -E "$dir/.*\.go:[0-9]+" | sed -e "s,^[ ]*$dir/,,")" + file_path=$(echo "$line" | cut -d':' -f1) + line_number=$(echo "$line" | cut -d':' -f2) + # Output the warning message in the format expected by GitHub Actions + echo "::error file=$file_path,line=$line_number::$(echo "$failure" | jq -r . | sed -e "s,$dir/,," | sed -e 's,$,%0A,g' | tr -d '\n')" +done + +exit "$fail" \ No newline at end of file diff --git a/scripts/to-annotation b/scripts/to-annotation new file mode 100755 index 0000000000..3548662db8 --- /dev/null +++ b/scripts/to-annotation @@ -0,0 +1,10 @@ +#!/bin/bash +set -euo pipefail +dir="$(pwd)" +sed -e "s,$dir/,," | while read line; do + file_path=$(echo "$line" | cut -d':' -f1) + line_number=$(echo "$line" | cut -d':' -f2) + column_number=$(echo "$line" | cut -d':' -f3) + line=$(echo "$line" | cut -d':' -f4-) + echo "::error file=$file_path,line=$line_number,col=$column_number::$line" +done \ No newline at end of file