Skip to content

Commit 0962477

Browse files
committed
add scripts for claude actions
1 parent b0f0e16 commit 0962477

7 files changed

Lines changed: 356 additions & 112 deletions

File tree

CLAUDE.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,32 @@ Remember to update both:
3535
2. Version reference in README.md examples
3636
3. Create a git tag matching the version
3737

38+
## Implementation Plan for Code Modification
39+
40+
We're implementing several approaches for Claude to make code changes:
41+
42+
1. **Suggested Changes Mode (Priority 1)**
43+
- Claude generates code suggestions formatted as GitHub suggested changes
44+
- Developers can review and apply with one click
45+
- Safest approach but limited to smaller changes
46+
47+
2. **Two-step Approval (Priority 2)**
48+
- Claude proposes changes in a comment
49+
- Developer must explicitly approve with a follow-up command
50+
- Only then are changes committed and pushed
51+
- Example: "claude-approve: [reference ID]"
52+
53+
3. **Draft PR Approach (Priority 3)**
54+
- Create a separate branch with Claude's changes
55+
- Open a draft PR against the original PR branch
56+
- Completely isolates changes for review
57+
58+
4. **Special Command Prefix (Priority 4)**
59+
- Regular comments: "claude: [question]" → just get a response
60+
- For changes: "claude-fix: [request]" → makes and commits changes
61+
- Clear differentiation of intent
62+
3863
## Future Features
3964

40-
- Code change functionality (planned)
4165
- Additional modes for different types of Claude integration
4266
- Support for custom prompts and templates

README.md

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
11
# Claude Code GitHub Action
22

3-
This GitHub Action integrates Claude Code in your GitHub workflows, enabling AI-assisted code reviews and responses to PR comments.
3+
This GitHub Action integrates Claude Code in your GitHub workflows, enabling AI-assisted code reviews, suggestions, and responses to PR comments.
44

55
## Features
66

7-
- Process PR comments prefixed with "claude:"
7+
- Process PR comments prefixed with "claude:" for general analysis
8+
- Process PR comments prefixed with "claude-suggest:" for clickable code suggestions
89
- Provide rich context about the PR to Claude, including file diffs
910
- Get AI-powered code analysis and suggestions
11+
- Create GitHub-compatible suggested changes that can be applied with one click
1012
- Simple setup with minimal configuration
1113
- Uses GitHub CLI and Claude Code CLI for reliability
1214

1315
## Usage
1416

15-
Create a workflow file that responds to comments containing the "claude:" prefix:
17+
Create a workflow file (`.github/workflows/claude-code.yml`) that responds to comments with the appropriate prefixes:
1618

