chore: add huobao upstream watch
This commit is contained in:
124
scripts/check-huobao-upstream.sh
Executable file
124
scripts/check-huobao-upstream.sh
Executable file
@@ -0,0 +1,124 @@
|
|||||||
|
#!/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"
|
||||||
29
scripts/install-huobao-upstream-watch.sh
Executable file
29
scripts/install-huobao-upstream-watch.sh
Executable file
@@ -0,0 +1,29 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||||
|
LABEL="com.skg.huobao-canvas.upstream-watch"
|
||||||
|
SOURCE_PLIST="$ROOT_DIR/scripts/launchd/$LABEL.plist"
|
||||||
|
INSTALL_DIR="$HOME/Library/LaunchAgents"
|
||||||
|
INSTALL_PLIST="$INSTALL_DIR/$LABEL.plist"
|
||||||
|
LAUNCHD_DOMAIN="gui/$(id -u)"
|
||||||
|
LOG_DIR="$ROOT_DIR/.logs/upstream-watch"
|
||||||
|
|
||||||
|
if [[ ! -f "$SOURCE_PLIST" ]]; then
|
||||||
|
echo "missing launchd plist: $SOURCE_PLIST" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
mkdir -p "$INSTALL_DIR" "$LOG_DIR"
|
||||||
|
cp "$SOURCE_PLIST" "$INSTALL_PLIST"
|
||||||
|
plutil -lint "$INSTALL_PLIST" >/dev/null
|
||||||
|
|
||||||
|
launchctl bootout "$LAUNCHD_DOMAIN/$LABEL" >/dev/null 2>&1 || true
|
||||||
|
launchctl bootstrap "$LAUNCHD_DOMAIN" "$INSTALL_PLIST"
|
||||||
|
launchctl kickstart -k "$LAUNCHD_DOMAIN/$LABEL"
|
||||||
|
|
||||||
|
echo "huobao-canvas upstream watch installed"
|
||||||
|
echo "label: $LABEL"
|
||||||
|
echo "schedule: daily 09:30 local time"
|
||||||
|
echo "plist: $INSTALL_PLIST"
|
||||||
|
echo "state/logs: $LOG_DIR"
|
||||||
40
scripts/launchd/com.skg.huobao-canvas.upstream-watch.plist
Normal file
40
scripts/launchd/com.skg.huobao-canvas.upstream-watch.plist
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||||
|
<plist version="1.0">
|
||||||
|
<dict>
|
||||||
|
<key>Label</key>
|
||||||
|
<string>com.skg.huobao-canvas.upstream-watch</string>
|
||||||
|
<key>ProgramArguments</key>
|
||||||
|
<array>
|
||||||
|
<string>/bin/zsh</string>
|
||||||
|
<string>-lc</string>
|
||||||
|
<string>cd /Users/kangwan/Projects/business/20260512-20260512-skg-tk-二创验证 && exec ./scripts/check-huobao-upstream.sh</string>
|
||||||
|
</array>
|
||||||
|
<key>WorkingDirectory</key>
|
||||||
|
<string>/Users/kangwan/Projects/business/20260512-20260512-skg-tk-二创验证</string>
|
||||||
|
<key>EnvironmentVariables</key>
|
||||||
|
<dict>
|
||||||
|
<key>PATH</key>
|
||||||
|
<string>/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
|
||||||
|
<key>HUOBAO_WATCH_NOTIFY</key>
|
||||||
|
<string>1</string>
|
||||||
|
<key>HUOBAO_WATCH_NOTIFY_UNCHANGED</key>
|
||||||
|
<string>0</string>
|
||||||
|
</dict>
|
||||||
|
<key>StandardOutPath</key>
|
||||||
|
<string>/Users/kangwan/Projects/business/20260512-20260512-skg-tk-二创验证/.logs/upstream-watch/launchd.out.log</string>
|
||||||
|
<key>StandardErrorPath</key>
|
||||||
|
<string>/Users/kangwan/Projects/business/20260512-20260512-skg-tk-二创验证/.logs/upstream-watch/launchd.err.log</string>
|
||||||
|
<key>RunAtLoad</key>
|
||||||
|
<true/>
|
||||||
|
<key>StartCalendarInterval</key>
|
||||||
|
<dict>
|
||||||
|
<key>Hour</key>
|
||||||
|
<integer>9</integer>
|
||||||
|
<key>Minute</key>
|
||||||
|
<integer>30</integer>
|
||||||
|
</dict>
|
||||||
|
<key>KeepAlive</key>
|
||||||
|
<false/>
|
||||||
|
</dict>
|
||||||
|
</plist>
|
||||||
Reference in New Issue
Block a user