@@ -14,21 +14,125 @@ jobs:
1414 with :
1515 ref : ${{ github.head_ref || github.ref_name }}
1616
17+ - name : Decrypt Existing Stats
18+ env :
19+ STATS_ENCRYPTION_KEY : ${{ secrets.STATS_ENCRYPTION_KEY }}
20+ run : |
21+ # Decrypt existing files if they exist
22+ if [ -f "stats/clones.json.enc" ]; then
23+ echo "Decrypting clones.json..."
24+ openssl enc -aes-256-cbc -d -pbkdf2 -in stats/clones.json.enc -out stats/clones.json -k "$STATS_ENCRYPTION_KEY"
25+ else
26+ echo "No encrypted clones.json found, starting fresh"
27+ echo '{"count": 0, "uniques": 0, "clones": []}' > stats/clones.json
28+ fi
29+
30+ if [ -f "stats/views.json.enc" ]; then
31+ echo "Decrypting views.json..."
32+ openssl enc -aes-256-cbc -d -pbkdf2 -in stats/views.json.enc -out stats/views.json -k "$STATS_ENCRYPTION_KEY"
33+ else
34+ echo "No encrypted views.json found, starting fresh"
35+ echo '{"count": 0, "uniques": 0, "views": []}' > stats/views.json
36+ fi
37+
1738 - name : Collect Traffic Data
1839 env :
1940 GH_TOKEN : ${{ secrets.REPO_STATS_TOKEN}}
2041 run : |
2142 echo "Collecting stats at $(date)"
2243
23- # Collect clones
24- curl -v -H "Authorization: token $GH_TOKEN" \
44+ # Collect new clones data
45+ curl -s -H "Authorization: token $GH_TOKEN" \
2546 https://api.github.com/repos/${{ github.repository }}/traffic/clones \
26- > stats/clones_$(date +%Y%m%d) .json
47+ > /tmp/new_clones .json
2748
28- # Collect views
29- curl -v -H "Authorization: token $GH_TOKEN" \
49+ # Collect new views data
50+ curl -s -H "Authorization: token $GH_TOKEN" \
3051 https://api.github.com/repos/${{ github.repository }}/traffic/views \
31- > stats/views_$(date +%Y%m%d).json
52+ > /tmp/new_views.json
53+
54+ # Merge clones data
55+ python3 << 'PYTHON'
56+ import json
57+ from collections import defaultdict
58+
59+ # Read existing consolidated data
60+ try:
61+ with open('stats/clones.json', 'r') as f:
62+ existing = json.load(f)
63+ existing_clones = {entry['timestamp']: entry for entry in existing['clones']}
64+ except FileNotFoundError:
65+ existing_clones = {}
66+
67+ # Read new data
68+ with open('/tmp/new_clones.json', 'r') as f:
69+ new_data = json.load(f)
70+
71+ # Merge - new data overwrites existing for same timestamps
72+ for entry in new_data['clones']:
73+ existing_clones[entry['timestamp']] = entry
74+
75+ # Create consolidated output
76+ clones_array = [existing_clones[ts] for ts in sorted(existing_clones.keys())]
77+ output = {
78+ 'count': sum(e['count'] for e in clones_array),
79+ 'uniques': max(e['uniques'] for e in clones_array) if clones_array else 0,
80+ 'clones': clones_array
81+ }
82+
83+ with open('stats/clones.json', 'w') as f:
84+ json.dump(output, f, indent=2)
85+
86+ print(f"Updated clones.json with {len(clones_array)} daily entries")
87+ PYTHON
88+
89+ # Merge views data
90+ python3 << 'PYTHON'
91+ import json
92+ from collections import defaultdict
93+
94+ # Read existing consolidated data
95+ try:
96+ with open('stats/views.json', 'r') as f:
97+ existing = json.load(f)
98+ existing_views = {entry['timestamp']: entry for entry in existing['views']}
99+ except FileNotFoundError:
100+ existing_views = {}
101+
102+ # Read new data
103+ with open('/tmp/new_views.json', 'r') as f:
104+ new_data = json.load(f)
105+
106+ # Merge - new data overwrites existing for same timestamps
107+ for entry in new_data['views']:
108+ existing_views[entry['timestamp']] = entry
109+
110+ # Create consolidated output
111+ views_array = [existing_views[ts] for ts in sorted(existing_views.keys())]
112+ output = {
113+ 'count': sum(e['count'] for e in views_array),
114+ 'uniques': max(e['uniques'] for e in views_array) if views_array else 0,
115+ 'views': views_array
116+ }
117+
118+ with open('stats/views.json', 'w') as f:
119+ json.dump(output, f, indent=2)
120+
121+ print(f"Updated views.json with {len(views_array)} daily entries")
122+ PYTHON
123+
124+ - name : Encrypt Stats Files
125+ env :
126+ STATS_ENCRYPTION_KEY : ${{ secrets.STATS_ENCRYPTION_KEY }}
127+ run : |
128+ echo "Encrypting stats files..."
129+ openssl enc -aes-256-cbc -pbkdf2 -in stats/clones.json -out stats/clones.json.enc -k "$STATS_ENCRYPTION_KEY"
130+ openssl enc -aes-256-cbc -pbkdf2 -in stats/views.json -out stats/views.json.enc -k "$STATS_ENCRYPTION_KEY"
131+
132+ # Remove unencrypted files
133+ rm stats/clones.json stats/views.json
134+
135+ echo "Encryption complete"
32136
33137 - name : Commit Stats
34138 run : |
38142 echo "Git status before add:"
39143 git status
40144
41- git add -f stats/*.json
145+ git add -f stats/*.json.enc
42146
43147 echo "Git status after add:"
44148 git status
0 commit comments