diff --git a/.github/workflows/claude-review.yml b/.github/workflows/claude-review.yml index b87110ea0..0beb47a98 100644 --- a/.github/workflows/claude-review.yml +++ b/.github/workflows/claude-review.yml @@ -29,4 +29,4 @@ jobs: Be constructive and specific in your feedback. Give inline comments where applicable. anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} - allowed_tools: "mcp__github__add_pull_request_review_comment" + allowed_tools: "mcp__github__create_pending_pull_request_review,mcp__github__add_pull_request_review_comment_to_pending_review,mcp__github__submit_pending_pull_request_review,mcp__github__get_pull_request_diff" diff --git a/.github/workflows/issue-triage.yml b/.github/workflows/issue-triage.yml index 4eb7fd54f..7d821a287 100644 --- a/.github/workflows/issue-triage.yml +++ b/.github/workflows/issue-triage.yml @@ -32,7 +32,7 @@ jobs: "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN", - "ghcr.io/github/github-mcp-server:sha-7aced2b" + "ghcr.io/github/github-mcp-server:sha-6d69797" ], "env": { "GITHUB_PERSONAL_ACCESS_TOKEN": "${{ secrets.GITHUB_TOKEN }}" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 000000000..97d9652d3 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,138 @@ +name: Create Release + +on: + workflow_dispatch: + inputs: + dry_run: + description: "Dry run (only show what would be created)" + required: false + type: boolean + default: false + +jobs: + create-release: + runs-on: ubuntu-latest + permissions: + contents: write + outputs: + next_version: ${{ steps.next_version.outputs.next_version }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Get latest tag + id: get_latest_tag + run: | + # Get only version tags (v + number pattern) + latest_tag=$(git tag -l 'v[0-9]*' | sort -V | tail -1 || echo "v0.0.0") + if [ -z "$latest_tag" ]; then + latest_tag="v0.0.0" + fi + echo "latest_tag=$latest_tag" >> $GITHUB_OUTPUT + echo "Latest tag: $latest_tag" + + - name: Calculate next version + id: next_version + run: | + latest_tag="${{ steps.get_latest_tag.outputs.latest_tag }}" + # Remove 'v' prefix and split by dots + version=${latest_tag#v} + IFS='.' read -ra VERSION_PARTS <<< "$version" + + # Increment patch version + major=${VERSION_PARTS[0]:-0} + minor=${VERSION_PARTS[1]:-0} + patch=${VERSION_PARTS[2]:-0} + patch=$((patch + 1)) + + next_version="v${major}.${minor}.${patch}" + echo "next_version=$next_version" >> $GITHUB_OUTPUT + echo "Next version: $next_version" + + - name: Display dry run info + if: ${{ inputs.dry_run }} + run: | + echo "🔍 DRY RUN MODE" + echo "Would create tag: ${{ steps.next_version.outputs.next_version }}" + echo "From commit: ${{ github.sha }}" + echo "Previous tag: ${{ steps.get_latest_tag.outputs.latest_tag }}" + + - name: Create and push tag + if: ${{ !inputs.dry_run }} + run: | + next_version="${{ steps.next_version.outputs.next_version }}" + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + + git tag -a "$next_version" -m "Release $next_version" + git push origin "$next_version" + + - name: Create Release + if: ${{ !inputs.dry_run }} + env: + GH_TOKEN: ${{ github.token }} + run: | + next_version="${{ steps.next_version.outputs.next_version }}" + + gh release create "$next_version" \ + --title "$next_version" \ + --generate-notes \ + --latest=false # We want to keep beta as the latest + + update-beta-tag: + needs: create-release + if: ${{ !inputs.dry_run }} + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Update beta tag + run: | + # Get the latest version tag + VERSION=$(git tag -l 'v[0-9]*' | sort -V | tail -1) + + # Update the beta tag to point to this release + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git tag -fa beta -m "Update beta tag to ${VERSION}" + git push origin beta --force + + - name: Update beta release to be latest + env: + GH_TOKEN: ${{ github.token }} + run: | + # Update beta release to be marked as latest + gh release edit beta --latest + + update-major-tag: + needs: create-release + if: ${{ !inputs.dry_run }} + runs-on: ubuntu-latest + permissions: + contents: write + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Update major version tag + run: | + next_version="${{ needs.create-release.outputs.next_version }}" + # Extract major version (e.g., v0 from v0.0.20) + major_version=$(echo "$next_version" | cut -d. -f1) + + # Update the major version tag to point to this release + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git tag -fa "$major_version" -m "Update $major_version tag to $next_version" + git push origin "$major_version" --force + + echo "Updated $major_version tag to point to $next_version" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 96824d164..74e61409f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -50,20 +50,6 @@ Thank you for your interest in contributing to Claude Code Action! This document bun test ``` -2. **Integration Tests** (using GitHub Actions locally): - - ```bash - ./test-local.sh - ``` - - This script: - - - Installs `act` if not present (requires Homebrew on macOS) - - Runs the GitHub Action workflow locally using Docker - - Requires your `ANTHROPIC_API_KEY` to be set - - On Apple Silicon Macs, the script automatically adds the `--container-architecture linux/amd64` flag to avoid compatibility issues. - ## Pull Request Process 1. Create a new branch from `main`: @@ -103,13 +89,7 @@ Thank you for your interest in contributing to Claude Code Action! This document When modifying the action: -1. Test locally with the test script: - - ```bash - ./test-local.sh - ``` - -2. Test in a real GitHub Actions workflow by: +1. Test in a real GitHub Actions workflow by: - Creating a test repository - Using your branch as the action source: ```yaml diff --git a/README.md b/README.md index 4c4a0376b..0dceb8cd0 100644 --- a/README.md +++ b/README.md @@ -70,6 +70,8 @@ jobs: # NODE_ENV: test # DEBUG: true # API_URL: https://api.example.com + # Optional: limit the number of conversation turns + # max_turns: "5" ``` ## Inputs @@ -78,6 +80,7 @@ jobs: | --------------------- | -------------------------------------------------------------------------------------------------------------------- | -------- | --------- | | `anthropic_api_key` | Anthropic API key (required for direct API, not needed for Bedrock/Vertex) | No\* | - | | `direct_prompt` | Direct prompt for Claude to execute automatically without needing a trigger (for automated workflows) | No | - | +| `max_turns` | Maximum number of conversation turns Claude can take (limits back-and-forth exchanges) | No | - | | `timeout_minutes` | Timeout in minutes for execution | No | `30` | | `github_token` | GitHub token for Claude to operate with. **Only include this if you're connecting a custom GitHub app of your own!** | No | - | | `model` | Model to use (provider-specific format required for Bedrock/Vertex) | No | - | @@ -146,6 +149,40 @@ For MCP servers that require sensitive information like API keys or tokens, use # ... other inputs ``` +#### Using Python MCP Servers with uv + +For Python-based MCP servers managed with `uv`, you need to specify the directory containing your server: + +```yaml +- uses: anthropics/claude-code-action@beta + with: + anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} + mcp_config: | + { + "mcpServers": { + "my-python-server": { + "type": "stdio", + "command": "uv", + "args": [ + "--directory", + "${{ github.workspace }}/path/to/server/", + "run", + "server_file.py" + ] + } + } + } + allowed_tools: "my-python-server__" # Replace with your server's tool names + # ... other inputs +``` + +For example, if your Python MCP server is at `mcp_servers/weather.py`, you would use: + +```yaml +"args": + ["--directory", "${{ github.workspace }}/mcp_servers/", "run", "weather.py"] +``` + **Important**: - Always use GitHub Secrets (`${{ secrets.SECRET_NAME }}`) for sensitive values like API keys, tokens, or passwords. Never hardcode secrets directly in the workflow file. @@ -311,6 +348,24 @@ You can pass custom environment variables to Claude Code execution using the `cl The `claude_env` input accepts YAML format where each line defines a key-value pair. These environment variables will be available to Claude Code during execution, allowing it to run tests, build processes, or other commands that depend on specific environment configurations. +### Limiting Conversation Turns + +You can use the `max_turns` parameter to limit the number of back-and-forth exchanges Claude can have during task execution. This is useful for: + +- Controlling costs by preventing runaway conversations +- Setting time boundaries for automated workflows +- Ensuring predictable behavior in CI/CD pipelines + +```yaml +- uses: anthropics/claude-code-action@beta + with: + anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} + max_turns: "5" # Limit to 5 conversation turns + # ... other inputs +``` + +When the turn limit is reached, Claude will stop execution gracefully. Choose a value that gives Claude enough turns to complete typical tasks while preventing excessive usage. + ### Custom Tools By default, Claude only has access to: @@ -326,8 +381,15 @@ Claude does **not** have access to execute arbitrary Bash commands by default. I ```yaml - uses: anthropics/claude-code-action@beta with: - allowed_tools: "Bash(npm install),Bash(npm run test),Edit,Replace,NotebookEditCell" - disallowed_tools: "TaskOutput,KillTask" + allowed_tools: | + Bash(npm install) + Bash(npm run test) + Edit + Replace + NotebookEditCell + disallowed_tools: | + TaskOutput + KillTask # ... other inputs ``` diff --git a/ROADMAP.md b/ROADMAP.md new file mode 100644 index 000000000..9bf66c447 --- /dev/null +++ b/ROADMAP.md @@ -0,0 +1,20 @@ +# Claude Code GitHub Action Roadmap + +Thank you for trying out the beta of our GitHub Action! This document outlines our path to `v1.0`. Items are not necessarily in priority order. + +## Path to 1.0 + +- **Ability to see GitHub Action CI results** - This will enable Claude to look at CI failures and make updates to PRs to fix test failures, lint errors, and the like. +- **Cross-repo support** - Enable Claude to work across multiple repositories in a single session +- **Ability to modify workflow files** - Let Claude update GitHub Actions workflows and other CI configuration files +- **Support for workflow_dispatch and repository_dispatch events** - Dispatch Claude on events triggered via API from other workflows or from other services +- **Ability to disable commit signing** - Option to turn off GPG signing for environments where it's not required. This will enable Claude to use normal `git` bash commands for committing. This will likely become the default behavior once added. +- **Better code review behavior** - Support inline comments on specific lines, provide higher quality reviews with more actionable feedback +- **Support triggering @claude from bot users** - Allow automation and bot accounts to invoke Claude +- **Customizable base prompts** - Full control over Claude's initial context with template variables like `$PR_COMMENTS`, `$PR_FILES`, etc. Users can replace our default prompt entirely while still accessing key contextual data + +--- + +**Note:** This roadmap represents our current vision for reaching `v1.0` and is subject to change based on user feedback and development priorities. + +We welcome feedback on these planned features! If you're interested in contributing to any of these features, please open an issue to discuss implementation details with us. We're also open to suggestions for new features not listed here. diff --git a/action.yml b/action.yml index 71594cd67..6c459174a 100644 --- a/action.yml +++ b/action.yml @@ -62,6 +62,10 @@ inputs: required: false default: "false" + max_turns: + description: "Maximum number of conversation turns" + required: false + default: "" timeout_minutes: description: "Timeout in minutes for execution" required: false @@ -96,6 +100,7 @@ runs: ASSIGNEE_TRIGGER: ${{ inputs.assignee_trigger }} BASE_BRANCH: ${{ inputs.base_branch }} ALLOWED_TOOLS: ${{ inputs.allowed_tools }} + DISALLOWED_TOOLS: ${{ inputs.disallowed_tools }} CUSTOM_INSTRUCTIONS: ${{ inputs.custom_instructions }} DIRECT_PROMPT: ${{ inputs.direct_prompt }} MCP_CONFIG: ${{ inputs.mcp_config }} @@ -105,12 +110,13 @@ runs: - name: Run Claude Code id: claude-code if: steps.prepare.outputs.contains_trigger == 'true' - uses: anthropics/claude-code-base-action@9e4e150978667888ba2108a2ee63a79bf9cfbe06 # v0.0.10 + uses: anthropics/claude-code-base-action@f382bd1ea00f26043eb461ebabebe0d850572a71 # v0.0.24 with: prompt_file: ${{ runner.temp }}/claude-prompts/claude-prompt.txt allowed_tools: ${{ env.ALLOWED_TOOLS }} disallowed_tools: ${{ env.DISALLOWED_TOOLS }} timeout_minutes: ${{ inputs.timeout_minutes }} + max_turns: ${{ inputs.max_turns }} model: ${{ inputs.model || inputs.anthropic_model }} mcp_config: ${{ steps.prepare.outputs.mcp_config }} use_bedrock: ${{ inputs.use_bedrock }} diff --git a/example-dispatch-workflow.yml b/example-dispatch-workflow.yml new file mode 100644 index 000000000..74cd95def --- /dev/null +++ b/example-dispatch-workflow.yml @@ -0,0 +1,73 @@ +name: Claude Task Executor + +on: + repository_dispatch: + types: [claude-task] + +permissions: + contents: write + pull-requests: write + issues: write + id-token: write # Required for OIDC authentication + +jobs: + execute-claude-task: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Execute Claude Task + uses: anthropics/claude-code-action@main + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + # Base branch for creating task branches + base_branch: main + # Optional: Custom instructions for Claude + custom_instructions: | + Follow the CLAUDE.md guidelines strictly. + Commit changes with descriptive messages. + # Optional: Tool restrictions + allowed_tools: | + file_editor + bash_command + github_comment + mcp__github__create_or_update_file + # Optional: Anthropic API configuration + anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }} + # Or use AWS Bedrock + # aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }} + # aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + # aws_region: us-east-1 + # Or use Google Vertex AI + # google_credentials: ${{ secrets.GOOGLE_CREDENTIALS }} + # vertex_project: my-project + # vertex_location: us-central1 +# Example: Triggering this workflow from another service +# +# curl -X POST \ +# https://api.github.com/repos/owner/repo/dispatches \ +# -H "Authorization: token $GITHUB_TOKEN" \ +# -H "Accept: application/vnd.github.v3+json" \ +# -d '{ +# "event_type": "claude-task", +# "client_payload": { +# "description": "Analyze the codebase and create a comprehensive test suite for the authentication module", +# "progress_endpoint": "https://api.example.com/claude/progress", +# "correlation_id": "task-auth-tests-2024-01-17" +# } +# }' +# +# The progress_endpoint will receive POST requests with: +# { +# "repository": "owner/repo", +# "run_id": "123456789", +# "correlation_id": "task-auth-tests-2024-01-17", +# "status": "in_progress" | "completed" | "failed", +# "message": "Current progress description", +# "completed_tasks": ["task1", "task2"], +# "current_task": "Working on task3", +# "timestamp": "2024-01-17T12:00:00Z" +# } +# +# Authentication: Progress updates include a GitHub OIDC token in the Authorization header diff --git a/examples/claude-auto-review.yml b/examples/claude-auto-review.yml index bf1bfa510..0b2e0ba4f 100644 --- a/examples/claude-auto-review.yml +++ b/examples/claude-auto-review.yml @@ -35,4 +35,4 @@ jobs: Provide constructive feedback with specific suggestions for improvement. Use inline comments to highlight specific areas of concern. - # allowed_tools: "mcp__github__add_pull_request_review_comment" + # allowed_tools: "mcp__github__create_pending_pull_request_review,mcp__github__add_pull_request_review_comment_to_pending_review,mcp__github__submit_pending_pull_request_review,mcp__github__get_pull_request_diff" diff --git a/pr-summary.md b/pr-summary.md new file mode 100644 index 000000000..08306497f --- /dev/null +++ b/pr-summary.md @@ -0,0 +1,118 @@ +## Summary + +Adds support for `repository_dispatch` events, enabling backend services to programmatically trigger Claude to perform tasks and receive progress updates via API. + +## Architecture + +```mermaid +sequenceDiagram + participant Backend as Backend Service + participant GH as GitHub + participant Action as Claude Action + participant Claude as Claude + participant MCP as Progress MCP Server + participant API as Progress API + + Backend->>GH: POST /repos/{owner}/{repo}/dispatches + Note over Backend,GH: Payload includes:
- description (task)
- progress_endpoint
- correlation_id + + GH->>Action: Trigger workflow
(repository_dispatch) + + Action->>Action: Parse dispatch payload + Note over Action: Extract task description,
endpoint, correlation_id + + Action->>MCP: Install Progress Server + Note over MCP: Configure with:
- PROGRESS_ENDPOINT
- CORRELATION_ID
- GITHUB_RUN_ID + + Action->>Claude: Execute task with
MCP tools available + + loop Task Execution + Claude->>MCP: update_claude_progress() + MCP->>MCP: Get OIDC token + MCP->>API: POST progress update + Note over API: Payload includes:
- correlation_id
- status
- message
- completed_tasks + API->>Backend: Forward update + end + + Claude->>Action: Task complete + Action->>GH: Commit changes +``` + +## Key Features + +### 1. Repository Dispatch Support + +- New event handler for `repository_dispatch` events +- Extracts task description, progress endpoint, and correlation ID from `client_payload` +- Bypasses GitHub UI interaction for fully programmatic operation + +### 2. Progress Reporting MCP Server + +- New MCP server (`progress-server.ts`) for sending progress updates +- OIDC authentication for secure API communication +- Includes correlation ID in all updates for request tracking + +### 3. Simplified Dispatch Prompts + +- Focused instructions for dispatch events (no PR/issue context) +- Clear directives: answer questions or implement changes +- Automatic progress updates at start and completion + +## Implementation Details + +### Triggering a Dispatch + +```bash +curl -X POST \ + https://api.github.com/repos/{owner}/{repo}/dispatches \ + -H "Authorization: token $GITHUB_TOKEN" \ + -H "Accept: application/vnd.github.v3+json" \ + -d '{ + "event_type": "claude-task", + "client_payload": { + "description": "Implement a new feature that...", + "progress_endpoint": "https://api.example.com/progress", + "correlation_id": "req-123-abc" + } + }' +``` + +### Progress Update Payload + +```json +{ + "repository": "owner/repo", + "run_id": "123456789", + "correlation_id": "req-123-abc", + "status": "in_progress", + "message": "Implementing feature...", + "completed_tasks": ["Setup environment", "Created base structure"], + "current_task": "Writing tests", + "timestamp": "2024-01-17T12:00:00Z" +} +``` + +## Security + +- **OIDC Authentication**: All progress updates use GitHub OIDC tokens +- **Correlation IDs**: Included in request body (not URL) for security +- **Endpoint Validation**: Progress endpoint must be explicitly provided +- **No Credential Storage**: Tokens are generated per-request + +## Testing + +To test the repository_dispatch flow: + +1. Configure workflow with `repository_dispatch` trigger +2. Send dispatch event with required payload +3. Monitor GitHub Actions logs for execution +4. Verify progress updates at configured endpoint + +## Changes + +- Added `repository_dispatch` event handling in `context.ts` +- Created new `progress-server.ts` MCP server +- Updated `isDispatch` flag across all event types +- Modified prompt generation for dispatch events +- Made `githubData` optional for dispatch workflows +- Added correlation ID support throughout the pipeline diff --git a/src/create-prompt/index.ts b/src/create-prompt/index.ts index 4a9f17b2b..d498cf9b5 100644 --- a/src/create-prompt/index.ts +++ b/src/create-prompt/index.ts @@ -24,6 +24,7 @@ export type { CommonFields, PreparedContext } from "./types"; const BASE_ALLOWED_TOOLS = [ "Edit", + "MultiEdit", "Glob", "Grep", "LS", @@ -417,6 +418,7 @@ ${ } ${context.claudeCommentId} ${context.triggerUsername ?? "Unknown"} +${githubData.triggerDisplayName ?? context.triggerUsername ?? "Unknown"} ${context.triggerPhrase} ${ (eventData.eventName === "issue_comment" || @@ -502,12 +504,14 @@ ${context.directPrompt ? ` - DIRECT INSTRUCTION: A direct instruction was prov ? ` - Push directly using mcp__github_file_ops__commit_files to the existing branch (works for both new and existing files). - Use mcp__github_file_ops__commit_files to commit files atomically in a single commit (supports single or multiple files). - - When pushing changes with this tool and TRIGGER_USERNAME is not "Unknown", include a "Co-authored-by: ${context.triggerUsername} <${context.triggerUsername}@users.noreply.github.com>" line in the commit message.` + - When pushing changes with this tool and the trigger user is not "Unknown", include a Co-authored-by trailer in the commit message. + - Use: "Co-authored-by: ${githubData.triggerDisplayName ?? context.triggerUsername} <${context.triggerUsername}@users.noreply.github.com>"` : ` - You are already on the correct branch (${eventData.claudeBranch || "the PR branch"}). Do not create a new branch. - Push changes directly to the current branch using mcp__github_file_ops__commit_files (works for both new and existing files) - Use mcp__github_file_ops__commit_files to commit files atomically in a single commit (supports single or multiple files). - - When pushing changes and TRIGGER_USERNAME is not "Unknown", include a "Co-authored-by: ${context.triggerUsername} <${context.triggerUsername}@users.noreply.github.com>" line in the commit message. + - When pushing changes and the trigger user is not "Unknown", include a Co-authored-by trailer in the commit message. + - Use: "Co-authored-by: ${githubData.triggerDisplayName ?? context.triggerUsername} <${context.triggerUsername}@users.noreply.github.com>" ${ eventData.claudeBranch ? `- Provide a URL to create a PR manually in this format: diff --git a/src/entrypoints/prepare.ts b/src/entrypoints/prepare.ts index 6b240d8ab..f8b5dc2af 100644 --- a/src/entrypoints/prepare.ts +++ b/src/entrypoints/prepare.ts @@ -59,6 +59,7 @@ async function run() { repository: `${context.repository.owner}/${context.repository.repo}`, prNumber: context.entityNumber.toString(), isPR: context.isPR, + triggerUsername: context.actor, }); // Step 8: Setup branch diff --git a/src/github/api/queries/github.ts b/src/github/api/queries/github.ts index 20b5db9c4..e0e4c259d 100644 --- a/src/github/api/queries/github.ts +++ b/src/github/api/queries/github.ts @@ -104,3 +104,11 @@ export const ISSUE_QUERY = ` } } `; + +export const USER_QUERY = ` + query($login: String!) { + user(login: $login) { + name + } + } +`; diff --git a/src/github/context.ts b/src/github/context.ts index 1e193038b..f0e81b598 100644 --- a/src/github/context.ts +++ b/src/github/context.ts @@ -1,6 +1,7 @@ import * as github from "@actions/github"; import type { IssuesEvent, + IssuesAssignedEvent, IssueCommentEvent, PullRequestEvent, PullRequestReviewEvent, @@ -52,14 +53,8 @@ export function parseGitHubContext(): ParsedGitHubContext { inputs: { triggerPhrase: process.env.TRIGGER_PHRASE ?? "@claude", assigneeTrigger: process.env.ASSIGNEE_TRIGGER ?? "", - allowedTools: (process.env.ALLOWED_TOOLS ?? "") - .split(",") - .map((tool) => tool.trim()) - .filter((tool) => tool.length > 0), - disallowedTools: (process.env.DISALLOWED_TOOLS ?? "") - .split(",") - .map((tool) => tool.trim()) - .filter((tool) => tool.length > 0), + allowedTools: parseMultilineInput(process.env.ALLOWED_TOOLS ?? ""), + disallowedTools: parseMultilineInput(process.env.DISALLOWED_TOOLS ?? ""), customInstructions: process.env.CUSTOM_INSTRUCTIONS ?? "", directPrompt: process.env.DIRECT_PROMPT ?? "", baseBranch: process.env.BASE_BRANCH, @@ -116,6 +111,14 @@ export function parseGitHubContext(): ParsedGitHubContext { } } +export function parseMultilineInput(s: string): string[] { + return s + .split(/,|[\n\r]+/) + .map((tool) => tool.replace(/#.+$/, "")) + .map((tool) => tool.trim()) + .filter((tool) => tool.length > 0); +} + export function isIssuesEvent( context: ParsedGitHubContext, ): context is ParsedGitHubContext & { payload: IssuesEvent } { @@ -145,3 +148,9 @@ export function isPullRequestReviewCommentEvent( ): context is ParsedGitHubContext & { payload: PullRequestReviewCommentEvent } { return context.eventName === "pull_request_review_comment"; } + +export function isIssuesAssignedEvent( + context: ParsedGitHubContext, +): context is ParsedGitHubContext & { payload: IssuesAssignedEvent } { + return isIssuesEvent(context) && context.eventAction === "assigned"; +} diff --git a/src/github/data/fetcher.ts b/src/github/data/fetcher.ts index a5b0b0ae3..b1dc26d39 100644 --- a/src/github/data/fetcher.ts +++ b/src/github/data/fetcher.ts @@ -1,6 +1,6 @@ import { execSync } from "child_process"; import type { Octokits } from "../api/client"; -import { ISSUE_QUERY, PR_QUERY } from "../api/queries/github"; +import { ISSUE_QUERY, PR_QUERY, USER_QUERY } from "../api/queries/github"; import type { GitHubComment, GitHubFile, @@ -18,6 +18,7 @@ type FetchDataParams = { repository: string; prNumber: string; isPR: boolean; + triggerUsername?: string; }; export type GitHubFileWithSHA = GitHubFile & { @@ -31,6 +32,7 @@ export type FetchDataResult = { changedFilesWithSHA: GitHubFileWithSHA[]; reviewData: { nodes: GitHubReview[] } | null; imageUrlMap: Map; + triggerDisplayName?: string | null; }; export async function fetchGitHubData({ @@ -38,6 +40,7 @@ export async function fetchGitHubData({ repository, prNumber, isPR, + triggerUsername, }: FetchDataParams): Promise { const [owner, repo] = repository.split("/"); if (!owner || !repo) { @@ -191,6 +194,12 @@ export async function fetchGitHubData({ allComments, ); + // Fetch trigger user display name if username is provided + let triggerDisplayName: string | null | undefined; + if (triggerUsername) { + triggerDisplayName = await fetchUserDisplayName(octokits, triggerUsername); + } + return { contextData, comments, @@ -198,5 +207,27 @@ export async function fetchGitHubData({ changedFilesWithSHA, reviewData, imageUrlMap, + triggerDisplayName, + }; +} + +export type UserQueryResponse = { + user: { + name: string | null; }; +}; + +export async function fetchUserDisplayName( + octokits: Octokits, + login: string, +): Promise { + try { + const result = await octokits.graphql(USER_QUERY, { + login, + }); + return result.user.name; + } catch (error) { + console.warn(`Failed to fetch user display name for ${login}:`, error); + return null; + } } diff --git a/src/github/operations/branch.ts b/src/github/operations/branch.ts index 337964885..f0b1a959b 100644 --- a/src/github/operations/branch.ts +++ b/src/github/operations/branch.ts @@ -45,9 +45,16 @@ export async function setupBranch( const branchName = prData.headRefName; - // Execute git commands to checkout PR branch (shallow fetch for performance) - // Fetch the branch with a depth of 20 to avoid fetching too much history, while still allowing for some context - await $`git fetch origin --depth=20 ${branchName}`; + // Determine optimal fetch depth based on PR commit count, with a minimum of 20 + const commitCount = prData.commits.totalCount; + const fetchDepth = Math.max(commitCount, 20); + + console.log( + `PR #${entityNumber}: ${commitCount} commits, using fetch depth ${fetchDepth}`, + ); + + // Execute git commands to checkout PR branch (dynamic depth based on PR size) + await $`git fetch origin --depth=${fetchDepth} ${branchName}`; await $`git checkout ${branchName}`; console.log(`Successfully checked out PR branch for PR #${entityNumber}`); diff --git a/src/github/types.ts b/src/github/types.ts index 28c4aa170..c46c29f8c 100644 --- a/src/github/types.ts +++ b/src/github/types.ts @@ -1,6 +1,7 @@ // Types for GitHub GraphQL query responses export type GitHubAuthor = { login: string; + name?: string; }; export type GitHubComment = { diff --git a/src/github/validation/trigger.ts b/src/github/validation/trigger.ts index 6a06153a8..40ee933fc 100644 --- a/src/github/validation/trigger.ts +++ b/src/github/validation/trigger.ts @@ -3,6 +3,7 @@ import * as core from "@actions/core"; import { isIssuesEvent, + isIssuesAssignedEvent, isIssueCommentEvent, isPullRequestEvent, isPullRequestReviewEvent, @@ -22,10 +23,10 @@ export function checkContainsTrigger(context: ParsedGitHubContext): boolean { } // Check for assignee trigger - if (isIssuesEvent(context) && context.eventAction === "assigned") { + if (isIssuesAssignedEvent(context)) { // Remove @ symbol from assignee_trigger if present let triggerUser = assigneeTrigger.replace(/^@/, ""); - const assigneeUsername = context.payload.issue.assignee?.login || ""; + const assigneeUsername = context.payload.assignee?.login || ""; if (triggerUser && assigneeUsername === triggerUser) { console.log(`Issue assigned to trigger user '${triggerUser}'`); diff --git a/src/mcp/github-file-ops-server.ts b/src/mcp/github-file-ops-server.ts index a34f11595..9a769af1a 100644 --- a/src/mcp/github-file-ops-server.ts +++ b/src/mcp/github-file-ops-server.ts @@ -466,6 +466,7 @@ server.tool( const octokit = new Octokit({ auth: githubToken, + baseUrl: GITHUB_API_URL, }); const isPullRequestReviewComment = diff --git a/src/mcp/install-mcp-server.ts b/src/mcp/install-mcp-server.ts index 0eba6af54..8748f67d7 100644 --- a/src/mcp/install-mcp-server.ts +++ b/src/mcp/install-mcp-server.ts @@ -1,4 +1,5 @@ import * as core from "@actions/core"; +import { GITHUB_API_URL } from "../github/api/config"; type PrepareConfigParams = { githubToken: string; @@ -46,6 +47,7 @@ export async function prepareMcpConfig( ...(claudeCommentId && { CLAUDE_COMMENT_ID: claudeCommentId }), GITHUB_EVENT_NAME: process.env.GITHUB_EVENT_NAME || "", IS_PR: process.env.IS_PR || "false", + GITHUB_API_URL: GITHUB_API_URL, }, }, }, @@ -60,7 +62,7 @@ export async function prepareMcpConfig( "--rm", "-e", "GITHUB_PERSONAL_ACCESS_TOKEN", - "ghcr.io/github/github-mcp-server:sha-e9f748f", // https://github.com/github/github-mcp-server/releases/tag/v0.4.0 + "ghcr.io/github/github-mcp-server:sha-6d69797", // https://github.com/github/github-mcp-server/releases/tag/v0.5.0 ], env: { GITHUB_PERSONAL_ACCESS_TOKEN: githubToken, diff --git a/test/create-prompt.test.ts b/test/create-prompt.test.ts index 65c5625c2..472ff65ba 100644 --- a/test/create-prompt.test.ts +++ b/test/create-prompt.test.ts @@ -316,7 +316,7 @@ describe("generatePrompt", () => { expect(prompt).toContain("johndoe"); expect(prompt).toContain( - "Co-authored-by: johndoe ", + 'Use: "Co-authored-by: johndoe "', ); }); diff --git a/test/github/context.test.ts b/test/github/context.test.ts new file mode 100644 index 000000000..bfdf0265d --- /dev/null +++ b/test/github/context.test.ts @@ -0,0 +1,57 @@ +import { describe, it, expect } from "bun:test"; +import { parseMultilineInput } from "../../src/github/context"; + +describe("parseMultilineInput", () => { + it("should parse a comma-separated string", () => { + const input = `Bash(bun install),Bash(bun test:*),Bash(bun typecheck)`; + const result = parseMultilineInput(input); + expect(result).toEqual([ + "Bash(bun install)", + "Bash(bun test:*)", + "Bash(bun typecheck)", + ]); + }); + + it("should parse multiline string", () => { + const input = `Bash(bun install) +Bash(bun test:*) +Bash(bun typecheck)`; + const result = parseMultilineInput(input); + expect(result).toEqual([ + "Bash(bun install)", + "Bash(bun test:*)", + "Bash(bun typecheck)", + ]); + }); + + it("should parse comma-separated multiline line", () => { + const input = `Bash(bun install),Bash(bun test:*) +Bash(bun typecheck)`; + const result = parseMultilineInput(input); + expect(result).toEqual([ + "Bash(bun install)", + "Bash(bun test:*)", + "Bash(bun typecheck)", + ]); + }); + + it("should ignore comments", () => { + const input = `Bash(bun install), +Bash(bun test:*) # For testing +# For type checking +Bash(bun typecheck) +`; + const result = parseMultilineInput(input); + expect(result).toEqual([ + "Bash(bun install)", + "Bash(bun test:*)", + "Bash(bun typecheck)", + ]); + }); + + it("should parse an empty string", () => { + const input = ""; + const result = parseMultilineInput(input); + expect(result).toEqual([]); + }); +}); diff --git a/test/mockContext.ts b/test/mockContext.ts index 692137cc9..65250c138 100644 --- a/test/mockContext.ts +++ b/test/mockContext.ts @@ -91,6 +91,12 @@ export const mockIssueAssignedContext: ParsedGitHubContext = { actor: "admin-user", payload: { action: "assigned", + assignee: { + login: "claude-bot", + id: 11111, + avatar_url: "https://avatars.githubusercontent.com/u/11111", + html_url: "https://github.com/claude-bot", + }, issue: { number: 123, title: "Feature: Add dark mode support", diff --git a/test/trigger-validation.test.ts b/test/trigger-validation.test.ts index bbe40bde5..6c368b07e 100644 --- a/test/trigger-validation.test.ts +++ b/test/trigger-validation.test.ts @@ -87,6 +87,11 @@ describe("checkContainsTrigger", () => { ...mockIssueAssignedContext, payload: { ...mockIssueAssignedContext.payload, + assignee: { + ...(mockIssueAssignedContext.payload as IssuesAssignedEvent) + .assignee, + login: "otherUser", + }, issue: { ...(mockIssueAssignedContext.payload as IssuesAssignedEvent).issue, assignee: {