-
Notifications
You must be signed in to change notification settings - Fork 597
131 lines (111 loc) · 4.89 KB
/
file-size-checker.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
name: File Size Checker
# Add required permissions
permissions:
contents: read
pull-requests: write
statuses: write
on:
pull_request:
types: [opened, synchronize]
jobs:
check-file-sizes:
name: File Size Check
runs-on: ubuntu-latest
steps:
# - name: Setup environment
# run: |
# apt-get update
# apt-get install -y git bc
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check file sizes
id: check-sizes
run: |
# Initialize variables for tracking findings
large_files=""
huge_files=""
# Get all files in the PR
echo "Files changed in PR:"
git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }}
for file in $(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }}); do
if [ -f "$file" ]; then
size=$(stat -c%s "$file")
size_mb=$(echo "scale=2; $size/1048576" | bc)
echo "Checking $file: ${size_mb}MB"
# Check for files over 40MB
if (( $(echo "$size_mb > 40" | bc -l) )); then
huge_files="${huge_files}* ${file} (${size_mb}MB)\n"
# Check for files over 10MB
elif (( $(echo "$size_mb > 10" | bc -l) )); then
large_files="${large_files}* ${file} (${size_mb}MB)\n"
fi
fi
done
# Print findings for debugging
echo "Large files found:"
echo -e "$large_files"
echo "Huge files found:"
echo -e "$huge_files"
# Set outputs for use in next steps
echo "large_files<<EOF" >> $GITHUB_OUTPUT
echo -e "$large_files" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "huge_files<<EOF" >> $GITHUB_OUTPUT
echo -e "$huge_files" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
# Fail if huge files are found
if [ ! -z "$huge_files" ]; then
echo "❌ Files over 40MB found!"
exit 1
fi
- name: Update Status and Comment
if: always()
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const hugeFiles = `${{ steps.check-sizes.outputs.huge_files }}`;
const largeFiles = `${{ steps.check-sizes.outputs.large_files }}`;
try {
// Only create status check if we have permission (not a fork PR)
if (context.payload.pull_request.head.repo.full_name === context.payload.repository.full_name) {
await github.rest.repos.createCommitStatus({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
sha: context.payload.pull_request.head.sha,
state: hugeFiles ? 'failure' : 'success',
context: 'File Size Check',
description: hugeFiles ? 'Files over 40MB found' : 'All files within size limits',
target_url: `https://github.com/${context.payload.repository.owner.login}/${context.payload.repository.name}/actions/runs/${context.runId}`
});
}
// Comments should work for both fork and non-fork PRs
if (hugeFiles || largeFiles) {
let comment = '## ⚠️ File Size Check Results\n\n';
if (hugeFiles) {
comment += '### 🚫 Files over 40MB (Not Allowed):\n' + hugeFiles + '\n';
comment += '**These files must be removed from git history before the PR can be merged.**\n\n';
}
if (largeFiles) {
comment += '### ⚠️ Large Files (Over 10MB):\n' + largeFiles + '\n';
comment += 'Consider reducing the size of these files if possible.\n';
}
await github.rest.issues.createComment({
issue_number: context.payload.pull_request.number,
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
body: comment
});
}
} catch (error) {
console.error('Error:', error);
console.error('Context:', JSON.stringify(context.payload, null, 2));
// Only ignore status check permission errors for fork PRs
if (error.status === 403 && context.payload.pull_request.head.repo.full_name !== context.payload.repository.full_name) {
console.log('Ignoring status check permission error for fork PR');
} else {
core.setFailed(error.message);
}
}