Refresh profile badges #101
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: Refresh profile badges | |
| # The org profile README badges used shields.io live endpoints (github | |
| # stars / release / downloads / contributors and docker pulls). Those call | |
| # GitHub or Docker Hub at render time through shields' shared infrastructure | |
| # and intermittently fail with "rate limited by upstream service" or | |
| # "Unable to select next github token from pool". Instead, refresh small | |
| # shields "endpoint" JSONs here once a day; the README badges read those | |
| # static files from raw.githubusercontent and never trigger a live upstream | |
| # call, so they cannot be rate-limited. If a refresh fails, the last good | |
| # values stay committed and the badges keep rendering. | |
| on: | |
| schedule: | |
| - cron: '17 6 * * *' # daily | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| jobs: | |
| refresh: | |
| runs-on: ubuntu-latest | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| REPO: oasdiff/oasdiff | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Build badge endpoint JSONs | |
| run: | | |
| set -euo pipefail | |
| mkdir -p badges | |
| # Round half-up (GitHub's display convention) so e.g. 1250 -> 1.3k, not Python's | |
| # banker's-rounding 1.2k, keeping these badges consistent with GitHub's own counts. | |
| hum() { python3 -c "from decimal import Decimal as D, ROUND_HALF_UP as H; n=$1; q=lambda d: (D(n)/d).quantize(D('0.1'), rounding=H); print(f'{q(1_000_000)}M' if n>=1_000_000 else (f'{q(1_000)}k' if n>=1_000 else str(n)))"; } | |
| emit() { printf '{"schemaVersion":1,"label":"%s","message":"%s","color":"%s"}\n' "$1" "$2" "$3" > "badges/$4"; } | |
| stars=$(gh api "repos/$REPO" -q .stargazers_count) | |
| emit "stars" "$(hum "$stars")" "blue" stars.json | |
| rel=$(gh api "repos/$REPO/releases/latest" -q .tag_name) | |
| emit "release" "$rel" "blue" release.json | |
| dl=$(gh api --paginate "repos/$REPO/releases" -q '[.[].assets[].download_count]|add//0' | awk '{s+=$1} END{print s+0}') | |
| emit "release downloads" "$(hum "$dl")" "blue" downloads.json | |
| contrib=$(gh api --paginate "repos/$REPO/contributors?per_page=100&anon=1" -q 'length' | awk '{s+=$1} END{print s+0}') | |
| emit "contributors" "$contrib" "blue" contributors.json | |
| pulls=$(curl -fsSL https://hub.docker.com/v2/repositories/tufin/oasdiff/ | python3 -c "import sys,json;print(json.load(sys.stdin)['pull_count'])") | |
| emit "docker pulls" "$(hum "$pulls")" "blue" docker-pulls.json | |
| echo "--- generated badges ---"; cat badges/*.json | |
| - name: Commit if changed | |
| run: | | |
| if [ -n "$(git status --porcelain badges/)" ]; then | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| git add badges/ | |
| git commit -m "chore: refresh profile badges" | |
| git push | |
| else | |
| echo "badges unchanged" | |
| fi |