-
Notifications
You must be signed in to change notification settings - Fork 0
110 lines (89 loc) · 3.47 KB
/
Copy pathbuild-org-statistics.yml
File metadata and controls
110 lines (89 loc) · 3.47 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
name: Build Organization Language Statistics
on:
workflow_dispatch:
schedule:
- cron: '0 9 * * 1'
jobs:
language-stats:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v6
- name: Build and Update Stats
uses: nick-fields/retry@v3
env:
GH_TOKEN: ${{ secrets.ORG_STATS_PAT }}
ORG: ${{ github.repository_owner }}
with:
max_attempts: 3
timeout_seconds: 30
command: |
gh repo list $ORG --limit 1000 --json name --jq '.[].name' | while read repo; do
gh api repos/$ORG/$repo/languages 2>/dev/null || echo '{}'
sleep 0.1
done | jq -s '
reduce .[] as $lang (
{};
reduce ($lang | to_entries[]) as $entry (
.;
.[$entry.key] += $entry.value
)
)
| to_entries
| sort_by(-.value)
| (map(.value) | add) as $total
| map({language: .key, bytes: .value, percentage: (.value / $total * 100 | (. * 10 | round) / 10)})
' > stats.json
UPDATED_AT=$(date -u +"%Y-%m-%d")
export UPDATED_AT
python3 - <<'PYEOF'
import json, os
updated_at = os.environ['UPDATED_AT']
with open('stats.json') as f:
stats = json.load(f)
def bar(pct):
return f"}/?width=200&progress_color=58A6FF)"
rows = []
for i, s in enumerate(stats, 1):
rows.append(
f"| {i} | {s['language']} | {s['bytes']:,} | {bar(s['percentage'])} |"
)
table = (
"| # | Language | Bytes | Share |\n"
"|--:|----------|------:|:------|\n"
+ "\n".join(rows)
)
note = (
f"\n\n_Generated automatically by "
f"[`.github/workflows/build-org-statistics.yml`](.github/workflows/build-org-statistics.yml)"
f" · Last updated: {updated_at}_"
)
with open('README.md') as f:
content = f.read()
section_header = "## Technology Stack"
idx = content.find(section_header)
if idx == -1:
print("Section not found in README.md")
exit(1)
end_of_header = content.find('\n', idx) + 1
rest = content[end_of_header:]
next_section = -1
for pattern in ['\n## ', '\n---']:
pos = rest.find(pattern)
if pos != -1 and (next_section == -1 or pos < next_section):
next_section = pos
after_section = rest[next_section:] if next_section != -1 else ""
new_content = content[:end_of_header] + "\n" + table + note + "\n" + after_section
with open('README.md', 'w') as f:
f.write(new_content)
print("README.md updated successfully")
PYEOF
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add README.md
git diff --staged --quiet || git commit -m "Update technology stack statistics"
git push
- name: Debug stats.json
run: cat stats.json