Skip to content

Commit 59495ed

Browse files
authored
feat: update shared-check-pr-label-and-branch (#153)
1 parent e8ed341 commit 59495ed

File tree

2 files changed

+153
-50
lines changed

2 files changed

+153
-50
lines changed

.github/workflows/shared-check-pr-label-and-branch.yml

Lines changed: 147 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ on:
1212
required: true
1313
type: string
1414
description: "Target label to check for in the PR"
15-
branch_prefix:
15+
branch_prefixes:
1616
required: false
1717
type: string
18-
description: "Required prefix for the branch name"
19-
default: ""
18+
description: "JSON array of allowed prefixes for the branch name"
19+
default: "[]"
20+
branch_names:
21+
required: false
22+
type: string
23+
description: "JSON array of allowed exact branch names"
24+
default: "[]"
2025
outputs:
2126
is_valid:
2227
value: ${{ jobs.check-pr-label-and-branch.outputs.is_valid }}
@@ -30,69 +35,167 @@ jobs:
3035
name: check-pr-label-and-branch
3136
runs-on: ubuntu-latest
3237
outputs:
33-
is_valid: ${{ steps.check.outputs.is_valid }}
34-
has_label: ${{ steps.check.outputs.has_label }}
35-
is_valid_branch: ${{ steps.check.outputs.is_valid_branch }}
38+
is_valid: ${{ steps.combine-results.outputs.is_valid }}
39+
has_label: ${{ steps.check-label.outputs.has_label }}
40+
is_valid_branch: ${{ steps.combine-results.outputs.is_valid_branch }}
3641
steps:
37-
- name: Check for label and branch prefix
42+
- name: check-label
3843
env:
3944
LABELS_JSON: ${{ inputs.labels }}
40-
id: check
45+
id: check-label
4146
run: |
4247
TARGET_LABEL="${{ inputs.target_label }}"
43-
BRANCH_PREFIX="${{ inputs.branch_prefix }}"
48+
echo "Checking for label: $TARGET_LABEL"
49+
echo "Input labels: $LABELS_JSON"
50+
51+
# Use jq to check if the label exists in the labels array
52+
HAS_LABEL=$(echo "$LABELS_JSON" | \
53+
jq --arg target "$TARGET_LABEL" \
54+
'[.[] | select(.name == $target)] | length > 0')
55+
56+
echo "Has label: $HAS_LABEL"
57+
echo "has_label=$HAS_LABEL" >> $GITHUB_OUTPUT
58+
59+
if [ "$HAS_LABEL" = "true" ]; then
60+
echo "✅ Label '$TARGET_LABEL' found"
61+
else
62+
echo "❌ Label '$TARGET_LABEL' not found"
63+
fi
64+
65+
- name: check-branch-exact
66+
id: check-branch-exact
67+
run: |
68+
# Escape JSON properly for bash
69+
BRANCH_NAMES='${{ inputs.branch_names }}'
4470
CURRENT_BRANCH="${{ github.head_ref }}"
4571
46-
echo "Checking for label: $TARGET_LABEL"
47-
echo "Required branch prefix: $BRANCH_PREFIX"
72+
echo "Checking exact branch names: $BRANCH_NAMES"
4873
echo "Current branch: $CURRENT_BRANCH"
49-
echo "Input labels: $LABELS_JSON"
5074
51-
# Check if branch has the required prefix
52-
if [ -n "$BRANCH_PREFIX" ]; then
53-
if [[ "$CURRENT_BRANCH" == "$BRANCH_PREFIX"* ]]; then
75+
# Check exact branch names
76+
if [ "$BRANCH_NAMES" != "[]" ]; then
77+
EXACT_MATCH=$(echo "$BRANCH_NAMES" | \
78+
jq --arg branch "$CURRENT_BRANCH" \
79+
'[.[] | select(. == $branch)] | length > 0')
80+
81+
echo "Has exact match: $EXACT_MATCH"
82+
echo "has_exact_match=$EXACT_MATCH" >> $GITHUB_OUTPUT
83+
84+
if [ "$EXACT_MATCH" = "true" ]; then
85+
echo "✅ Branch name matches exactly"
86+
else
87+
echo "❌ Branch name does not match any exact names"
88+
fi
89+
else
90+
echo "ℹ️ No exact branch name restrictions"
91+
echo "has_exact_match=false" >> $GITHUB_OUTPUT
92+
fi
93+
94+
- name: check-branch-prefixes
95+
id: check-branch-prefixes
96+
run: |
97+
# Escape JSON properly for bash
98+
BRANCH_PREFIXES='${{ inputs.branch_prefixes }}'
99+
CURRENT_BRANCH="${{ github.head_ref }}"
100+
101+
echo "Checking branch prefixes: $BRANCH_PREFIXES"
102+
echo "Current branch: $CURRENT_BRANCH"
103+
104+
# Check if current branch starts with any of the allowed prefixes
105+
if [ "$BRANCH_PREFIXES" != "[]" ]; then
106+
PREFIX_MATCH=false
107+
# Loop through each prefix and check
108+
while IFS= read -r prefix; do
109+
if [[ "$CURRENT_BRANCH" == "$prefix"* ]]; then
110+
PREFIX_MATCH=true
111+
echo "✅ Branch starts with prefix: $prefix"
112+
break
113+
fi
114+
done < <(echo "$BRANCH_PREFIXES" | jq -r '.[]')
115+
116+
echo "Has prefix match: $PREFIX_MATCH"
117+
echo "has_prefix_match=$PREFIX_MATCH" >> $GITHUB_OUTPUT
118+
119+
if [ "$PREFIX_MATCH" = "true" ]; then
54120
echo "✅ Branch prefix matches"
55-
HAS_BRANCH_PREFIX=true
56121
else
57-
echo "❌ Branch prefix does not match"
58-
HAS_BRANCH_PREFIX=false
122+
echo "❌ Branch does not start with any allowed prefixes"
59123
fi
60124
else
61-
echo "ℹ️ No branch prefix requirement"
62-
HAS_BRANCH_PREFIX=true
125+
echo "ℹ️ No branch prefix restrictions"
126+
echo "has_prefix_match=false" >> $GITHUB_OUTPUT
63127
fi
64128
65-
# Use jq to check if the label exists in the labels array
66-
HAS_LABEL=$(echo "$LABELS_JSON" | \
67-
jq --arg target "$TARGET_LABEL" \
68-
'[.[] | select(.name == $target)] | length > 0')
129+
- name: combine-results
130+
id: combine-results
131+
run: |
132+
HAS_LABEL="${{ steps.check-label.outputs.has_label }}"
133+
HAS_EXACT_MATCH="${{ steps.check-branch-exact.outputs.has_exact_match }}"
134+
HAS_PREFIX_MATCH="${{ steps.check-branch-prefixes.outputs.has_prefix_match }}"
135+
# Escape JSON properly for bash
136+
BRANCH_NAMES='${{ inputs.branch_names }}'
137+
BRANCH_PREFIXES='${{ inputs.branch_prefixes }}'
69138
70-
echo "Has label: $HAS_LABEL"
139+
echo "Combining results:"
140+
echo "- Has label: $HAS_LABEL"
141+
echo "- Has exact match: $HAS_EXACT_MATCH"
142+
echo "- Has prefix match: $HAS_PREFIX_MATCH"
71143
72-
# Output individual condition results
73-
echo "has_label=$HAS_LABEL" >> $GITHUB_OUTPUT
74-
echo "is_valid_branch=$HAS_BRANCH_PREFIX" >> $GITHUB_OUTPUT
144+
# Determine if branch is valid
145+
IS_VALID_BRANCH=false
75146
76-
# Both conditions must be true
77-
if [ "$HAS_LABEL" = "true" ] && [ "$HAS_BRANCH_PREFIX" = "true" ]; then
78-
echo "is_valid=true" >> $GITHUB_OUTPUT
79-
echo "✅ Both label '$TARGET_LABEL' and branch prefix '$BRANCH_PREFIX' found"
147+
# Valid if exact match exists
148+
if [ "$HAS_EXACT_MATCH" = "true" ]; then
149+
IS_VALID_BRANCH=true
150+
echo "Branch valid due to exact name match"
151+
# Or if prefix match exists
152+
elif [ "$HAS_PREFIX_MATCH" = "true" ]; then
153+
IS_VALID_BRANCH=true
154+
echo "Branch valid due to prefix match"
155+
# Or if no restrictions specified
156+
elif [ "$BRANCH_NAMES" = "[]" ] && [ "$BRANCH_PREFIXES" = "[]" ]; then
157+
IS_VALID_BRANCH=true
158+
echo "Branch valid (no restrictions specified)"
80159
else
81-
echo "is_valid=false" >> $GITHUB_OUTPUT
160+
echo "Branch not valid (no match found)"
161+
fi
162+
163+
echo "is_valid_branch=$IS_VALID_BRANCH" >> $GITHUB_OUTPUT
164+
165+
# Final validation requires both label and valid branch
166+
if [ "$HAS_LABEL" = "true" ] && [ "$IS_VALID_BRANCH" = "true" ]; then
167+
IS_VALID=true
168+
echo "✅ Both label and branch validation passed"
169+
else
170+
IS_VALID=false
171+
echo "❌ Validation failed"
82172
if [ "$HAS_LABEL" != "true" ]; then
83-
echo "❌ Label '$TARGET_LABEL' not found"
84-
else
85-
echo "✅ Label '$TARGET_LABEL' found"
173+
echo " - Label validation failed"
86174
fi
87-
if [ "$HAS_BRANCH_PREFIX" != "true" ]; then
88-
echo "❌ Branch prefix '$BRANCH_PREFIX' not found"
89-
else
90-
echo "✅ Branch prefix '$BRANCH_PREFIX' found"
175+
if [ "$IS_VALID_BRANCH" != "true" ]; then
176+
echo " - Branch validation failed"
91177
fi
92178
fi
93179
94-
- name: Display Validation Result
180+
echo "is_valid=$IS_VALID" >> $GITHUB_OUTPUT
181+
182+
- name: display-results
95183
run: |
96-
echo "Validation result: ${{ steps.check.outputs.is_valid }}"
97-
echo "Has label: ${{ steps.check.outputs.has_label }}"
98-
echo "Is valid branch: ${{ steps.check.outputs.is_valid_branch }}"
184+
echo "=========================================="
185+
echo " VALIDATION SUMMARY"
186+
echo "=========================================="
187+
echo ""
188+
echo "Label Check:"
189+
echo " - Target label: ${{ inputs.target_label }}"
190+
echo " - Has label: ${{ steps.check-label.outputs.has_label }}"
191+
echo ""
192+
echo "Branch Validation:"
193+
echo " - Current branch: ${{ github.head_ref }}"
194+
echo " - Exact match: ${{ steps.check-branch-exact.outputs.has_exact_match }}"
195+
echo " - Prefix match: ${{ steps.check-branch-prefixes.outputs.has_prefix_match }}"
196+
echo " - Valid branch: ${{ steps.combine-results.outputs.is_valid_branch }}"
197+
echo ""
198+
echo "Final Result:"
199+
echo " - Overall valid: ${{ steps.combine-results.outputs.is_valid }}"
200+
echo ""
201+
echo "=========================================="

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)