Skip to content

Commit 9374fe7

Browse files
committed
Fix shell quoting issues in aicode-preprocess workflow
- Use multiline format (EOF) for JSON output to avoid shell interpretation - Replace direct JSON string interpolation with jq for proper escaping - Add robust JSON parsing with fallback logic - Fix Slack notification payload construction using jq - Prevents errors when task text contains quotes or special characters
1 parent b58e685 commit 9374fe7

1 file changed

Lines changed: 62 additions & 11 deletions

File tree

.github/workflows/aicode-preprocess.yml

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ jobs:
5959
JSON_LINE=$(echo "$OUTPUT" | grep "ENHANCED_TASK_JSON:" || true)
6060
if [ -n "$JSON_LINE" ]; then
6161
JSON_DATA=$(echo "$JSON_LINE" | sed 's/ENHANCED_TASK_JSON://')
62-
echo "task_json=$JSON_DATA" >> $GITHUB_OUTPUT
62+
# Use multiline format to avoid shell quoting issues with JSON
63+
echo "task_json<<EOF" >> $GITHUB_OUTPUT
64+
echo "$JSON_DATA" >> $GITHUB_OUTPUT
65+
echo "EOF" >> $GITHUB_OUTPUT
6366
6467
# Extract Confluence URL
6568
CONFLUENCE_URL=$(echo "$JSON_DATA" | jq -r '.confluenceUrl // "N/A"')
@@ -72,17 +75,35 @@ jobs:
7275
if: always()
7376
run: |
7477
# Read enhanced task from JSON output to avoid shell quoting issues
75-
TASK_JSON="${{ steps.preprocess.outputs.task_json }}"
78+
# Use jq to safely parse the JSON string from GitHub Actions output
79+
TASK_JSON_RAW="${{ steps.preprocess.outputs.task_json }}"
7680
77-
if [ -n "$TASK_JSON" ] && [ "$TASK_JSON" != "null" ]; then
78-
# Extract values from JSON using jq (handles all escaping automatically)
79-
ENHANCED_TASK=$(echo "$TASK_JSON" | jq -r '.enhanced // empty')
80-
ORIGINAL_TASK=$(echo "$TASK_JSON" | jq -r '.original // empty')
81-
CONFLUENCE_URL=$(echo "$TASK_JSON" | jq -r '.confluenceUrl // "N/A"')
81+
if [ -n "$TASK_JSON_RAW" ] && [ "$TASK_JSON_RAW" != "null" ] && [ "$TASK_JSON_RAW" != "" ]; then
82+
# Try to parse as JSON - if it's already a JSON string, parse it first
83+
if echo "$TASK_JSON_RAW" | jq -e . >/dev/null 2>&1; then
84+
# It's valid JSON, extract values
85+
ENHANCED_TASK=$(echo "$TASK_JSON_RAW" | jq -r '.enhanced // empty')
86+
ORIGINAL_TASK=$(echo "$TASK_JSON_RAW" | jq -r '.original // empty')
87+
CONFLUENCE_URL=$(echo "$TASK_JSON_RAW" | jq -r '.confluenceUrl // "N/A"')
88+
else
89+
# Might be a JSON string that needs to be parsed (double-encoded)
90+
# Try to unescape and parse
91+
TASK_JSON_PARSED=$(echo "$TASK_JSON_RAW" | jq -Rr '.')
92+
if echo "$TASK_JSON_PARSED" | jq -e . >/dev/null 2>&1; then
93+
ENHANCED_TASK=$(echo "$TASK_JSON_PARSED" | jq -r '.enhanced // empty')
94+
ORIGINAL_TASK=$(echo "$TASK_JSON_PARSED" | jq -r '.original // empty')
95+
CONFLUENCE_URL=$(echo "$TASK_JSON_PARSED" | jq -r '.confluenceUrl // "N/A"')
96+
else
97+
# Fallback to outputs if JSON parsing fails
98+
ENHANCED_TASK="${{ steps.preprocess.outputs.enhanced_task }}"
99+
ORIGINAL_TASK="${{ github.event.client_payload.task }}"
100+
CONFLUENCE_URL="${{ steps.preprocess.outputs.confluence_url }}"
101+
fi
102+
fi
82103
else
83104
# Fallback to outputs if JSON not available
84-
ENHANCED_TASK="${{ steps.preprocess.outputs.enhanced_task }}"
85-
ORIGINAL_TASK="${{ github.event.client_payload.task }}"
105+
ENHANCED_TASK="${{ steps.preprocess.outputs.enhanced_task }}"
106+
ORIGINAL_TASK="${{ github.event.client_payload.task }}"
86107
CONFLUENCE_URL="${{ steps.preprocess.outputs.confluence_url }}"
87108
fi
88109
@@ -125,13 +146,43 @@ jobs:
125146
if: always()
126147
run: |
127148
if [ "${{ steps.preprocess.outcome }}" == "success" ]; then
149+
# Use jq to properly construct JSON payload with proper escaping
150+
ORIGINAL_TASK="${{ github.event.client_payload.task }}"
151+
ENHANCED_TASK="${{ steps.preprocess.outputs.enhanced_task }}"
152+
153+
PAYLOAD=$(jq -n \
154+
--arg text "📝 Task preprocessed and enhanced. Triggering code generation..." \
155+
--arg original "$ORIGINAL_TASK" \
156+
--arg enhanced "$ENHANCED_TASK" \
157+
'{
158+
text: $text,
159+
blocks: [{
160+
type: "section",
161+
text: {
162+
type: "mrkdwn",
163+
text: ("📝 *Task Preprocessing Complete*\n\n*Original:* " + $original + "\n\n*Enhanced:* " + $enhanced)
164+
}
165+
}]
166+
}')
167+
128168
curl -X POST "${{ secrets.SLACK_WEBHOOK_URL }}" \
129169
-H "Content-Type: application/json" \
130-
-d "{\"text\":\"📝 Task preprocessed and enhanced. Triggering code generation...\",\"blocks\":[{\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"📝 *Task Preprocessing Complete*\\n\\n*Original:* ${{ github.event.client_payload.task }}\\n\\n*Enhanced:* ${{ steps.preprocess.outputs.enhanced_task }}\"}}]}"
170+
-d "$PAYLOAD"
131171
else
172+
PAYLOAD=$(jq -n '{
173+
text: "⚠️ Preprocessing failed, using original task",
174+
blocks: [{
175+
type: "section",
176+
text: {
177+
type: "mrkdwn",
178+
text: "⚠️ *Preprocessing Warning*\n\nPreprocessing failed, but code generation will continue with original task."
179+
}
180+
}]
181+
}')
182+
132183
curl -X POST "${{ secrets.SLACK_WEBHOOK_URL }}" \
133184
-H "Content-Type: application/json" \
134-
-d "{\"text\":\"⚠️ Preprocessing failed, using original task\",\"blocks\":[{\"type\":\"section\",\"text\":{\"type\":\"mrkdwn\",\"text\":\"⚠️ *Preprocessing Warning*\\n\\nPreprocessing failed, but code generation will continue with original task.\"}}]}"
185+
-d "$PAYLOAD"
135186
fi
136187
env:
137188
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

0 commit comments

Comments
 (0)