Files
20260512-skg-tk/scripts/check-huobao-upstream.sh
2026-06-01 11:06:24 +08:00

125 lines
3.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
REPO_URL="${HUOBAO_WATCH_REPO_URL:-https://github.com/chatfire-AI/huobao-canvas}"
REF_NAME="${HUOBAO_WATCH_REF:-refs/heads/main}"
STATE_DIR="${HUOBAO_WATCH_STATE_DIR:-$ROOT_DIR/.logs/upstream-watch}"
LAST_SHA_FILE="$STATE_DIR/huobao-canvas.last-sha"
LAST_CHECK_FILE="$STATE_DIR/huobao-canvas.last-check"
LATEST_REPORT_FILE="$STATE_DIR/huobao-canvas.latest-update.md"
LOG_FILE="$STATE_DIR/huobao-canvas.watch.log"
mkdir -p "$STATE_DIR"
timestamp() {
date "+%Y-%m-%d %H:%M:%S %z"
}
log() {
printf "%s %s\n" "$(timestamp)" "$*" | tee -a "$LOG_FILE"
}
notify() {
local title="$1"
local message="$2"
if [[ "${HUOBAO_WATCH_NOTIFY:-1}" != "1" ]]; then
return 0
fi
if ! command -v osascript >/dev/null 2>&1; then
return 0
fi
/usr/bin/osascript \
-e 'on run argv' \
-e 'display notification (item 2 of argv) with title (item 1 of argv)' \
-e 'end run' \
"$title" "$message" >/dev/null 2>&1 || true
}
git_no_proxy() {
git -c http.proxy= -c https.proxy= "$@"
}
remote_line="$(git_no_proxy ls-remote "$REPO_URL" "$REF_NAME" | head -n 1 || true)"
if [[ -z "$remote_line" ]]; then
log "ERROR failed to query $REPO_URL $REF_NAME"
notify "huobao-canvas 检查失败" "无法读取 GitHub 上游,请看 $LOG_FILE"
exit 1
fi
current_sha="$(awk '{print $1}' <<<"$remote_line")"
if [[ -z "$current_sha" ]]; then
log "ERROR empty sha from $REPO_URL $REF_NAME"
notify "huobao-canvas 检查失败" "上游返回空提交号,请看 $LOG_FILE"
exit 1
fi
previous_sha=""
if [[ -f "$LAST_SHA_FILE" ]]; then
previous_sha="$(tr -d '[:space:]' < "$LAST_SHA_FILE")"
fi
printf "%s %s %s\n" "$(timestamp)" "$REPO_URL" "$current_sha" > "$LAST_CHECK_FILE"
if [[ -z "$previous_sha" ]]; then
printf "%s\n" "$current_sha" > "$LAST_SHA_FILE"
log "initialized huobao-canvas upstream watch at $current_sha"
if [[ "${HUOBAO_WATCH_NOTIFY_ON_INIT:-0}" == "1" ]]; then
notify "huobao-canvas 已开始关注" "当前 main: ${current_sha:0:7}"
fi
exit 0
fi
if [[ "$current_sha" == "$previous_sha" ]]; then
log "unchanged huobao-canvas main at ${current_sha:0:7}"
if [[ "${HUOBAO_WATCH_NOTIFY_UNCHANGED:-0}" == "1" ]]; then
notify "huobao-canvas 无更新" "当前 main 仍是 ${current_sha:0:7}"
fi
exit 0
fi
tmp_dir="$(mktemp -d "${TMPDIR:-/tmp}/huobao-canvas-watch.XXXXXX")"
cleanup() {
rm -rf "$tmp_dir"
}
trap cleanup EXIT
repo_dir="$tmp_dir/repo"
git_no_proxy clone --depth=50 "$REPO_URL" "$repo_dir" >/dev/null 2>&1 || {
log "ERROR update detected but failed to clone $REPO_URL"
notify "huobao-canvas 有更新但拉取失败" "${previous_sha:0:7} -> ${current_sha:0:7},请看 $LOG_FILE"
exit 1
}
latest_subject="$(git -C "$repo_dir" log -1 --format=%s "$current_sha" 2>/dev/null || echo "unknown commit")"
latest_author="$(git -C "$repo_dir" log -1 --format=%an "$current_sha" 2>/dev/null || echo "unknown author")"
latest_date="$(git -C "$repo_dir" log -1 --date=format-local:"%Y-%m-%d %H:%M:%S %z" --format=%ad "$current_sha" 2>/dev/null || echo "unknown date")"
commit_list="$(git -C "$repo_dir" log --oneline --max-count=20 "$previous_sha..$current_sha" 2>/dev/null || git -C "$repo_dir" log -1 --oneline "$current_sha")"
compare_url="https://github.com/chatfire-AI/huobao-canvas/compare/$previous_sha...$current_sha"
cat > "$LATEST_REPORT_FILE" <<REPORT
# huobao-canvas upstream update
- Checked at: $(timestamp)
- Repository: $REPO_URL
- Ref: $REF_NAME
- Previous: $previous_sha
- Current: $current_sha
- Latest commit: ${current_sha:0:7} $latest_subject
- Author: $latest_author
- Commit date: $latest_date
- Compare: $compare_url
## Commits
\`\`\`
$commit_list
\`\`\`
REPORT
printf "%s\n" "$current_sha" > "$LAST_SHA_FILE"
log "UPDATED huobao-canvas ${previous_sha:0:7} -> ${current_sha:0:7}: $latest_subject"
notify "huobao-canvas 有更新" "${previous_sha:0:7} -> ${current_sha:0:7}: $latest_subject"