Skip to content

Commit c6c2e6a

Browse files
fix: resolve jq escape errors in OpenCode AI PR review workflow
The previous fix had issues with backtick escaping in jq strings. Replaced inline jq string construction with printf for building the prompt, then pass the complete prompt to jq using --arg. This avoids escape character issues with backticks and newlines in the JSON construction.
1 parent 7b47837 commit c6c2e6a

3 files changed

Lines changed: 42 additions & 39 deletions

File tree

.github/workflows/opencode-pr.yml

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,22 @@ jobs:
6767
# Read the diff file
6868
DIFF_CONTENT=$(cat "$PR_DIFF")
6969
70-
# Build prompt using jq
71-
jq -n \
72-
--arg title "$PR_TITLE" \
73-
--arg desc "$PR_DESCRIPTION" \
74-
--arg previous "$PREVIOUS_COMMENTS" \
75-
--arg diff "$DIFF_CONTENT" \
76-
'{
77-
model: "kimi-latest",
78-
messages: [{
79-
role: "user",
80-
content: "You are an expert code reviewer. Review the following pull request:\n\nTitle: \($title)\n\nDescription: \($desc)\n\nPrevious Review Context:\n\($previous)\n\nCode Changes:\n\`\`\`diff\n\($diff)\n\`\`\`\n\nProvide a structured review with:\n1. Overall assessment (merge-ready, needs changes, etc.)\n2. Critical issues (if any)\n3. High-priority improvements\n4. Medium-priority suggestions\n5. Low-priority nitpicks\n6. Confidence score (0-100)\n\nFormat as markdown with clear headers."
81-
}]
82-
}' > prompt.json
70+
# Build prompt using printf and jq
71+
PROMPT=$(printf '%s\n\nTitle: %s\n\nDescription: %s\n\nPrevious Review Context:\n%s\n\nCode Changes:\n%s\n\nProvide a structured review with:\n1. Overall assessment (merge-ready, needs changes, etc.)\n2. Critical issues (if any)\n3. High-priority improvements\n4. Medium-priority suggestions\n5. Low-priority nitpicks\n6. Confidence score (0-100)\n\nFormat as markdown with clear headers.' \
72+
"You are an expert code reviewer. Review the following pull request:" \
73+
"$PR_TITLE" \
74+
"$PR_DESCRIPTION" \
75+
"$PREVIOUS_COMMENTS" \
76+
"$DIFF_CONTENT")
77+
78+
# Create JSON payload using jq
79+
jq -n --arg content "$PROMPT" '{
80+
model: "kimi-latest",
81+
messages: [{
82+
role: "user",
83+
content: $content
84+
}]
85+
}' > prompt.json
8386
8487
# Call OpenCode AI API
8588
curl -X POST https://api.opencode.ai/v1/chat/completions \

workflows/opencode/pr/opencode-pr-nix.yml

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,16 @@ jobs:
8080
nix develop --command bash -c '
8181
DIFF_CONTENT=$(cat pr_diff.txt)
8282
83-
# Build prompt using jq
84-
jq -n \
85-
--arg title "$PR_TITLE" \
86-
--arg desc "$PR_DESCRIPTION" \
87-
--arg previous "$PREVIOUS_COMMENTS" \
88-
--arg diff "$DIFF_CONTENT" \
89-
\'{
90-
model: "kimi-latest",
91-
messages: [{
92-
role: "user",
93-
content: "You are an expert code reviewer. Review the following pull request:\n\nTitle: \($title)\n\nDescription: \($desc)\n\nPrevious Review Context:\n\($previous)\n\nCode Changes:\n\`\`\`diff\n\($diff)\n\`\`\`\n\nProvide a structured review with:\n1. Overall assessment (merge-ready, needs changes, etc.)\n2. Critical issues (if any)\n3. High-priority improvements\n4. Medium-priority suggestions\n5. Low-priority nitpicks\n6. Confidence score (0-100)\n\nFormat as markdown with clear headers."
94-
}]
95-
}\' > prompt.json
83+
# Build prompt using printf and jq
84+
PROMPT=$(printf "%s\n\nTitle: %s\n\nDescription: %s\n\nPrevious Review Context:\n%s\n\nCode Changes:\n%s\n\nProvide a structured review with:\n1. Overall assessment (merge-ready, needs changes, etc.)\n2. Critical issues (if any)\n3. High-priority improvements\n4. Medium-priority suggestions\n5. Low-priority nitpicks\n6. Confidence score (0-100)\n\nFormat as markdown with clear headers." \
85+
"You are an expert code reviewer. Review the following pull request:" \
86+
"$PR_TITLE" \
87+
"$PR_DESCRIPTION" \
88+
"$PREVIOUS_COMMENTS" \
89+
"$DIFF_CONTENT")
90+
91+
# Create JSON payload using jq
92+
jq -n --arg content "$PROMPT" "{\n model: \\"kimi-latest\\",\n messages: [{\n role: \\"user\\",\n content: $content\n }]\n }" > prompt.json
9693
9794
curl -X POST https://api.opencode.ai/v1/chat/completions \
9895
-H "Authorization: Bearer $KIMI_API_KEY" \

workflows/opencode/pr/opencode-pr.yml

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,22 @@ jobs:
6767
# Read the diff file
6868
DIFF_CONTENT=$(cat "$PR_DIFF")
6969
70-
# Build prompt using jq
71-
jq -n \
72-
--arg title "$PR_TITLE" \
73-
--arg desc "$PR_DESCRIPTION" \
74-
--arg previous "$PREVIOUS_COMMENTS" \
75-
--arg diff "$DIFF_CONTENT" \
76-
'{
77-
model: "kimi-latest",
78-
messages: [{
79-
role: "user",
80-
content: "You are an expert code reviewer. Review the following pull request:\n\nTitle: \($title)\n\nDescription: \($desc)\n\nPrevious Review Context:\n\($previous)\n\nCode Changes:\n\`\`\`diff\n\($diff)\n\`\`\`\n\nProvide a structured review with:\n1. Overall assessment (merge-ready, needs changes, etc.)\n2. Critical issues (if any)\n3. High-priority improvements\n4. Medium-priority suggestions\n5. Low-priority nitpicks\n6. Confidence score (0-100)\n\nFormat as markdown with clear headers."
81-
}]
82-
}' > prompt.json
70+
# Build prompt using printf and jq
71+
PROMPT=$(printf '%s\n\nTitle: %s\n\nDescription: %s\n\nPrevious Review Context:\n%s\n\nCode Changes:\n%s\n\nProvide a structured review with:\n1. Overall assessment (merge-ready, needs changes, etc.)\n2. Critical issues (if any)\n3. High-priority improvements\n4. Medium-priority suggestions\n5. Low-priority nitpicks\n6. Confidence score (0-100)\n\nFormat as markdown with clear headers.' \
72+
"You are an expert code reviewer. Review the following pull request:" \
73+
"$PR_TITLE" \
74+
"$PR_DESCRIPTION" \
75+
"$PREVIOUS_COMMENTS" \
76+
"$DIFF_CONTENT")
77+
78+
# Create JSON payload using jq
79+
jq -n --arg content "$PROMPT" '{
80+
model: "kimi-latest",
81+
messages: [{
82+
role: "user",
83+
content: $content
84+
}]
85+
}' > prompt.json
8386
8487
# Call OpenCode AI API
8588
curl -X POST https://api.opencode.ai/v1/chat/completions \

0 commit comments

Comments
 (0)