Files
Dress/.github/workflows/check_file_size.yml
Asahina Mafuyu 16c356e6a9
All checks were successful
Mark stale issues and pull requests / stale (push) Successful in 4s
[Enhancement] 添加自动修复工作流 (#337)
* feat: add GitHub Action to automatically strip EXIF data and compress images via PR comments

* fix: 提升尝试压缩后修复失败的用户体验

* feat: 现在修复结果会直接基于源 comment 做 update

* fix: 修复工作流仍然报告失败的问题

* doc: 添加自动修复引导

* ci: 改用 `github.rest.repos.compareCommitsWithBasehead`

* ci: 增强检查工作流
2026-04-08 00:31:01 +08:00

102 lines
3.3 KiB
YAML

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")}
---
<small>温馨提示: 您可以直接使用 `/auto-fix` 命令来自动修复这些文件。</small>
<small>Warm reminder: You can use the `/auto-fix` command to automatically fix these files.</small>
`.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.");
}