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