|
1 | | -name: Badgetizr PR Badges |
| 1 | +name: Badgetizr (gh replacement) |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | pull_request: |
5 | | - types: |
6 | | - - opened |
7 | | - - reopened |
8 | | - - synchronize |
9 | | - - edited |
10 | | - - closed |
| 5 | + types: [opened, edited, synchronize, reopened, closed] |
| 6 | + |
| 7 | +env: |
| 8 | + REPO: ${{ github.repository }} |
11 | 9 |
|
12 | 10 | concurrency: |
13 | 11 | group: badgetizr-${{ github.event.pull_request.number }} |
14 | 12 | cancel-in-progress: true |
15 | 13 |
|
16 | 14 | jobs: |
17 | | - badgetizr-start: |
18 | | - # Skip Dependabot PRs |
19 | | - if: ${{ !startsWith(github.head_ref, 'dependabot/') }} |
| 15 | + update-pr-badges: |
20 | 16 | runs-on: ubuntu-latest |
21 | | - |
22 | 17 | steps: |
23 | 18 | - name: Checkout |
24 | 19 | uses: actions/checkout@v4 |
25 | 20 |
|
26 | | - - name: Badgetizr - Start |
27 | | - uses: aiKrice/homebrew-badgetizr@3.0.2 |
28 | | - with: |
29 | | - pr_id: ${{ github.event.pull_request.number }} |
30 | | - configuration: .badgetizr.yml |
31 | | - pr_destination_branch: ${{ github.event.pull_request.base.ref }} |
32 | | - pr_build_number: ${{ github.run_id }} |
33 | | - pr_build_url: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
34 | | - ci_status: "started" |
35 | | - ci_text: "Running CI" |
| 21 | + - name: Install gh (if missing) |
| 22 | + run: | |
| 23 | + if ! command -v gh >/dev/null; then |
| 24 | + sudo apt-get update |
| 25 | + sudo apt-get install -y gh |
| 26 | + fi |
| 27 | +
|
| 28 | + - name: Authenticate gh with GH_PAT |
36 | 29 | env: |
37 | | - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 30 | + GH_PAT: ${{ secrets.GH_PAT }} |
| 31 | + run: | |
| 32 | + echo "$GH_PAT" | gh auth login --with-token |
| 33 | + gh auth status |
38 | 34 |
|
39 | | - # Wait for other CI jobs to complete |
40 | | - badgetizr-result: |
41 | | - needs: [badgetizr-start] |
42 | | - runs-on: ubuntu-latest |
43 | | - if: ${{ always() && !startsWith(github.head_ref, 'dependabot/') }} |
| 35 | + - name: Generate and Update Badges |
| 36 | + env: |
| 37 | + PR_NUM: ${{ github.event.pull_request.number }} |
| 38 | + RUN_URL: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 39 | + GH_EVENT_NAME: ${{ github.event_name }} |
| 40 | + run: | |
| 41 | + set -x # Debug mode |
44 | 42 |
|
45 | | - steps: |
46 | | - - name: Checkout |
47 | | - uses: actions/checkout@v4 |
| 43 | + # 1. Fetch PR Data |
| 44 | + echo "Fetching PR data..." |
| 45 | + # Simplified fields to avoid 'Unknown field' errors |
| 46 | + PR_JSON=$(gh pr view "$PR_NUM" --json title,body,state,labels) |
48 | 47 |
|
49 | | - - name: Badgetizr - Success |
50 | | - if: ${{ success() }} |
51 | | - uses: aiKrice/homebrew-badgetizr@3.0.2 |
52 | | - with: |
53 | | - pr_id: ${{ github.event.pull_request.number }} |
54 | | - configuration: .badgetizr.yml |
55 | | - pr_destination_branch: ${{ github.event.pull_request.base.ref }} |
56 | | - pr_build_number: ${{ github.run_id }} |
57 | | - pr_build_url: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
58 | | - ci_status: "passed" |
59 | | - env: |
60 | | - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
61 | | - |
62 | | - - name: Badgetizr - Failed |
63 | | - if: ${{ failure() }} |
64 | | - uses: aiKrice/homebrew-badgetizr@3.0.2 |
65 | | - with: |
66 | | - pr_id: ${{ github.event.pull_request.number }} |
67 | | - configuration: .badgetizr.yml |
68 | | - pr_destination_branch: ${{ github.event.pull_request.base.ref }} |
69 | | - pr_build_number: ${{ github.run_id }} |
70 | | - pr_build_url: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
71 | | - ci_status: "failed" |
72 | | - env: |
73 | | - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 48 | + TITLE=$(echo "$PR_JSON" | jq -r .title) |
| 49 | + BODY=$(echo "$PR_JSON" | jq -r .body) |
| 50 | + # Safely extract label names |
| 51 | + LABELS=$(echo "$PR_JSON" | jq -r '.labels[].name' 2>/dev/null || echo "") |
| 52 | + STATE=$(echo "$PR_JSON" | jq -r .state) |
| 53 | +
|
| 54 | + # 2. Determine CI Status |
| 55 | + CI_STATUS="Running" |
| 56 | + CI_COLOR="yellow" |
| 57 | + # In a real setup, you might query other workflow runs here. |
| 58 | + # For now, we point to this run. |
| 59 | +
|
| 60 | + # Badges Construction |
| 61 | + BADGES="" |
| 62 | +
|
| 63 | + # Badge: CI |
| 64 | + BADGES="${BADGES} [](${RUN_URL})" |
| 65 | +
|
| 66 | + # Badge: WIP |
| 67 | + # Check title or labels |
| 68 | + if echo "$TITLE" | grep -qiE "WIP|Work In Progress" || echo "$LABELS" | grep -q "work in progress"; then |
| 69 | + BADGES="${BADGES} []()" |
| 70 | + fi |
| 71 | +
|
| 72 | + # Badge: Type |
| 73 | + if echo "$TITLE" | grep -qiE "^(feat|feat\(.*\)):"; then |
| 74 | + BADGES="${BADGES} []()" |
| 75 | + elif echo "$TITLE" | grep -qiE "^(fix|fix\(.*\)):"; then |
| 76 | + BADGES="${BADGES} []()" |
| 77 | + elif echo "$TITLE" | grep -qiE "^(docs|chore\(docs\)):"; then |
| 78 | + BADGES="${BADGES} []()" |
| 79 | + elif echo "$TITLE" | grep -qiE "^(test|test\(.*\)):"; then |
| 80 | + BADGES="${BADGES} []()" |
| 81 | + elif echo "$TITLE" | grep -qiE "^(sec|security):"; then |
| 82 | + BADGES="${BADGES} []()" |
| 83 | + fi |
| 84 | +
|
| 85 | + # Badge: Breaking |
| 86 | + if echo "$TITLE" | grep -q "!:"; then |
| 87 | + BADGES="${BADGES} []()" |
| 88 | + fi |
| 89 | +
|
| 90 | + echo "Generated Badges: $BADGES" |
| 91 | +
|
| 92 | + # 3. Update PR Body |
| 93 | + START_MARKER="<!-- badgetizr-start -->" |
| 94 | + END_MARKER="<!-- badgetizr-end -->" |
| 95 | + BADGE_BLOCK="${START_MARKER}\n${BADGES}\n${END_MARKER}" |
| 96 | +
|
| 97 | + # Check if body is empty or null, handle gracefully |
| 98 | + if [ "$BODY" == "null" ]; then BODY=""; fi |
| 99 | +
|
| 100 | + echo "$BODY" > pr_body.txt |
| 101 | +
|
| 102 | + if grep -q "$START_MARKER" pr_body.txt; then |
| 103 | + # Remove old block |
| 104 | + sed -i "/$START_MARKER/,/$END_MARKER/d" pr_body.txt |
| 105 | + # Prepend new block (and ensure newline) |
| 106 | + NEW_BODY="${BADGE_BLOCK}\n\n$(cat pr_body.txt)" |
| 107 | + else |
| 108 | + # Prepend to top |
| 109 | + NEW_BODY="${BADGE_BLOCK}\n\n$(cat pr_body.txt)" |
| 110 | + fi |
| 111 | +
|
| 112 | + # Update via gh |
| 113 | + # Use file input for body to avoid shell escaping issues |
| 114 | + echo -e "$NEW_BODY" > new_body.txt |
| 115 | + gh pr edit "$PR_NUM" --body-file new_body.txt |
| 116 | + echo "PR Updated." |
0 commit comments