Skip to content

Commit

Permalink
Fix 2097: Introduce pre push hook for buf (#2776)
Browse files Browse the repository at this point in the history
* introducing pre-push for buf

* nit fix

* nit fix

* nit fix

* os check while downloading buf

* new line eof

* adding files path

* root changed

* nit fixes
  • Loading branch information
anandwana001 authored Mar 3, 2021
1 parent f26d769 commit eccfd63
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 15 deletions.
8 changes: 3 additions & 5 deletions .github/workflows/static_checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand All @@ -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: |
Expand Down
10 changes: 5 additions & 5 deletions buf.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
17 changes: 17 additions & 0 deletions scripts/buf_download.sh
Original file line number Diff line number Diff line change
@@ -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
69 changes: 69 additions & 0 deletions scripts/buf_lint_check.sh
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions scripts/pre-push.sh
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
3 changes: 3 additions & 0 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit eccfd63

Please sign in to comment.