Update Organization Stats #186
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Update Organization Stats | |
| on: | |
| schedule: | |
| - cron: '0 6 * * *' # Daily at 6 AM UTC | |
| workflow_dispatch: | |
| permissions: | |
| contents: write # needed only to push svg | |
| concurrency: | |
| group: metrics-stats | |
| cancel-in-progress: true | |
| jobs: | |
| update-readme: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v3 | |
| with: | |
| persist-credentials: true | |
| - name: Generate metrics | |
| uses: lowlighter/metrics@latest | |
| with: | |
| token: ${{ secrets.METRICS_TOKEN }} | |
| user: BurkimbIA | |
| template: classic | |
| base: header, activity, community, repositories | |
| config_timezone: Africa/Ouagadougou | |
| plugin_repositories: yes | |
| plugin_repositories_featured: BurkimbIA/burkimbia | |
| plugin_activity: yes | |
| plugin_activity_limit: 5 | |
| plugin_activity_days: 14 | |
| plugin_activity_filter: all | |
| output_action: none | |
| filename: github-metrics.svg | |
| committer_branch: "" | |
| - name: Compute comprehensive organization stats | |
| env: | |
| GH_TOKEN: ${{ secrets.METRICS_TOKEN }} | |
| run: | | |
| echo "Fetching comprehensive stats for organization BurkimbIA" | |
| # Initialize counters | |
| public_repos=0 | |
| private_repos=0 | |
| total_commits=0 | |
| total_prs=0 | |
| declare -A contributors | |
| # Load contributors from metadata | |
| contributors_list=() | |
| if [ -f ".github/workflows/metadata.json" ]; then | |
| contributors_list=($(jq -r '.contributors[]' .github/workflows/metadata.json)) | |
| fi | |
| # Initialize contributor counters | |
| for contributor in "${contributors_list[@]}"; do | |
| contributors[$contributor]=0 | |
| done | |
| # Fetch all repositories (public and private) | |
| page=1 | |
| while : ; do | |
| resp=$(curl -s -H "Authorization: Bearer $GH_TOKEN" "https://api.github.com/orgs/BurkimbIA/repos?per_page=100&page=$page") | |
| count=$(echo "$resp" | jq 'length') | |
| if [ "$count" -eq 0 ]; then | |
| break | |
| fi | |
| # Count public/private repos | |
| public_count=$(echo "$resp" | jq '[.[] | select(.private==false)] | length') | |
| private_count=$(echo "$resp" | jq '[.[] | select(.private==true)] | length') | |
| public_repos=$((public_repos + public_count)) | |
| private_repos=$((private_repos + private_count)) | |
| # Process each repository for commits and PRs | |
| echo "$resp" | jq -r '.[].name' | while read repo; do | |
| if [ -n "$repo" ]; then | |
| echo "Processing repo: $repo" | |
| # Count commits (limited to avoid API rate limits) | |
| commits_resp=$(curl -s -H "Authorization: Bearer $GH_TOKEN" "https://api.github.com/repos/BurkimbIA/$repo/commits?per_page=100") | |
| if [ "$commits_resp" != "null" ] && [ "$commits_resp" != "[]" ]; then | |
| commit_count=$(echo "$commits_resp" | jq 'length // 0') | |
| total_commits=$((total_commits + commit_count)) | |
| # Count commits per contributor from metadata | |
| for contributor in "${contributors_list[@]}"; do | |
| contributor_commits=$(echo "$commits_resp" | jq --arg author "$contributor" '[.[] | select(.author.login == $author)] | length') | |
| if [ -f "/tmp/contrib_${contributor}.txt" ]; then | |
| current_count=$(cat "/tmp/contrib_${contributor}.txt") | |
| else | |
| current_count=0 | |
| fi | |
| new_count=$((current_count + contributor_commits)) | |
| echo "$new_count" > "/tmp/contrib_${contributor}.txt" | |
| done | |
| fi | |
| # Count pull requests | |
| prs_resp=$(curl -s -H "Authorization: Bearer $GH_TOKEN" "https://api.github.com/repos/BurkimbIA/$repo/pulls?state=all&per_page=100") | |
| if [ "$prs_resp" != "null" ] && [ "$prs_resp" != "[]" ]; then | |
| pr_count=$(echo "$prs_resp" | jq 'length // 0') | |
| total_prs=$((total_prs + pr_count)) | |
| fi | |
| fi | |
| done | |
| page=$((page + 1)) | |
| done | |
| # Count unique contributors | |
| total_contributors=${#contributors_list[@]} | |
| # Calculate commits per contributor | |
| commits_per_contributor=0 | |
| if [ "$total_contributors" -gt 0 ]; then | |
| commits_per_contributor=$((total_commits / total_contributors)) | |
| fi | |
| timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ") | |
| mkdir -p metrics_renders | |
| # Build contributors object | |
| contributors_json="{" | |
| first=true | |
| for contributor in "${contributors_list[@]}"; do | |
| if [ -f "/tmp/contrib_${contributor}.txt" ]; then | |
| contrib_count=$(cat "/tmp/contrib_${contributor}.txt") | |
| else | |
| contrib_count=0 | |
| fi | |
| if [ "$first" = true ]; then | |
| first=false | |
| else | |
| contributors_json+="," | |
| fi | |
| contributors_json+="\"$contributor\": $contrib_count" | |
| done | |
| contributors_json+="}" | |
| cat > metrics_renders/org-stats.json <<EOF | |
| { | |
| "organization": "BurkimbIA", | |
| "public_repositories": $public_repos, | |
| "private_repositories": $private_repos, | |
| "total_repositories": $((public_repos + private_repos)), | |
| "total_commits": $total_commits, | |
| "total_pull_requests": $total_prs, | |
| "total_contributors": $total_contributors, | |
| "commits_per_contributor": $commits_per_contributor, | |
| "contributor_commits": $contributors_json, | |
| "updated_at": "$timestamp" | |
| } | |
| EOF | |
| echo "Organization stats computed:" | |
| echo " Public repos: $public_repos" | |
| echo " Private repos: $private_repos" | |
| echo " Total commits: $total_commits" | |
| echo " Total PRs: $total_prs" | |
| echo " Contributors: $total_contributors" | |
| echo " Commits/contributor: $commits_per_contributor" | |
| echo " Individual contributions: $contributors_json" | |
| - name: Commit metrics (always) | |
| run: | | |
| git config user.name "github-actions" | |
| git config user.email "github-actions@github.com" | |
| # Debug: List all generated files | |
| echo "Files in current directory:" | |
| ls -la | |
| echo "Files in metrics_renders (if exists):" | |
| ls -la metrics_renders/ || echo "metrics_renders directory not found" | |
| # Move SVG to correct location if needed | |
| if [ -f "github-metrics.svg" ]; then | |
| echo "Found github-metrics.svg at root, moving to metrics_renders/" | |
| mkdir -p metrics_renders | |
| mv github-metrics.svg metrics_renders/ | |
| elif [ -f "metrics_renders/github-metrics.svg" ]; then | |
| echo "SVG already in metrics_renders/" | |
| else | |
| echo "ERROR: github-metrics.svg not found anywhere!" | |
| echo "Checking for any SVG files:" | |
| find . -name "*.svg" -type f | |
| fi | |
| # Always add and commit (even if no changes) | |
| git add metrics_renders/ || echo "No metrics_renders to add" | |
| git commit -m "chore(metrics): refresh metrics svg + org stats" || echo "No changes to commit" | |
| git push || echo "Nothing pushed" |