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

Improve format check #4096

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 92 additions & 10 deletions .github/workflows/format_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
- name: Fetch latest changes
run: git fetch origin

# Step 4: Find changed Java files
- name: Get changed Java files
id: changed_files
run: |
Expand All @@ -37,24 +38,61 @@ jobs:
# Write the multiline content to a file
echo "$CHANGED_FILES" > changed_files.txt

# Step 4: Get a list of changed Java files in the PR
- name: Check Java file format
# Step 5: Get formatting issues for source branch
- name: Get formatting issues for source branch
id: number_of_formatting_issues_source
run: |
# Check if the changed_files.txt exists
# Ensure there are changed files
if [ ! -f changed_files.txt ]; then
echo "No changed files found."
exit 0
fi
echo "No Java files changed."
else
echo "Processing the following changed Java files:"

cat changed_files.txt
# Read the multiline content from the file
CHANGED_FILES=$(cat changed_files.txt)

# Read the multiline content from the file
CHANGED_FILES=$(cat changed_files.txt)
# # Checkout source branch and store the unformatted file
# git checkout ${{ github.event.pull_request.head.ref }}

echo "::group::Iterating changed files in source branch"
# Iterate over the CHANGED_FILES variable, assuming files are separated by newlines
while IFS= read -r FILE; do
# Skip empty lines if any
if [ -n "$FILE" ]; then
FILE_NAME=$(basename "$FILE")
echo "Checking for $FILE_NAME"

# Run your formatter validation for each file
mvn formatter:format -f formatter-pom.xml "-Dformatter.includes=**/$FILE_NAME"
fi
done <<< "$CHANGED_FILES"
echo "::endgroup::"

git diff --stat
# Write diff into a file
git diff --stat > formatting_diff_source.txt
fi
git reset --hard

# Step 6: Get formatting issues for base branch
- name: Get formatting issues for base branch
id: number_of_formatting_issues_base
run: |
# Ensure there are changed files
if [ -z "$CHANGED_FILES" ]; then
if [ ! -f changed_files.txt ]; then
echo "No Java files changed."
else
echo "Processing the following changed Java files:"

cat changed_files.txt
# Read the multiline content from the file
CHANGED_FILES=$(cat changed_files.txt)

# Checkout source branch and store the unformatted file
git checkout ${{ github.event.pull_request.base.ref }}

echo "::group::Iterating changed files in base branch"
# Iterate over the CHANGED_FILES variable, assuming files are separated by newlines
while IFS= read -r FILE; do
# Skip empty lines if any
Expand All @@ -63,7 +101,51 @@ jobs:
echo "Checking for $FILE_NAME"

# Run your formatter validation for each file
mvn formatter:validate -f formatter-pom.xml "-Dformatter.includes=**/$FILE_NAME"
mvn formatter:format -f formatter-pom.xml "-Dformatter.includes=**/$FILE_NAME"
fi
done <<< "$CHANGED_FILES"
echo "::endgroup::"

git diff --stat
# Write diff into a file
git diff --stat > formatting_diff_base.txt
fi
git reset --hard

# Step 7: Compare format diffs
- name: Compare format diffs
run: |
format_source=$(head -n -1 formatting_diff_source.txt | awk '{print $1 " " $3}')
echo -e "Formatting diff in source branch: \n $format_source"
format_base=$(head -n -1 formatting_diff_base.txt | awk '{print $1 " " $3}')
echo -e "Formatting diff in base branch: \n $format_base"
comm -13 <(echo -e "$format_base") <(echo -e "$format_source") >diff_result.txt

if [ -s diff_result.txt ]; then

while read -r line; do
source_path=$(echo "$line" | awk '{print $1}')
source_index=$(echo "$line" | awk '{print $2}')

base_line=$(grep "^$source_path " formatting_diff_base.txt)
# If the line does not exist, it means it is new in the source branch, and thus, any formatting changes there are treated as new issues.
if [ -z "$base_line" ]; then
echo "$source_path $source_index" >> new_formatting_issues.txt
continue
fi

base_index=$(echo "$base_line" | awk '{print $2}')
if [ "$source_index" -gt "$base_index" ]; then
echo "$source_path $source_index" >> new_formatting_issues.txt
fi
done < diff_result.txt

fi

if [ -s new_formatting_issues.txt ]; then
echo "There are formatting issues introduced with this PR."
cat new_formatting_issues.txt
exit 1 # Fail the workflow
else
echo "No new formatting issue detected."
fi
Loading