mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-01-22 22:44:13 +08:00
Add label trigger functionality to Claude Code Action (#177)
- introduced a new input parameter `label_trigger` in `action.yml` to allow triggering actions based on specific labels applied to issues. - Enhanced the context preparation and event handling in the code to support the new labled event.
This commit is contained in:
committed by
GitHub
parent
c831be8f54
commit
b0d9b8c4cd
@@ -226,6 +226,33 @@ describe("generatePrompt", () => {
|
||||
);
|
||||
});
|
||||
|
||||
test("should generate prompt for issue labeled event", () => {
|
||||
const envVars: PreparedContext = {
|
||||
repository: "owner/repo",
|
||||
claudeCommentId: "12345",
|
||||
triggerPhrase: "@claude",
|
||||
eventData: {
|
||||
eventName: "issues",
|
||||
eventAction: "labeled",
|
||||
isPR: false,
|
||||
issueNumber: "888",
|
||||
baseBranch: "main",
|
||||
claudeBranch: "claude/issue-888-20240101_120000",
|
||||
labelTrigger: "claude-task",
|
||||
},
|
||||
};
|
||||
|
||||
const prompt = generatePrompt(envVars, mockGitHubData);
|
||||
|
||||
expect(prompt).toContain("<event_type>ISSUE_LABELED</event_type>");
|
||||
expect(prompt).toContain(
|
||||
"<trigger_context>issue labeled with 'claude-task'</trigger_context>",
|
||||
);
|
||||
expect(prompt).toContain(
|
||||
"[Create a PR](https://github.com/owner/repo/compare/main",
|
||||
);
|
||||
});
|
||||
|
||||
test("should include direct prompt when provided", () => {
|
||||
const envVars: PreparedContext = {
|
||||
repository: "owner/repo",
|
||||
@@ -615,6 +642,28 @@ describe("getEventTypeAndContext", () => {
|
||||
expect(result.triggerContext).toBe("issue assigned to 'claude-bot'");
|
||||
});
|
||||
|
||||
test("should return correct type and context for issue labeled", () => {
|
||||
const envVars: PreparedContext = {
|
||||
repository: "owner/repo",
|
||||
claudeCommentId: "12345",
|
||||
triggerPhrase: "@claude",
|
||||
eventData: {
|
||||
eventName: "issues",
|
||||
eventAction: "labeled",
|
||||
isPR: false,
|
||||
issueNumber: "888",
|
||||
baseBranch: "main",
|
||||
claudeBranch: "claude/issue-888-20240101_120000",
|
||||
labelTrigger: "claude-task",
|
||||
},
|
||||
};
|
||||
|
||||
const result = getEventTypeAndContext(envVars);
|
||||
|
||||
expect(result.eventType).toBe("ISSUE_LABELED");
|
||||
expect(result.triggerContext).toBe("issue labeled with 'claude-task'");
|
||||
});
|
||||
|
||||
test("should return correct type and context for issue assigned without assigneeTrigger", () => {
|
||||
const envVars: PreparedContext = {
|
||||
repository: "owner/repo",
|
||||
|
||||
@@ -10,6 +10,7 @@ import type {
|
||||
const defaultInputs = {
|
||||
triggerPhrase: "/claude",
|
||||
assigneeTrigger: "",
|
||||
labelTrigger: "",
|
||||
anthropicModel: "claude-3-7-sonnet-20250219",
|
||||
allowedTools: [] as string[],
|
||||
disallowedTools: [] as string[],
|
||||
@@ -128,6 +129,46 @@ export const mockIssueAssignedContext: ParsedGitHubContext = {
|
||||
inputs: { ...defaultInputs, assigneeTrigger: "@claude-bot" },
|
||||
};
|
||||
|
||||
export const mockIssueLabeledContext: ParsedGitHubContext = {
|
||||
runId: "1234567890",
|
||||
eventName: "issues",
|
||||
eventAction: "labeled",
|
||||
repository: defaultRepository,
|
||||
actor: "admin-user",
|
||||
payload: {
|
||||
action: "labeled",
|
||||
issue: {
|
||||
number: 1234,
|
||||
title: "Enhancement: Improve search functionality",
|
||||
body: "The current search is too slow and needs optimization",
|
||||
user: {
|
||||
login: "alice-wonder",
|
||||
id: 54321,
|
||||
avatar_url: "https://avatars.githubusercontent.com/u/54321",
|
||||
html_url: "https://github.com/alice-wonder",
|
||||
},
|
||||
assignee: null,
|
||||
},
|
||||
label: {
|
||||
id: 987654321,
|
||||
name: "claude-task",
|
||||
color: "f29513",
|
||||
description: "Label for Claude AI interactions",
|
||||
},
|
||||
repository: {
|
||||
name: "test-repo",
|
||||
full_name: "test-owner/test-repo",
|
||||
private: false,
|
||||
owner: {
|
||||
login: "test-owner",
|
||||
},
|
||||
},
|
||||
} as IssuesEvent,
|
||||
entityNumber: 1234,
|
||||
isPR: false,
|
||||
inputs: { ...defaultInputs, labelTrigger: "claude-task" },
|
||||
};
|
||||
|
||||
// Issue comment on issue event
|
||||
export const mockIssueCommentContext: ParsedGitHubContext = {
|
||||
runId: "1234567890",
|
||||
|
||||
@@ -62,6 +62,7 @@ describe("checkWritePermissions", () => {
|
||||
inputs: {
|
||||
triggerPhrase: "@claude",
|
||||
assigneeTrigger: "",
|
||||
labelTrigger: "",
|
||||
allowedTools: [],
|
||||
disallowedTools: [],
|
||||
customInstructions: "",
|
||||
|
||||
@@ -6,6 +6,7 @@ import { describe, it, expect } from "bun:test";
|
||||
import {
|
||||
createMockContext,
|
||||
mockIssueAssignedContext,
|
||||
mockIssueLabeledContext,
|
||||
mockIssueCommentContext,
|
||||
mockIssueOpenedContext,
|
||||
mockPullRequestReviewContext,
|
||||
@@ -29,6 +30,7 @@ describe("checkContainsTrigger", () => {
|
||||
inputs: {
|
||||
triggerPhrase: "/claude",
|
||||
assigneeTrigger: "",
|
||||
labelTrigger: "",
|
||||
directPrompt: "Fix the bug in the login form",
|
||||
allowedTools: [],
|
||||
disallowedTools: [],
|
||||
@@ -55,6 +57,7 @@ describe("checkContainsTrigger", () => {
|
||||
inputs: {
|
||||
triggerPhrase: "/claude",
|
||||
assigneeTrigger: "",
|
||||
labelTrigger: "",
|
||||
directPrompt: "",
|
||||
allowedTools: [],
|
||||
disallowedTools: [],
|
||||
@@ -107,6 +110,39 @@ describe("checkContainsTrigger", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("label trigger", () => {
|
||||
it("should return true when issue is labeled with the trigger label", () => {
|
||||
const context = mockIssueLabeledContext;
|
||||
expect(checkContainsTrigger(context)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false when issue is labeled with a different label", () => {
|
||||
const context = {
|
||||
...mockIssueLabeledContext,
|
||||
payload: {
|
||||
...mockIssueLabeledContext.payload,
|
||||
label: {
|
||||
...(mockIssueLabeledContext.payload as any).label,
|
||||
name: "bug",
|
||||
},
|
||||
},
|
||||
} as ParsedGitHubContext;
|
||||
expect(checkContainsTrigger(context)).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for non-labeled events", () => {
|
||||
const context = {
|
||||
...mockIssueLabeledContext,
|
||||
eventAction: "opened",
|
||||
payload: {
|
||||
...mockIssueLabeledContext.payload,
|
||||
action: "opened",
|
||||
},
|
||||
} as ParsedGitHubContext;
|
||||
expect(checkContainsTrigger(context)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("issue body and title trigger", () => {
|
||||
it("should return true when issue body contains trigger phrase", () => {
|
||||
const context = mockIssueOpenedContext;
|
||||
@@ -232,6 +268,7 @@ describe("checkContainsTrigger", () => {
|
||||
inputs: {
|
||||
triggerPhrase: "@claude",
|
||||
assigneeTrigger: "",
|
||||
labelTrigger: "",
|
||||
directPrompt: "",
|
||||
allowedTools: [],
|
||||
disallowedTools: [],
|
||||
@@ -259,6 +296,7 @@ describe("checkContainsTrigger", () => {
|
||||
inputs: {
|
||||
triggerPhrase: "@claude",
|
||||
assigneeTrigger: "",
|
||||
labelTrigger: "",
|
||||
directPrompt: "",
|
||||
allowedTools: [],
|
||||
disallowedTools: [],
|
||||
@@ -286,6 +324,7 @@ describe("checkContainsTrigger", () => {
|
||||
inputs: {
|
||||
triggerPhrase: "@claude",
|
||||
assigneeTrigger: "",
|
||||
labelTrigger: "",
|
||||
directPrompt: "",
|
||||
allowedTools: [],
|
||||
disallowedTools: [],
|
||||
|
||||
Reference in New Issue
Block a user