Repulse (GitHub Pulse Analytics) #419
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: Repulse (GitHub Pulse Analytics) | |
| on: | |
| schedule: | |
| - cron: '45 22 * * *' # runs daily at midnight UTC | |
| workflow_dispatch: # manual trigger | |
| permissions: | |
| contents: write | |
| actions: read | |
| jobs: | |
| repulse_analytics: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v3 | |
| - name: Install GitHub CLI | |
| run: sudo apt-get update && sudo apt-get install -y gh | |
| - name: Authenticate GitHub CLI | |
| run: gh auth setup-git | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Fetch and Download Previous Database Artifact via GitHub CLI | |
| run: | | |
| mkdir -p data | |
| echo "Searching for the latest available github_traffic_db artifact..." | |
| RUN_IDS=$(gh api repos/${{ github.repository }}/actions/runs --jq '[.workflow_runs | map(select(.conclusion=="success")) | .[].id]') | |
| if [[ -z "$RUN_IDS" || "$RUN_IDS" == "null" ]]; then | |
| echo "No previous successful workflow runs found. Attempting to download default backup..." | |
| else | |
| echo "Found successful workflow runs: $RUN_IDS" | |
| for RUN_ID in $(echo "$RUN_IDS" | jq -r '.[]'); do | |
| echo "Checking run ID: $RUN_ID for github_traffic_db artifact..." | |
| ARTIFACT_ID=$(gh api repos/${{ github.repository }}/actions/runs/$RUN_ID/artifacts --jq '.artifacts | map(select(.name=="github_traffic_db")) | first | .id') | |
| if [[ -n "$ARTIFACT_ID" && "$ARTIFACT_ID" != "null" ]]; then | |
| echo "Found artifact ID: $ARTIFACT_ID in run ID: $RUN_ID" | |
| gh api -H "Accept: application/vnd.github.v3+json" \ | |
| "repos/${{ github.repository }}/actions/artifacts/$ARTIFACT_ID/zip" > db_artifact.zip | |
| unzip db_artifact.zip -d data/ | |
| rm db_artifact.zip | |
| echo "Successfully restored previous database from run ID: $RUN_ID" | |
| exit 0 | |
| fi | |
| done | |
| echo "No previous github_traffic_db artifact found in past runs. Attempting to download default backup..." | |
| fi | |
| gh release download test --repo "${{ github.repository }}" --pattern "github_traffic.db" --dir data | |
| if [[ -f "data/github_traffic.db" ]]; then | |
| FILE_TYPE=$(file data/github_traffic.db) | |
| if [[ ! "$FILE_TYPE" =~ "SQLite" ]]; then | |
| echo "Downloaded file is not a valid SQLite database. Deleting and starting fresh." | |
| rm data/github_traffic.db | |
| else | |
| echo "Successfully downloaded default database from release." | |
| exit 0 | |
| fi | |
| else | |
| echo "Failed to download default database. Creating a new one." | |
| fi | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| continue-on-error: true | |
| - name: Set up Python | |
| uses: actions/setup-python@v4 | |
| with: | |
| python-version: '3.9' | |
| - name: Install Python dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install -r requirements.txt || pip install matplotlib pandas dotenv scipy lxml | |
| - name: Set up Go '1.23.5' | |
| uses: actions/setup-go@v4 | |
| with: | |
| go-version: '1.23.5' | |
| - name: Install Go dependencies | |
| run: go mod tidy | |
| - name: Load environment variables | |
| run: | | |
| echo "GH_TOKEN=${{ secrets.GH_TOKEN }}" >> $GITHUB_ENV | |
| echo "OWNER=${{ secrets.OWNER }}" >> $GITHUB_ENV | |
| echo "REPO=${{ secrets.REPO }}" >> $GITHUB_ENV | |
| echo "DARK_MODE=true" >> $GITHUB_ENV | |
| - name: Ensure Graphs Directory Exists | |
| run: | | |
| mkdir -p assets/graphs | |
| - name: Verify SQLite Database File | |
| run: | | |
| if [[ -f "data/github_traffic.db" ]]; then | |
| file data/github_traffic.db | |
| sqlite3 data/github_traffic.db "PRAGMA integrity_check;" | |
| else | |
| echo "Database file does not exist. Proceeding with a new database." | |
| fi | |
| - name: Run Repulse Analytics | |
| run: go run cmd/repulse/main.go | |
| - name: Generate Traffic Badges | |
| run: | | |
| npm install --prefix analytics | |
| node analytics/generate_badges.js | |
| - name: Upload database artifact | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: github_traffic_db | |
| path: data/github_traffic.db | |
| retention-days: 7 | |
| - name: Commit and Push Graphs to Orphan Branch | |
| run: | | |
| git config --global user.name "github-actions[bot]" | |
| git config --global user.email "github-actions[bot]@users.noreply.github.com" | |
| cp assets/qp-logo-query-packages.svg /tmp/qp-logo-query-packages.svg | |
| git fetch origin gh-pages || true | |
| git checkout --orphan gh-pages || git checkout gh-pages | |
| git rm -rf . || true | |
| mkdir -p assets/graphs assets/badges | |
| cp -r assets/graphs/* . | |
| cp -r assets/badges/* . | |
| cp /tmp/qp-logo-query-packages.svg . | |
| git add *.svg | |
| git commit -m "Update traffic graphs" | |
| # squash every 5 commits to keep history clean | |
| commit_count=$(git rev-list --count HEAD) | |
| if [ "$commit_count" -gt 5 ]; then | |
| git reset $(git rev-list --max-parents=0 HEAD) # keep only the latest commit | |
| git commit --amend -m "Squashed traffic graph updates" | |
| fi | |
| git push -f origin gh-pages |