Skip to content

Commit daae5bd

Browse files
committed
fix: fix shared workflow bud
1 parent 6e74709 commit daae5bd

File tree

2 files changed

+230
-0
lines changed

2 files changed

+230
-0
lines changed

test.sh

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/bin/bash
2+
3+
TARGET_LABEL="deploy"
4+
BRANCH_PREFIX=""
5+
CURRENT_BRANCH="release/1/dev"
6+
7+
echo "Checking for label: $TARGET_LABEL"
8+
echo "Required branch prefix: $BRANCH_PREFIX"
9+
echo "Current branch: $CURRENT_BRANCH"
10+
11+
# Create a temporary file with the labels JSON
12+
cat > /tmp/labels.json << 'EOF'
13+
[
14+
{
15+
"color": "d73a4a",
16+
"default": true,
17+
"description": "Something isn't working",
18+
"id": 9778996043,
19+
"name": "bug",
20+
"node_id": "LA_kwDOQje8Gc8AAAACRt-jSw",
21+
"url": "https://api.github.com/repos/wislertt/zerv-flow-demo-test/labels/bug"
22+
},
23+
{
24+
"color": "0075ca",
25+
"default": true,
26+
"description": "Improvements or additions to documentation",
27+
"id": 9778996045,
28+
"name": "documentation",
29+
"node_id": "LA_kwDOQje8Gc8AAAACRt-jTQ",
30+
"url": "https://api.github.com/repos/wislertt/zerv-flow-demo-test/labels/documentation"
31+
},
32+
{
33+
"color": "c5def5",
34+
"default": false,
35+
"description": "",
36+
"id": 9801717827,
37+
"name": "deploy-d",
38+
"node_id": "LA_kwDOQje8Gc8AAAACSDpYQw",
39+
"url": "https://api.github.com/repos/wislertt/zerv-flow-demo-test/labels/deploy-d"
40+
},
41+
{
42+
"color": "c5def5",
43+
"default": false,
44+
"description": "",
45+
"id": 9801718458,
46+
"name": "deploy-n",
47+
"node_id": "LA_kwDOQje8Gc8AAAACSDpaug",
48+
"url": "https://api.github.com/repos/wislertt/zerv-flow-demo-test/labels/deploy-n"
49+
},
50+
{
51+
"color": "c5def5",
52+
"default": false,
53+
"description": "",
54+
"id": 9804113951,
55+
"name": "deploy",
56+
"node_id": "LA_kwDOQje8Gc8AAAACSF7oHw",
57+
"url": "https://api.github.com/repos/wislertt/zerv-flow-demo-test/labels/deploy"
58+
}
59+
]
60+
EOF
61+
62+
# Check if branch has the required prefix
63+
if [ -n "$BRANCH_PREFIX" ]; then
64+
if [[ "$CURRENT_BRANCH" == "$BRANCH_PREFIX"* ]]; then
65+
echo "✅ Branch prefix matches"
66+
HAS_BRANCH_PREFIX=true
67+
else
68+
echo "❌ Branch prefix does not match"
69+
HAS_BRANCH_PREFIX=false
70+
fi
71+
else
72+
echo "ℹ️ No branch prefix requirement"
73+
HAS_BRANCH_PREFIX=true
74+
fi
75+
76+
# Use jq to check if the target label exists
77+
if jq -e --arg target "$TARGET_LABEL" '.[] | select(.name == $target)' /tmp/labels.json > /dev/null 2>&1; then
78+
HAS_LABEL=true
79+
else
80+
HAS_LABEL=false
81+
fi
82+
83+
# Clean up the temporary file
84+
rm -f /tmp/labels.json
85+
86+
echo "Has label: $HAS_LABEL"
87+
88+
# Output individual condition results
89+
echo "has_label=$HAS_LABEL"
90+
echo "is_valid_branch=$HAS_BRANCH_PREFIX"
91+
92+
# Both conditions must be true
93+
if [ "$HAS_LABEL" = "true" ] && [ "$HAS_BRANCH_PREFIX" = "true" ]; then
94+
echo "is_valid=true"
95+
echo "✅ Both label '$TARGET_LABEL' and branch prefix '$BRANCH_PREFIX' found"
96+
else
97+
echo "is_valid=false"
98+
if [ "$HAS_LABEL" != "true" ]; then
99+
echo "❌ Label '$TARGET_LABEL' not found"
100+
else
101+
echo "✅ Label '$TARGET_LABEL' found"
102+
fi
103+
if [ "$HAS_BRANCH_PREFIX" != "true" ]; then
104+
echo "❌ Branch prefix '$BRANCH_PREFIX' not found"
105+
else
106+
echo "✅ Branch prefix '$BRANCH_PREFIX' found"
107+
fi
108+
fi