1719
```yaml
1820
name: Claude Code Integration
1921

2022
on:
2123
issue_comment:
2224
types: [created]
23-
pull_request_review_comment:
24-
types: [created]
2525

2626
jobs:
27-
process-pr-comment:
27+
process-pr-review:
2828
runs-on: ubuntu-latest
2929
if: ${{ github.event_name == 'issue_comment' && github.event.issue.pull_request && startsWith(github.event.comment.body, 'claude:') }}
3030
permissions:
3131
contents: read
3232
pull-requests: write
3333
issues: write
3434
steps:
35+
- name: Checkout code
36+
uses: actions/checkout@v4
37+
with:
38+
fetch-depth: 0
39+
3540
- name: Get PR details
3641
id: pr
3742
run: |
@@ -43,60 +48,114 @@ jobs:
4348
echo "feedback=$FEEDBACK" >> $GITHUB_OUTPUT
4449
4550
- name: Process with Claude Code
46-
uses: fractureinc/claude-code-github-action@v0.1.7
51+
uses: fractureinc/claude-code-github-action@v0.2.0
4752
with:
4853
mode: 'review'
4954
pr-number: ${{ steps.pr.outputs.number }}
5055
feedback: ${{ steps.pr.outputs.feedback }}
5156
anthropic-api-key: ${{ secrets.ANTHROPIC_API_KEY }}
5257
github-token: ${{ secrets.GITHUB_TOKEN }}
58+
59+
process-pr-suggestions:
60+
runs-on: ubuntu-latest
61+
if: ${{ github.event_name == 'issue_comment' && github.event.issue.pull_request && startsWith(github.event.comment.body, 'claude-suggest:') }}
62+
permissions:
63+
contents: read
64+
pull-requests: write
65+
issues: write
66+
steps:
67+
- name: Checkout code
68+
uses: actions/checkout@v4
69+
with:
70+
fetch-depth: 0
71+
72+
- name: Get PR details
73+
id: pr
74+
run: |
75+
PR_NUMBER="${{ github.event.issue.number }}"
76+
FEEDBACK="${{ github.event.comment.body }}"
77+
# Remove the "claude-suggest:" prefix
78+
FEEDBACK="${FEEDBACK#claude-suggest:}"
79+
echo "number=$PR_NUMBER" >> $GITHUB_OUTPUT
80+
echo "feedback=$FEEDBACK" >> $GITHUB_OUTPUT
81+
82+
- name: Process with Claude Code Suggestions
83+
uses: fractureinc/claude-code-github-action@v0.2.0
84+
with:
85+
mode: 'suggest'
86+
pr-number: ${{ steps.pr.outputs.number }}
87+
feedback: ${{ steps.pr.outputs.feedback }}
88+
anthropic-api-key: ${{ secrets.ANTHROPIC_API_KEY }}
89+
github-token: ${{ secrets.GITHUB_TOKEN }}
5390
```
5491
5592
## Configuration
5693
5794
| Input | Description | Required | Default |
5895
|-------|-------------|----------|---------|
59-
| `mode` | Operation mode (review or direct) | Yes | `review` |
96+
| `mode` | Operation mode (review, suggest, direct) | Yes | `review` |
6097
| `pr-number` | Pull request number | Yes* | |
6198
| `feedback` | User query text | Yes | |
6299
| `anthropic-api-key` | Anthropic API key | Yes | |
63100
| `github-token` | GitHub token | Yes | |
64101
| `output-file` | Output file path (for direct mode) | No | `claude-code-output` |
65102

66-
\* Required when mode is 'review'
103+
\* Required when mode is 'review' or 'suggest'
67104

68105
## Enhanced Context for Claude
69106

70-
With version 0.1.7, Claude now receives complete context for your PRs, including:
107+
With version 0.2.0, Claude now receives complete context for your PRs, including:
71108

72109
- PR metadata (title, description, branch info)
73110
- List of all files changed
74111
- Complete diff of all changes in the PR
75112
- Repository information
113+
- Full repository checkout for improved code understanding
76114

77-
This allows Claude to provide much more accurate and helpful responses about your code changes.
115+
## Available Modes
116+
117+
### Review Mode (`mode: 'review'`)
118+
119+
Standard mode that provides Claude's analysis and feedback about your PR changes as a comment.
120+
121+
### Suggest Mode (`mode: 'suggest'`)
122+
123+
Creates GitHub-compatible suggested changes that can be applied with one click directly from the PR interface.
124+
125+
### Direct Mode (`mode: 'direct'`)
126+
127+
Sends a query directly to Claude and saves the response to a file without PR context.
78128

79129
## Example Queries
80130

81-
Here are some example queries you can use with the claude: prefix:
131+
### Review Mode Examples (prefix: `claude:`)
82132

83133
- `claude: Explain the changes in this PR`
84134
- `claude: Can you suggest improvements to the code?`
85135
- `claude: Are there any security issues in these changes?`
86136
- `claude: How would you refactor this to be more maintainable?`
87137
- `claude: What tests should be added for this code?`
88138

139+
### Suggest Mode Examples (prefix: `claude-suggest:`)
140+
141+
- `claude-suggest: Improve error handling in the API client`
142+
- `claude-suggest: Fix any potential memory leaks`
143+
- `claude-suggest: Optimize the database query on line 25`
144+
- `claude-suggest: Make this code more readable`
145+
89146
## How It Works
90147

