Repository navigation
ci(badge): sum the Gallery's per-version downloads into a README badge - #49
Conversation
The Gallery's per-package total is broken (PowerShell/PowerShellGallery #347 and #350): 0 for this module, stuck or wrong for others, and it is what the page and the shields.io powershellgallery/dt badge read. The per-version counts still move. A daily workflow reads every version from the v2 feed, sums VersionDownloadCount and publishes a shields.io endpoint file on the badges branch when the number changes; the README badge reads that file. Other module repositories can call the workflow with their package name.
fadwen
left a comment
There was a problem hiding this comment.
Notes on the lines whose reason the diff does not show.
| label = $env:LABEL | ||
| message = $total.ToString('N0', [cultureinfo]::InvariantCulture) | ||
| color = 'blue' | ||
| cacheSeconds = 3600 |
There was a problem hiding this comment.
An hour of shields.io caching, because the number changes once a day at most and the raw.githubusercontent.com URL behind it is itself cached for a few minutes; without this shields re-fetches on every README view.
| run: | | ||
| set -euo pipefail | ||
| mkdir badges && cd badges | ||
| git init -q -b badges |
There was a problem hiding this comment.
A fresh repository in a scratch folder rather than a checkout of main: the badges branch is an orphan with no history in common with main, and this keeps the badge commits out of the module's history and the module's files out of the badge branch. The fetch below picks up the existing branch when there is one, so the first run creates it and every later run appends.
| if git fetch -q origin badges 2>/dev/null; then | ||
| git reset -q --hard origin/badges | ||
| fi | ||
| if [ -f downloads.json ] && cmp -s downloads.json ../downloads.json; then |
There was a problem hiding this comment.
Byte comparison of the JSON rather than of the number, so a change to the label or colour also publishes, and a run that moved nothing leaves the branch alone. Over a quiet month that is the difference between no commits and thirty.
Summary
The PowerShell Gallery's per-package download total is broken and has been for months: PowerShell/PowerShellGallery#347 (version counts not updating, May 2026) and #350 (total disagrees with the version history, July 2026, "working on fixing" as of 14 October). For this module the v2 feed returns
DownloadCount = 0on all 10 versions, which is what the Gallery page and the shields.iopowershellgallery/dtbadge read, so that badge says "downloads: 0" in red. The per-versionVersionDownloadCountvalues are live and sum to 82.This adds a daily workflow that does the sum and publishes it as a shields.io endpoint badge, and a README badge that reads it.
Changes
.github/workflows/gallery-downloads.yml: on a daily schedule, on manual dispatch, or when called from another repository, reads every page ofFindPackagesById()for the package (default: the repository name), sumsVersionDownloadCount, writesdownloads.jsonin the shields endpoint schema, and commits it to an orphanbadgesbranch only when the number changed.GITHUB_TOKENwithcontents: writeis enough; no gist, no extra secret.README.md: a "Gallery downloads" badge next to the version badge, throughimg.shields.io/endpointpointing at the rawdownloads.jsonon thebadgesbranch.CHANGELOG.md: Added entry under Unreleased.Verification
DownloadCount0.badgesbranch come after merge; until then the badge renders as "invalid" or "resource not found".Notes
After merge:
gh workflow run gallery-downloads.ymlonce to create thebadgesbranch, then daily at 06:17 UTC.Another module's repository gets the same badge with this caller, replacing the package name and adding the matching README badge URL for its own owner and name:
When the Gallery fixes its total the badge keeps working; nothing to undo. If the per-version counts stall again (#347), the badge stalls with them: it is never more wrong than the Gallery's own version history.