|
| 1 | +name: Line Length Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [ '*' ] |
| 6 | + |
| 7 | +jobs: |
| 8 | + line-length-check: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + name: Check 80 character line limit |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Checkout code |
| 14 | + uses: actions/checkout@v4 |
| 15 | + with: |
| 16 | + fetch-depth: 0 |
| 17 | + |
| 18 | + - name: Check line length in PR changes |
| 19 | + run: | |
| 20 | + # Get the base branch (usually main/master) |
| 21 | + BASE_BRANCH="${{ github.event.pull_request.base.ref }}" |
| 22 | +
|
| 23 | + echo "Checking line length (max 80 characters) for changed files in " \ |
| 24 | + "src/, examples/, and native/ directories..." |
| 25 | + echo "================================================================" |
| 26 | +
|
| 27 | + # Create temporary files with unique names |
| 28 | + changed_files=$(mktemp) |
| 29 | + violations_file=$(mktemp) |
| 30 | +
|
| 31 | + # Get all changed files in this PR and filter for target directories |
| 32 | + git diff --name-only "origin/$BASE_BRANCH"...HEAD | \ |
| 33 | + grep -E '^(src/|examples/|native/)' > "$changed_files" || true |
| 34 | +
|
| 35 | + # Initialize violation count |
| 36 | + violation_count=0 |
| 37 | +
|
| 38 | + # Check each changed file |
| 39 | + while IFS= read -r file; do |
| 40 | + if [[ -f "$file" ]]; then |
| 41 | + # Skip WolfSSLProvider.java... |
| 42 | + # that legitimately exceed 80 characters |
| 43 | + if [[ "$file" == "src/java/com/wolfssl/provider/jsse/WolfSSLProvider.java" ]]; then |
| 44 | + echo "⚠️ Skipping $file (contains security service mappings)" |
| 45 | + continue |
| 46 | + fi |
| 47 | +
|
| 48 | + echo "Checking: $file" |
| 49 | +
|
| 50 | + # Get added lines with actual file line numbers and check their length |
| 51 | + new_line_num=0 |
| 52 | + git diff "origin/$BASE_BRANCH"...HEAD "$file" | \ |
| 53 | + while IFS= read -r line; do |
| 54 | + # Track line numbers from diff headers - format: @@ -old_start,old_count +new_start,new_count @@ |
| 55 | + if [[ "$line" =~ ^@@.*\+([0-9]+) ]]; then |
| 56 | + # Extract starting line number for new file (after +) |
| 57 | + # Subtract 1 because we'll increment before processing first line |
| 58 | + new_line_num=$((${BASH_REMATCH[1]} - 1)) |
| 59 | + elif [[ "$line" =~ ^(\+[^+].*) ]]; then |
| 60 | + # This is an added line (not a +++ header) |
| 61 | + # Increment line number BEFORE processing (since this line exists in new file) |
| 62 | + new_line_num=$((new_line_num + 1)) |
| 63 | + added_line="${line:1}" # Remove leading + |
| 64 | + char_count=${#added_line} |
| 65 | +
|
| 66 | + # Skip JNI method signatures and calls to avoid false positives |
| 67 | + # These are auto-generated names that can't be shortened |
| 68 | + if [[ $char_count -gt 80 ]]; then |
| 69 | + # Check if this is a JNI method signature, call, or parameter line that should be ignored |
| 70 | + if [[ "$added_line" =~ JNIEXPORT.*JNICALL.*Java_com_wolfssl_ ]] || \ |
| 71 | + [[ "$added_line" =~ Java_com_wolfssl_.*\( ]] || \ |
| 72 | + [[ "$added_line" =~ ^[[:space:]]*return[[:space:]]+Java_com_wolfssl_.* ]] || \ |
| 73 | + [[ "$added_line" =~ ^[[:space:]]*\(JNIEnv\*[[:space:]]+env.*\) ]] || \ |
| 74 | + [[ "$added_line" =~ ^[[:space:]]*JNIEnv\*[[:space:]]+env.* ]]; then |
| 75 | + echo "⚠️ $file:$new_line_num - Skipping JNI method signature/call/parameters ($char_count characters)" |
| 76 | + echo " Line: $added_line" |
| 77 | + else |
| 78 | + echo "❌ $file:$new_line_num - Line too long ($char_count characters)" |
| 79 | + echo " Line: $added_line" |
| 80 | + echo "violation" >> "$violations_file" |
| 81 | + fi |
| 82 | + fi |
| 83 | + elif [[ "$line" =~ ^[[:space:]] ]]; then |
| 84 | + # Context line (unchanged) - increment new file line number |
| 85 | + new_line_num=$((new_line_num + 1)) |
| 86 | + # Removed lines (starting with -) don't affect new file line numbers |
| 87 | + fi |
| 88 | + done |
| 89 | + fi |
| 90 | + done < "$changed_files" |
| 91 | +
|
| 92 | + # Count violations |
| 93 | + if [[ -f "$violations_file" ]]; then |
| 94 | + violation_count=$(grep -c "violation" "$violations_file" || echo 0) |
| 95 | + else |
| 96 | + violation_count=0 |
| 97 | + fi |
| 98 | +
|
| 99 | + echo "================================================================" |
| 100 | +
|
| 101 | + if [[ $violation_count -gt 0 ]]; then |
| 102 | + echo "❌ Found $violation_count line(s) exceeding 80 " \ |
| 103 | + "characters in PR changes" |
| 104 | + echo "" |
| 105 | + echo "Please ensure all lines are 80 characters or less " \ |
| 106 | + "as per coding standards." |
| 107 | + echo "You can check line length in your editor or use this command:" |
| 108 | + echo " grep -n '.\{81,\}' <filename>" |
| 109 | + rm -f "$violations_file" "$changed_files" |
| 110 | + exit 1 |
| 111 | + else |
| 112 | + echo "✅ All changed lines are within the 80 character limit" |
| 113 | + rm -f "$violations_file" "$changed_files" |
| 114 | + exit 0 |
| 115 | + fi |
0 commit comments