Repository navigation
137 lines (123 loc) · 4.95 KB
/
Copy pathtrack-adoption.yml
File metadata and controls
137 lines (123 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
name: Track adoption
on:
schedule:
- cron: "0 8 * * *" # daily at 08:00 UTC
workflow_dispatch:
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}
cancel-in-progress: false
jobs:
track:
name: Emit per-repo adoption events
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Search GitHub for .archgate/ folders
id: search
env:
# Classic PAT with `public_repo` scope. The default GITHUB_TOKEN is an
# app-installation token and is not guaranteed to work against the
# code-search API, which requires a user account.
GH_TOKEN: ${{ secrets.GH_SEARCH_TOKEN }}
run: |
set -euo pipefail
# Code search is capped at 1000 results per query. If we ever approach
# that, re-shard (e.g. by language or owner) before the count saturates.
repos_json=$(gh search code 'path:.archgate/' \
--limit 1000 \
--json repository \
--jq '
[.[].repository]
| map(select(.isPrivate == false))
| unique_by(.nameWithOwner)
| map({
full_name: .nameWithOwner,
owner: (.nameWithOwner | split("/")[0]),
name: (.nameWithOwner | split("/")[1]),
url: .url,
is_fork: .isFork
})
')
repo_count=$(jq 'length' <<< "$repos_json")
file_count=$(gh api -X GET search/code -f q='path:.archgate/' --jq '.total_count')
echo "repo_count=$repo_count" >> "$GITHUB_OUTPUT"
{
echo "repos_json<<EOF"
echo "$repos_json"
echo "EOF"
} >> "$GITHUB_OUTPUT"
echo "::notice::Found $repo_count public repos ($file_count file matches)"
# GitHub code search caps results at 1000 per query. Beyond that, the
# repo count plateaus silently. Warn early so we shard before saturating.
if [ "$file_count" -ge 1000 ]; then
echo "::error::File match count is ${file_count} — AT the 1000-result search cap. Repo discovery is now incomplete; shard the query (see comment above) before trusting this data."
elif [ "$file_count" -ge 800 ]; then
echo "::warning::File match count is ${file_count} — approaching the 1000-result search cap. Plan query sharding before we saturate."
fi
- name: Send per-repo events to PostHog
env:
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}
POSTHOG_HOST: ${{ vars.POSTHOG_HOST || 'https://eu.i.posthog.com' }}
REPOS_JSON: ${{ steps.search.outputs.repos_json }}
run: |
set -euo pipefail
date_utc=$(date -u +%Y-%m-%d)
timestamp="${date_utc}T$(date -u +%H:%M:%SZ)"
# Deterministic UUIDv5 per (event, UTC date, repo). PostHog dedupes
# events that share a uuid with a previously-ingested event, so a
# second workflow run on the same day (e.g. manual dispatch after
# the cron) is a no-op at ingestion.
uuids_json=$(python3 - "$date_utc" <<'PY'
import json, os, sys, uuid
date = sys.argv[1]
repos = json.loads(os.environ["REPOS_JSON"])
out = {
r["full_name"]: str(
uuid.uuid5(
uuid.NAMESPACE_URL,
f"archgate_repo_seen:{date}:{r['full_name']}",
)
)
for r in repos
}
print(json.dumps(out))
PY
)
# One event per repo, batched into a single PostHog call.
# distinct_id = "owner/name" makes each repo a "person" in PostHog,
# so first-seen / last-seen and per-repo timelines come for free.
# $set populates the person properties so the Persons view is useful.
payload=$(jq -n \
--arg api_key "$POSTHOG_API_KEY" \
--arg timestamp "$timestamp" \
--argjson repos "$REPOS_JSON" \
--argjson uuids "$uuids_json" \
'{
api_key: $api_key,
batch: ($repos | map({
event: "archgate_repo_seen",
distinct_id: .full_name,
uuid: $uuids[.full_name],
timestamp: $timestamp,
properties: {
owner: .owner,
name: .name,
full_name: .full_name,
url: .url,
is_fork: .is_fork,
"$lib": "github-actions",
"$set": {
owner: .owner,
name: .name,
full_name: .full_name,
url: .url,
is_fork: .is_fork
}
}
}))
}')
curl -fsS -X POST "$POSTHOG_HOST/batch/" \
-H "Content-Type: application/json" \
--data-binary "$payload"