Skip to content

Commit 1be4374

Browse files
Merge pull request #68 from gobackup/fix/fix-code-review-stage
Enhance code review prompt and check if there is any issue to comment
2 parents 2b64997 + 84a4443 commit 1be4374

1 file changed

Lines changed: 25 additions & 28 deletions

File tree

.github/workflows/ai-review.yml

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,7 @@ jobs:
6666
DIFF_CONTENT=$(cat pr_diff.txt | jq -Rs .)
6767
6868
# Prepare the review prompt
69-
PROMPT="You are an expert code reviewer. Please review the following code changes and provide:
70-
1. A summary of the changes
71-
2. Potential issues or bugs
72-
3. Security concerns if any
73-
4. Performance considerations
74-
5. Best practices and suggestions for improvement
75-
6. Positive aspects of the code
76-
77-
Focus on Go code, Kubernetes manifests, and Helm charts.
69+
PROMPT="Act as a senior Go and Kubernetes engineer. Review the diff and point out only real bugs, security concerns, or risky changes. Keep the review concise and specific, referencing files or resources when it helps. If there is nothing actionable to flag, respond with NO_ISSUES and nothing else.
7870
7971
Code diff:
8072
$DIFF_CONTENT"
@@ -101,34 +93,39 @@ jobs:
10193
}")
10294
10395
# Extract the review content
104-
REVIEW=$(echo "$RESPONSE" | jq -r '.choices[0].message.content // "Error: Unable to get AI review"')
105-
106-
# Save review to file
107-
echo "$REVIEW" > review.txt
108-
109-
# Check for API errors
96+
REVIEW=$(echo "$RESPONSE" | jq -r '.choices[0].message.content // ""')
97+
HAS_ISSUES=false
98+
11099
if echo "$RESPONSE" | jq -e '.error' > /dev/null 2>&1; then
111100
ERROR_MSG=$(echo "$RESPONSE" | jq -r '.error.message // "Unknown error"')
112-
echo "API Error: $ERROR_MSG"
113-
echo "API Error: $ERROR_MSG" > review.txt
101+
REVIEW="AI review failed: $ERROR_MSG"
102+
HAS_ISSUES=false
103+
else
104+
TRIMMED=$(echo "$REVIEW" | tr -d '[:space:]')
105+
if [ -z "$TRIMMED" ] || [ "$TRIMMED" = "NO_ISSUES" ]; then
106+
HAS_ISSUES=false
107+
REVIEW="NO_ISSUES"
108+
else
109+
HAS_ISSUES=true
110+
fi
114111
fi
112+
113+
# Save review to file
114+
echo "$REVIEW" > review.txt
115+
echo "has_issues=$HAS_ISSUES" >> "$GITHUB_OUTPUT"
115116
116117
- name: Post AI Review Comment
117-
if: always()
118+
if: steps.ai-review.outputs.has_issues == 'true'
118119
uses: actions/github-script@v7
119120
with:
120121
github-token: ${{ secrets.GITHUB_TOKEN }}
121122
script: |
122123
const fs = require('fs');
123-
const reviewContent = fs.readFileSync('review.txt', 'utf8');
124-
125-
const comment = `## 🤖 AI Code Review
126-
127-
${reviewContent}
128-
129-
---
130-
*This review was automatically generated by AI. Please use your judgment and verify the suggestions.*`;
131-
124+
const reviewContent = fs.readFileSync('review.txt', 'utf8').trim();
125+
const marker = '<!-- ai-review -->';
126+
const header = '### AI Code Review (automated)\n\n';
127+
const comment = `${header}${reviewContent}\n\n${marker}`;
128+
132129
// Check if there's already an AI review comment
133130
const { data: comments } = await github.rest.issues.listComments({
134131
owner: context.repo.owner,
@@ -138,7 +135,7 @@ jobs:
138135
139136
const botComment = comments.find(comment =>
140137
comment.user.type === 'Bot' &&
141-
comment.body.includes('🤖 AI Code Review')
138+
comment.body.includes(marker)
142139
);
143140
144141
if (botComment) {

0 commit comments

Comments
 (0)