Update Badge Stats #437
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 Badge Stats | |
| on: | |
| schedule: | |
| - cron: '0 6,14,22 * * *' | |
| workflow_dispatch: | |
| jobs: | |
| update-badges: | |
| runs-on: ubuntu-latest | |
| if: ${{ github.repository == 'pixel-agents-hq/pixel-agents' }} | |
| steps: | |
| - name: Fetch VS Code Marketplace stats | |
| id: vscode | |
| run: | | |
| RESPONSE=$(curl -s -X POST \ | |
| 'https://marketplace.visualstudio.com/_apis/public/gallery/extensionquery' \ | |
| -H 'Content-Type: application/json' \ | |
| -H 'Accept: application/json;api-version=7.2-preview.1' \ | |
| -d '{ | |
| "filters": [{ | |
| "criteria": [{"filterType": 7, "value": "pablodelucca.pixel-agents"}] | |
| }], | |
| "flags": 914 | |
| }') | |
| INSTALLS=$(echo "$RESPONSE" | jq '[.results[0].extensions[0].statistics[] | select(.statisticName == "install") | .value][0] // 0 | floor') | |
| DOWNLOADS=$(echo "$RESPONSE" | jq '[.results[0].extensions[0].statistics[] | select(.statisticName == "downloadCount") | .value][0] // 0 | floor') | |
| VERSION=$(echo "$RESPONSE" | jq -r '.results[0].extensions[0].versions[0].version // "unknown"') | |
| echo "installs=$INSTALLS" >> "$GITHUB_OUTPUT" | |
| echo "downloads=$DOWNLOADS" >> "$GITHUB_OUTPUT" | |
| echo "version=$VERSION" >> "$GITHUB_OUTPUT" | |
| - name: Fetch Open VSX stats | |
| id: openvsx | |
| run: | | |
| DOWNLOADS=$(curl -s 'https://open-vsx.org/api/pablodelucca/pixel-agents' | jq '.downloadCount // 0') | |
| echo "downloads=$DOWNLOADS" >> "$GITHUB_OUTPUT" | |
| - name: Fetch GitHub Releases downloads | |
| id: releases | |
| run: | | |
| TOTAL=$(curl -s https://api.github.com/repos/${{ github.repository }}/releases \ | |
| -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | |
| | jq '[.[].assets[].download_count] | add // 0') | |
| echo "downloads=$TOTAL" >> "$GITHUB_OUTPUT" | |
| - name: Fetch npm downloads | |
| id: npm | |
| shell: bash | |
| run: | | |
| START_DATE=$(curl --fail --silent --show-error 'https://registry.npmjs.org/pixel-agents' | jq -er '.time.created[0:10]') | |
| END_DATE=$(curl --fail --silent --show-error 'https://api.npmjs.org/downloads/point/last-day/pixel-agents' | jq -er '.end') | |
| TOTAL=0 | |
| CURSOR="$START_DATE" | |
| while [[ "$CURSOR" < "$END_DATE" || "$CURSOR" == "$END_DATE" ]]; do | |
| CHUNK_END=$(date -u -d "$CURSOR + 1 year - 1 day" +%F) | |
| if [[ "$CHUNK_END" > "$END_DATE" ]]; then | |
| CHUNK_END="$END_DATE" | |
| fi | |
| DOWNLOADS=$(curl --fail --silent --show-error \ | |
| "https://api.npmjs.org/downloads/point/$CURSOR:$CHUNK_END/pixel-agents" \ | |
| | jq -er '.downloads') | |
| TOTAL=$((TOTAL + DOWNLOADS)) | |
| CURSOR=$(date -u -d "$CHUNK_END + 1 day" +%F) | |
| done | |
| echo "downloads=$TOTAL" >> "$GITHUB_OUTPUT" | |
| - name: Format install count | |
| id: format | |
| run: | | |
| TOTAL=$(( ${{ steps.vscode.outputs.installs }} + ${{ steps.vscode.outputs.downloads }} + ${{ steps.openvsx.outputs.downloads }} + ${{ steps.releases.outputs.downloads }} )) | |
| if [ "$TOTAL" -ge 1000 ]; then | |
| MSG="$(awk "BEGIN {printf \"%.1f\", $TOTAL / 1000}")k installs" | |
| else | |
| MSG="$TOTAL installs" | |
| fi | |
| echo "message=$MSG" >> "$GITHUB_OUTPUT" | |
| - name: Format npm download count | |
| id: format_npm | |
| run: | | |
| TOTAL=${{ steps.npm.outputs.downloads }} | |
| if [ "$TOTAL" -ge 1000000 ]; then | |
| MSG="$(awk "BEGIN {printf \"%.1f\", $TOTAL / 1000000}")M downloads" | |
| elif [ "$TOTAL" -ge 1000 ]; then | |
| MSG="$(awk "BEGIN {printf \"%.0f\", $TOTAL / 1000}")k downloads" | |
| else | |
| MSG="$TOTAL downloads" | |
| fi | |
| echo "message=$MSG" >> "$GITHUB_OUTPUT" | |
| - name: Update version badge in gist | |
| uses: schneegans/dynamic-badges-action@v1.8.0 | |
| with: | |
| auth: ${{ secrets.GIST_SECRET }} | |
| gistID: ${{ secrets.GIST_ID }} | |
| filename: version.json | |
| label: version | |
| message: v${{ steps.vscode.outputs.version }} | |
| color: '0183ff' | |
| namedLogo: visualstudiocode | |
| logoColor: white | |
| - name: Update installs badge in gist | |
| uses: schneegans/dynamic-badges-action@v1.8.0 | |
| with: | |
| auth: ${{ secrets.GIST_SECRET }} | |
| gistID: ${{ secrets.GIST_ID }} | |
| filename: installs.json | |
| label: marketplaces | |
| message: ${{ steps.format.outputs.message }} | |
| color: '0183ff' | |
| - name: Update npm downloads badge in gist | |
| uses: schneegans/dynamic-badges-action@v1.8.0 | |
| with: | |
| auth: ${{ secrets.GIST_SECRET }} | |
| gistID: ${{ secrets.GIST_ID }} | |
| filename: npm-downloads.json | |
| label: npm | |
| message: ${{ steps.format_npm.outputs.message }} | |
| color: 'cb3837' | |
| namedLogo: npm | |
| logoColor: white |