From 82fa1454149a1942a63623748063491195a64c01 Mon Sep 17 00:00:00 2001 From: Brandon Duffany Date: Wed, 26 Jan 2022 21:10:51 -0500 Subject: [PATCH] Add CI check and fix script for go deps --- .github/workflows/checkstyle.yaml | 19 +++++++++-- tools/fix_go_deps.sh | 53 +++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100755 tools/fix_go_deps.sh diff --git a/.github/workflows/checkstyle.yaml b/.github/workflows/checkstyle.yaml index 7acfa062a531..2efe28bdd9a1 100644 --- a/.github/workflows/checkstyle.yaml +++ b/.github/workflows/checkstyle.yaml @@ -17,6 +17,11 @@ jobs: - name: Checkout uses: actions/checkout@v1 + - name: Setup go + uses: actions/setup-go@v2 + with: + go-version: "1.17.2" + - name: gofmt run: | gofmt -d . > gofmt-diff.txt || true @@ -25,18 +30,25 @@ jobs: - name: buildifier run: | - go get -u github.com/bazelbuild/buildtools/buildifier + go install github.com/bazelbuild/buildtools/buildifier@3.4.0 "$(go env GOPATH)/bin/buildifier" -d -r . > buildifier-diff.txt || true echo "buildifier diff:" cat buildifier-diff.txt - name: gazelle run: | - go get github.com/bazelbuild/bazel-gazelle/cmd/gazelle + go install github.com/bazelbuild/bazel-gazelle/cmd/gazelle@v0.23.0 "$(go env GOPATH)/bin/gazelle" -mode diff > gazelle-diff.txt || true echo "gazelle diff:" cat gazelle-diff.txt + - name: go deps + run: | + go install github.com/bazelbuild/bazel-gazelle/cmd/gazelle@v0.23.0 + GAZELLE_PATH="$(go env GOPATH)/bin/gazelle" tools/fix_go_deps.sh --diff &> go-deps-diff.txt || true + echo "go deps diff:" + cat go-deps-diff.txt + - name: clang-format run: | git ls-files | grep '\.proto$' | xargs --no-run-if-empty -d'\n' clang-format -i --style=Google --dry-run &> clang-format-errors.txt || true @@ -64,12 +76,15 @@ jobs: cat buildifier-diff.txt echo "===== gazelle diff =====" cat gazelle-diff.txt + echo "===== go deps diff (fix with tools/fix_go_deps.sh) =====" + cat go-deps-diff.txt echo "===== clang-format errors =====" cat clang-format-errors.txt echo "===== prettier errors =====" cat prettier-errors.txt if [ -s gazelle-diff.txt ]; then exit 1; fi + if [ -s go-deps-diff.txt ]; then exit 1; fi if [ -s gofmt-diff.txt ]; then exit 1; fi if [ -s buildifier-diff.txt ]; then exit 1; fi if [ -s clang-format-errors.txt ]; then exit 1; fi diff --git a/tools/fix_go_deps.sh b/tools/fix_go_deps.sh new file mode 100755 index 000000000000..f4bc9014b1ff --- /dev/null +++ b/tools/fix_go_deps.sh @@ -0,0 +1,53 @@ +#!/bin/bash +set -euo pipefail + +: "${GAZELLE_PATH:=}" + +GAZELLE_COMMAND=(bazelisk run //:gazelle --) +if [[ "$GAZELLE_PATH" ]]; then + GAZELLE_COMMAND=("$GAZELLE_PATH") +fi + +DIFF_MODE=0 +if [[ "${1:-}" == "-d" ]] || [[ "${1:-}" == "--diff" ]]; then + DIFF_MODE=1 +fi + +if ((DIFF_MODE)); then + if ! git diff --quiet; then + echo >&2 "Git working tree is dirty. To run in diff mode, 'check_go_deps.sh' must be run from a clean tree." + git status --short --untracked=no 1>&2 + exit 1 + fi +fi + +tmp_log_file=$(mktemp) +cleanup() { + if ((DIFF_MODE)); then + git restore go.mod go.sum deps.bzl + fi + rm -r "$tmp_log_file" +} +trap cleanup EXIT + +# go mod tidy fails if generated sources are not checked into the repo, +# and we don't want to require that (yet, at least). So use the `-e` +# option to ask `go mod tidy` to proceed even if it encounters errors +# loading packages. +if ! go mod tidy -e &>"$tmp_log_file"; then + echo -e "Command 'go mod tidy -e' failed. Logs:" >&2 + cat "$tmp_log_file" >&2 + exit 1 +fi + +# Update deps.bzl (using Gazelle) +if ! "${GAZELLE_COMMAND[@]}" update-repos -from_file=go.mod \ + -to_macro=deps.bzl%install_buildbuddy_dependencies &>"$tmp_log_file"; then + echo -e "Auto-updating 'deps.bzl' failed. Logs:" >&2 + cat "$tmp_log_file" >&2 + exit 1 +fi + +if ((DIFF_MODE)); then + git diff +fi