Skip to content

Commit 5d07d7d

Browse files
committed
Refactor Claude Code Action to auto-generate prompts from GitHub context when no input is provided, improving usability and functionality. Update workflow to remove redundant prompt generation step, streamlining the process.
1 parent 291acd4 commit 5d07d7d

File tree

2 files changed

+38
-48
lines changed

2 files changed

+38
-48
lines changed

.github/actions/claude-code-action/action.yml

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,49 @@ runs:
6363
shell: bash
6464
id: prepare_prompt
6565
run: |
66-
# Check if either prompt or prompt_file is provided
66+
# If neither prompt nor prompt_file is provided, auto-generate from GitHub context
6767
if [ -z "${{ inputs.prompt }}" ] && [ -z "${{ inputs.prompt_file }}" ]; then
68-
echo "::error::Neither 'prompt' nor 'prompt_file' was provided. At least one is required."
69-
exit 1
70-
fi
71-
72-
# Determine which prompt source to use
73-
if [ ! -z "${{ inputs.prompt_file }}" ]; then
68+
echo "Auto-generating prompt from GitHub context..."
69+
mkdir -p /tmp/claude-action
70+
71+
# Extract the user's request from the GitHub event (mimicking official behavior)
72+
if [[ "${{ github.event_name }}" == "issue_comment" ]]; then
73+
PROMPT_TEXT="${{ github.event.comment.body }}"
74+
CONTEXT="Issue Comment on: ${{ github.event.issue.title }}"
75+
elif [[ "${{ github.event_name }}" == "pull_request_review_comment" ]]; then
76+
PROMPT_TEXT="${{ github.event.comment.body }}"
77+
CONTEXT="PR Comment on: ${{ github.event.pull_request.title }}"
78+
elif [[ "${{ github.event_name }}" == "pull_request_review" ]]; then
79+
PROMPT_TEXT="${{ github.event.review.body }}"
80+
CONTEXT="PR Review on: ${{ github.event.pull_request.title }}"
81+
elif [[ "${{ github.event_name }}" == "issues" ]]; then
82+
PROMPT_TEXT="${{ github.event.issue.body }}"
83+
CONTEXT="Issue: ${{ github.event.issue.title }}"
84+
else
85+
PROMPT_TEXT="Help with this repository"
86+
CONTEXT="Repository assistance requested"
87+
fi
88+
89+
# Create GitHub context prompt (similar to official action)
90+
cat > /tmp/claude-action/github-context-prompt.txt << EOF
91+
You are Claude Code, an AI assistant helping with GitHub workflows and code.
92+
93+
Repository: ${{ github.repository }}
94+
Context: $CONTEXT
95+
96+
User Request:
97+
$PROMPT_TEXT
98+
99+
Please analyze the request and provide helpful assistance. You have access to repository tools and can help with code analysis, implementations, and GitHub workflows.
100+
EOF
101+
102+
PROMPT_PATH="/tmp/claude-action/github-context-prompt.txt"
103+
elif [ ! -z "${{ inputs.prompt_file }}" ]; then
74104
# Check if the prompt file exists
75105
if [ ! -f "${{ inputs.prompt_file }}" ]; then
76106
echo "::error::Prompt file '${{ inputs.prompt_file }}' does not exist."
77107
exit 1
78108
fi
79-
80-
# Use the provided prompt file
81109
PROMPT_PATH="${{ inputs.prompt_file }}"
82110
else
83111
mkdir -p /tmp/claude-action

.github/workflows/claude.yml

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,49 +29,11 @@ jobs:
2929
with:
3030
fetch-depth: 1
3131

32-
- name: Generate Prompt from GitHub Context
33-
id: generate-prompt
34-
run: |
35-
mkdir -p /tmp/claude-prompts
36-
37-
# Extract the user's request from the GitHub event
38-
if [[ "${{ github.event_name }}" == "issue_comment" ]]; then
39-
PROMPT_TEXT="${{ github.event.comment.body }}"
40-
CONTEXT="Issue Comment: ${{ github.event.issue.title }}"
41-
elif [[ "${{ github.event_name }}" == "pull_request_review_comment" ]]; then
42-
PROMPT_TEXT="${{ github.event.comment.body }}"
43-
CONTEXT="PR Comment: ${{ github.event.pull_request.title }}"
44-
elif [[ "${{ github.event_name }}" == "pull_request_review" ]]; then
45-
PROMPT_TEXT="${{ github.event.review.body }}"
46-
CONTEXT="PR Review: ${{ github.event.pull_request.title }}"
47-
elif [[ "${{ github.event_name }}" == "issues" ]]; then
48-
PROMPT_TEXT="${{ github.event.issue.body }}"
49-
CONTEXT="Issue: ${{ github.event.issue.title }}"
50-
fi
51-
52-
# Create the prompt file
53-
cat > /tmp/claude-prompts/github-context-prompt.txt << EOF
54-
You are Claude Code, an AI assistant that helps with code and GitHub workflows.
55-
56-
Context: $CONTEXT
57-
Repository: ${{ github.repository }}
58-
59-
User Request:
60-
$PROMPT_TEXT
61-
62-
Please analyze the request and provide helpful assistance. You have access to the repository context and can help with code analysis, suggestions, and implementations.
63-
EOF
64-
65-
echo "prompt_file=/tmp/claude-prompts/github-context-prompt.txt" >> $GITHUB_OUTPUT
66-
shell: bash
67-
6832
- name: Run Claude Code
6933
id: claude
7034
uses: ./.github/actions/claude-code-action
7135
with:
72-
prompt_file: ${{ steps.generate-prompt.outputs.prompt_file }}
73-
install_github_mcp: "true"
74-
timeout_minutes: "10"
7536
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
7637
github_token: ${{ secrets.GITHUB_TOKEN }}
38+
install_github_mcp: "true"
7739

0 commit comments

Comments
 (0)