test_workflow.sh

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/bin/bash
2+
3+
# Dummy inputs simulating GitHub Actions
4+
TARGET_LABEL="deploy"
5+
BRANCH_PREFIX=""
6+
CURRENT_BRANCH="release/1/dev"
7+
8+
# This is the dummy value for ${{ inputs.labels }}
9+
# In GitHub Actions, this comes from the workflow inputs
10+
# Here we're simulating it with a variable
11+
INPUT_LABELS='[
12+
{
13+
"color": "d73a4a",
14+
"default": true,
15+
"description": "Something isn'\''t working",
16+
"id": 9778996043,
17+
"name": "bug",
18+
"node_id": "LA_kwDOQje8Gc8AAAACRt-jSw",
19+
"url": "https://api.github.com/repos/wislertt/zerv-flow-demo-test/labels/bug"
20+
},
21+
{
22+
"color": "0075ca",
23+
"default": true,
24+
"description": "Improvements or additions to documentation",
25+
"id": 9778996045,
26+
"name": "documentation",
27+
"node_id": "LA_kwDOQje8Gc8AAAACRt-jTQ",
28+
"url": "https://api.github.com/repos/wislertt/zerv-flow-demo-test/labels/documentation"
29+
},
30+
{
31+
"color": "c5def5",
32+
"default": false,
33+
"description": "",
34+
"id": 9801717827,
35+
"name": "deploy-d",
36+
"node_id": "LA_kwDOQje8Gc8AAAACSDpYQw",
37+
"url": "https://api.github.com/repos/wislertt/zerv-flow-demo-test/labels/deploy-d"
38+
},
39+
{
40+
"color": "c5def5",
41+
"default": false,
42+
"description": "",
43+
"id": 9801718458,
44+
"name": "deploy-n",
45+
"node_id": "LA_kwDOQje8Gc8AAAACSDpaug",
46+
"url": "https://api.github.com/repos/wislertt/zerv-flow-demo-test/labels/deploy-n"
47+
},
48+
{
49+
"color": "c5def5",
50+
"default": false,
51+
"description": "",
52+
"id": 9804113951,
53+
"name": "deploy",
54+
"node_id": "LA_kwDOQje8Gc8AAAACSF7oHw",
55+
"url": "https://api.github.com/repos/wislertt/zerv-flow-demo-test/labels/deploy"
56+
}
57+
]'
58+
59+
echo "Checking for label: $TARGET_LABEL"
60+
echo "Required branch prefix: $BRANCH_PREFIX"
61+
echo "Current branch: $CURRENT_BRANCH"
62+
echo "Input labels: $INPUT_LABELS"
63+
64+
# Check if branch has the required prefix
65+
if [ -n "$BRANCH_PREFIX" ]; then
66+
if [[ "$CURRENT_BRANCH" == "$BRANCH_PREFIX"* ]]; then
67+
echo "✅ Branch prefix matches"
68+
HAS_BRANCH_PREFIX=true
69+
else
70+
echo "❌ Branch prefix does not match"
71+
HAS_BRANCH_PREFIX=false
72+
fi
73+
else
74+
echo "ℹ️ No branch prefix requirement"
75+
HAS_BRANCH_PREFIX=true
76+
fi
77+
78+
# Simple approach: pass JSON to jq via stdin to avoid shell parsing
79+
# This is the key fix for handling JSON with special characters
80+
# Using printf with the INPUT_LABELS variable (simulating ${{ inputs.labels }})
81+
if printf '%s\n' "$INPUT_LABELS" | \
82+
jq -e --arg target "$TARGET_LABEL" \
83+
'.[] | select(.name == $target)' > /dev/null 2>&1; then
84+
HAS_LABEL=true
85+
else
86+
HAS_LABEL=false
87+
fi
88+
89+
echo "Has label: $HAS_LABEL"
90+
91+
# Create a dummy GITHUB_OUTPUT file for demonstration
92+
GITHUB_OUTPUT="/tmp/github_output.txt"
93+
94+
# Output individual condition results
95+
echo "has_label=$HAS_LABEL" >> "$GITHUB_OUTPUT"
96+
echo "is_valid_branch=$HAS_BRANCH_PREFIX" >> "$GITHUB_OUTPUT"
97+
98+
# Both conditions must be true
99+
if [ "$HAS_LABEL" = "true" ] && [ "$HAS_BRANCH_PREFIX" = "true" ]; then
100+
echo "is_valid=true" >> "$GITHUB_OUTPUT"
101+
echo "✅ Both label '$TARGET_LABEL' and branch prefix '$BRANCH_PREFIX' found"
102+
else
103+
echo "is_valid=false" >> "$GITHUB_OUTPUT"
104+
if [ "$HAS_LABEL" != "true" ]; then
105+
echo "❌ Label '$TARGET_LABEL' not found"
106+
else
107+
echo "✅ Label '$TARGET_LABEL' found"
108+
fi
109+
if [ "$HAS_BRANCH_PREFIX" != "true" ]; then
110+
echo "❌ Branch prefix '$BRANCH_PREFIX' not found"
111+
else
112+
echo "✅ Branch prefix '$BRANCH_PREFIX' found"
113+
fi
114+
fi
115+
116+
# No cleanup needed since we're using a variable now
117+
118+
# Display the outputs (simulating GitHub Actions outputs)
119+
echo ""
120+
echo "=== GitHub Outputs ==="
121+
cat "$GITHUB_OUTPUT"
122+
rm -f "$GITHUB_OUTPUT"

0 commit comments

Comments
 (0)