check-protobuf.sh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env bash
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # check-protobuf.sh — detect breaking changes in proto files.
  12. set -euo pipefail
  13. cd "$(dirname "$0")/../../../.."
  14. . misc/shlib/shlib.bash
  15. . misc/buildkite/git.bash
  16. if ! buf --version >/dev/null 2>/dev/null; then
  17. echo "lint: buf is not installed"
  18. echo "hint: refer to https://buf.build/docs/installation for install instructions"
  19. exit 1
  20. fi
  21. CURRENT_GIT_BRANCH=$(try git branch --show-current)
  22. IN_BUILDKITE=in_ci
  23. IN_BUILDKITE_PR=0
  24. ON_MAIN_BRANCH=0
  25. IN_LOCAL_NON_MAIN_BRANCH=0
  26. if [[ ${BUILDKITE_PULL_REQUEST:-false} != "false" ]]; then
  27. IN_BUILDKITE_PR=1
  28. fi
  29. if [[ "$CURRENT_GIT_BRANCH" == "main" ]]; then
  30. ON_MAIN_BRANCH=1
  31. fi
  32. if [[ "$IN_BUILDKITE" != 1 && "$ON_MAIN_BRANCH" != 1 ]]; then
  33. IN_LOCAL_NON_MAIN_BRANCH=1
  34. fi
  35. echo $IN_BUILDKITE_PR
  36. echo $IN_LOCAL_NON_MAIN_BRANCH
  37. if [[ "${1:-}" = --offline ]]; then
  38. fetch_from_git=false
  39. else
  40. fetch_from_git=true
  41. fi
  42. if [[ $IN_BUILDKITE_PR || $IN_LOCAL_NON_MAIN_BRANCH ]]; then
  43. # see ./ci/test/lint-buf/README.md
  44. if $fetch_from_git; then
  45. fetch_pr_target_branch
  46. fi
  47. ci_collapsed_heading "Verify that protobuf config is up-to-date"
  48. try bin/pyactivate ./ci/test/lint-buf/generate-buf-config.py
  49. try yamllint src/buf.yaml
  50. try git diff --name-only --exit-code src/buf.yaml
  51. ci_collapsed_heading "Lint protobuf"
  52. COMMON_ANCESTOR="$(get_common_ancestor_commit_of_pr_and_target)"
  53. # Default is depth 50, which can be insufficient to grab the relevant ancestor commit
  54. try buf breaking src --against ".git#ref=$COMMON_ANCESTOR,subdir=src,depth=10000" --verbose
  55. ci_collapsed_heading "Lint protobuf formatting"
  56. # Proto formatting
  57. try buf format src --diff --exit-code
  58. fi
  59. try_status_report