仿 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>
217 lines
11 KiB
YAML
217 lines
11 KiB
YAML
# Check PR Content
|
||
#
|
||
# 综合检查 PR 描述与变更内容 (纯 shell + 平台 API, 不依赖第三方 action).
|
||
#
|
||
# 检查 pr-type (PR body 中的 <!-- pr-type: xxx -->):
|
||
# - 缺省无标签 -> 当作 images
|
||
# - 所有类型 -> 自查清单须存在且全部勾选
|
||
# - images -> 须有图片变更; 目录须符合 A-Z/#/昵称/
|
||
# - ci_wf -> 变更须均在 .gitea/ 下
|
||
# - docs/others -> 仅清单检查
|
||
#
|
||
# 任一项不通过: 评论 + fail
|
||
# 评论计数: 每条 check 失败评论带 <!-- check-pr-content --> marker
|
||
# 计数达到 3 后不再发 check 评论, 改为 @ 维护者 (.gitea/MAINTAINERS.yml) 通知, 带 <!-- maintainer-notify --> marker
|
||
# 通知只发一次; 之后完全静默 (检查照跑, 不发任何评论)
|
||
#
|
||
# 要求:
|
||
# - 平台启用 Gitea Actions 并注册 ubuntu runner
|
||
# - 若 Gitea 版本不支持 pull_request_target, 请改为 pull_request
|
||
name: Check PR Content
|
||
|
||
on:
|
||
pull_request_target:
|
||
types: [opened, synchronize, reopened, edited]
|
||
|
||
concurrency:
|
||
group: pr-content-${{ github.event.pull_request.number }}
|
||
cancel-in-progress: false
|
||
|
||
jobs:
|
||
check-content:
|
||
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 PR content
|
||
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 }}
|
||
run: |
|
||
set -euo pipefail
|
||
OWNER="${REPO%%/*}"
|
||
NAME="${REPO#*/}"
|
||
|
||
api() {
|
||
curl -fsS -H "Authorization: token ${TOKEN}" -H "Content-Type: application/json" "$@"
|
||
}
|
||
|
||
IMAGE_EXT_RE='\.(jpe?g|jfif|png|webp|tiff?|hei[cf]|avif|gif|bmp)$'
|
||
ACTIVE_RE='^(added|modified|renamed|copied)$'
|
||
CHECK_COMMENT_MARKER='<!-- check-pr-content -->'
|
||
NOTIFY_MARKER='<!-- maintainer-notify -->'
|
||
MAX_CHECK_COMMENTS=3
|
||
|
||
# 1. 读取 PR body (避免在 yaml 中直接注入多行 payload)
|
||
PR_BODY=$(api "$API/repos/$OWNER/$NAME/pulls/$PR_NUM" | jq -r '.body // ""')
|
||
|
||
# 2. 解析 pr-type
|
||
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:]')
|
||
if [ -z "$PR_TYPE" ]; then
|
||
PR_TYPE_MISSING=1
|
||
PR_TYPE=images
|
||
else
|
||
PR_TYPE_MISSING=0
|
||
fi
|
||
|
||
# 3. 自查清单
|
||
CHECK_TOTAL=$(printf '%s\n' "$PR_BODY" | grep -cE '^[[:space:]]*-[[:space:]]*\[[ xX]\]' || true)
|
||
UNCHECKED=$(printf '%s\n' "$PR_BODY" | grep -E '^[[:space:]]*-[[:space:]]*\[[[:space:]]\][[:space:]]*(.*)$' | sed -E 's/^[[:space:]]*-[[:space:]]*\[[[:space:]]\][[:space:]]*/- /' || true)
|
||
|
||
# 4. 变更文件列表 (分页)
|
||
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
|
||
|
||
ACTIVE_FILES=$(awk -F'\t' -v re="$ACTIVE_RE" '$1 ~ re {print $2}' "$FILES_TMP")
|
||
|
||
expected_bucket() {
|
||
first=$(printf '%s' "$1" | cut -c1 | tr '[:lower:]' '[:upper:]')
|
||
case "$first" in
|
||
[A-Z]) echo "$first" ;;
|
||
*) echo '#' ;;
|
||
esac
|
||
}
|
||
|
||
SECTIONS=()
|
||
FAILED=0
|
||
|
||
# 5. 清单检查 (所有 pr-type)
|
||
if [ "$CHECK_TOTAL" -eq 0 ]; then
|
||
FAILED=1
|
||
SECTIONS+=("我们未在 PR 描述中找到自查清单, 请使用仓库提供的 Pull Request 模板, 勿删除清单项:\nWe could not find a checklist in the PR body. Please use the repository Pull Request template and do not remove the checklist items.")
|
||
elif [ -n "$UNCHECKED" ]; then
|
||
FAILED=1
|
||
SECTIONS+=("我们注意到 PR 模板中的自查清单尚未全部勾选, 请确认并勾选后再次提交:\nWe noticed that the PR template checklist is incomplete. Please check all items and update the PR:\n\n$(printf '%s\n' "$UNCHECKED")")
|
||
fi
|
||
|
||
# 6. 按 pr-type 检查
|
||
case "$PR_TYPE" in
|
||
images)
|
||
IMAGES=$(printf '%s\n' "$ACTIVE_FILES" | grep -iE "$IMAGE_EXT_RE" || true)
|
||
if [ -z "$IMAGES" ]; then
|
||
FAILED=1
|
||
if [ "$PR_TYPE_MISSING" -eq 1 ]; then
|
||
SECTIONS+=("PR 描述中未找到 pr-type 标签, 已按图片提交 (images) 处理; 但未检测到任何图片文件变更.\nNo pr-type tag was found in the PR body, so it was treated as an image submission (images); no changed image files were detected.\n\n若本次不是提交照片, 请关闭后把 pr-type 改为 ci_wf / docs / others.\nIf this PR is not for photos, please close it and set pr-type to ci_wf / docs / others.\n\n若确为图片提交, 请将图片放入对应目录后再 push.\nIf this is a photo submission, please add images under the proper folders and push again.")
|
||
else
|
||
SECTIONS+=("当前 PR 类型为图片提交 (pr-type: images), 但未检测到任何图片文件变更.\nThis PR is typed as an image submission (pr-type: images), but no changed image files were detected.\n\n若本次不是提交照片, 请关闭后把 pr-type 改为 ci_wf / docs / others.\nIf this PR is not for photos, please close it and set pr-type to ci_wf / docs / others.\n\n若确为图片提交, 请将图片放入对应目录后再 push.\nIf this is a photo submission, please add images under the proper folders and push again.")
|
||
fi
|
||
fi
|
||
|
||
# 目录归类
|
||
INVALID=()
|
||
while IFS= read -r file; do
|
||
[ -z "$file" ] && continue
|
||
parts0=$(printf '%s' "$file" | cut -d/ -f1)
|
||
parts1=$(printf '%s' "$file" | cut -d/ -f2)
|
||
parts_count=$(printf '%s' "$file" | awk -F/ '{print NF}')
|
||
case "$parts0" in
|
||
.gitea|.github|en-US) continue ;;
|
||
esac
|
||
if [ "$parts_count" -eq 1 ]; then
|
||
case "$parts0" in
|
||
.gitattributes|.gitignore|CONTRIBUTING.md|EXIF.md|GUIDE.md|LICENSE|README.md|Q\&A.md|PARTIAL_CLONE.md) continue ;;
|
||
esac
|
||
INVALID+=("- ${file}(文件应位于 A-Z/# 下的个人文件夹内)")
|
||
continue
|
||
fi
|
||
if [ "$parts_count" -lt 3 ]; then
|
||
INVALID+=("- ${file}(文件应位于 A-Z/# 下的个人文件夹内)")
|
||
continue
|
||
fi
|
||
expected=$(expected_bucket "$parts1")
|
||
if [ "$parts0" != "$expected" ]; then
|
||
INVALID+=("- ${file}(应位于 ${expected}/${parts1}/...)")
|
||
fi
|
||
done <<< "$ACTIVE_FILES"
|
||
|
||
if [ "${#INVALID[@]}" -gt 0 ]; then
|
||
FAILED=1
|
||
SECTIONS+=("我们注意到以下文件的目录归类不正确, 请调整路径后再次推送:\nWe noticed that the following files are not under the correct A-Z/# folder. Please fix the paths and push again:\n\n$(printf '%s\n' "${INVALID[@]}")\n\n正确示例 / Examples:\n- M/moe/photo.jpg\n- #/233/photo.jpg")
|
||
fi
|
||
;;
|
||
ci_wf)
|
||
OUTSIDE=$(printf '%s\n' "$ACTIVE_FILES" | grep -vE '^(\.gitea/|$)' || true)
|
||
if [ -n "$OUTSIDE" ]; then
|
||
FAILED=1
|
||
SECTIONS+=("当前 PR 类型为 CI / 工作流修改 (pr-type: ci_wf), 但检测到 \`.gitea/\` 目录之外的文件变更:\nThis PR is typed as a CI / workflow change (pr-type: ci_wf), but files outside \`.gitea/\` were changed:\n\n$(printf '%s\n' "$OUTSIDE" | sed 's/^/- /')\n\n请将非 CI 改动拆到其他 PR, 或把 pr-type 改为对应类型 (images / docs / others).\nPlease split non-CI changes into another PR, or switch pr-type to the matching type (images / docs / others).")
|
||
fi
|
||
;;
|
||
docs|others)
|
||
echo "pr-type is \"$PR_TYPE\", only checklist rules apply."
|
||
;;
|
||
*)
|
||
FAILED=1
|
||
SECTIONS+=("未识别的 pr-type: \"$PR_TYPE\". 请使用模板中的 images / ci_wf / docs / others.\nUnrecognized pr-type: \"$PR_TYPE\". Please use images / ci_wf / docs / others from the template.")
|
||
;;
|
||
esac
|
||
|
||
# 7. 评论策略: 3 条 check 评论 → 维护者通知(一次) → 静默
|
||
if [ "${#SECTIONS[@]}" -gt 0 ]; then
|
||
COMMENT_PAGE=1
|
||
COMMENTS_TMP=$(mktemp)
|
||
while :; do
|
||
resp=$(api "$API/repos/$OWNER/$NAME/issues/$PR_NUM/comments?limit=50&page=$COMMENT_PAGE")
|
||
n=$(echo "$resp" | jq 'if type=="array" then length else 0 end')
|
||
echo "$resp" | jq -r '.[] | .body // ""' >> "$COMMENTS_TMP" || true
|
||
[ "$n" -lt 50 ] && break
|
||
COMMENT_PAGE=$((COMMENT_PAGE + 1))
|
||
done
|
||
|
||
CHECK_COUNT=$(grep -cF "$CHECK_COMMENT_MARKER" "$COMMENTS_TMP" || true)
|
||
NOTIFIED=$(grep -cF "$NOTIFY_MARKER" "$COMMENTS_TMP" || true)
|
||
|
||
BODY_MSG=$(printf '%s\n\n---\n\n' "${SECTIONS[@]}")
|
||
BODY_MSG=${BODY_MSG%$'\n\n---\n\n'}
|
||
|
||
if [ "$NOTIFIED" -gt 0 ]; then
|
||
echo "Maintainer already notified. Silent mode: no further comments."
|
||
elif [ "$CHECK_COUNT" -lt "$MAX_CHECK_COMMENTS" ]; then
|
||
COMMENT_JSON=$(jq -n --arg b "${BODY_MSG}\n\n${CHECK_COMMENT_MARKER}" '{"body": $b}')
|
||
api -X POST -d "$COMMENT_JSON" "$API/repos/$OWNER/$NAME/issues/$PR_NUM/comments"
|
||
else
|
||
# 读取维护者名单
|
||
MAINTAINERS=""
|
||
MAINT_JSON=$(api "$API/repos/$OWNER/$NAME/contents/.gitea/MAINTAINERS.yml" || true)
|
||
if [ -n "$MAINT_JSON" ]; then
|
||
MAINTAINERS=$(echo "$MAINT_JSON" | jq -r '.content // ""' | base64 -d 2>/dev/null \
|
||
| grep -oE '^[[:space:]]*-[[:space:]]*@?[A-Za-z0-9-]+[[:space:]]*$' \
|
||
| sed -E 's/^[[:space:]]*-[[:space:]]*@?//; s/[[:space:]]*$//' \
|
||
| sed 's/^/@/' | tr '\n' ' ')
|
||
fi
|
||
if [ -z "$MAINTAINERS" ]; then
|
||
MAINTAINERS='(MAINTAINERS.yml 中暂无维护者)'
|
||
fi
|
||
NOTIFY_JSON=$(jq -n --arg b "该 PR 已连续多次未通过内容检查, 请维护者人工处理:\nThis PR has repeatedly failed the content check. Please have a maintainer review it manually:\n\n${MAINTAINERS}\n\n在维护者处理前, 本检查将不再自动评论.\nThis check will stay silent until a maintainer takes over.\n\n${NOTIFY_MARKER}" '{"body": $b}')
|
||
api -X POST -d "$NOTIFY_JSON" "$API/repos/$OWNER/$NAME/issues/$PR_NUM/comments"
|
||
fi
|
||
fi
|
||
|
||
if [ "$FAILED" -ne 0 ]; then
|
||
echo "PR content check failed."
|
||
exit 1
|
||
fi
|
||
echo "PR content check passed."
|