diff --git a/.github/workflows/static_checks.yml b/.github/workflows/static_checks.yml index 0158df4dc13..3a25f011bd5 100644 --- a/.github/workflows/static_checks.yml +++ b/.github/workflows/static_checks.yml @@ -49,10 +49,7 @@ jobs: - name: Download Buf run: | cd $HOME/oppia-android-tools - BUFVERSION="v0.15.0" - echo Using Bufbuild version $BUFVERSION - curl -sSL https://github.com/bufbuild/buf/releases/download/$BUFVERSION/buf-$(uname -s)-$(uname -m) > buf - chmod a+x buf + bash /home/runner/work/oppia-android/oppia-android/scripts/buf_download.sh - name: Download Buildifier run: | @@ -68,7 +65,8 @@ jobs: bash /home/runner/work/oppia-android/oppia-android/scripts/ktlint_lint_check.sh $HOME - name: Protobuf lint check - run: $HOME/oppia-android-tools/buf check lint --input=model/src/main/proto --input-config buf.yaml && echo "Protobuf lint check completed successfully" + run: | + bash /home/runner/work/oppia-android/oppia-android/scripts/buf_lint_check.sh $HOME - name: Bazel lint check run: | diff --git a/buf.yaml b/buf.yaml index e2f6b92b458..a6fdcc22ed7 100644 --- a/buf.yaml +++ b/buf.yaml @@ -1,20 +1,20 @@ # buf.yaml files should always be in the root of your repository. # More details can be found on our wiki page {@link 'https://github.com/oppia/oppia-android/wiki/Buf-Guide' + +version: v1beta1 + build: roots: # Roots is the directories that all .proto files are contained within, and # all imports must derive from. # Only *.proto files within these directory will be compiled. - - . - # Excludes is the list of directories within root to exclude. - excludes: - - model/build + - model/src/main/proto # Lint contains the options for lint checks. lint: # Use is the list of checker categories and ids to use for buf check lint. use: - # Three categories {@link 'https://buf.build/docs/lint-checkers' + # Three categories {@link 'https://docs.buf.build/lint-rules' # The BASIC category includes everything from the MINIMAL category, as well as the STYLE_BASIC category. # The MINIMAL category represents what we consider to be fundamental rules for modern Protobuf development, # regardless of style diff --git a/scripts/buf_download.sh b/scripts/buf_download.sh new file mode 100644 index 00000000000..da6da5f9b9c --- /dev/null +++ b/scripts/buf_download.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# Download buf +download_buf() { + BUFVERSION="v0.37.1" + echo Using Bufbuild version $BUFVERSION + curl -sSLOC - https://github.com/bufbuild/buf/releases/download/$BUFVERSION/buf-$(uname -s)-$(uname -m) + chmod a+x buf-$(uname -s)-$(uname -m) + echo Buf downloaded +} + +if [[ "$OSTYPE" == "linux-gnu"* || "$OSTYPE" == "darwin"* ]]; then + download_buf +else + echo "Protobuf lint check not available on $OSTYPE" + exit 1 +fi diff --git a/scripts/buf_lint_check.sh b/scripts/buf_lint_check.sh new file mode 100644 index 00000000000..16ac1dfc185 --- /dev/null +++ b/scripts/buf_lint_check.sh @@ -0,0 +1,69 @@ +#!/bin/bash + +jar_file_path=$? +config_file_path=$? +os_type=$? +github_actions_path=$1 + +lint_protobuf_files() { + echo "********************************" + echo "Checking Protobuf file formatting" + echo "********************************" + + $jar_file_path lint --config $config_file_path + + status=$? + + if [ "$status" = 0 ] ; then + echo "Protobuf lint check completed successfully" + exit 0 + else + echo "********************************" + echo "Protobuf lint check issues found. Please fix them before pushing your code." + echo "********************************" + exit 1 + fi +} + +populate_jar_config_file_paths() { + buf_file_name=$? + + if [ "$1" = "Linux" ]; then + buf_file_name="buf-Linux-x86_64" + else + buf_file_name="buf-Darwin-x86_64" + fi + + if [ $github_actions_path ]; then + jar_file_path="$github_actions_path/oppia-android-tools/$buf_file_name" + config_file_path="/home/runner/work/oppia-android/oppia-android/buf.yaml" + else + jar_file_path="../oppia-android-tools/$buf_file_name" + config_file_path="buf.yaml" + fi +} + +ensure_jar_config_file_paths_are_set() { + if [ $github_actions_path ]; then + populate_jar_config_file_paths $os_type $github_actions_path + else + populate_jar_config_file_paths $os_type + fi +} + +check_os_type() { + if [[ "$OSTYPE" == "linux-gnu"* ]]; then + os_type="Linux" + elif [[ "$OSTYPE" == "darwin"* ]]; then + os_type="Darwin" + else + echo "Protobuf lint check not available on $OSTYPE" + exit 1 + fi +} + +check_os_type + +ensure_jar_config_file_paths_are_set $github_actions_path + +lint_protobuf_files diff --git a/scripts/pre-push.sh b/scripts/pre-push.sh index 1d4c7eaaa83..d6c05bc0a8f 100755 --- a/scripts/pre-push.sh +++ b/scripts/pre-push.sh @@ -1,11 +1,12 @@ #!/bin/bash # This script will run the pre-push checks in the given order -# - ktlint -# - checkstyle -# - (others in the future) +# - ktlint +# - checkstyle +# - buf +# - (others in the future) -if bash scripts/ktlint_lint_check.sh && bash scripts/checkstyle_lint_check.sh ; then +if bash scripts/ktlint_lint_check.sh && bash scripts/checkstyle_lint_check.sh && bash scripts/buf_lint_check.sh ; then echo "All checks passed successfully" exit 0 else @@ -14,4 +15,3 @@ fi # TODO(#1736): Add Bazel Linter to the project # TODO(#970): Add XML Linter to the project -# TODO(#2097): Add Buf Linter to the project diff --git a/scripts/setup.sh b/scripts/setup.sh index 06a80e8cfb7..3b48ab248ba 100644 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -22,3 +22,6 @@ bash ../oppia-android/scripts/ktlint_download.sh # Download checkstyle bash ../oppia-android/scripts/checkstyle_download.sh + +# Download buf +bash ../oppia-android/scripts/buf_download.sh