diff --git a/.github/scripts/render-readme-downloads-badge.mjs b/.github/scripts/render-readme-downloads-badge.mjs new file mode 100644 index 000000000..efe0d670c --- /dev/null +++ b/.github/scripts/render-readme-downloads-badge.mjs @@ -0,0 +1,112 @@ +import { mkdir, writeFile } from 'node:fs/promises' +import { dirname } from 'node:path' + +const repository = process.env.GITHUB_REPOSITORY ?? 'stablyai/orca' +const token = process.env.GITHUB_TOKEN +const outputPath = process.env.DOWNLOADS_BADGE_PATH ?? 'docs/assets/readme-downloads.svg' + +const headers = { + Accept: 'application/vnd.github+json', + 'User-Agent': 'orca-readme-downloads-badge', + 'X-GitHub-Api-Version': '2022-11-28' +} + +if (token) { + headers.Authorization = `Bearer ${token}` +} + +async function fetchJson(url) { + const response = await fetch(url, { headers }) + if (!response.ok) { + throw new Error(`GitHub API request failed: ${response.status} ${response.statusText}`) + } + + return response.json() +} + +async function getTotalReleaseDownloads() { + let page = 1 + let total = 0 + + while (true) { + const releases = await fetchJson( + `https://api.github.com/repos/${repository}/releases?per_page=100&page=${page}` + ) + + if (releases.length === 0) { + return total + } + + for (const release of releases) { + if (release.draft) { + continue + } + + for (const asset of release.assets ?? []) { + total += asset.download_count ?? 0 + } + } + + page += 1 + } +} + +function formatDownloads(total) { + if (total < 1000) { + return String(total) + } + + if (total < 1_000_000) { + return `${Math.round(total / 1000)}k` + } + + const rounded = total / 1_000_000 + return `${rounded >= 10 ? Math.round(rounded) : rounded.toFixed(1)}m` +} + +function textWidth(label) { + return Math.ceil(label.length * 7.1 + 10) +} + +function escapeXml(value) { + return value.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>') +} + +function renderBadge(value) { + const label = 'downloads' + const leftWidth = textWidth(label) + const rightWidth = textWidth(value) + const width = leftWidth + rightWidth + const labelX = leftWidth / 2 + const valueX = leftWidth + rightWidth / 2 + + return ` +` +} + +const total = await getTotalReleaseDownloads() +const badge = renderBadge(formatDownloads(total)) + +await mkdir(dirname(outputPath), { recursive: true }) +await writeFile(outputPath, badge) +console.log(`Rendered ${outputPath} from ${total} downloads.`) diff --git a/.github/workflows/readme-downloads-badge.yml b/.github/workflows/readme-downloads-badge.yml new file mode 100644 index 000000000..c97b1bb75 --- /dev/null +++ b/.github/workflows/readme-downloads-badge.yml @@ -0,0 +1,48 @@ +name: README Downloads Badge + +on: + workflow_dispatch: + schedule: + - cron: '17 */6 * * *' + release: + types: + - published + - edited + - deleted + +permissions: + contents: write + +concurrency: + group: readme-downloads-badge + cancel-in-progress: false + +jobs: + update: + # Why: this workflow commits to main, so forks should not create divergent badge commits. + if: github.repository == 'stablyai/orca' + runs-on: ubuntu-latest + steps: + - name: Checkout main + uses: actions/checkout@v6 + with: + ref: main + + - name: Render downloads badge + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: node .github/scripts/render-readme-downloads-badge.mjs + + - name: Commit badge update + run: | + set -euo pipefail + if git diff --quiet -- docs/assets/readme-downloads.svg; then + echo "Downloads badge is already current." + exit 0 + fi + + git config user.name "github-actions[bot]" + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" + git add docs/assets/readme-downloads.svg + git commit -m "Update README downloads badge" + git push origin main diff --git a/README.md b/README.md index 0415570b8..31b9c5817 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@
-
+
diff --git a/docs/assets/readme-downloads.svg b/docs/assets/readme-downloads.svg
new file mode 100644
index 000000000..d23726970
--- /dev/null
+++ b/docs/assets/readme-downloads.svg
@@ -0,0 +1,21 @@
+
diff --git a/docs/readme/README.es.md b/docs/readme/README.es.md
index a92cfbea7..2a707c891 100644
--- a/docs/readme/README.es.md
+++ b/docs/readme/README.es.md
@@ -4,7 +4,7 @@
-
+
diff --git a/docs/readme/README.ja.md b/docs/readme/README.ja.md
index 0273366ed..a602c3fed 100644
--- a/docs/readme/README.ja.md
+++ b/docs/readme/README.ja.md
@@ -4,7 +4,7 @@
-
+
diff --git a/docs/readme/README.ko.md b/docs/readme/README.ko.md
index 812813e1d..3ef640623 100644
--- a/docs/readme/README.ko.md
+++ b/docs/readme/README.ko.md
@@ -4,7 +4,7 @@
-
+
diff --git a/docs/readme/README.zh-CN.md b/docs/readme/README.zh-CN.md
index 496475b20..6839197a3 100644
--- a/docs/readme/README.zh-CN.md
+++ b/docs/readme/README.zh-CN.md
@@ -4,7 +4,7 @@