Skip to content

Commit 7916ef0

Browse files
Thomo1318Thomo1318
authored andcommitted
feat(ci): add test type badge support
1 parent 7a0f7bb commit 7916ef0

File tree

4 files changed

+289
-61
lines changed

4 files changed

+289
-61
lines changed

.badgetizr.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,33 @@ badge_dynamic:
3535
settings:
3636
patterns:
3737
- badge_docs:
38-
sed_pattern: '(docs:|chore\(docs\):)'
38+
sed_pattern: '(.*(docs:|chore\(docs\):).*)'
3939
label: "Type"
4040
value: "Documentation"
4141
color: "blue"
4242
- badge_feat:
43-
sed_pattern: '(feat:|feat\(.*\):)'
43+
sed_pattern: '(.*(feat:|feat\(.*\):).*)'
4444
label: "Type"
4545
value: "Feature"
4646
color: "green"
4747
- badge_fix:
48-
sed_pattern: '(fix:|fix\(.*\):)'
48+
sed_pattern: '(.*(fix:|fix\(.*\):).*)'
4949
label: "Type"
5050
value: "Bugfix"
5151
color: "orange"
5252
- badge_security:
53-
sed_pattern: "(security:|sec:)"
53+
sed_pattern: "(.*(security:|sec:).*)"
5454
label: "Type"
5555
value: "Security"
5656
color: "red"
5757
- badge_breaking:
58-
sed_pattern: "(BREAKING CHANGE|!:)"
58+
sed_pattern: "(.*(BREAKING CHANGE|!:).*)"
5959
label: "⚠️"
6060
value: "Breaking"
6161
color: "red"
6262

6363
badge_ready_for_approval:
64-
enabled: "true"
64+
enabled: "false"
6565
settings:
6666
color: "darkgreen"
6767
label: "Ready"

.github/workflows/badgetizr.yml

Lines changed: 98 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,116 @@
1-
name: Badgetizr PR Badges
1+
name: Badgetizr (gh replacement)
22

33
on:
44
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 }}
119

1210
concurrency:
1311
group: badgetizr-${{ github.event.pull_request.number }}
1412
cancel-in-progress: true
1513

1614
jobs:
17-
badgetizr-start:
18-
# Skip Dependabot PRs
19-
if: ${{ !startsWith(github.head_ref, 'dependabot/') }}
15+
update-pr-badges:
2016
runs-on: ubuntu-latest
21-
2217
steps:
2318
- name: Checkout
2419
uses: actions/checkout@v4
2520

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
3629
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
3834
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
4442
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)
4847
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} [![CI](https://img.shields.io/badge/CI-${CI_STATUS}-${CI_COLOR}?style=flat-square&logo=githubactions)](${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} [![WIP](https://img.shields.io/badge/Status-WIP-orange?style=flat-square&logo=vlcmediaplayer)]()"
70+
fi
71+
72+
# Badge: Type
73+
if echo "$TITLE" | grep -qiE "^(feat|feat\(.*\)):"; then
74+
BADGES="${BADGES} [![Type](https://img.shields.io/badge/Type-Feature-green?style=flat-square)]()"
75+
elif echo "$TITLE" | grep -qiE "^(fix|fix\(.*\)):"; then
76+
BADGES="${BADGES} [![Type](https://img.shields.io/badge/Type-Bugfix-orange?style=flat-square)]()"
77+
elif echo "$TITLE" | grep -qiE "^(docs|chore\(docs\)):"; then
78+
BADGES="${BADGES} [![Type](https://img.shields.io/badge/Type-Documentation-blue?style=flat-square)]()"
79+
elif echo "$TITLE" | grep -qiE "^(test|test\(.*\)):"; then
80+
BADGES="${BADGES} [![Type](https://img.shields.io/badge/Type-Test-yellowgreen?style=flat-square)]()"
81+
elif echo "$TITLE" | grep -qiE "^(sec|security):"; then
82+
BADGES="${BADGES} [![Type](https://img.shields.io/badge/Type-Security-red?style=flat-square)]()"
83+
fi
84+
85+
# Badge: Breaking
86+
if echo "$TITLE" | grep -q "!:"; then
87+
BADGES="${BADGES} [![Breaking](https://img.shields.io/badge/⚠️-Breaking-red?style=flat-square)]()"
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

Comments
 (0)