Compare commits

...

9 Commits

Author SHA1 Message Date
Claude
b868c24c84 feat: improve autofix suggestion detection for PR comments
Add detection logic to recognize actionable suggestions in PR comments, even when
they come from bot accounts like claude[bot]. This addresses the issue where the
autofix workflow incorrectly classified clear bug fix suggestions as not actionable.

New features:
- detectActionableSuggestion(): Analyzes comment body for actionable patterns
- Detects GitHub inline committable suggestions (```suggestion blocks)
- Detects clear bug fix language patterns (e.g., "should be", "change to", "replace with")
- Detects code alternatives and fix recommendations
- Returns confidence level (high/medium/low) and reason for the determination
- checkContainsActionableSuggestion(): Context-aware wrapper for GitHub events
- checkContainsTriggerOrActionableSuggestion(): Combined trigger and suggestion check
- checkIsActionableForAutofix(): Convenience function for autofix workflows
- extractSuggestionCode(): Extracts code from GitHub suggestion blocks

This enables workflows to automatically apply suggestions from code review comments,
improving the developer experience for PR reviews.
2026-01-12 03:04:17 +00:00
GitHub Actions
b6e5a9f27a chore: bump Claude Code to 2.1.4 and Agent SDK to 0.2.4 2026-01-11 00:27:43 +00:00
GitHub Actions
5d91d7d217 chore: bump Claude Code to 2.1.3 and Agent SDK to 0.2.3 2026-01-09 23:31:55 +00:00
GitHub Actions
90006bcae7 chore: bump Claude Code to 2.1.2 and Agent SDK to 0.2.2 2026-01-09 00:03:55 +00:00
Alexander Bartash
005436f51d fix: parse ALL --allowed-tools flags, not just the first one (#801)
The parseAllowedTools() function previously used .match() which only
returns the first match. This caused tools specified in subsequent
--allowed-tools flags to be ignored during MCP server initialization.

Changes:
- Add /g flag to regex patterns for global matching
- Use matchAll() to find all occurrences
- Deduplicate tools while preserving order
- Make unquoted pattern not match quoted values

Fixes #800

 #vibe

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-09 01:36:12 +05:30
Ashwin Bhat
1b8ee3b941 fix: add missing import and update tests for branch template feature (#799)
* fix: add missing import and update tests for branch template feature

- Add missing `import { $ } from 'bun'` in branch.ts
- Add missing `labels` property to pull-request-target.test.ts fixture
- Update branch-template tests to expect 5-word descriptions

* address review feedback: update comment and add truncation test
2026-01-08 07:07:54 +05:30
Cole D
c247cb152d feat: custom branch name templates (#571)
* Add branch-name-template config option

* Logging

* Use branch name template

* Add label to template variables

* Add description template variable

* More concise description for branch_name_template

* Remove more granular time template variables

* Only fetch first label

* Add check for empty template-generated name

* Clean up comments, docstrings

* Merge createBranchTemplateVariables into generateBranchName

* Still replace undefined values

* Fall back to default on duplicate branch

* Parameterize description wordcount

* Remove some over-explanatory comments

* NUM_DESCRIPTION_WORDS: 3 -> 5
2026-01-08 06:47:26 +05:30
GitHub Actions
cefa60067a chore: bump Claude Code to 2.1.1 and Agent SDK to 0.2.1 2026-01-07 21:30:16 +00:00
GitHub Actions
7a708f68fa chore: bump Claude Code to 2.1.0 and Agent SDK to 0.2.0 2026-01-07 20:03:23 +00:00
20 changed files with 1111 additions and 34 deletions

View File

@@ -23,6 +23,10 @@ inputs:
description: "The prefix to use for Claude branches (defaults to 'claude/', use 'claude-' for dash format)" description: "The prefix to use for Claude branches (defaults to 'claude/', use 'claude-' for dash format)"
required: false required: false
default: "claude/" default: "claude/"
branch_name_template:
description: "Template for branch naming. Available variables: {{prefix}}, {{entityType}}, {{entityNumber}}, {{timestamp}}, {{sha}}, {{label}}, {{description}}. {{label}} will be first label from the issue/PR, or {{entityType}} as a fallback. {{description}} will be the first 5 words of the issue/PR title in kebab-case. Default: '{{prefix}}{{entityType}}-{{entityNumber}}-{{timestamp}}'"
required: false
default: ""
allowed_bots: allowed_bots:
description: "Comma-separated list of allowed bot usernames, or '*' to allow all bots. Empty string (default) allows no bots." description: "Comma-separated list of allowed bot usernames, or '*' to allow all bots. Empty string (default) allows no bots."
required: false required: false
@@ -178,6 +182,7 @@ runs:
LABEL_TRIGGER: ${{ inputs.label_trigger }} LABEL_TRIGGER: ${{ inputs.label_trigger }}
BASE_BRANCH: ${{ inputs.base_branch }} BASE_BRANCH: ${{ inputs.base_branch }}
BRANCH_PREFIX: ${{ inputs.branch_prefix }} BRANCH_PREFIX: ${{ inputs.branch_prefix }}
BRANCH_NAME_TEMPLATE: ${{ inputs.branch_name_template }}
OVERRIDE_GITHUB_TOKEN: ${{ inputs.github_token }} OVERRIDE_GITHUB_TOKEN: ${{ inputs.github_token }}
ALLOWED_BOTS: ${{ inputs.allowed_bots }} ALLOWED_BOTS: ${{ inputs.allowed_bots }}
ALLOWED_NON_WRITE_USERS: ${{ inputs.allowed_non_write_users }} ALLOWED_NON_WRITE_USERS: ${{ inputs.allowed_non_write_users }}
@@ -208,7 +213,7 @@ runs:
# Install Claude Code if no custom executable is provided # Install Claude Code if no custom executable is provided
if [ -z "$PATH_TO_CLAUDE_CODE_EXECUTABLE" ]; then if [ -z "$PATH_TO_CLAUDE_CODE_EXECUTABLE" ]; then
CLAUDE_CODE_VERSION="2.0.76" CLAUDE_CODE_VERSION="2.1.4"
echo "Installing Claude Code v${CLAUDE_CODE_VERSION}..." echo "Installing Claude Code v${CLAUDE_CODE_VERSION}..."
for attempt in 1 2 3; do for attempt in 1 2 3; do
echo "Installation attempt $attempt..." echo "Installation attempt $attempt..."

View File

@@ -124,7 +124,7 @@ runs:
PATH_TO_CLAUDE_CODE_EXECUTABLE: ${{ inputs.path_to_claude_code_executable }} PATH_TO_CLAUDE_CODE_EXECUTABLE: ${{ inputs.path_to_claude_code_executable }}
run: | run: |
if [ -z "$PATH_TO_CLAUDE_CODE_EXECUTABLE" ]; then if [ -z "$PATH_TO_CLAUDE_CODE_EXECUTABLE" ]; then
CLAUDE_CODE_VERSION="2.0.76" CLAUDE_CODE_VERSION="2.1.4"
echo "Installing Claude Code v${CLAUDE_CODE_VERSION}..." echo "Installing Claude Code v${CLAUDE_CODE_VERSION}..."
for attempt in 1 2 3; do for attempt in 1 2 3; do
echo "Installation attempt $attempt..." echo "Installation attempt $attempt..."

View File

@@ -6,7 +6,7 @@
"name": "@anthropic-ai/claude-code-base-action", "name": "@anthropic-ai/claude-code-base-action",
"dependencies": { "dependencies": {
"@actions/core": "^1.10.1", "@actions/core": "^1.10.1",
"@anthropic-ai/claude-agent-sdk": "^0.1.76", "@anthropic-ai/claude-agent-sdk": "^0.2.4",
"shell-quote": "^1.8.3", "shell-quote": "^1.8.3",
}, },
"devDependencies": { "devDependencies": {
@@ -27,7 +27,7 @@
"@actions/io": ["@actions/io@1.1.3", "", {}, "sha512-wi9JjgKLYS7U/z8PPbco+PvTb/nRWjeoFlJ1Qer83k/3C5PHQi28hiVdeE2kHXmIL99mQFawx8qt/JPjZilJ8Q=="], "@actions/io": ["@actions/io@1.1.3", "", {}, "sha512-wi9JjgKLYS7U/z8PPbco+PvTb/nRWjeoFlJ1Qer83k/3C5PHQi28hiVdeE2kHXmIL99mQFawx8qt/JPjZilJ8Q=="],
"@anthropic-ai/claude-agent-sdk": ["@anthropic-ai/claude-agent-sdk@0.1.76", "", { "optionalDependencies": { "@img/sharp-darwin-arm64": "^0.33.5", "@img/sharp-darwin-x64": "^0.33.5", "@img/sharp-linux-arm": "^0.33.5", "@img/sharp-linux-arm64": "^0.33.5", "@img/sharp-linux-x64": "^0.33.5", "@img/sharp-linuxmusl-arm64": "^0.33.5", "@img/sharp-linuxmusl-x64": "^0.33.5", "@img/sharp-win32-x64": "^0.33.5" }, "peerDependencies": { "zod": "^3.24.1 || ^4.0.0" } }, "sha512-s7RvpXoFaLXLG7A1cJBAPD8ilwOhhc/12fb5mJXRuD561o4FmPtQ+WRfuy9akMmrFRfLsKv8Ornw3ClGAPL2fw=="], "@anthropic-ai/claude-agent-sdk": ["@anthropic-ai/claude-agent-sdk@0.2.4", "", { "optionalDependencies": { "@img/sharp-darwin-arm64": "^0.33.5", "@img/sharp-darwin-x64": "^0.33.5", "@img/sharp-linux-arm": "^0.33.5", "@img/sharp-linux-arm64": "^0.33.5", "@img/sharp-linux-x64": "^0.33.5", "@img/sharp-linuxmusl-arm64": "^0.33.5", "@img/sharp-linuxmusl-x64": "^0.33.5", "@img/sharp-win32-x64": "^0.33.5" }, "peerDependencies": { "zod": "^4.0.0" } }, "sha512-5RpMO8aLEwuAd8h7/QHMCKzdVSihZCtHGnouPp+Isvc7zPzQXKb6GvUitkbs3wIBgIbXA/vXQmIi126uw9qo0A=="],
"@fastify/busboy": ["@fastify/busboy@2.1.1", "", {}, "sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA=="], "@fastify/busboy": ["@fastify/busboy@2.1.1", "", {}, "sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA=="],

View File

@@ -11,7 +11,7 @@
}, },
"dependencies": { "dependencies": {
"@actions/core": "^1.10.1", "@actions/core": "^1.10.1",
"@anthropic-ai/claude-agent-sdk": "^0.1.76", "@anthropic-ai/claude-agent-sdk": "^0.2.4",
"shell-quote": "^1.8.3" "shell-quote": "^1.8.3"
}, },
"devDependencies": { "devDependencies": {

View File

@@ -7,7 +7,7 @@
"dependencies": { "dependencies": {
"@actions/core": "^1.10.1", "@actions/core": "^1.10.1",
"@actions/github": "^6.0.1", "@actions/github": "^6.0.1",
"@anthropic-ai/claude-agent-sdk": "^0.1.76", "@anthropic-ai/claude-agent-sdk": "^0.2.4",
"@modelcontextprotocol/sdk": "^1.11.0", "@modelcontextprotocol/sdk": "^1.11.0",
"@octokit/graphql": "^8.2.2", "@octokit/graphql": "^8.2.2",
"@octokit/rest": "^21.1.1", "@octokit/rest": "^21.1.1",
@@ -37,7 +37,7 @@
"@actions/io": ["@actions/io@1.1.3", "", {}, "sha512-wi9JjgKLYS7U/z8PPbco+PvTb/nRWjeoFlJ1Qer83k/3C5PHQi28hiVdeE2kHXmIL99mQFawx8qt/JPjZilJ8Q=="], "@actions/io": ["@actions/io@1.1.3", "", {}, "sha512-wi9JjgKLYS7U/z8PPbco+PvTb/nRWjeoFlJ1Qer83k/3C5PHQi28hiVdeE2kHXmIL99mQFawx8qt/JPjZilJ8Q=="],
"@anthropic-ai/claude-agent-sdk": ["@anthropic-ai/claude-agent-sdk@0.1.76", "", { "optionalDependencies": { "@img/sharp-darwin-arm64": "^0.33.5", "@img/sharp-darwin-x64": "^0.33.5", "@img/sharp-linux-arm": "^0.33.5", "@img/sharp-linux-arm64": "^0.33.5", "@img/sharp-linux-x64": "^0.33.5", "@img/sharp-linuxmusl-arm64": "^0.33.5", "@img/sharp-linuxmusl-x64": "^0.33.5", "@img/sharp-win32-x64": "^0.33.5" }, "peerDependencies": { "zod": "^3.24.1 || ^4.0.0" } }, "sha512-s7RvpXoFaLXLG7A1cJBAPD8ilwOhhc/12fb5mJXRuD561o4FmPtQ+WRfuy9akMmrFRfLsKv8Ornw3ClGAPL2fw=="], "@anthropic-ai/claude-agent-sdk": ["@anthropic-ai/claude-agent-sdk@0.2.4", "", { "optionalDependencies": { "@img/sharp-darwin-arm64": "^0.33.5", "@img/sharp-darwin-x64": "^0.33.5", "@img/sharp-linux-arm": "^0.33.5", "@img/sharp-linux-arm64": "^0.33.5", "@img/sharp-linux-x64": "^0.33.5", "@img/sharp-linuxmusl-arm64": "^0.33.5", "@img/sharp-linuxmusl-x64": "^0.33.5", "@img/sharp-win32-x64": "^0.33.5" }, "peerDependencies": { "zod": "^4.0.0" } }, "sha512-5RpMO8aLEwuAd8h7/QHMCKzdVSihZCtHGnouPp+Isvc7zPzQXKb6GvUitkbs3wIBgIbXA/vXQmIi126uw9qo0A=="],
"@fastify/busboy": ["@fastify/busboy@2.1.1", "", {}, "sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA=="], "@fastify/busboy": ["@fastify/busboy@2.1.1", "", {}, "sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA=="],

View File

@@ -12,7 +12,7 @@
"dependencies": { "dependencies": {
"@actions/core": "^1.10.1", "@actions/core": "^1.10.1",
"@actions/github": "^6.0.1", "@actions/github": "^6.0.1",
"@anthropic-ai/claude-agent-sdk": "^0.1.76", "@anthropic-ai/claude-agent-sdk": "^0.2.4",
"@modelcontextprotocol/sdk": "^1.11.0", "@modelcontextprotocol/sdk": "^1.11.0",
"@octokit/graphql": "^8.2.2", "@octokit/graphql": "^8.2.2",
"@octokit/rest": "^21.1.1", "@octokit/rest": "^21.1.1",

View File

@@ -18,6 +18,11 @@ export const PR_QUERY = `
additions additions
deletions deletions
state state
labels(first: 1) {
nodes {
name
}
}
commits(first: 100) { commits(first: 100) {
totalCount totalCount
nodes { nodes {
@@ -101,6 +106,11 @@ export const ISSUE_QUERY = `
updatedAt updatedAt
lastEditedAt lastEditedAt
state state
labels(first: 1) {
nodes {
name
}
}
comments(first: 100) { comments(first: 100) {
nodes { nodes {
id id

View File

@@ -88,6 +88,7 @@ type BaseContext = {
labelTrigger: string; labelTrigger: string;
baseBranch?: string; baseBranch?: string;
branchPrefix: string; branchPrefix: string;
branchNameTemplate?: string;
useStickyComment: boolean; useStickyComment: boolean;
useCommitSigning: boolean; useCommitSigning: boolean;
sshSigningKey: string; sshSigningKey: string;
@@ -145,6 +146,7 @@ export function parseGitHubContext(): GitHubContext {
labelTrigger: process.env.LABEL_TRIGGER ?? "", labelTrigger: process.env.LABEL_TRIGGER ?? "",
baseBranch: process.env.BASE_BRANCH, baseBranch: process.env.BASE_BRANCH,
branchPrefix: process.env.BRANCH_PREFIX ?? "claude/", branchPrefix: process.env.BRANCH_PREFIX ?? "claude/",
branchNameTemplate: process.env.BRANCH_NAME_TEMPLATE,
useStickyComment: process.env.USE_STICKY_COMMENT === "true", useStickyComment: process.env.USE_STICKY_COMMENT === "true",
useCommitSigning: process.env.USE_COMMIT_SIGNING === "true", useCommitSigning: process.env.USE_COMMIT_SIGNING === "true",
sshSigningKey: process.env.SSH_SIGNING_KEY || "", sshSigningKey: process.env.SSH_SIGNING_KEY || "",

View File

@@ -6,12 +6,22 @@
* - For Issues: Create a new branch * - For Issues: Create a new branch
*/ */
import { $ } from "bun";
import { execFileSync } from "child_process"; import { execFileSync } from "child_process";
import * as core from "@actions/core"; import * as core from "@actions/core";
import type { ParsedGitHubContext } from "../context"; import type { ParsedGitHubContext } from "../context";
import type { GitHubPullRequest } from "../types"; import type { GitHubPullRequest } from "../types";
import type { Octokits } from "../api/client"; import type { Octokits } from "../api/client";
import type { FetchDataResult } from "../data/fetcher"; import type { FetchDataResult } from "../data/fetcher";
import { generateBranchName } from "../../utils/branch-template";
/**
* Extracts the first label from GitHub data, or returns undefined if no labels exist
*/
function extractFirstLabel(githubData: FetchDataResult): string | undefined {
const labels = githubData.contextData.labels?.nodes;
return labels && labels.length > 0 ? labels[0]?.name : undefined;
}
/** /**
* Validates a git branch name against a strict whitelist pattern. * Validates a git branch name against a strict whitelist pattern.
@@ -125,7 +135,7 @@ export async function setupBranch(
): Promise<BranchInfo> { ): Promise<BranchInfo> {
const { owner, repo } = context.repository; const { owner, repo } = context.repository;
const entityNumber = context.entityNumber; const entityNumber = context.entityNumber;
const { baseBranch, branchPrefix } = context.inputs; const { baseBranch, branchPrefix, branchNameTemplate } = context.inputs;
const isPR = context.isPR; const isPR = context.isPR;
if (isPR) { if (isPR) {
@@ -191,17 +201,8 @@ export async function setupBranch(
// Generate branch name for either an issue or closed/merged PR // Generate branch name for either an issue or closed/merged PR
const entityType = isPR ? "pr" : "issue"; const entityType = isPR ? "pr" : "issue";
// Create Kubernetes-compatible timestamp: lowercase, hyphens only, shorter format // Get the SHA of the source branch to use in template
const now = new Date(); let sourceSHA: string | undefined;
const timestamp = `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, "0")}${String(now.getDate()).padStart(2, "0")}-${String(now.getHours()).padStart(2, "0")}${String(now.getMinutes()).padStart(2, "0")}`;
// Ensure branch name is Kubernetes-compatible:
// - Lowercase only
// - Alphanumeric with hyphens
// - No underscores
// - Max 50 chars (to allow for prefixes)
const branchName = `${branchPrefix}${entityType}-${entityNumber}-${timestamp}`;
const newBranch = branchName.toLowerCase().substring(0, 50);
try { try {
// Get the SHA of the source branch to verify it exists // Get the SHA of the source branch to verify it exists
@@ -211,8 +212,46 @@ export async function setupBranch(
ref: `heads/${sourceBranch}`, ref: `heads/${sourceBranch}`,
}); });
const currentSHA = sourceBranchRef.data.object.sha; sourceSHA = sourceBranchRef.data.object.sha;
console.log(`Source branch SHA: ${currentSHA}`); console.log(`Source branch SHA: ${sourceSHA}`);
// Extract first label from GitHub data
const firstLabel = extractFirstLabel(githubData);
// Extract title from GitHub data
const title = githubData.contextData.title;
// Generate branch name using template or default format
let newBranch = generateBranchName(
branchNameTemplate,
branchPrefix,
entityType,
entityNumber,
sourceSHA,
firstLabel,
title,
);
// Check if generated branch already exists on remote
try {
await $`git ls-remote --exit-code origin refs/heads/${newBranch}`.quiet();
// If we get here, branch exists (exit code 0)
console.log(
`Branch '${newBranch}' already exists, falling back to default format`,
);
newBranch = generateBranchName(
undefined, // Force default template
branchPrefix,
entityType,
entityNumber,
sourceSHA,
firstLabel,
title,
);
} catch {
// Branch doesn't exist (non-zero exit code), continue with generated name
}
// For commit signing, defer branch creation to the file ops server // For commit signing, defer branch creation to the file ops server
if (context.inputs.useCommitSigning) { if (context.inputs.useCommitSigning) {

View File

@@ -63,6 +63,11 @@ export type GitHubPullRequest = {
additions: number; additions: number;
deletions: number; deletions: number;
state: string; state: string;
labels: {
nodes: Array<{
name: string;
}>;
};
commits: { commits: {
totalCount: number; totalCount: number;
nodes: Array<{ nodes: Array<{
@@ -88,6 +93,11 @@ export type GitHubIssue = {
updatedAt?: string; updatedAt?: string;
lastEditedAt?: string; lastEditedAt?: string;
state: string; state: string;
labels: {
nodes: Array<{
name: string;
}>;
};
comments: { comments: {
nodes: GitHubComment[]; nodes: GitHubComment[];
}; };

View File

@@ -10,6 +10,11 @@ import {
isPullRequestReviewCommentEvent, isPullRequestReviewCommentEvent,
} from "../context"; } from "../context";
import type { ParsedGitHubContext } from "../context"; import type { ParsedGitHubContext } from "../context";
import {
detectActionableSuggestion,
isCommentActionableForAutofix,
type ActionableSuggestionResult,
} from "../../utils/detect-actionable-suggestion";
export function checkContainsTrigger(context: ParsedGitHubContext): boolean { export function checkContainsTrigger(context: ParsedGitHubContext): boolean {
const { const {
@@ -146,3 +151,89 @@ export async function checkTriggerAction(context: ParsedGitHubContext) {
core.setOutput("contains_trigger", containsTrigger.toString()); core.setOutput("contains_trigger", containsTrigger.toString());
return containsTrigger; return containsTrigger;
} }
/**
* Checks if the context contains an actionable suggestion that can be automatically fixed.
* This is useful for autofix workflows that want to respond to code review suggestions,
* even when they come from bot accounts like claude[bot].
*
* @param context - The parsed GitHub context
* @returns Detection result with confidence level and reason
*/
export function checkContainsActionableSuggestion(
context: ParsedGitHubContext,
): ActionableSuggestionResult {
// Extract comment body based on event type
let commentBody: string | undefined;
if (isPullRequestReviewCommentEvent(context)) {
commentBody = context.payload.comment.body;
} else if (isIssueCommentEvent(context)) {
commentBody = context.payload.comment.body;
} else if (isPullRequestReviewEvent(context)) {
commentBody = context.payload.review.body ?? undefined;
}
return detectActionableSuggestion(commentBody);
}
/**
* Enhanced trigger check that also considers actionable suggestions.
* This function first checks for the standard trigger phrase, and if not found,
* optionally checks for actionable suggestions when `checkSuggestions` is true.
*
* @param context - The parsed GitHub context
* @param checkSuggestions - Whether to also check for actionable suggestions (default: false)
* @returns Whether the action should be triggered
*/
export function checkContainsTriggerOrActionableSuggestion(
context: ParsedGitHubContext,
checkSuggestions: boolean = false,
): boolean {
// First, check for standard trigger
if (checkContainsTrigger(context)) {
return true;
}
// If checkSuggestions is enabled, also check for actionable suggestions
if (checkSuggestions) {
const suggestionResult = checkContainsActionableSuggestion(context);
if (suggestionResult.isActionable) {
console.log(
`Comment contains actionable suggestion: ${suggestionResult.reason} (confidence: ${suggestionResult.confidence})`,
);
return true;
}
}
return false;
}
/**
* Checks if a PR comment is actionable for autofix purposes.
* This is a convenience function for workflows that want to automatically
* apply suggestions from code review comments.
*
* @param context - The parsed GitHub context
* @returns Whether the comment should be treated as actionable for autofix
*/
export function checkIsActionableForAutofix(
context: ParsedGitHubContext,
): boolean {
// Only applicable to PR review comment events
if (!isPullRequestReviewCommentEvent(context)) {
return false;
}
const commentBody = context.payload.comment.body;
const authorUsername = context.payload.comment.user?.login;
return isCommentActionableForAutofix(commentBody, authorUsername);
}
// Re-export the types and functions from the utility module for convenience
export {
detectActionableSuggestion,
isCommentActionableForAutofix,
type ActionableSuggestionResult,
} from "../../utils/detect-actionable-suggestion";

View File

@@ -1,22 +1,33 @@
export function parseAllowedTools(claudeArgs: string): string[] { export function parseAllowedTools(claudeArgs: string): string[] {
// Match --allowedTools or --allowed-tools followed by the value // Match --allowedTools or --allowed-tools followed by the value
// Handle both quoted and unquoted values // Handle both quoted and unquoted values
// Use /g flag to find ALL occurrences, not just the first one
const patterns = [ const patterns = [
/--(?:allowedTools|allowed-tools)\s+"([^"]+)"/, // Double quoted /--(?:allowedTools|allowed-tools)\s+"([^"]+)"/g, // Double quoted
/--(?:allowedTools|allowed-tools)\s+'([^']+)'/, // Single quoted /--(?:allowedTools|allowed-tools)\s+'([^']+)'/g, // Single quoted
/--(?:allowedTools|allowed-tools)\s+([^\s]+)/, // Unquoted /--(?:allowedTools|allowed-tools)\s+([^'"\s][^\s]*)/g, // Unquoted (must not start with quote)
]; ];
const tools: string[] = [];
const seen = new Set<string>();
for (const pattern of patterns) { for (const pattern of patterns) {
const match = claudeArgs.match(pattern); for (const match of claudeArgs.matchAll(pattern)) {
if (match && match[1]) { if (match[1]) {
// Don't return if the value starts with -- (another flag) // Don't add if the value starts with -- (another flag)
if (match[1].startsWith("--")) { if (match[1].startsWith("--")) {
return []; continue;
}
for (const tool of match[1].split(",")) {
const trimmed = tool.trim();
if (trimmed && !seen.has(trimmed)) {
seen.add(trimmed);
tools.push(trimmed);
}
}
} }
return match[1].split(",").map((t) => t.trim());
} }
} }
return []; return tools;
} }

View File

@@ -0,0 +1,99 @@
#!/usr/bin/env bun
/**
* Branch name template parsing and variable substitution utilities
*/
const NUM_DESCRIPTION_WORDS = 5;
/**
* Extracts the first 5 words from a title and converts them to kebab-case
*/
function extractDescription(
title: string,
numWords: number = NUM_DESCRIPTION_WORDS,
): string {
if (!title || title.trim() === "") {
return "";
}
return title
.trim()
.split(/\s+/)
.slice(0, numWords) // Only first `numWords` words
.join("-")
.toLowerCase()
.replace(/[^a-z0-9-]/g, "") // Remove non-alphanumeric except hyphens
.replace(/-+/g, "-") // Replace multiple hyphens with single
.replace(/^-|-$/g, ""); // Remove leading/trailing hyphens
}
export interface BranchTemplateVariables {
prefix: string;
entityType: string;
entityNumber: number;
timestamp: string;
sha?: string;
label?: string;
description?: string;
}
/**
* Replaces template variables in a branch name template
* Template format: {{variableName}}
*/
export function applyBranchTemplate(
template: string,
variables: BranchTemplateVariables,
): string {
let result = template;
// Replace each variable
Object.entries(variables).forEach(([key, value]) => {
const placeholder = `{{${key}}}`;
const replacement = value ? String(value) : "";
result = result.replaceAll(placeholder, replacement);
});
return result;
}
/**
* Generates a branch name from the provided `template` and set of `variables`. Uses a default format if the template is empty or produces an empty result.
*/
export function generateBranchName(
template: string | undefined,
branchPrefix: string,
entityType: string,
entityNumber: number,
sha?: string,
label?: string,
title?: string,
): string {
const now = new Date();
const variables: BranchTemplateVariables = {
prefix: branchPrefix,
entityType,
entityNumber,
timestamp: `${now.getFullYear()}${String(now.getMonth() + 1).padStart(2, "0")}${String(now.getDate()).padStart(2, "0")}-${String(now.getHours()).padStart(2, "0")}${String(now.getMinutes()).padStart(2, "0")}`,
sha: sha?.substring(0, 8), // First 8 characters of SHA
label: label || entityType, // Fall back to entityType if no label
description: title ? extractDescription(title) : undefined,
};
if (template?.trim()) {
const branchName = applyBranchTemplate(template, variables);
// Some templates could produce empty results- validate
if (branchName.trim().length > 0) return branchName;
console.log(
`Branch template '${template}' generated empty result, falling back to default format`,
);
}
const branchName = `${branchPrefix}${entityType}-${entityNumber}-${variables.timestamp}`;
// Kubernetes compatible: lowercase, max 50 chars, alphanumeric and hyphens only
return branchName.toLowerCase().substring(0, 50);
}

View File

@@ -0,0 +1,213 @@
#!/usr/bin/env bun
/**
* Detects if a PR comment contains actionable suggestions that can be automatically fixed.
*
* This module identifies:
* 1. GitHub inline committable suggestions (```suggestion blocks)
* 2. Clear bug fix suggestions with specific patterns
* 3. Code fix recommendations with explicit changes
*/
/**
* Patterns that indicate a comment contains a GitHub inline committable suggestion.
* These are code blocks that GitHub renders with a "Commit suggestion" button.
*/
const COMMITTABLE_SUGGESTION_PATTERN = /```suggestion\b[\s\S]*?```/i;
/**
* Patterns that indicate a clear, actionable bug fix suggestion.
* These phrases typically precede concrete fix recommendations.
*/
const BUG_FIX_PATTERNS = [
// Direct fix suggestions
/\bshould\s+(?:be|use|return|change\s+to)\b/i,
/\bchange\s+(?:this\s+)?to\b/i,
/\breplace\s+(?:this\s+)?with\b/i,
/\buse\s+(?:this\s+)?instead\b/i,
/\binstead\s+of\s+.*?,?\s*use\b/i,
// Bug identification with fix
/\b(?:bug|issue|error|problem):\s*.*(?:fix|change|update|replace)/i,
/\bfix(?:ed)?\s+by\s+(?:chang|replac|updat)/i,
/\bto\s+fix\s+(?:this|the)\b/i,
// Explicit code changes
/\bthe\s+(?:correct|proper|right)\s+(?:code|syntax|value|approach)\s+(?:is|would\s+be)\b/i,
/\bshould\s+(?:read|look\s+like)\b/i,
// Missing/wrong patterns
/\bmissing\s+(?:a\s+)?(?:semicolon|bracket|parenthesis|quote|import|return|await)\b/i,
/\bextra\s+(?:semicolon|bracket|parenthesis|quote)\b/i,
/\bwrong\s+(?:type|value|variable|import|parameter)\b/i,
/\btypo\s+(?:in|here)\b/i,
];
/**
* Patterns that suggest code alternatives (less strong than direct fixes but still actionable).
*/
const CODE_ALTERNATIVE_PATTERNS = [
/```[\w]*\n[\s\S]+?\n```/, // Any code block (might contain the fix)
/\b(?:try|consider)\s+(?:using|changing|replacing)\b/i,
/\bhere'?s?\s+(?:the|a)\s+(?:fix|solution|correction)\b/i,
/\b(?:correct|fixed|updated)\s+(?:version|code|implementation)\b/i,
];
export interface ActionableSuggestionResult {
/** Whether the comment contains an actionable suggestion */
isActionable: boolean;
/** Whether the comment contains a GitHub inline committable suggestion */
hasCommittableSuggestion: boolean;
/** Whether the comment contains clear bug fix language */
hasBugFixSuggestion: boolean;
/** Whether the comment contains code alternatives */
hasCodeAlternative: boolean;
/** Confidence level: 'high', 'medium', or 'low' */
confidence: "high" | "medium" | "low";
/** Reason for the determination */
reason: string;
}
/**
* Detects if a comment contains actionable suggestions that can be automatically fixed.
*
* @param commentBody - The body of the PR comment to analyze
* @returns Object with detection results and confidence level
*
* @example
* ```ts
* const result = detectActionableSuggestion("```suggestion\nfixed code\n```");
* // { isActionable: true, hasCommittableSuggestion: true, confidence: 'high', ... }
*
* const result2 = detectActionableSuggestion("You should use `const` instead of `let` here");
* // { isActionable: true, hasBugFixSuggestion: true, confidence: 'medium', ... }
* ```
*/
export function detectActionableSuggestion(
commentBody: string | undefined | null,
): ActionableSuggestionResult {
if (!commentBody) {
return {
isActionable: false,
hasCommittableSuggestion: false,
hasBugFixSuggestion: false,
hasCodeAlternative: false,
confidence: "low",
reason: "Empty or missing comment body",
};
}
// Check for GitHub inline committable suggestion (highest confidence)
const hasCommittableSuggestion =
COMMITTABLE_SUGGESTION_PATTERN.test(commentBody);
if (hasCommittableSuggestion) {
return {
isActionable: true,
hasCommittableSuggestion: true,
hasBugFixSuggestion: false,
hasCodeAlternative: false,
confidence: "high",
reason: "Contains GitHub inline committable suggestion (```suggestion)",
};
}
// Check for clear bug fix patterns (medium-high confidence)
const matchedBugFixPattern = BUG_FIX_PATTERNS.find((pattern) =>
pattern.test(commentBody),
);
if (matchedBugFixPattern) {
// Higher confidence if also contains a code block
const hasCodeBlock = CODE_ALTERNATIVE_PATTERNS[0].test(commentBody);
return {
isActionable: true,
hasCommittableSuggestion: false,
hasBugFixSuggestion: true,
hasCodeAlternative: hasCodeBlock,
confidence: hasCodeBlock ? "high" : "medium",
reason: hasCodeBlock
? "Contains clear bug fix suggestion with code example"
: "Contains clear bug fix suggestion",
};
}
// Check for code alternatives (medium confidence)
const matchedAlternativePattern = CODE_ALTERNATIVE_PATTERNS.find((pattern) =>
pattern.test(commentBody),
);
if (matchedAlternativePattern) {
return {
isActionable: true,
hasCommittableSuggestion: false,
hasBugFixSuggestion: false,
hasCodeAlternative: true,
confidence: "medium",
reason: "Contains code alternative or fix suggestion",
};
}
return {
isActionable: false,
hasCommittableSuggestion: false,
hasBugFixSuggestion: false,
hasCodeAlternative: false,
confidence: "low",
reason: "No actionable suggestion patterns detected",
};
}
/**
* Checks if a comment should be treated as actionable for autofix purposes,
* even if it comes from a bot account like claude[bot].
*
* This is particularly useful for workflows that want to automatically apply
* suggestions from code review comments.
*
* @param commentBody - The body of the PR comment
* @param authorUsername - The username of the comment author
* @returns Whether the comment should be treated as actionable
*/
export function isCommentActionableForAutofix(
commentBody: string | undefined | null,
authorUsername?: string,
): boolean {
const result = detectActionableSuggestion(commentBody);
// If it's already clearly actionable (high confidence), return true
if (result.confidence === "high") {
return true;
}
// For medium confidence, be more lenient
if (result.confidence === "medium" && result.isActionable) {
return true;
}
return false;
}
/**
* Extracts the suggested code from a GitHub inline committable suggestion block.
*
* @param commentBody - The body of the PR comment
* @returns The suggested code content, or null if no suggestion block found
*
* @example
* ```ts
* const code = extractSuggestionCode("```suggestion\nconst x = 1;\n```");
* // "const x = 1;"
* ```
*/
export function extractSuggestionCode(
commentBody: string | undefined | null,
): string | null {
if (!commentBody) {
return null;
}
const match = commentBody.match(/```suggestion\b\n?([\s\S]*?)```/i);
if (match && match[1] !== undefined) {
return match[1].trim();
}
return null;
}

View File

@@ -0,0 +1,247 @@
#!/usr/bin/env bun
import { describe, it, expect } from "bun:test";
import {
applyBranchTemplate,
generateBranchName,
} from "../src/utils/branch-template";
describe("branch template utilities", () => {
describe("applyBranchTemplate", () => {
it("should replace all template variables", () => {
const template =
"{{prefix}}{{entityType}}-{{entityNumber}}-{{timestamp}}";
const variables = {
prefix: "feat/",
entityType: "issue",
entityNumber: 123,
timestamp: "20240301-1430",
sha: "abcd1234",
};
const result = applyBranchTemplate(template, variables);
expect(result).toBe("feat/issue-123-20240301-1430");
});
it("should handle custom templates with multiple variables", () => {
const template =
"{{prefix}}fix/{{entityType}}_{{entityNumber}}_{{timestamp}}_{{sha}}";
const variables = {
prefix: "claude-",
entityType: "pr",
entityNumber: 456,
timestamp: "20240301-1430",
sha: "abcd1234",
};
const result = applyBranchTemplate(template, variables);
expect(result).toBe("claude-fix/pr_456_20240301-1430_abcd1234");
});
it("should handle templates with missing variables gracefully", () => {
const template = "{{prefix}}{{entityType}}-{{missing}}-{{entityNumber}}";
const variables = {
prefix: "feat/",
entityType: "issue",
entityNumber: 123,
timestamp: "20240301-1430",
};
const result = applyBranchTemplate(template, variables);
expect(result).toBe("feat/issue-{{missing}}-123");
});
});
describe("generateBranchName", () => {
it("should use custom template when provided", () => {
const template = "{{prefix}}custom-{{entityType}}_{{entityNumber}}";
const result = generateBranchName(template, "feature/", "issue", 123);
expect(result).toBe("feature/custom-issue_123");
});
it("should use default format when template is empty", () => {
const result = generateBranchName("", "claude/", "issue", 123);
expect(result).toMatch(/^claude\/issue-123-\d{8}-\d{4}$/);
});
it("should use default format when template is undefined", () => {
const result = generateBranchName(undefined, "claude/", "pr", 456);
expect(result).toMatch(/^claude\/pr-456-\d{8}-\d{4}$/);
});
it("should preserve custom template formatting (no automatic lowercase/truncation)", () => {
const template = "{{prefix}}UPPERCASE_Branch-Name_{{entityNumber}}";
const result = generateBranchName(template, "Feature/", "issue", 123);
expect(result).toBe("Feature/UPPERCASE_Branch-Name_123");
});
it("should not truncate custom template results", () => {
const template =
"{{prefix}}very-long-branch-name-that-exceeds-the-maximum-allowed-length-{{entityNumber}}";
const result = generateBranchName(template, "feature/", "issue", 123);
expect(result).toBe(
"feature/very-long-branch-name-that-exceeds-the-maximum-allowed-length-123",
);
});
it("should apply Kubernetes-compatible transformations to default template only", () => {
const result = generateBranchName(undefined, "Feature/", "issue", 123);
expect(result).toMatch(/^feature\/issue-123-\d{8}-\d{4}$/);
expect(result.length).toBeLessThanOrEqual(50);
});
it("should handle SHA in template", () => {
const template = "{{prefix}}{{entityType}}-{{entityNumber}}-{{sha}}";
const result = generateBranchName(
template,
"fix/",
"pr",
789,
"abcdef123456",
);
expect(result).toBe("fix/pr-789-abcdef12");
});
it("should use label in template when provided", () => {
const template = "{{prefix}}{{label}}/{{entityNumber}}";
const result = generateBranchName(
template,
"feature/",
"issue",
123,
undefined,
"bug",
);
expect(result).toBe("feature/bug/123");
});
it("should fallback to entityType when label template is used but no label provided", () => {
const template = "{{prefix}}{{label}}-{{entityNumber}}";
const result = generateBranchName(template, "fix/", "pr", 456);
expect(result).toBe("fix/pr-456");
});
it("should handle template with both label and entityType", () => {
const template = "{{prefix}}{{label}}-{{entityType}}_{{entityNumber}}";
const result = generateBranchName(
template,
"dev/",
"issue",
789,
undefined,
"enhancement",
);
expect(result).toBe("dev/enhancement-issue_789");
});
it("should use description in template when provided", () => {
const template = "{{prefix}}{{description}}/{{entityNumber}}";
const result = generateBranchName(
template,
"feature/",
"issue",
123,
undefined,
undefined,
"Fix login bug with OAuth",
);
expect(result).toBe("feature/fix-login-bug-with-oauth/123");
});
it("should handle template with multiple variables including description", () => {
const template =
"{{prefix}}{{label}}/{{description}}-{{entityType}}_{{entityNumber}}";
const result = generateBranchName(
template,
"dev/",
"issue",
456,
undefined,
"bug",
"User authentication fails completely",
);
expect(result).toBe(
"dev/bug/user-authentication-fails-completely-issue_456",
);
});
it("should handle description with special characters in template", () => {
const template = "{{prefix}}{{description}}-{{entityNumber}}";
const result = generateBranchName(
template,
"fix/",
"pr",
789,
undefined,
undefined,
"Add: User Registration & Email Validation",
);
expect(result).toBe("fix/add-user-registration-email-789");
});
it("should truncate descriptions to exactly 5 words", () => {
const result = generateBranchName(
"{{prefix}}{{description}}/{{entityNumber}}",
"feature/",
"issue",
999,
undefined,
undefined,
"This is a very long title with many more than five words in it",
);
expect(result).toBe("feature/this-is-a-very-long/999");
});
it("should handle empty description in template", () => {
const template = "{{prefix}}{{description}}-{{entityNumber}}";
const result = generateBranchName(
template,
"test/",
"issue",
101,
undefined,
undefined,
"",
);
expect(result).toBe("test/-101");
});
it("should fallback to default format when template produces empty result", () => {
const template = "{{description}}"; // Will be empty if no title provided
const result = generateBranchName(template, "claude/", "issue", 123);
expect(result).toMatch(/^claude\/issue-123-\d{8}-\d{4}$/);
expect(result.length).toBeLessThanOrEqual(50);
});
it("should fallback to default format when template produces only whitespace", () => {
const template = " {{description}} "; // Will be " " if description is empty
const result = generateBranchName(
template,
"fix/",
"pr",
456,
undefined,
undefined,
"",
);
expect(result).toMatch(/^fix\/pr-456-\d{8}-\d{4}$/);
expect(result.length).toBeLessThanOrEqual(50);
});
});
});

View File

@@ -61,6 +61,7 @@ describe("generatePrompt", () => {
body: "This is a test PR", body: "This is a test PR",
author: { login: "testuser" }, author: { login: "testuser" },
state: "OPEN", state: "OPEN",
labels: { nodes: [] },
createdAt: "2023-01-01T00:00:00Z", createdAt: "2023-01-01T00:00:00Z",
additions: 15, additions: 15,
deletions: 5, deletions: 5,
@@ -475,6 +476,7 @@ describe("generatePrompt", () => {
body: "The login form is not working", body: "The login form is not working",
author: { login: "testuser" }, author: { login: "testuser" },
state: "OPEN", state: "OPEN",
labels: { nodes: [] },
createdAt: "2023-01-01T00:00:00Z", createdAt: "2023-01-01T00:00:00Z",
comments: { comments: {
nodes: [], nodes: [],

View File

@@ -28,6 +28,9 @@ describe("formatContext", () => {
additions: 50, additions: 50,
deletions: 30, deletions: 30,
state: "OPEN", state: "OPEN",
labels: {
nodes: [],
},
commits: { commits: {
totalCount: 3, totalCount: 3,
nodes: [], nodes: [],
@@ -63,6 +66,9 @@ Changed Files: 2 files`,
author: { login: "test-user" }, author: { login: "test-user" },
createdAt: "2023-01-01T00:00:00Z", createdAt: "2023-01-01T00:00:00Z",
state: "OPEN", state: "OPEN",
labels: {
nodes: [],
},
comments: { comments: {
nodes: [], nodes: [],
}, },

View File

@@ -0,0 +1,309 @@
import { describe, expect, it } from "bun:test";
import {
detectActionableSuggestion,
isCommentActionableForAutofix,
extractSuggestionCode,
} from "../src/utils/detect-actionable-suggestion";
describe("detectActionableSuggestion", () => {
describe("GitHub inline committable suggestions", () => {
it("should detect suggestion blocks with high confidence", () => {
const comment = `Here's a fix:
\`\`\`suggestion
const x = 1;
\`\`\``;
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
expect(result.hasCommittableSuggestion).toBe(true);
expect(result.confidence).toBe("high");
expect(result.reason).toContain("committable suggestion");
});
it("should detect suggestion blocks with multiple lines", () => {
const comment = `\`\`\`suggestion
function foo() {
return bar();
}
\`\`\``;
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
expect(result.hasCommittableSuggestion).toBe(true);
expect(result.confidence).toBe("high");
});
it("should detect suggestion blocks case-insensitively", () => {
const comment = `\`\`\`SUGGESTION
const x = 1;
\`\`\``;
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
expect(result.hasCommittableSuggestion).toBe(true);
});
it("should not confuse regular code blocks with suggestion blocks", () => {
const comment = `\`\`\`javascript
const x = 1;
\`\`\``;
const result = detectActionableSuggestion(comment);
expect(result.hasCommittableSuggestion).toBe(false);
// But it should still detect the code alternative
expect(result.hasCodeAlternative).toBe(true);
});
});
describe("bug fix suggestions", () => {
it('should detect "should be" patterns', () => {
const comment = "This should be `const` instead of `let`";
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
expect(result.hasBugFixSuggestion).toBe(true);
expect(result.confidence).toBe("medium");
});
it('should detect "change to" patterns', () => {
const comment = "Change this to use async/await";
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
expect(result.hasBugFixSuggestion).toBe(true);
});
it('should detect "replace with" patterns', () => {
const comment = "Replace this with Array.from()";
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
expect(result.hasBugFixSuggestion).toBe(true);
});
it('should detect "use instead" patterns', () => {
const comment = "Use this instead of the deprecated method";
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
expect(result.hasBugFixSuggestion).toBe(true);
});
it('should detect "instead of X, use Y" patterns', () => {
const comment = "Instead of forEach, use map";
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
expect(result.hasBugFixSuggestion).toBe(true);
});
it('should detect "to fix this" patterns', () => {
const comment = "To fix this, you need to add the await keyword";
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
expect(result.hasBugFixSuggestion).toBe(true);
});
it('should detect "the correct code is" patterns', () => {
const comment = "The correct code would be: return null;";
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
expect(result.hasBugFixSuggestion).toBe(true);
});
it('should detect "missing semicolon" patterns', () => {
const comment = "Missing a semicolon at the end";
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
expect(result.hasBugFixSuggestion).toBe(true);
});
it('should detect "typo" patterns', () => {
const comment = "Typo here: teh should be the";
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
expect(result.hasBugFixSuggestion).toBe(true);
});
it('should detect "wrong type" patterns', () => {
const comment = "Wrong type here, should be string not number";
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
expect(result.hasBugFixSuggestion).toBe(true);
});
it("should have high confidence when bug fix suggestion includes code block", () => {
const comment = `You should use const here:
\`\`\`javascript
const x = 1;
\`\`\``;
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
expect(result.hasBugFixSuggestion).toBe(true);
expect(result.hasCodeAlternative).toBe(true);
expect(result.confidence).toBe("high");
});
});
describe("code alternatives", () => {
it('should detect "try using" patterns', () => {
const comment = "Try using Array.map() instead";
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
});
it('should detect "here\'s the fix" patterns', () => {
const comment = "Here's the fix for this issue";
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
});
it("should detect code blocks as potential alternatives", () => {
const comment = `Try this approach:
\`\`\`
const result = [];
\`\`\``;
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
expect(result.hasCodeAlternative).toBe(true);
});
});
describe("non-actionable comments", () => {
it("should not flag general questions", () => {
const comment = "Why is this returning undefined?";
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(false);
expect(result.confidence).toBe("low");
});
it("should not flag simple observations", () => {
const comment = "This looks interesting";
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(false);
});
it("should not flag approval comments", () => {
const comment = "LGTM! :+1:";
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(false);
});
it("should handle empty comments", () => {
const result = detectActionableSuggestion("");
expect(result.isActionable).toBe(false);
expect(result.reason).toContain("Empty");
});
it("should handle null comments", () => {
const result = detectActionableSuggestion(null);
expect(result.isActionable).toBe(false);
});
it("should handle undefined comments", () => {
const result = detectActionableSuggestion(undefined);
expect(result.isActionable).toBe(false);
});
});
describe("edge cases", () => {
it("should handle comments with both suggestion block and bug fix language", () => {
const comment = `This should be fixed. Here's the suggestion:
\`\`\`suggestion
const x = 1;
\`\`\``;
const result = detectActionableSuggestion(comment);
// Suggestion block takes precedence (high confidence)
expect(result.isActionable).toBe(true);
expect(result.hasCommittableSuggestion).toBe(true);
expect(result.confidence).toBe("high");
});
it("should handle very long comments", () => {
const longContent = "a".repeat(10000);
const comment = `${longContent}
\`\`\`suggestion
const x = 1;
\`\`\``;
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
expect(result.hasCommittableSuggestion).toBe(true);
});
it("should handle comments with special characters", () => {
const comment =
"You should be using `const` here! @#$%^&* Change this to `let`";
const result = detectActionableSuggestion(comment);
expect(result.isActionable).toBe(true);
});
});
});
describe("isCommentActionableForAutofix", () => {
it("should return true for high confidence suggestions", () => {
const comment = `\`\`\`suggestion
const x = 1;
\`\`\``;
expect(isCommentActionableForAutofix(comment)).toBe(true);
});
it("should return true for medium confidence suggestions", () => {
const comment = "You should use const here instead of let";
expect(isCommentActionableForAutofix(comment)).toBe(true);
});
it("should return false for non-actionable comments", () => {
const comment = "This looks fine to me";
expect(isCommentActionableForAutofix(comment)).toBe(false);
});
it("should handle bot authors correctly", () => {
const comment = `\`\`\`suggestion
const x = 1;
\`\`\``;
// Should still return true even for bot authors
expect(isCommentActionableForAutofix(comment, "claude[bot]")).toBe(true);
});
it("should handle empty comments", () => {
expect(isCommentActionableForAutofix("")).toBe(false);
expect(isCommentActionableForAutofix(null)).toBe(false);
expect(isCommentActionableForAutofix(undefined)).toBe(false);
});
});
describe("extractSuggestionCode", () => {
it("should extract code from suggestion block", () => {
const comment = `Here's a fix:
\`\`\`suggestion
const x = 1;
\`\`\``;
expect(extractSuggestionCode(comment)).toBe("const x = 1;");
});
it("should extract multi-line code from suggestion block", () => {
const comment = `\`\`\`suggestion
function foo() {
return bar();
}
\`\`\``;
expect(extractSuggestionCode(comment)).toBe(
"function foo() {\n return bar();\n}",
);
});
it("should handle empty suggestion blocks", () => {
const comment = `\`\`\`suggestion
\`\`\``;
expect(extractSuggestionCode(comment)).toBe("");
});
it("should return null for comments without suggestion blocks", () => {
const comment = "Just a regular comment";
expect(extractSuggestionCode(comment)).toBe(null);
});
it("should return null for empty comments", () => {
expect(extractSuggestionCode("")).toBe(null);
expect(extractSuggestionCode(null)).toBe(null);
expect(extractSuggestionCode(undefined)).toBe(null);
});
it("should not extract from regular code blocks", () => {
const comment = `\`\`\`javascript
const x = 1;
\`\`\``;
expect(extractSuggestionCode(comment)).toBe(null);
});
});

View File

@@ -35,12 +35,44 @@ describe("parseAllowedTools", () => {
expect(parseAllowedTools("")).toEqual([]); expect(parseAllowedTools("")).toEqual([]);
}); });
test("handles duplicate --allowedTools flags", () => { test("handles --allowedTools followed by another --allowedTools flag", () => {
const args = "--allowedTools --allowedTools mcp__github__*"; const args = "--allowedTools --allowedTools mcp__github__*";
// Should not match the first one since the value is another flag // The second --allowedTools is consumed as a value of the first, then skipped.
// This is an edge case with malformed input - returns empty.
expect(parseAllowedTools(args)).toEqual([]); expect(parseAllowedTools(args)).toEqual([]);
}); });
test("parses multiple separate --allowed-tools flags", () => {
const args =
"--allowed-tools 'mcp__context7__*' --allowed-tools 'Read,Glob' --allowed-tools 'mcp__github_inline_comment__*'";
expect(parseAllowedTools(args)).toEqual([
"mcp__context7__*",
"Read",
"Glob",
"mcp__github_inline_comment__*",
]);
});
test("parses multiple --allowed-tools flags on separate lines", () => {
const args = `--model 'claude-haiku'
--allowed-tools 'mcp__context7__*'
--allowed-tools 'Read,Glob,Grep'
--allowed-tools 'mcp__github_inline_comment__create_inline_comment'`;
expect(parseAllowedTools(args)).toEqual([
"mcp__context7__*",
"Read",
"Glob",
"Grep",
"mcp__github_inline_comment__create_inline_comment",
]);
});
test("deduplicates tools from multiple flags", () => {
const args =
"--allowed-tools 'Read,Glob' --allowed-tools 'Glob,Grep' --allowed-tools 'Read'";
expect(parseAllowedTools(args)).toEqual(["Read", "Glob", "Grep"]);
});
test("handles typo --alloedTools", () => { test("handles typo --alloedTools", () => {
const args = "--alloedTools mcp__github__*"; const args = "--alloedTools mcp__github__*";
expect(parseAllowedTools(args)).toEqual([]); expect(parseAllowedTools(args)).toEqual([]);

View File

@@ -87,6 +87,7 @@ describe("pull_request_target event support", () => {
}, },
comments: { nodes: [] }, comments: { nodes: [] },
reviews: { nodes: [] }, reviews: { nodes: [] },
labels: { nodes: [] },
}, },
comments: [], comments: [],
changedFiles: [], changedFiles: [],