name: Check File Size on: pull_request_target: types: [opened, synchronize, reopened] permissions: contents: read pull-requests: write issues: write jobs: check-size: runs-on: ubuntu-latest steps: - name: Check file sizes uses: actions/github-script@v7 with: script: | const MAX_SIZE = 1024 * 1024; // 1 MB const prNumber = context.payload.pull_request.number; const owner = context.repo.owner; const repo = context.repo.repo; const pr = context.payload.pull_request; let oversized = []; let page = 1; let files = []; while (true) { const compare = await github.rest.repos.compareCommitsWithBasehead({ owner, repo, basehead: `${pr.base.sha}...${pr.head.sha}`, per_page: 100, page }); const pageFiles = compare.data.files || []; if (pageFiles.length === 0) break; files.push(...pageFiles); if (pageFiles.length < 100) break; page++; } for (const file of files) { try { // 只检查新增或修改文件 if (!['added','modified','renamed','copied'].includes(file.status)) continue; // 先编码 %,再编码 # 和 ?,避免双重编码问题。 const encodedPath = file.filename .replace(/%/g, '%25') .replace(/#/g, '%23') .replace(/\?/g, '%3F'); const content = await github.rest.repos.getContent({ owner: pr.head.repo.owner.login, repo: pr.head.repo.name, path: encodedPath, ref: pr.head.sha }); const size = content.data.size; if (size > MAX_SIZE) { oversized.push(`- ${file.filename} (${size} bytes)`); } } catch (error) { core.warning(`Skip file ${file.filename}: ${error.message}`); continue; } } if (oversized.length > 0) { const body = ` 我们注意到以下文件的大小超过了 1MB, 请在压缩后再次提交 Pull Request: We have noted that the following files exceed 1MB in size. Please resubmit your pull request after compressing them: ${oversized.join("\n")} --- 温馨提示: 您可以直接使用 `/auto-fix` 命令来自动修复这些文件。 Warm reminder: You can use the `/auto-fix` command to automatically fix these files. `.trim(); await github.rest.issues.createComment({ owner, repo, issue_number: prNumber, body }); core.setFailed("File size check failed"); } else { core.info("All changed files are under 1 MB."); }