Skip to content

Commit d618649

Browse files
committed
feat: update shared-check-pr-label-and-branch
1 parent e8ed341 commit d618649

File tree

2 files changed

+144
-50
lines changed

2 files changed

+144
-50
lines changed

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

Lines changed: 138 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,158 @@ 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+
BRANCH_NAMES="${{ inputs.branch_names }}"
4469
CURRENT_BRANCH="${{ github.head_ref }}"
4570
46-
echo "Checking for label: $TARGET_LABEL"
47-
echo "Required branch prefix: $BRANCH_PREFIX"
71+
echo "Checking exact branch names: $BRANCH_NAMES"
4872
echo "Current branch: $CURRENT_BRANCH"
49-
echo "Input labels: $LABELS_JSON"
5073
51-
# Check if branch has the required prefix
52-
if [ -n "$BRANCH_PREFIX" ]; then
53-
if [[ "$CURRENT_BRANCH" == "$BRANCH_PREFIX"* ]]; then
74+
# Check exact branch names
75+
if [ "$BRANCH_NAMES" != "[]" ]; then
76+
EXACT_MATCH=$(echo "$BRANCH_NAMES" | \
77+
jq --arg branch "$CURRENT_BRANCH" \
78+
'[.[] | select(. == $branch)] | length > 0')
79+
80+
echo "Has exact match: $EXACT_MATCH"
81+
echo "has_exact_match=$EXACT_MATCH" >> $GITHUB_OUTPUT
82+
83+
if [ "$EXACT_MATCH" = "true" ]; then
84+
echo "✅ Branch name matches exactly"
85+
else
86+
echo "❌ Branch name does not match any exact names"
87+
fi
88+
else
89+
echo "ℹ️ No exact branch name restrictions"
90+
echo "has_exact_match=false" >> $GITHUB_OUTPUT
91+
fi
92+
93+
- name: check-branch-prefixes
94+
id: check-branch-prefixes
95+
run: |
96+
BRANCH_PREFIXES="${{ inputs.branch_prefixes }}"
97+
CURRENT_BRANCH="${{ github.head_ref }}"
98+
99+
echo "Checking branch prefixes: $BRANCH_PREFIXES"
100+
echo "Current branch: $CURRENT_BRANCH"
101+
102+
# Check if current branch starts with any of the allowed prefixes
103+
if [ "$BRANCH_PREFIXES" != "[]" ]; then
104+
PREFIX_MATCH=$(echo "$BRANCH_PREFIXES" | \
105+
jq --arg branch "$CURRENT_BRANCH" \
106+
'[.[] | select($branch | startswith(.))] | length > 0')
107+
108+
echo "Has prefix match: $PREFIX_MATCH"
109+
echo "has_prefix_match=$PREFIX_MATCH" >> $GITHUB_OUTPUT
110+
111+
if [ "$PREFIX_MATCH" = "true" ]; then
54112
echo "✅ Branch prefix matches"
55-
HAS_BRANCH_PREFIX=true
56113
else
57-
echo "❌ Branch prefix does not match"
58-
HAS_BRANCH_PREFIX=false
114+
echo "❌ Branch does not start with any allowed prefixes"
59115
fi
60116
else
61-
echo "ℹ️ No branch prefix requirement"
62-
HAS_BRANCH_PREFIX=true
117+
echo "ℹ️ No branch prefix restrictions"
118+
echo "has_prefix_match=false" >> $GITHUB_OUTPUT
63119
fi
64120
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')
121+
- name: combine-results
122+
id: combine-results
123+
run: |
124+
HAS_LABEL="${{ steps.check-label.outputs.has_label }}"
125+
HAS_EXACT_MATCH="${{ steps.check-branch-exact.outputs.has_exact_match }}"
126+
HAS_PREFIX_MATCH="${{ steps.check-branch-prefixes.outputs.has_prefix_match }}"
127+
BRANCH_NAMES="${{ inputs.branch_names }}"
128+
BRANCH_PREFIXES="${{ inputs.branch_prefixes }}"
69129
70-
echo "Has label: $HAS_LABEL"
130+
echo "Combining results:"
131+
echo "- Has label: $HAS_LABEL"
132+
echo "- Has exact match: $HAS_EXACT_MATCH"
133+
echo "- Has prefix match: $HAS_PREFIX_MATCH"
71134
72-
# Output individual condition results
73-
echo "has_label=$HAS_LABEL" >> $GITHUB_OUTPUT
74-
echo "is_valid_branch=$HAS_BRANCH_PREFIX" >> $GITHUB_OUTPUT
135+
# Determine if branch is valid
136+
IS_VALID_BRANCH=false
75137
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"
138+
# Valid if exact match exists
139+
if [ "$HAS_EXACT_MATCH" = "true" ]; then
140+
IS_VALID_BRANCH=true
141+
echo "Branch valid due to exact name match"
142+
# Or if prefix match exists
143+
elif [ "$HAS_PREFIX_MATCH" = "true" ]; then
144+
IS_VALID_BRANCH=true
145+
echo "Branch valid due to prefix match"
146+
# Or if no restrictions specified
147+
elif [ "$BRANCH_NAMES" = "[]" ] && [ "$BRANCH_PREFIXES" = "[]" ]; then
148+
IS_VALID_BRANCH=true
149+
echo "Branch valid (no restrictions specified)"
80150
else
81-
echo "is_valid=false" >> $GITHUB_OUTPUT
151+
echo "Branch not valid (no match found)"
152+
fi
153+
154+
echo "is_valid_branch=$IS_VALID_BRANCH" >> $GITHUB_OUTPUT
155+
156+
# Final validation requires both label and valid branch
157+
if [ "$HAS_LABEL" = "true" ] && [ "$IS_VALID_BRANCH" = "true" ]; then
158+
IS_VALID=true
159+
echo "✅ Both label and branch validation passed"
160+
else
161+
IS_VALID=false
162+
echo "❌ Validation failed"
82163
if [ "$HAS_LABEL" != "true" ]; then
83-
echo "❌ Label '$TARGET_LABEL' not found"
84-
else
85-
echo "✅ Label '$TARGET_LABEL' found"
164+
echo " - Label validation failed"
86165
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"
166+
if [ "$IS_VALID_BRANCH" != "true" ]; then
167+
echo " - Branch validation failed"
91168
fi
92169
fi
93170
94-
- name: Display Validation Result
171+
echo "is_valid=$IS_VALID" >> $GITHUB_OUTPUT
172+
173+
- name: display-results
95174
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 }}"
175+
echo "=========================================="
176+
echo " VALIDATION SUMMARY"
177+
echo "=========================================="
178+
echo ""
179+
echo "Label Check:"
180+
echo " - Target label: ${{ inputs.target_label }}"
181+
echo " - Has label: ${{ steps.check-label.outputs.has_label }}"
182+
echo ""
183+
echo "Branch Validation:"
184+
echo " - Current branch: ${{ github.head_ref }}"
185+
echo " - Exact match: ${{ steps.check-branch-exact.outputs.has_exact_match }}"
186+
echo " - Prefix match: ${{ steps.check-branch-prefixes.outputs.has_prefix_match }}"
187+
echo " - Valid branch: ${{ steps.combine-results.outputs.is_valid_branch }}"
188+
echo ""
189+
echo "Final Result:"
190+
echo " - Overall valid: ${{ steps.combine-results.outputs.is_valid }}"
191+
echo ""
192+
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)