Skip to content

Commit 3c0253f

Browse files
committed
Add true GitHub suggestion support with suggest-review mode
1 parent a62d360 commit 3c0253f

5 files changed

Lines changed: 229 additions & 14 deletions

File tree

.github/workflows/claude-code.yml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ name: Claude Code Integration
33
on:
44
issue_comment:
55
types: [created]
6+
pull_request_review_comment:
7+
types: [created]
68

79
jobs:
10+
# Handle PR comments
811
process-pr-review:
912
runs-on: ubuntu-latest
1013
if: ${{ github.event_name == 'issue_comment' && github.event.issue.pull_request && startsWith(github.event.comment.body, 'claude:') }}
@@ -67,4 +70,86 @@ jobs:
6770
pr-number: ${{ steps.pr.outputs.number }}
6871
feedback: ${{ steps.pr.outputs.feedback }}
6972
anthropic-api-key: ${{ secrets.ANTHROPIC_API_KEY }}
73+
github-token: ${{ secrets.GITHUB_TOKEN }}
74+
75+
# Handle code review comments
76+
process-review-comment:
77+
runs-on: ubuntu-latest
78+
if: ${{ github.event_name == 'pull_request_review_comment' && startsWith(github.event.comment.body, 'claude:') }}
79+
permissions:
80+
contents: read
81+
pull-requests: write
82+
issues: write
83+
steps:
84+
- name: Checkout code
85+
uses: actions/checkout@v4
86+
with:
87+
fetch-depth: 0
88+
89+
- name: Get PR and comment details
90+
id: details
91+
run: |
92+
PR_NUMBER="${{ github.event.pull_request.number }}"
93+
FEEDBACK="${{ github.event.comment.body }}"
94+
# Remove the "claude:" prefix
95+
FEEDBACK="${FEEDBACK#claude:}"
96+
COMMENT_ID="${{ github.event.comment.id }}"
97+
FILE_PATH="${{ github.event.comment.path }}"
98+
LINE="${{ github.event.comment.line }}"
99+
100+
echo "number=$PR_NUMBER" >> $GITHUB_OUTPUT
101+
echo "feedback=$FEEDBACK" >> $GITHUB_OUTPUT
102+
echo "comment_id=$COMMENT_ID" >> $GITHUB_OUTPUT
103+
echo "file_path=$FILE_PATH" >> $GITHUB_OUTPUT
104+
echo "line=$LINE" >> $GITHUB_OUTPUT
105+
106+
- name: Process with Claude Code for code review comment
107+
uses: fractureinc/claude-code-github-action@v0.2.0
108+
with:
109+
mode: 'review'
110+
pr-number: ${{ steps.details.outputs.number }}
111+
feedback: ${{ steps.details.outputs.feedback }}
112+
anthropic-api-key: ${{ secrets.ANTHROPIC_API_KEY }}
113+
github-token: ${{ secrets.GITHUB_TOKEN }}
114+
115+
process-suggest-review-comment:
116+
runs-on: ubuntu-latest
117+
if: ${{ github.event_name == 'pull_request_review_comment' && startsWith(github.event.comment.body, 'claude-suggest:') }}
118+
permissions:
119+
contents: read
120+
pull-requests: write
121+
issues: write
122+
steps:
123+
- name: Checkout code
124+
uses: actions/checkout@v4
125+
with:
126+
fetch-depth: 0
127+
128+
- name: Get PR and comment details
129+
id: details
130+
run: |
131+
PR_NUMBER="${{ github.event.pull_request.number }}"
132+
FEEDBACK="${{ github.event.comment.body }}"
133+
# Remove the "claude-suggest:" prefix
134+
FEEDBACK="${FEEDBACK#claude-suggest:}"
135+
COMMENT_ID="${{ github.event.comment.id }}"
136+
FILE_PATH="${{ github.event.comment.path }}"
137+
LINE="${{ github.event.comment.line }}"
138+
139+
echo "number=$PR_NUMBER" >> $GITHUB_OUTPUT
140+
echo "feedback=$FEEDBACK" >> $GITHUB_OUTPUT
141+
echo "comment_id=$COMMENT_ID" >> $GITHUB_OUTPUT
142+
echo "file_path=$FILE_PATH" >> $GITHUB_OUTPUT
143+
echo "line=$LINE" >> $GITHUB_OUTPUT
144+
145+
- name: Process with Claude Code Suggestions for code review
146+
uses: fractureinc/claude-code-github-action@v0.2.1
147+
with:
148+
mode: 'suggest-review'
149+
pr-number: ${{ steps.details.outputs.number }}
150+
feedback: ${{ steps.details.outputs.feedback }}
151+
file-path: ${{ steps.details.outputs.file_path }}
152+
line-number: ${{ steps.details.outputs.line }}
153+
comment-id: ${{ steps.details.outputs.comment_id }}
154+
anthropic-api-key: ${{ secrets.ANTHROPIC_API_KEY }}
70155
github-token: ${{ secrets.GITHUB_TOKEN }}

