Skip to content

Commit

Permalink
Add CI check and fix script for go deps
Browse files Browse the repository at this point in the history
  • Loading branch information
bduffany committed Jan 27, 2022
1 parent a708710 commit 139d08e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
19 changes: 17 additions & 2 deletions .github/workflows/checkstyle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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@0.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@0.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
Expand Down Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions tools/fix_go_deps.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 139d08e

Please sign in to comment.