# Mark Stale Pull Requests # # 每日定时扫描: 长期无活动的 PR 先标记并提醒, 7 天后仍未回应则关闭. # (纯 shell + 平台 API; issue 不处理, 只处理 PR) # # 要求: 平台启用 Gitea Actions 并注册 ubuntu runner. name: Mark stale pull requests on: schedule: - cron: '0 11 * * *' jobs: stale: 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: Scan and mark stale PRs env: TOKEN: ${{ secrets.GITHUB_TOKEN }} API: ${{ gitea.server_url || 'https://git.xhmcl.com' }}/api/v1 REPO: ${{ github.repository }} STALE_DAYS: '30' CLOSE_DAYS: '7' run: | set -euo pipefail OWNER="${REPO%%/*}" NAME="${REPO#*/}" STALE_MARKER='' now=$(date -u +%s) stale_limit=$((STALE_DAYS * 86400)) close_limit=$((CLOSE_DAYS * 86400)) api() { curl -fsS -H "Authorization: token ${TOKEN}" -H "Content-Type: application/json" "$@"; } to_epoch() { date -u -d "$1" +%s 2>/dev/null || echo 0; } post_comment() { local num="$1" msg="$2" BODY_JSON=$(jq -n --arg b "$msg" '{"body": $b}') api -X POST -d "$BODY_JSON" "$API/repos/$OWNER/$NAME/issues/$num/comments" || true } # 逐页遍历 open PR page=1 while :; do prs=$(api "$API/repos/$OWNER/$NAME/pulls?state=open&limit=50&page=$page") n=$(echo "$prs" | jq 'if type=="array" then length else 0 end') # 每个 PR 一行 JSON, 逐行处理 (避免空格的字段拆分问题) while read -r pr_json; do [ -z "$pr_json" ] && continue num=$(echo "$pr_json" | jq -r '.number') updated=$(echo "$pr_json" | jq -r '.updated_at // .created_at') ts=$(to_epoch "$updated") [ "$ts" = "0" ] && ts=$now age=$((now - ts)) # 未到 stale 阈值, 跳过 [ "$age" -lt "$stale_limit" ] && continue # 该 PR 最后一次 stale 标记评论的时间 last_marker_ts=0 cpage=1 while :; do comments=$(api "$API/repos/$OWNER/$NAME/issues/$num/comments?limit=50&page=$cpage") cn=$(echo "$comments" | jq 'if type=="array" then length else 0 end') while IFS=$'\t' read -r cts cbody; do [ -z "$cts" ] && continue cts_epoch=$(to_epoch "$cts") [ "$cts_epoch" -gt "$last_marker_ts" ] && last_marker_ts="$cts_epoch" done < <(echo "$comments" | jq -r '.[] | "\(.created_at // "")\t\(.body // "")"' | grep -F "$STALE_MARKER") cpage=$((cpage + 1)) [ "$cn" -lt 50 ] && break done if [ "$last_marker_ts" -eq 0 ]; then # 第一次标记: 评论提醒 echo "Mark stale PR #$num" post_comment "$num" "这个 PR 已许久未活跃, 请在 7 天内回应, 否则将被关闭。 This PR has had no recent activity. Please respond within 7 days, otherwise it will be closed. $STALE_MARKER" elif [ $((now - last_marker_ts)) -ge "$close_limit" ]; then # 标记后超过 7 天仍未回应: 关闭 echo "Close stale PR #$num" api -X PATCH -d '{"state":"closed"}' "$API/repos/$OWNER/$NAME/issues/$num" || true post_comment "$num" "这个 PR 因长期未活跃已关闭, 如需继续请重新打开。 This PR has been closed due to long-term inactivity. Please reopen it if you wish to continue." fi done < <(echo "$prs" | jq -c '.[]') [ "$n" -lt 50 ] && break page=$((page + 1)) done echo "Stale scan finished."