README.md

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ This GitHub Action integrates Claude Code in your GitHub workflows, enabling AI-
55
## Features
66

77
- Process PR comments prefixed with "claude:" for general analysis
8-
- Process PR comments prefixed with "claude-suggest:" for clickable code suggestions
8+
- Process PR comments prefixed with "claude-suggest:" for code suggestions
9+
- Process code review comments to provide in-line analysis and suggestions
910
- Provide rich context about the PR to Claude, including file diffs
1011
- Get AI-powered code analysis and suggestions
1112
- Create GitHub-compatible suggested changes that can be applied with one click
@@ -48,7 +49,7 @@ jobs:
4849
echo "feedback=$FEEDBACK" >> $GITHUB_OUTPUT
4950
5051
- name: Process with Claude Code
51-
uses: fractureinc/claude-code-github-action@v0.2.0
52+
uses: fractureinc/claude-code-github-action@v0.2.1
5253
with:
5354
mode: 'review'
5455
pr-number: ${{ steps.pr.outputs.number }}
@@ -80,7 +81,7 @@ jobs:
8081
echo "feedback=$FEEDBACK" >> $GITHUB_OUTPUT
8182
8283
- name: Process with Claude Code Suggestions
83-
uses: fractureinc/claude-code-github-action@v0.2.0
84+
uses: fractureinc/claude-code-github-action@v0.2.1
8485
with:
8586
mode: 'suggest'
8687
pr-number: ${{ steps.pr.outputs.number }}
@@ -104,7 +105,7 @@ jobs:
104105

105106
## Enhanced Context for Claude
106107

107-
With version 0.2.0, Claude now receives complete context for your PRs, including:
108+
With version 0.2.1, Claude now receives complete context for your PRs, including:
108109

109110
- PR metadata (title, description, branch info)
110111
- List of all files changed
@@ -120,7 +121,11 @@ Standard mode that provides Claude's analysis and feedback about your PR changes
120121

121122
### Suggest Mode (`mode: 'suggest'`)
122123

123-
Creates GitHub-compatible suggested changes that can be applied with one click directly from the PR interface.
124+
Creates suggested changes in a PR comment that outline potential code improvements.
125+
126+
### Suggest Review Mode (`mode: 'suggest-review'`)
127+
128+
Creates true GitHub-compatible suggestions that can be applied with one click directly from the code review interface. These are attached to specific lines of code.
124129

125130
### Direct Mode (`mode: 'direct'`)
126131

@@ -146,17 +151,19 @@ Sends a query directly to Claude and saves the response to a file without PR con
146151

147152
## How It Works
148153

