diff --git a/.github/workflows/format_check.yml b/.github/workflows/format_check.yml index 1e1c1e8535..5b90abf103 100644 --- a/.github/workflows/format_check.yml +++ b/.github/workflows/format_check.yml @@ -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: | @@ -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 @@ -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 \ No newline at end of file