|
| 1 | +name: Enforce Max Lines Changed Per File |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - '**/*.java' # Monitor changes to Java files |
| 7 | + - '**/*.avsc' # Monitor changes to Avro schema files |
| 8 | + - '**/*.proto' # Monitor changes to proto schema files |
| 9 | + |
| 10 | +jobs: |
| 11 | + enforce-lines-changed: |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Checkout code |
| 16 | + uses: actions/checkout@v3 |
| 17 | + |
| 18 | + - name: Get PR Metadata |
| 19 | + id: pr_details |
| 20 | + uses: actions-ecosystem/action-get-PR@v2 |
| 21 | + with: |
| 22 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 23 | + |
| 24 | + - name: Check for Override Keyword |
| 25 | + id: check_override |
| 26 | + run: | |
| 27 | + # Define the keyword for override |
| 28 | + OVERRIDE_KEYWORD="DIFFSIZEOVERRIDE" |
| 29 | + |
| 30 | + # Check PR title and body for the keyword |
| 31 | + if echo "${{ steps.pr_details.outputs.title }}${{ steps.pr_details.outputs.body }}" | grep -iq "$OVERRIDE_KEYWORD"; then |
| 32 | + echo "Override keyword found. Skipping validation." |
| 33 | + echo "override=true" >> $GITHUB_OUTPUT |
| 34 | + else |
| 35 | + echo "override=false" >> $GITHUB_OUTPUT |
| 36 | + fi |
| 37 | + - name: Calculate Lines Changed Per File |
| 38 | + if: ${{ steps.check_override.outputs.override != 'true' }} |
| 39 | + id: lines_changed_per_file |
| 40 | + run: | |
| 41 | + # Define the maximum allowed lines changed per file |
| 42 | + MAX_LINES=500 |
| 43 | +
|
| 44 | + # Get the diff of the PR and process each file |
| 45 | + EXCEEDED=false |
| 46 | + JAVA_FILE=false |
| 47 | + SCHEMA_FILE=false |
| 48 | + while IFS=$'\t' read -r added removed file; do |
| 49 | + # Skip test files |
| 50 | + if [[ "$file" != *src/main* ]]; then |
| 51 | + echo "Skipping file: $file" |
| 52 | + continue |
| 53 | + fi |
| 54 | + |
| 55 | + if [[ "$file" == *.java ]]; then |
| 56 | + JAVA_FILE=true |
| 57 | + else |
| 58 | + SCHEMA_FILE=true |
| 59 | + fi |
| 60 | +
|
| 61 | + # Calculate total lines changed for the file |
| 62 | + TOTAL=$((added + removed)) |
| 63 | + echo "File: $file, Lines Changed: $TOTAL" |
| 64 | +
|
| 65 | + # Check if the total exceeds the max allowed |
| 66 | + if [ "$TOTAL" -gt "$MAX_LINES" ]; then |
| 67 | + echo "File $file exceeds the maximum allowed lines changed ($TOTAL > $MAX_LINES)" |
| 68 | + EXCEEDED=true |
| 69 | + fi |
| 70 | + done < <(git diff --numstat origin/main | grep -E '\.(java|avsc|proto)$') |
| 71 | +
|
| 72 | + # Fail if there are both schema and Java file changes |
| 73 | + if [ "$EXCEEDED" = true ]; then |
| 74 | + echo "Above files exceed the maximum allowed lines changed ($MAX_LINES)." |
| 75 | + exit 1 |
| 76 | + fi |
| 77 | + |
| 78 | + # Fail if any file exceeded the limit |
| 79 | + if [ "JAVA_FILE" = true && "SCHEMA_FILE" = true ]; then |
| 80 | + echo "The PR has both schema and Java code changes, please make a separate PR for schema changes." |
| 81 | + exit 1 |
| 82 | + fi |
| 83 | +
|
| 84 | + - name: Notify |
| 85 | + if: failure() |
| 86 | + run: | |
| 87 | + echo "One or more files in the PR exceed the maximum allowed lines changed. Please review and adjust your changes to your files." |
0 commit comments