149-
1. The action is triggered when a comment with the appropriate prefix is detected on a PR
150-
2. The action extracts the PR number and the user's query
154+
1. The action is triggered when a comment with the appropriate prefix is detected (either on the PR or in code review)
155+
2. The action extracts the PR number, user's query, and (for code review comments) the file path and line number
151156
3. The repository is checked out to provide full code context
152157
4. Using GitHub CLI, the action fetches comprehensive information about the PR including:
153158
- PR metadata
154-
- List of files changed
159+
- List of files changed
155160
- Complete diff of all changes
161+
- For code review comments, specific file content and context around the commented line
156162
5. This rich context is provided to Claude along with the user's query
157163
6. Claude processes the information and provides a helpful response
158164
7. For review mode: The response is posted as a comment on the PR
159-
8. For suggest mode: Claude formats responses as GitHub suggested changes that can be applied with one click
165+
8. For suggest mode: Claude formats responses with code suggestions in the PR comment
166+
9. For suggest-review mode: Claude creates true GitHub-compatible suggestions attached to specific lines of code
160167

161168
## Permissions
162169

action.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ branding:
66

77
inputs:
88
mode:
9-
description: 'The mode to run the action in (review, pr-comment, suggest, direct)'
9+
description: 'The mode to run the action in (review, suggest, suggest-review, direct)'
1010
required: true
1111
default: 'review'
1212
pr-number:
@@ -15,6 +15,15 @@ inputs:
1515
feedback:
1616
description: 'The feedback text from the comment'
1717
required: false
18+
file-path:
19+
description: 'Path to the file being reviewed (for suggest-review mode)'
20+
required: false
21+
line-number:
22+
description: 'Line number in the file (for suggest-review mode)'
23+
required: false
24+
comment-id:
25+
description: 'GitHub comment ID to reply to (for suggest-review mode)'
26+
required: false
1827
anthropic-api-key:
1928
description: 'Anthropic API key for Claude access'
2029
required: true
@@ -63,4 +72,11 @@ runs:
6372
shell: bash
6473
run: |
6574
chmod +x ${{ github.action_path }}/scripts/direct-mode.sh
66-
${{ github.action_path }}/scripts/direct-mode.sh "${{ inputs.feedback }}" "${{ inputs.anthropic-api-key }}" "${{ inputs.output-file }}"
75+
${{ github.action_path }}/scripts/direct-mode.sh "${{ inputs.feedback }}" "${{ inputs.anthropic-api-key }}" "${{ inputs.output-file }}"
76+
77+
- name: Process In-line Code Suggestions
78+
if: inputs.mode == 'suggest-review'
79+
shell: bash
80+
run: |
81+
chmod +x ${{ github.action_path }}/scripts/suggest-review-mode.sh
82+
${{ github.action_path }}/scripts/suggest-review-mode.sh "${{ inputs.pr-number }}" "${{ inputs.feedback }}" "${{ inputs.file-path }}" "${{ inputs.line-number }}" "${{ inputs.comment-id }}" "${{ inputs.anthropic-api-key }}" "${{ inputs.github-token }}"

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "claude-code-github-action",
3-
"version": "0.2.0",
4-
"description": "GitHub action for Claude Code Integration in PR comments, reviews and code suggestions",
3+
"version": "0.2.1",
4+
"description": "GitHub action for Claude Code Integration in PR comments, reviews and inline code suggestions",
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
@@ -19,7 +19,8 @@
1919
"ai",
2020
"review",
2121
"pull-request",
22-
"suggestions"
22+
"suggestions",
23+
"inline"
2324
],
2425
"author": "Fracture Inc",
2526
"license": "MIT",

