mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-01-22 14:24:13 +08:00
Add mode support (#333)
* Add mode support * update "as any" with proper "as unknwon as ModeName" casting * Add documentation to README and registry.ts * Add tests for differen event types, integration flows, and error conditions * Clean up some tests * Minor test fix * Minor formatting test + switch from interface to type * correct the order of mkdir call * always configureGitAuth as there's already a fallback to handle null users by using the bot ID * simplify registry setup --------- Co-authored-by: km-anthropic <km-anthropic@users.noreply.github.com>
This commit is contained in:
@@ -24,6 +24,7 @@ describe("prepareMcpConfig", () => {
|
||||
entityNumber: 123,
|
||||
isPR: false,
|
||||
inputs: {
|
||||
mode: "tag",
|
||||
triggerPhrase: "@claude",
|
||||
assigneeTrigger: "",
|
||||
labelTrigger: "",
|
||||
|
||||
@@ -8,6 +8,7 @@ import type {
|
||||
} from "@octokit/webhooks-types";
|
||||
|
||||
const defaultInputs = {
|
||||
mode: "tag" as const,
|
||||
triggerPhrase: "/claude",
|
||||
assigneeTrigger: "",
|
||||
labelTrigger: "",
|
||||
|
||||
28
test/modes/registry.test.ts
Normal file
28
test/modes/registry.test.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { describe, test, expect } from "bun:test";
|
||||
import { getMode, isValidMode, type ModeName } from "../../src/modes/registry";
|
||||
import { tagMode } from "../../src/modes/tag";
|
||||
|
||||
describe("Mode Registry", () => {
|
||||
test("getMode returns tag mode by default", () => {
|
||||
const mode = getMode("tag");
|
||||
expect(mode).toBe(tagMode);
|
||||
expect(mode.name).toBe("tag");
|
||||
});
|
||||
|
||||
test("getMode throws error for invalid mode", () => {
|
||||
const invalidMode = "invalid" as unknown as ModeName;
|
||||
expect(() => getMode(invalidMode)).toThrow(
|
||||
"Invalid mode 'invalid'. Valid modes are: 'tag'. Please check your workflow configuration.",
|
||||
);
|
||||
});
|
||||
|
||||
test("isValidMode returns true for tag mode", () => {
|
||||
expect(isValidMode("tag")).toBe(true);
|
||||
});
|
||||
|
||||
test("isValidMode returns false for invalid mode", () => {
|
||||
expect(isValidMode("invalid")).toBe(false);
|
||||
expect(isValidMode("review")).toBe(false);
|
||||
expect(isValidMode("freeform")).toBe(false);
|
||||
});
|
||||
});
|
||||
92
test/modes/tag.test.ts
Normal file
92
test/modes/tag.test.ts
Normal file
@@ -0,0 +1,92 @@
|
||||
import { describe, test, expect, beforeEach } from "bun:test";
|
||||
import { tagMode } from "../../src/modes/tag";
|
||||
import type { ParsedGitHubContext } from "../../src/github/context";
|
||||
import type { IssueCommentEvent } from "@octokit/webhooks-types";
|
||||
import { createMockContext } from "../mockContext";
|
||||
|
||||
describe("Tag Mode", () => {
|
||||
let mockContext: ParsedGitHubContext;
|
||||
|
||||
beforeEach(() => {
|
||||
mockContext = createMockContext({
|
||||
eventName: "issue_comment",
|
||||
isPR: false,
|
||||
});
|
||||
});
|
||||
|
||||
test("tag mode has correct properties", () => {
|
||||
expect(tagMode.name).toBe("tag");
|
||||
expect(tagMode.description).toBe(
|
||||
"Traditional implementation mode triggered by @claude mentions",
|
||||
);
|
||||
expect(tagMode.shouldCreateTrackingComment()).toBe(true);
|
||||
});
|
||||
|
||||
test("shouldTrigger delegates to checkContainsTrigger", () => {
|
||||
const contextWithTrigger = createMockContext({
|
||||
eventName: "issue_comment",
|
||||
isPR: false,
|
||||
inputs: {
|
||||
...createMockContext().inputs,
|
||||
triggerPhrase: "@claude",
|
||||
},
|
||||
payload: {
|
||||
comment: {
|
||||
body: "Hey @claude, can you help?",
|
||||
},
|
||||
} as IssueCommentEvent,
|
||||
});
|
||||
|
||||
expect(tagMode.shouldTrigger(contextWithTrigger)).toBe(true);
|
||||
|
||||
const contextWithoutTrigger = createMockContext({
|
||||
eventName: "issue_comment",
|
||||
isPR: false,
|
||||
inputs: {
|
||||
...createMockContext().inputs,
|
||||
triggerPhrase: "@claude",
|
||||
},
|
||||
payload: {
|
||||
comment: {
|
||||
body: "This is just a regular comment",
|
||||
},
|
||||
} as IssueCommentEvent,
|
||||
});
|
||||
|
||||
expect(tagMode.shouldTrigger(contextWithoutTrigger)).toBe(false);
|
||||
});
|
||||
|
||||
test("prepareContext includes all required data", () => {
|
||||
const data = {
|
||||
commentId: 123,
|
||||
baseBranch: "main",
|
||||
claudeBranch: "claude/fix-bug",
|
||||
};
|
||||
|
||||
const context = tagMode.prepareContext(mockContext, data);
|
||||
|
||||
expect(context.mode).toBe("tag");
|
||||
expect(context.githubContext).toBe(mockContext);
|
||||
expect(context.commentId).toBe(123);
|
||||
expect(context.baseBranch).toBe("main");
|
||||
expect(context.claudeBranch).toBe("claude/fix-bug");
|
||||
});
|
||||
|
||||
test("prepareContext works without data", () => {
|
||||
const context = tagMode.prepareContext(mockContext);
|
||||
|
||||
expect(context.mode).toBe("tag");
|
||||
expect(context.githubContext).toBe(mockContext);
|
||||
expect(context.commentId).toBeUndefined();
|
||||
expect(context.baseBranch).toBeUndefined();
|
||||
expect(context.claudeBranch).toBeUndefined();
|
||||
});
|
||||
|
||||
test("getAllowedTools returns empty array", () => {
|
||||
expect(tagMode.getAllowedTools()).toEqual([]);
|
||||
});
|
||||
|
||||
test("getDisallowedTools returns empty array", () => {
|
||||
expect(tagMode.getDisallowedTools()).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -60,6 +60,7 @@ describe("checkWritePermissions", () => {
|
||||
entityNumber: 1,
|
||||
isPR: false,
|
||||
inputs: {
|
||||
mode: "tag",
|
||||
triggerPhrase: "@claude",
|
||||
assigneeTrigger: "",
|
||||
labelTrigger: "",
|
||||
|
||||
@@ -28,6 +28,7 @@ describe("checkContainsTrigger", () => {
|
||||
eventName: "issues",
|
||||
eventAction: "opened",
|
||||
inputs: {
|
||||
mode: "tag",
|
||||
triggerPhrase: "/claude",
|
||||
assigneeTrigger: "",
|
||||
labelTrigger: "",
|
||||
@@ -60,6 +61,7 @@ describe("checkContainsTrigger", () => {
|
||||
},
|
||||
} as IssuesEvent,
|
||||
inputs: {
|
||||
mode: "tag",
|
||||
triggerPhrase: "/claude",
|
||||
assigneeTrigger: "",
|
||||
labelTrigger: "",
|
||||
@@ -276,6 +278,7 @@ describe("checkContainsTrigger", () => {
|
||||
},
|
||||
} as PullRequestEvent,
|
||||
inputs: {
|
||||
mode: "tag",
|
||||
triggerPhrase: "@claude",
|
||||
assigneeTrigger: "",
|
||||
labelTrigger: "",
|
||||
@@ -309,6 +312,7 @@ describe("checkContainsTrigger", () => {
|
||||
},
|
||||
} as PullRequestEvent,
|
||||
inputs: {
|
||||
mode: "tag",
|
||||
triggerPhrase: "@claude",
|
||||
assigneeTrigger: "",
|
||||
labelTrigger: "",
|
||||
@@ -342,6 +346,7 @@ describe("checkContainsTrigger", () => {
|
||||
},
|
||||
} as PullRequestEvent,
|
||||
inputs: {
|
||||
mode: "tag",
|
||||
triggerPhrase: "@claude",
|
||||
assigneeTrigger: "",
|
||||
labelTrigger: "",
|
||||
|
||||
Reference in New Issue
Block a user