|
| 1 | +name: File Size Checker |
| 2 | + |
| 3 | +# Add required permissions |
| 4 | +permissions: |
| 5 | + contents: read |
| 6 | + pull-requests: write |
| 7 | + statuses: write |
| 8 | + |
| 9 | +on: |
| 10 | + pull_request: |
| 11 | + types: [opened, synchronize] |
| 12 | + |
| 13 | +jobs: |
| 14 | + check-file-sizes: |
| 15 | + name: File Size Check |
| 16 | + runs-on: ubuntu-latest |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Checkout code |
| 20 | + uses: actions/checkout@v4 |
| 21 | + with: |
| 22 | + fetch-depth: 0 |
| 23 | + |
| 24 | + - name: Check file sizes |
| 25 | + id: check-sizes |
| 26 | + run: | |
| 27 | + # Initialize variables for tracking findings |
| 28 | + large_files="" |
| 29 | + huge_files="" |
| 30 | +
|
| 31 | + # Get all files in the PR |
| 32 | + echo "Files changed in PR:" |
| 33 | + git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} |
| 34 | +
|
| 35 | + for file in $(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }}); do |
| 36 | + if [ -f "$file" ]; then |
| 37 | + size=$(stat -c%s "$file") |
| 38 | + size_mb=$(echo "scale=2; $size/1048576" | bc) |
| 39 | +
|
| 40 | + echo "Checking $file: ${size_mb}MB" |
| 41 | +
|
| 42 | + # Check for files over 40MB |
| 43 | + if (( $(echo "$size_mb > 40" | bc -l) )); then |
| 44 | + huge_files="${huge_files}* ${file} (${size_mb}MB)\n" |
| 45 | + # Check for files over 10MB |
| 46 | + elif (( $(echo "$size_mb > 10" | bc -l) )); then |
| 47 | + large_files="${large_files}* ${file} (${size_mb}MB)\n" |
| 48 | + fi |
| 49 | + fi |
| 50 | + done |
| 51 | +
|
| 52 | + # Print findings for debugging |
| 53 | + echo "Large files found:" |
| 54 | + echo -e "$large_files" |
| 55 | + echo "Huge files found:" |
| 56 | + echo -e "$huge_files" |
| 57 | +
|
| 58 | + # Set outputs for use in next steps |
| 59 | + echo "large_files<<EOF" >> $GITHUB_OUTPUT |
| 60 | + echo -e "$large_files" >> $GITHUB_OUTPUT |
| 61 | + echo "EOF" >> $GITHUB_OUTPUT |
| 62 | +
|
| 63 | + echo "huge_files<<EOF" >> $GITHUB_OUTPUT |
| 64 | + echo -e "$huge_files" >> $GITHUB_OUTPUT |
| 65 | + echo "EOF" >> $GITHUB_OUTPUT |
| 66 | +
|
| 67 | + # Fail if huge files are found |
| 68 | + if [ ! -z "$huge_files" ]; then |
| 69 | + echo "❌ Files over 40MB found!" |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | +
|
| 73 | + - name: Update Status and Comment |
| 74 | + if: always() |
| 75 | + uses: actions/github-script@v7 |
| 76 | + with: |
| 77 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 78 | + script: | |
| 79 | + const hugeFiles = `${{ steps.check-sizes.outputs.huge_files }}`; |
| 80 | + const largeFiles = `${{ steps.check-sizes.outputs.large_files }}`; |
| 81 | +
|
| 82 | + try { |
| 83 | + // Only comment if issues were found |
| 84 | + if (hugeFiles || largeFiles) { |
| 85 | + let comment = '## ⚠️ File Size Check Results\n\n'; |
| 86 | +
|
| 87 | + if (hugeFiles) { |
| 88 | + comment += '### 🚫 Files over 40MB (Not Allowed):\n' + hugeFiles + '\n'; |
| 89 | + comment += '**These files must be removed from git history before the PR can be merged.**\n\n'; |
| 90 | + } |
| 91 | +
|
| 92 | + if (largeFiles) { |
| 93 | + comment += '### ⚠️ Large Files (Over 10MB):\n' + largeFiles + '\n'; |
| 94 | + comment += 'Consider reducing the size of these files if possible.\n'; |
| 95 | + } |
| 96 | +
|
| 97 | + await github.rest.issues.createComment({ |
| 98 | + issue_number: context.payload.pull_request.number, |
| 99 | + owner: context.payload.repository.owner.login, |
| 100 | + repo: context.payload.repository.name, |
| 101 | + body: comment |
| 102 | + }); |
| 103 | + } |
| 104 | + } catch (error) { |
| 105 | + console.error('Error:', error); |
| 106 | + core.setFailed(error.message); |
| 107 | + } |
0 commit comments