scripts/suggest-review-mode.sh

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Get input parameters
5+
PR_NUMBER=$1
6+
FEEDBACK=$2
7+
FILE_PATH=$3
8+
LINE_NUMBER=$4
9+
COMMENT_ID=$5
10+
ANTHROPIC_API_KEY=$6
11+
GITHUB_TOKEN=$7
12+
13+
# Set up authentication
14+
echo "$GITHUB_TOKEN" | gh auth login --with-token
15+
export ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY"
16+
17+
# Create a temp file for Claude's response
18+
RESPONSE_FILE=$(mktemp)
19+
20+
# Get PR details using GitHub CLI
21+
echo "Fetching PR details for PR #$PR_NUMBER"
22+
PR_DETAILS=$(gh pr view $PR_NUMBER --json title,body,baseRefName,headRefName,additions,deletions,changedFiles,state)
23+
24+
PR_TITLE=$(echo "$PR_DETAILS" | jq -r '.title')
25+
PR_BODY=$(echo "$PR_DETAILS" | jq -r '.body')
26+
PR_BASE=$(echo "$PR_DETAILS" | jq -r '.baseRefName')
27+
PR_HEAD=$(echo "$PR_DETAILS" | jq -r '.headRefName')
28+
PR_STATE=$(echo "$PR_DETAILS" | jq -r '.state')
29+
30+
# Checkout PR branch for full repo context
31+
echo "Checking out PR branch: $PR_HEAD"
32+
git fetch origin pull/$PR_NUMBER/head:$PR_HEAD
33+
git checkout $PR_HEAD
34+
35+
# Get the specific file content
36+
FILE_CONTENT=$(cat "$FILE_PATH")
37+
38+
# Get a context window around the line in question (10 lines before and after)
39+
LINE_START=$((LINE_NUMBER - 10))
40+
if [ $LINE_START -lt 1 ]; then
41+
LINE_START=1
42+
fi
43+
LINE_END=$((LINE_NUMBER + 10))
44+
CONTEXT_CONTENT=$(sed -n "${LINE_START},${LINE_END}p" "$FILE_PATH")
45+
46+
# Get repo information
47+
REPO_INFO=$(gh repo view --json name,description,defaultBranchRef,languages)
48+
REPO_NAME=$(echo "$REPO_INFO" | jq -r '.name')
49+
REPO_DESC=$(echo "$REPO_INFO" | jq -r '.description')
50+
REPO_DEFAULT_BRANCH=$(echo "$REPO_INFO" | jq -r '.defaultBranchRef.name')
51+
REPO_LANGUAGES=$(echo "$REPO_INFO" | jq -r '.languages[].name' | tr '\n' ', ' | sed 's/,$//')
52+
53+
# Build the prompt for Claude to generate a suggested change
54+
PROMPT=$(cat <<EOF
55+
This is a GitHub code review. Create a suggestion for the specific code at line $LINE_NUMBER in file '$FILE_PATH'.
56+
57+
Your response will be directly used by GitHub to create a suggestion on this line of code.
58+
You MUST format your response as a SINGLE suggestion using the exact GitHub suggestion format:
59+
60+
\`\`\`suggestion
61+
[Your improved code here]
62+
\`\`\`
63+
64+
Guidelines:
65+
1. Focus ONLY on line $LINE_NUMBER and immediately surrounding lines
66+
2. Keep the suggestion concise - it should replace just what needs to be changed
67+
3. Maintain the existing indentation and code style
68+
4. Ensure your suggestion is syntactically correct
69+
5. Start your response with a brief explanation, then provide the suggestion block
70+
6. Your suggestion will be applied directly to the code via GitHub's suggestion feature
71+
72+
Repository: $REPO_NAME
73+
PR: $PR_TITLE
74+
File being reviewed: $FILE_PATH
75+
Line number: $LINE_NUMBER
76+
77+
Context (code around line $LINE_NUMBER):
78+
\`\`\`
79+
$CONTEXT_CONTENT
80+
\`\`\`
81+
82+
Complete file content:
83+
\`\`\`
84+
$FILE_CONTENT
85+
\`\`\`
86+
87+
User query:
88+
$FEEDBACK
89+
90+
Provide a single, specific suggestion that addresses this query for the code at line $LINE_NUMBER.
91+
EOF
92+
)
93+
94+
# Run Claude CLI
95+
echo "Sending request to Claude for in-line suggestion..."
96+
echo "$PROMPT" | claude -p - > "$RESPONSE_FILE"
97+
98+
# Reply to the specific comment with Claude's suggestion
99+
echo "Posting Claude's suggested change as a reply to the comment"
100+
gh api --method POST "/repos/:owner/:repo/pulls/$PR_NUMBER/comments/$COMMENT_ID/replies" \
101+
-F body="@$RESPONSE_FILE"
102+
103+
# Clean up
104+
rm -f "$RESPONSE_FILE"
105+
106+
echo "Claude's in-line suggestion posted successfully!"

0 commit comments

Comments
 (0)