mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-01-22 22:44:13 +08:00
Add override prompt variable (#301)
* Add override prompt variable * create test * Fix typechecks * remove use of `any` for additional type-safety --------- Co-authored-by: km-anthropic <km-anthropic@users.noreply.github.com>
This commit is contained in:
@@ -322,6 +322,148 @@ describe("generatePrompt", () => {
|
||||
expect(prompt).toContain("CUSTOM INSTRUCTIONS:\nAlways use TypeScript");
|
||||
});
|
||||
|
||||
test("should use override_prompt when provided", () => {
|
||||
const envVars: PreparedContext = {
|
||||
repository: "owner/repo",
|
||||
claudeCommentId: "12345",
|
||||
triggerPhrase: "@claude",
|
||||
overridePrompt: "Simple prompt for $REPOSITORY PR #$PR_NUMBER",
|
||||
eventData: {
|
||||
eventName: "pull_request",
|
||||
eventAction: "opened",
|
||||
isPR: true,
|
||||
prNumber: "123",
|
||||
},
|
||||
};
|
||||
|
||||
const prompt = generatePrompt(envVars, mockGitHubData, false);
|
||||
|
||||
expect(prompt).toBe("Simple prompt for owner/repo PR #123");
|
||||
expect(prompt).not.toContain("You are Claude, an AI assistant");
|
||||
});
|
||||
|
||||
test("should substitute all variables in override_prompt", () => {
|
||||
const envVars: PreparedContext = {
|
||||
repository: "test/repo",
|
||||
claudeCommentId: "12345",
|
||||
triggerPhrase: "@claude",
|
||||
triggerUsername: "john-doe",
|
||||
overridePrompt: `Repository: $REPOSITORY
|
||||
PR: $PR_NUMBER
|
||||
Title: $PR_TITLE
|
||||
Body: $PR_BODY
|
||||
Comments: $PR_COMMENTS
|
||||
Review Comments: $REVIEW_COMMENTS
|
||||
Changed Files: $CHANGED_FILES
|
||||
Trigger Comment: $TRIGGER_COMMENT
|
||||
Username: $TRIGGER_USERNAME
|
||||
Branch: $BRANCH_NAME
|
||||
Base: $BASE_BRANCH
|
||||
Event: $EVENT_TYPE
|
||||
Is PR: $IS_PR`,
|
||||
eventData: {
|
||||
eventName: "pull_request_review_comment",
|
||||
isPR: true,
|
||||
prNumber: "456",
|
||||
commentBody: "Please review this code",
|
||||
claudeBranch: "feature-branch",
|
||||
baseBranch: "main",
|
||||
},
|
||||
};
|
||||
|
||||
const prompt = generatePrompt(envVars, mockGitHubData, false);
|
||||
|
||||
expect(prompt).toContain("Repository: test/repo");
|
||||
expect(prompt).toContain("PR: 456");
|
||||
expect(prompt).toContain("Title: Test PR");
|
||||
expect(prompt).toContain("Body: This is a test PR");
|
||||
expect(prompt).toContain("Comments: ");
|
||||
expect(prompt).toContain("Review Comments: ");
|
||||
expect(prompt).toContain("Changed Files: ");
|
||||
expect(prompt).toContain("Trigger Comment: Please review this code");
|
||||
expect(prompt).toContain("Username: john-doe");
|
||||
expect(prompt).toContain("Branch: feature-branch");
|
||||
expect(prompt).toContain("Base: main");
|
||||
expect(prompt).toContain("Event: pull_request_review_comment");
|
||||
expect(prompt).toContain("Is PR: true");
|
||||
});
|
||||
|
||||
test("should handle override_prompt for issues", () => {
|
||||
const envVars: PreparedContext = {
|
||||
repository: "owner/repo",
|
||||
claudeCommentId: "12345",
|
||||
triggerPhrase: "@claude",
|
||||
overridePrompt: "Issue #$ISSUE_NUMBER: $ISSUE_TITLE in $REPOSITORY",
|
||||
eventData: {
|
||||
eventName: "issues",
|
||||
eventAction: "opened",
|
||||
isPR: false,
|
||||
issueNumber: "789",
|
||||
baseBranch: "main",
|
||||
claudeBranch: "claude/issue-789-20240101-1200",
|
||||
},
|
||||
};
|
||||
|
||||
const issueGitHubData = {
|
||||
...mockGitHubData,
|
||||
contextData: {
|
||||
title: "Bug: Login form broken",
|
||||
body: "The login form is not working",
|
||||
author: { login: "testuser" },
|
||||
state: "OPEN",
|
||||
createdAt: "2023-01-01T00:00:00Z",
|
||||
comments: {
|
||||
nodes: [],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const prompt = generatePrompt(envVars, issueGitHubData, false);
|
||||
|
||||
expect(prompt).toBe("Issue #789: Bug: Login form broken in owner/repo");
|
||||
});
|
||||
|
||||
test("should handle empty values in override_prompt substitution", () => {
|
||||
const envVars: PreparedContext = {
|
||||
repository: "owner/repo",
|
||||
claudeCommentId: "12345",
|
||||
triggerPhrase: "@claude",
|
||||
overridePrompt:
|
||||
"PR: $PR_NUMBER, Issue: $ISSUE_NUMBER, Comment: $TRIGGER_COMMENT",
|
||||
eventData: {
|
||||
eventName: "pull_request",
|
||||
eventAction: "opened",
|
||||
isPR: true,
|
||||
prNumber: "123",
|
||||
},
|
||||
};
|
||||
|
||||
const prompt = generatePrompt(envVars, mockGitHubData, false);
|
||||
|
||||
expect(prompt).toBe("PR: 123, Issue: , Comment: ");
|
||||
});
|
||||
|
||||
test("should not substitute variables when override_prompt is not provided", () => {
|
||||
const envVars: PreparedContext = {
|
||||
repository: "owner/repo",
|
||||
claudeCommentId: "12345",
|
||||
triggerPhrase: "@claude",
|
||||
eventData: {
|
||||
eventName: "issues",
|
||||
eventAction: "opened",
|
||||
isPR: false,
|
||||
issueNumber: "123",
|
||||
baseBranch: "main",
|
||||
claudeBranch: "claude/issue-123-20240101-1200",
|
||||
},
|
||||
};
|
||||
|
||||
const prompt = generatePrompt(envVars, mockGitHubData, false);
|
||||
|
||||
expect(prompt).toContain("You are Claude, an AI assistant");
|
||||
expect(prompt).toContain("<event_type>ISSUE_CREATED</event_type>");
|
||||
});
|
||||
|
||||
test("should include trigger username when provided", () => {
|
||||
const envVars: PreparedContext = {
|
||||
repository: "owner/repo",
|
||||
|
||||
Reference in New Issue
Block a user