diff --git a/.github/workflows/refresh-traffic.yml b/.github/workflows/refresh-traffic.yml index 7838699..7d6e72f 100644 --- a/.github/workflows/refresh-traffic.yml +++ b/.github/workflows/refresh-traffic.yml @@ -16,6 +16,7 @@ jobs: runs-on: ubuntu-latest permissions: contents: write + pull-requests: write steps: - uses: actions/checkout@v4 @@ -107,9 +108,35 @@ jobs: New-Item -ItemType Directory -Force -Path $dir | Out-Null $snap | ConvertTo-Json -Depth 10 | Set-Content -Encoding UTF8 (Join-Path $dir "_traffic.json") - - name: Commit and push + - name: Create branch, commit, push, and open PR + shell: bash + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | + set -euo pipefail + + if git diff --quiet data/_traffic.json; then + echo "No changes - skipping." + exit 0 + fi + + BRANCH="chore/traffic-$(date -u +%Y%m%d-%H%M%S)" git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" + git checkout -b "$BRANCH" git add data/_traffic.json - git diff --cached --quiet || (git commit -m "chore: refresh traffic snapshot $(date -u +%Y-%m-%d)" && git push) + git commit -m "chore: refresh traffic snapshot $(date -u +%Y-%m-%d)" + git push origin "$BRANCH" + + EXISTING_PR=$(gh pr list --state open --search "refresh traffic snapshot in:title" --json number 2>/dev/null | python3 -c "import json,sys; d=json.load(sys.stdin); print(d[0][\"number\"] if d else \"\")" || true) + if [[ -n "$EXISTING_PR" ]]; then + gh pr close "$EXISTING_PR" --comment "Superseded by new refresh." || true + fi + + gh pr create \ + --title "chore: refresh traffic snapshot $(date -u +%Y-%m-%d)" \ + --body "Auto-generated by refresh-traffic workflow. Updates data/_traffic.json with latest GitHub Traffic and NuGet download stats." \ + --base main \ + --head "$BRANCH" + + echo "PR opened from branch $BRANCH"