Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checkstyle in maven build #2610

Open
wants to merge 21 commits into
base: branch-25.02
Choose a base branch
from
Open
41 changes: 41 additions & 0 deletions .github/workflows/checkstyle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Copyright (c) 2023-2024, NVIDIA CORPORATION.
jlowe marked this conversation as resolved.
Show resolved Hide resolved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# A workflow to java style check
name: java format check

on:
pull_request:
types: [opened, synchronize, reopened]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- name: Set up JDK
uses: actions/setup-java@v4.5.0
with:
java-version: '8'
distribution: 'adopt'

- uses: actions/checkout@v4
with:
liurenjie1024 marked this conversation as resolved.
Show resolved Hide resolved
submodules: true
fetch-depth: 0

- name: Fetch branches and tags
run: git fetch --all

- name: Run checkstyle
run: bash ./dev/checkstyle.sh
44 changes: 44 additions & 0 deletions dev/checkstyle.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash
#
# Copyright (c) 2024, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#!/bin/bash

set -ex

# Assuming you are in the root of your git repository
MODIFIED_FILES=$(git diff --name-only "origin/${GITHUB_BASE_REF}")
Copy link
Collaborator

@pxLi pxLi Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree with Jason, We would like to not only check style for incremental changes.

and this script is current for github action only with the ENV provided by github.
can we just check against all eligible code with the merged commit for PR? and developers could also run the script to check their change locally before filing a PR

and in that case we do not need below in action def then

      with:
        submodules: true
        fetch-depth: 0

    - name: Fetch branches and tags
      run: git fetch --all

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments below.


SRC_DIR="src/main/java/"
TEST_SRC_DIR="src/test/java/"
# Filter out the Java files that have been modified
JAVA_FILES=()
for FILE in $MODIFIED_FILES; do
if [[ $FILE == *.java ]]; then
if [[ $FILE == $SRC_DIR* ]]; then
JAVA_FILES+=("${FILE#"$SRC_DIR"}") # Remove the src/main/java/ prefix
elif [[ $FILE == $TEST_SRC_DIR* ]]; then
JAVA_FILES+=("${FILE#"$TEST_SRC_DIR"}") # Remove the src/test/java/ prefix
fi
fi
done

# If there are Java files to check, run Checkstyle on them
if [ ${#JAVA_FILES[@]} -ne 0 ]; then
mvn checkstyle:check -Dcheckstyle.includes="$(echo "${JAVA_FILES[@]}" | tr ' ' ',')"
else
echo "No Java files modified, skipping Checkstyle."
fi
Loading
Loading