|
| 1 | +# Code Review Output Format |
| 2 | + |
| 3 | +Comment formatting, summary template, and posting mechanics for automated code reviews. |
| 4 | + |
| 5 | +----- |
| 6 | + |
| 7 | +## Inline Comments (New Issues Only) |
| 8 | + |
| 9 | +For each new issue, post an inline comment: |
| 10 | + |
| 11 | +```bash |
| 12 | +gh api "repos/${GITHUB_REPOSITORY}/pulls/${PR_NUMBER}/comments" \ |
| 13 | + -X POST \ |
| 14 | + -f body="🚨 **Security: SQL Injection** |
| 15 | +
|
| 16 | +This query uses string concatenation with user input, allowing SQL injection attacks. |
| 17 | +
|
| 18 | +**Suggested fix:** |
| 19 | +\`\`\`python |
| 20 | +query = \"SELECT * FROM users WHERE id = ?\" |
| 21 | +cursor.execute(query, (user_id,)) |
| 22 | +\`\`\`" \ |
| 23 | + -f commit_id="$GITHUB_SHA" \ |
| 24 | + -f path="src/db/users.py" \ |
| 25 | + -F line=45 |
| 26 | +``` |
| 27 | + |
| 28 | +**Comment Format:** |
| 29 | + |
| 30 | +~~~ |
| 31 | +[ICON] **[Category]: [Brief Title]** |
| 32 | +
|
| 33 | +[1-2 sentence explanation of impact/risk] |
| 34 | +
|
| 35 | +**Suggested fix:** |
| 36 | +```[language] |
| 37 | +[concrete code example] |
| 38 | +``` |
| 39 | +~~~ |
| 40 | + |
| 41 | +**Volume Limit:** Maximum 10 inline comments per run. Prioritize: Critical > Important > Suggestion. |
| 42 | + |
| 43 | +----- |
| 44 | + |
| 45 | +## Summary Comment (Always Required) |
| 46 | + |
| 47 | +**This summary must ALWAYS be posted or updated, even when no issues are found.** |
| 48 | + |
| 49 | +> **IMPORTANT**: The summary body MUST be multi-line GitHub-flavored Markdown. |
| 50 | +> Never flatten it into a single line or use pipe-delimited text. |
| 51 | +> Always use the exact structure shown below — headers, tables, bullet lists, and footer. |
| 52 | +
|
| 53 | +**Step 1 — Find existing summary comment:** |
| 54 | + |
| 55 | +```bash |
| 56 | +SUMMARY_ID=$(gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" \ |
| 57 | + --jq '.[] | select(.body | contains("<!-- CLAUDE_CODE_REVIEW -->")) | .id' | head -1) |
| 58 | +``` |
| 59 | + |
| 60 | +**Step 2 — Write the summary body to a temporary file:** |
| 61 | + |
| 62 | +You MUST write the summary to a temp file to preserve Markdown formatting. Set shell |
| 63 | +variables for your review data first, then write the file using `cat` with a heredoc. |
| 64 | +The file content must follow this exact structure — do not omit or reorder sections: |
| 65 | + |
| 66 | +```bash |
| 67 | +# Set these variables based on your review findings: |
| 68 | +SHORT_SHA=$(echo "$GITHUB_SHA" | head -c 7) |
| 69 | +REVIEW_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ") |
| 70 | +# STATUS: one of "✅ Approved", "⚠️ Comments", "🚨 Changes Requested" |
| 71 | +# CRITICAL_COUNT, IMPORTANT_COUNT, SUGGESTION_COUNT: integer counts |
| 72 | +# SUMMARY_TEXT: 1-3 sentence prose summary of the review |
| 73 | +# COMPLIANCE_LINES: multi-line string of "- ✅/❌ ..." items derived from AGENTS.md |
| 74 | +# (use the actual rules you read from AGENTS.md — do NOT use generic placeholders) |
| 75 | + |
| 76 | +cat > /tmp/review-summary.md << ENDOFSUMMARY |
| 77 | +<!-- CLAUDE_CODE_REVIEW --> |
| 78 | +## 🔍 Automated Code Review |
| 79 | +
|
| 80 | +| | | |
| 81 | +|---|---| |
| 82 | +| **Commit** | \`${SHORT_SHA}\` | |
| 83 | +| **Reviewed** | ${REVIEW_DATE} | |
| 84 | +| **Status** | ${STATUS} | |
| 85 | +
|
| 86 | +### Findings |
| 87 | +
|
| 88 | +| Severity | Count | |
| 89 | +|----------|-------| |
| 90 | +| 🚨 Critical | ${CRITICAL_COUNT} | |
| 91 | +| ⚠️ Important | ${IMPORTANT_COUNT} | |
| 92 | +| 💡 Suggestion | ${SUGGESTION_COUNT} | |
| 93 | +
|
| 94 | +### AGENTS.md Compliance |
| 95 | +
|
| 96 | +${COMPLIANCE_LINES} |
| 97 | +
|
| 98 | +### Summary |
| 99 | +
|
| 100 | +${SUMMARY_TEXT} |
| 101 | +
|
| 102 | +--- |
| 103 | +<sub>🤖 Automated review by Claude Code • [View CI Run](${CI_RUN_URL})</sub> |
| 104 | +ENDOFSUMMARY |
| 105 | +``` |
| 106 | + |
| 107 | +**Step 3 — Post or update the comment using the file:** |
| 108 | + |
| 109 | +Build a JSON payload from the file (this avoids shell escaping issues with `$()` subshells): |
| 110 | + |
| 111 | +```bash |
| 112 | +jq -n --rawfile body /tmp/review-summary.md '{"body": $body}' > /tmp/review-payload.json |
| 113 | + |
| 114 | +if [ -n "$SUMMARY_ID" ]; then |
| 115 | + gh api "repos/${GITHUB_REPOSITORY}/issues/comments/${SUMMARY_ID}" \ |
| 116 | + -X PATCH \ |
| 117 | + --input /tmp/review-payload.json |
| 118 | +else |
| 119 | + gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" \ |
| 120 | + -X POST \ |
| 121 | + --input /tmp/review-payload.json |
| 122 | +fi |
| 123 | +``` |
| 124 | + |
| 125 | +----- |
| 126 | + |
| 127 | +## Review Decision |
| 128 | + |
| 129 | +```bash |
| 130 | +CRITICAL_COUNT=0 # Set based on your findings |
| 131 | +IMPORTANT_COUNT=0 |
| 132 | + |
| 133 | +if [ "$CRITICAL_COUNT" -gt 0 ]; then |
| 134 | + gh pr review "$PR_NUMBER" --request-changes \ |
| 135 | + --body "🚨 Found ${CRITICAL_COUNT} critical issue(s) requiring changes. See inline comments for details." |
| 136 | +elif [ "$IMPORTANT_COUNT" -gt 0 ]; then |
| 137 | + gh pr review "$PR_NUMBER" --comment \ |
| 138 | + --body "⚠️ Found ${IMPORTANT_COUNT} issue(s) to consider. See inline comments." |
| 139 | +else |
| 140 | + gh pr review "$PR_NUMBER" --approve \ |
| 141 | + --body "✅ Code review passed. No issues found." |
| 142 | +fi |
| 143 | +``` |
| 144 | + |
| 145 | +----- |
| 146 | + |
| 147 | +## Summary Comment Formatting Requirements |
| 148 | + |
| 149 | +> **CRITICAL — Summary Comment Formatting** |
| 150 | +> |
| 151 | +> The summary comment posted to the PR **MUST** use full GitHub-flavored Markdown |
| 152 | +> with headers (`##`, `###`), tables (`| | |`), bullet lists, and the |
| 153 | +> `<!-- CLAUDE_CODE_REVIEW -->` marker. **Never** output a single-line or |
| 154 | +> pipe-delimited summary. |
| 155 | +> Every summary comment must contain all of these elements: |
| 156 | +> - The `<!-- CLAUDE_CODE_REVIEW -->` HTML comment marker (first line) |
| 157 | +> - A `## 🔍 Automated Code Review` heading |
| 158 | +> - A metadata table with Commit, Reviewed, and Status rows |
| 159 | +> - A `### Findings` section with a severity table or "No issues found." |
| 160 | +> - A `### AGENTS.md Compliance` section with checklist items **derived from the actual AGENTS.md rules** (not generic placeholders) |
| 161 | +> - A `### Summary` section with prose |
| 162 | +> - A footer with the CI run link |
| 163 | +
|
| 164 | +----- |
| 165 | + |
| 166 | +## Final Checklist (verify before finishing) |
| 167 | + |
| 168 | +Before completing the review, confirm: |
| 169 | + |
| 170 | +- [ ] Summary comment uses `<!-- CLAUDE_CODE_REVIEW -->` marker on the first line |
| 171 | +- [ ] Summary comment is **multi-line Markdown** with `##` / `###` headers and `| |` tables |
| 172 | +- [ ] Summary was written to a temp file and posted via `--body-file` or `cat` to preserve formatting |
| 173 | +- [ ] Summary is **not** a single line of pipe-delimited or dash-delimited text |
| 174 | +- [ ] CI run link is included in the footer |
0 commit comments