Collect data #17
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: Collect data | |
| on: | |
| schedule: | |
| # Shortly after midnight UTC so each snapshot lands at the start of the UTC date | |
| # it is keyed by. Not exactly 00:00 because that is GitHub's most congested slot | |
| # and scheduled runs there get delayed or dropped. | |
| - cron: '17 0 * * *' | |
| workflow_dispatch: | |
| permissions: {} | |
| jobs: | |
| collect: | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 15 | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| actions: write | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 | |
| with: | |
| persist-credentials: false | |
| - name: Collect stats | |
| run: node scripts/collect.mjs | |
| - name: Detect changes | |
| id: diff | |
| run: | | |
| if git diff --quiet docs/data; then | |
| echo "changed=false" >> "$GITHUB_OUTPUT" | |
| else | |
| echo "changed=true" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Open and merge data PR | |
| if: steps.diff.outputs.changed == 'true' | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| # Fine-grained PAT (this repo only, Pull requests: read/write). Branch | |
| # protection requires one approving review; the PR is authored by | |
| # github-actions[bot] via GITHUB_TOKEN, so an approval from this token's | |
| # owner satisfies it. The merge itself still uses GITHUB_TOKEN. | |
| APPROVE_TOKEN: ${{ secrets.DATA_PR_APPROVE_TOKEN }} | |
| REPO: ${{ github.repository }} | |
| run: | | |
| DATE=$(date -u +%Y-%m-%d) | |
| BRANCH="data/${DATE}-$(date -u +%H%M)" | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| git checkout -b "$BRANCH" | |
| git add docs/data | |
| git commit -m "Update stats data ($DATE)" | |
| git push "https://x-access-token:${GH_TOKEN}@github.com/${REPO}.git" "HEAD:refs/heads/${BRANCH}" | |
| PR_URL=$(gh pr create \ | |
| --title "Update stats data ($DATE)" \ | |
| --body "Daily data snapshot from bStats and the build-channel APIs." \ | |
| --head "$BRANCH") | |
| # PRs opened with GITHUB_TOKEN do not fire pull_request workflows, so run the | |
| # validation explicitly against the branch and wait for it before merging. | |
| gh workflow run validate.yml --ref "$BRANCH" | |
| # Filter to workflow_dispatch runs: the PR may also spawn a pull_request run | |
| # stuck in action_required (bot PRs need manual workflow approval), and the | |
| # poll must not pick that one up. | |
| sleep 15 | |
| RUN_ID="" | |
| for _ in $(seq 1 10); do | |
| RUN_ID=$(gh run list --workflow=validate.yml --branch "$BRANCH" --event workflow_dispatch --limit 1 --json databaseId --jq '.[0].databaseId // empty') | |
| [ -n "$RUN_ID" ] && break | |
| sleep 10 | |
| done | |
| if [ -z "$RUN_ID" ]; then | |
| echo "validation run never appeared" >&2 | |
| exit 1 | |
| fi | |
| gh run watch "$RUN_ID" --exit-status | |
| if [ -z "$APPROVE_TOKEN" ]; then | |
| echo "DATA_PR_APPROVE_TOKEN secret is not set; branch protection blocks the merge without an approval" >&2 | |
| exit 1 | |
| fi | |
| GH_TOKEN="$APPROVE_TOKEN" gh pr review "$PR_URL" --approve --body "Automated approval of the daily data snapshot after validation passed." | |
| gh pr merge "$PR_URL" --squash --delete-branch |