Files
dress-pr/.gitea/workflows/check_file_size.yml
T
xhgitandClaude Code cf17c9ea42 Initial commit: 小南娘 - 面向可爱的蓝孩子的 git 学习实践项目
仿 Dress 的照片投稿仓库, 适配自建 Gitea 平台 git.xhmcl.com:
- README / GUIDE (PR 教程) / CONTRIBUTING / Q&A / EXIF / PARTIAL_CLONE (中英双语)
- A-Z + # 首字母目录结构
- .gitea: PR 模板、维护者名单、7 个纯 bash + API 的 Actions 工作流 (可选功能)

Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-07 17:17:27 +08:00

116 lines
4.8 KiB
YAML

# Check File Size
#
# 仅在 PR 为图片提交时检查变更图片是否均 <= 1 MiB (纯 shell + 平台 API).
#
# 检查 pr-type (PR body 中的 <!-- pr-type: xxx -->):
# - images (或缺省无标签) -> 执行检查
# - ci_wf / docs / others -> 跳过, 不 fail
# 无变更图片时跳过, 不 fail (无图提醒交给 check_pr_content)
#
# 要求:
# - 平台启用 Gitea Actions 并注册 ubuntu runner
# - 若 Gitea 版本不支持 pull_request_target, 请改为 pull_request
name: Check File Size
on:
pull_request_target:
types: [opened, synchronize, reopened]
jobs:
check-size:
runs-on: ubuntu-latest
steps:
- name: Install jq
run: |
command -v jq && exit 0
sudo apt-get install -y --no-install-recommends jq \
|| (sudo apt-get update && sudo apt-get install -y --no-install-recommends jq)
- name: Check file sizes
env:
TOKEN: ${{ secrets.GITHUB_TOKEN }}
API: ${{ gitea.server_url || 'https://git.xhmcl.com' }}/api/v1
REPO: ${{ github.repository }}
PR_NUM: ${{ github.event.pull_request.number }}
HEAD_OWNER: ${{ github.event.pull_request.head.repo.owner.login }}
HEAD_REPO: ${{ github.event.pull_request.head.repo.name }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
set -euo pipefail
OWNER="${REPO%%/*}"
NAME="${REPO#*/}"
MAX_SIZE=$((1024 * 1024)) # 1 MiB
IMAGE_EXT_RE='\.(jpe?g|jfif|png|webp|tiff?|hei[cf]|avif|gif|bmp)$'
ACTIVE_RE='^(added|modified|renamed|copied)$'
api() {
curl -fsS -H "Authorization: token ${TOKEN}" -H "Content-Type: application/json" "$@"
}
PR_BODY=$(api "$API/repos/$OWNER/$NAME/pulls/$PR_NUM" | jq -r '.body // ""')
PR_TYPE=$(printf '%s\n' "$PR_BODY" | grep -oE 'pr-type:[[:space:]]*[A-Za-z_0-9]+' | head -1 | cut -d: -f2 | tr -d '[:space:]')
PR_TYPE=${PR_TYPE:-images}
if [ "$PR_TYPE" != "images" ]; then
echo "pr-type is \"$PR_TYPE\", skip file size check."
exit 0
fi
FILES_TMP=$(mktemp)
page=1
while :; do
resp=$(api "$API/repos/$OWNER/$NAME/pulls/$PR_NUM/files?limit=50&page=$page")
n=$(echo "$resp" | jq 'if type=="array" then length else 0 end')
echo "$resp" | jq -r '.[] | "\(.status // "unknown")\t\(.filename // "")"' >> "$FILES_TMP" || true
[ "$n" -lt 50 ] && break
page=$((page + 1))
done
IMAGES=$(awk -F'\t' -v re="$ACTIVE_RE" '$1 ~ re {print $2}' "$FILES_TMP" | grep -iE "$IMAGE_EXT_RE" || true)
if [ -z "$IMAGES" ]; then
echo "No changed images found, skip file size check."
exit 0
fi
encode_path() {
# '#' 必须转义, 否则会被当作 URL fragment
printf '%s' "$1" | sed -e 's/%/%25/g' -e 's/#/%23/g' -e 's/?/%3F/g' -e 's/ /%20/g'
}
OVERSIZED=()
while IFS= read -r file; do
[ -z "$file" ] && continue
enc=$(encode_path "$file")
content_json=$(api "$API/repos/$HEAD_OWNER/$HEAD_REPO/contents/$enc?ref=$HEAD_SHA" 2>/dev/null || true)
size=$(echo "$content_json" | jq -r 'if type=="object" and .size then .size else empty end' 2>/dev/null || true)
if [ -z "$size" ]; then
echo "Warning: cannot read $file (maybe a directory or deleted), skip."
continue
fi
if [ "$size" -gt "$MAX_SIZE" ]; then
OVERSIZED+=("- $file ($size bytes)")
fi
done <<< "$IMAGES"
if [ "${#OVERSIZED[@]}" -gt 0 ]; then
MSG="我们注意到以下文件的大小超过了 1 MiB, 请在压缩后再次提交 Pull Request:
We have noted that the following files exceed 1 MiB in size. Please resubmit your pull request after compressing them:
$(printf '%s\n' "${OVERSIZED[@]}")
---
温馨提示: 您可以直接使用 \`/auto-fix\` 命令来自动修复这些文件。
Warm reminder: You can use the \`/auto-fix\` command to automatically fix these files.
该命令会将图片压缩到 1 MiB 内, 并且去除**所有的 EXIF**信息, 若您不希望文件被改动, 请自行处理后重新提交 PR
This command will compress images to under 1 MiB and strip all EXIF metadata.
If you do not want your files modified, please fix them yourself and push again."
BODY_JSON=$(jq -n --arg b "$MSG" '{"body": $b}')
api -X POST -d "$BODY_JSON" "$API/repos/$OWNER/$NAME/issues/$PR_NUM/comments"
echo "File size check failed."
exit 1
fi
echo "All changed images are under 1 MiB."