91-
1. The action is triggered when a comment starting with "claude:" is detected on a PR
148+
1. The action is triggered when a comment with the appropriate prefix is detected on a PR
92149
2. The action extracts the PR number and the user's query
93-
3. Using GitHub CLI, the action fetches comprehensive information about the PR including:
150+
3. The repository is checked out to provide full code context
151+
4. Using GitHub CLI, the action fetches comprehensive information about the PR including:
94152
- PR metadata
95153
- List of files changed
96154
- Complete diff of all changes
97-
4. This rich context is provided to Claude along with the user's query
98-
5. Claude processes the information and provides a helpful response
99-
6. The response is posted as a comment on the PR
155+
5. This rich context is provided to Claude along with the user's query
156+
6. Claude processes the information and provides a helpful response
157+
7. For review mode: The response is posted as a comment on the PR
158+
8. For suggest mode: Claude formats responses as GitHub suggested changes that can be applied with one click
100159

101160
## Permissions
102161

action.yml

Lines changed: 16 additions & 91 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, comment, direct)'
9+
description: 'The mode to run the action in (review, pr-comment, suggest, direct)'
1010
required: true
1111
default: 'review'
1212
pr-number:
@@ -29,6 +29,9 @@ inputs:
2929
runs:
3030
using: 'composite'
3131
steps:
32+
- name: Checkout repository
33+
uses: actions/checkout@v4
34+
3235
- name: Setup GitHub CLI
3336
shell: bash
3437
run: |
@@ -41,101 +44,23 @@ runs:
4144
# Install Claude Code CLI
4245
npm install -g @anthropic-ai/claude-code
4346
44-
- name: Process PR comment with Claude
47+
- name: Process Review Request
4548
if: inputs.mode == 'review' || inputs.mode == 'pr-comment'
4649
shell: bash
4750
run: |
48-
# Set Anthropic API key
49-
export ANTHROPIC_API_KEY="${{ inputs.anthropic-api-key }}"
51+
chmod +x ${{ github.action_path }}/scripts/review-mode.sh
52+
${{ github.action_path }}/scripts/review-mode.sh "${{ inputs.pr-number }}" "${{ inputs.feedback }}" "${{ inputs.anthropic-api-key }}" "${{ inputs.github-token }}"
5053
51-
# Create a temp file for Claude's response
52-
RESPONSE_FILE=$(mktemp)
53-
54-
# Get PR details using GitHub CLI
55-
echo "Fetching PR details for PR #${{ inputs.pr-number }}"
56-
PR_DETAILS=$(gh pr view ${{ inputs.pr-number }} --json title,body,baseRefName,headRefName,additions,deletions,changedFiles,state)
57-
58-
PR_TITLE=$(echo "$PR_DETAILS" | jq -r '.title')
59-
PR_BODY=$(echo "$PR_DETAILS" | jq -r '.body')
60-
PR_BASE=$(echo "$PR_DETAILS" | jq -r '.baseRefName')
61-
PR_HEAD=$(echo "$PR_DETAILS" | jq -r '.headRefName')
62-
PR_STATE=$(echo "$PR_DETAILS" | jq -r '.state')
63-
PR_ADDITIONS=$(echo "$PR_DETAILS" | jq -r '.additions')
64-
PR_DELETIONS=$(echo "$PR_DETAILS" | jq -r '.deletions')
65-
PR_CHANGED_FILES=$(echo "$PR_DETAILS" | jq -r '.changedFiles')
66-
67-
# Get list of files changed
68-
echo "Fetching files changed in the PR"
69-
PR_FILES=$(gh pr view ${{ inputs.pr-number }} --json files)
70-
71-
# Get file content diffs
72-
echo "Fetching file content diffs"
73-
PR_DIFF=$(gh pr diff ${{ inputs.pr-number }})
74-
75-
# Get summary of files changed for the PR metadata
76-
FILES_LIST=$(echo "$PR_FILES" | jq -r '.files[] | "- " + .path + " (" + .status + ", +" + (.additions | tostring) + "/-" + (.deletions | tostring) + ")"')
77-
78-
# Get repo information
79-
REPO_INFO=$(gh repo view --json name,description,defaultBranchRef,languages)
80-
REPO_NAME=$(echo "$REPO_INFO" | jq -r '.name')
81-
REPO_DESC=$(echo "$REPO_INFO" | jq -r '.description')
82-
REPO_DEFAULT_BRANCH=$(echo "$REPO_INFO" | jq -r '.defaultBranchRef.name')
83-
REPO_LANGUAGES=$(echo "$REPO_INFO" | jq -r '.languages[].name' | tr '\n' ', ' | sed 's/,$//')
84-
85-
# Build the prompt with rich context
86-
PROMPT=$(cat <<EOF
87-
You are Claude, an AI assistant helping with code review in GitHub.
88-
89-
Repository: $REPO_NAME
90-
Repository Description: ${REPO_DESC:-No description provided}
91-
Default Branch: $REPO_DEFAULT_BRANCH
92-
Languages: ${REPO_LANGUAGES:-Unknown}
93-
94-
Pull Request #${{ inputs.pr-number }}: $PR_TITLE
95-
PR Description:
96-
${PR_BODY:-No description provided}
97-
98-
PR Status: $PR_STATE
99-
Branch: $PR_HEAD → $PR_BASE
100-
Changes: +$PR_ADDITIONS/-$PR_DELETIONS in $PR_CHANGED_FILES files
101-
102-
Files changed in this PR:
103-
$FILES_LIST
104-
105-
Diff of changes:
106-
\`\`\`diff
107-
$PR_DIFF
108-
\`\`\`
109-
110-
User query:
111-
${{ inputs.feedback }}
112-
113-
Provide a helpful, clear and concise response to this query in the context of this PR. Focus on giving accurate, actionable advice or explanations based on the code changes shown in the diff. Include code examples when appropriate.
114-
EOF
115-
)
116-
117-
# Run Claude CLI
118-
echo "Sending request to Claude..."
119-
echo "$PROMPT" | claude -p - > "$RESPONSE_FILE"
120-
121-
# Post response as PR comment
122-
echo "Posting Claude's response as PR comment"
123-
gh pr comment ${{ inputs.pr-number }} --body-file "$RESPONSE_FILE"
124-
125-
# Clean up
126-
rm -f "$RESPONSE_FILE"
127-
128-
echo "Claude's response posted successfully!"
54+
- name: Process Suggested Changes
55+
if: inputs.mode == 'suggest'
56+
shell: bash
57+
run: |
58+
chmod +x ${{ github.action_path }}/scripts/suggest-mode.sh
59+
${{ github.action_path }}/scripts/suggest-mode.sh "${{ inputs.pr-number }}" "${{ inputs.feedback }}" "${{ inputs.anthropic-api-key }}" "${{ inputs.github-token }}"
12960
130-
- name: Process direct request with Claude
61+
- name: Process Direct Request
13162
if: inputs.mode == 'direct'
13263
shell: bash
13364
run: |
134-
# Set Anthropic API key
135-
export ANTHROPIC_API_KEY="${{ inputs.anthropic-api-key }}"
136-
137-
# Run Claude CLI and capture output
138-
echo "Sending request to Claude..."
139-
echo "${{ inputs.feedback }}" | claude -p - > "${{ inputs.output-file }}"
140-
141-
echo "Claude's response written to ${{ inputs.output-file }}"
65+
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 }}"

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.1.7",
4-
"description": "GitHub action for Claude Code Integration in PR comments and reviews",
3+
"version": "0.2.0",
4+
"description": "GitHub action for Claude Code Integration in PR comments, reviews and code suggestions",
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
@@ -18,7 +18,8 @@
1818
"anthropic",
1919
"ai",
2020
"review",
21-
"pull-request"
21+
"pull-request",
22+
"suggestions"
2223
],
2324
"author": "Fracture Inc",
2425
"license": "MIT",

scripts/direct-mode.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Get input parameters
5+
FEEDBACK=$1
6+
ANTHROPIC_API_KEY=$2
7+
OUTPUT_FILE=$3
8+
9+
# Set Anthropic API key
10+
export ANTHROPIC_API_KEY="$ANTHROPIC_API_KEY"
11+
12+
# Run Claude CLI and capture output
13+
echo "Sending request to Claude..."
14+
echo "$FEEDBACK" | claude -p - > "$OUTPUT_FILE"
15+
16+
echo "Claude's response written to $OUTPUT_FILE"

0 commit comments

Comments
 (0)