Skip to content

Commit e16ee46

Browse files
committed
Enhance sync workflow with state management and download delta tracking
Added steps to restore and save the sync state using caching, enabling incremental download tracking. Updated the workflow to calculate and log download deltas, improving efficiency and clarity in reporting new downloads to PostHog.
1 parent be58d50 commit e16ee46

1 file changed

Lines changed: 53 additions & 5 deletions

File tree

.github/workflows/sync-github-downloads.yml

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ jobs:
1010
sync:
1111
runs-on: ubuntu-latest
1212
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v4
15+
16+
- name: Restore previous sync state
17+
uses: actions/cache/restore@v4
18+
with:
19+
path: .sync-state.json
20+
key: posthog-sync-state-${{ github.repository_id }}
21+
restore-keys: |
22+
posthog-sync-state-
23+
1324
- name: Fetch release data and sync to PostHog
1425
env:
1526
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}
@@ -26,6 +37,21 @@ jobs:
2637
POSTHOG_HOST="https://us.i.posthog.com"
2738
echo "Using default POSTHOG_HOST: ${POSTHOG_HOST}"
2839
fi
40+
41+
# 获取当前同步时间(ISO 8601 格式)
42+
sync_timestamp=$(date -u +"%Y-%m-%dT%H:%M:%S.000Z")
43+
echo "Sync timestamp: ${sync_timestamp}"
44+
45+
# 加载上次同步的状态(用于计算增量)
46+
if [ -f .sync-state.json ] && [ "$(cat .sync-state.json)" != "{}" ]; then
47+
echo "Loaded previous sync state"
48+
echo "true" > .has-previous-state
49+
else
50+
echo '{}' > .sync-state.json
51+
echo "false" > .has-previous-state
52+
echo "No previous sync state, starting fresh"
53+
fi
54+
2955
# 获取所有 releases
3056
releases=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" \
3157
"https://api.github.com/repos/mantra-hq/mantra-releases/releases?per_page=100")
@@ -43,7 +69,6 @@ jobs:
4369
echo "$release" | jq -c '.assets[]' | while read -r asset; do
4470
asset_name=$(echo "$asset" | jq -r '.name')
4571
download_count=$(echo "$asset" | jq -r '.download_count')
46-
asset_created=$(echo "$asset" | jq -r '.created_at')
4772
4873
# 跳过签名文件
4974
if [[ "$asset_name" == *.sig ]]; then
@@ -68,21 +93,38 @@ jobs:
6893
platform="linux"
6994
arch="x64"
7095
fi
96+
97+
# 计算增量下载数
98+
asset_key="${version}:${asset_name}"
99+
previous_count=$(jq -r --arg key "$asset_key" '.[$key] // 0' .sync-state.json)
100+
download_delta=$((download_count - previous_count))
71101
72-
# 发送到 PostHog
102+
# 更新状态文件
103+
jq --arg key "$asset_key" --argjson val "$download_count" \
104+
'.[$key] = $val' .sync-state.json > .sync-state.tmp && mv .sync-state.tmp .sync-state.json
105+
106+
# 跳过无增量的记录(首次同步除外)
107+
has_previous=$(cat .has-previous-state)
108+
if [ "$download_delta" -le 0 ] && [ "$has_previous" = "true" ]; then
109+
echo " - ${asset_name}: no new downloads (total: ${download_count})"
110+
continue
111+
fi
112+
113+
# 发送到 PostHog(使用当前同步时间)
73114
response=$(curl -s -w "\n%{http_code}" -X POST "${POSTHOG_HOST}/capture/" \
74115
-H "Content-Type: application/json" \
75116
-d "{
76117
\"api_key\": \"${POSTHOG_API_KEY}\",
77118
\"event\": \"github_release_download\",
78119
\"distinct_id\": \"github-release-sync\",
79-
\"timestamp\": \"${asset_created}\",
120+
\"timestamp\": \"${sync_timestamp}\",
80121
\"properties\": {
81122
\"version\": \"${version}\",
82123
\"asset_name\": \"${asset_name}\",
83124
\"platform\": \"${platform}\",
84125
\"arch\": \"${arch}\",
85126
\"download_count\": ${download_count},
127+
\"download_delta\": ${download_delta},
86128
\"published_at\": \"${published_at}\"
87129
}
88130
}")
@@ -91,13 +133,19 @@ jobs:
91133
response_body=$(echo "$response" | head -n -1)
92134
93135
if [ "$http_code" = "200" ]; then
94-
echo " ✓ ${asset_name}: ${download_count} downloads (${platform}/${arch})"
136+
echo " ✓ ${asset_name}: +${download_delta} new downloads (total: ${download_count}) (${platform}/${arch})"
95137
else
96138
echo " ✗ Failed: ${asset_name} (HTTP ${http_code})"
97139
echo " Response: ${response_body}"
98140
fi
99141
done
100142
done
101-
143+
102144
echo ""
103145
echo "Sync completed!"
146+
147+
- name: Save sync state
148+
uses: actions/cache/save@v4
149+
with:
150+
path: .sync-state.json
151+
key: posthog-sync-state-${{ github.repository_id }}

0 commit comments

Comments
 (0)