mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-01-28 02:42:25 +08:00
feat: add actor-based comment filtering to GitHub data fetching (#812)
- Introduced `include_comments_by_actor` and `exclude_comments_by_actor` inputs in action.yml to allow filtering of comments based on actor usernames. - Updated context parsing to handle new input fields. - Implemented `filterCommentsByActor` function to filter comments according to specified inclusion and exclusion patterns. - Modified `fetchGitHubData` to apply actor filters when retrieving comments from pull requests and issues. - Added comprehensive tests for the new filtering functionality. This enhancement provides more control over which comments are processed based on the actor, improving the flexibility of the workflow.
This commit is contained in:
@@ -98,6 +98,8 @@ type BaseContext = {
|
||||
allowedNonWriteUsers: string;
|
||||
trackProgress: boolean;
|
||||
includeFixLinks: boolean;
|
||||
includeCommentsByActor: string;
|
||||
excludeCommentsByActor: string;
|
||||
};
|
||||
};
|
||||
|
||||
@@ -156,6 +158,8 @@ export function parseGitHubContext(): GitHubContext {
|
||||
allowedNonWriteUsers: process.env.ALLOWED_NON_WRITE_USERS ?? "",
|
||||
trackProgress: process.env.TRACK_PROGRESS === "true",
|
||||
includeFixLinks: process.env.INCLUDE_FIX_LINKS === "true",
|
||||
includeCommentsByActor: process.env.INCLUDE_COMMENTS_BY_ACTOR ?? "",
|
||||
excludeCommentsByActor: process.env.EXCLUDE_COMMENTS_BY_ACTOR ?? "",
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -20,6 +20,10 @@ import type {
|
||||
} from "../types";
|
||||
import type { CommentWithImages } from "../utils/image-downloader";
|
||||
import { downloadCommentImages } from "../utils/image-downloader";
|
||||
import {
|
||||
parseActorFilter,
|
||||
shouldIncludeCommentByActor,
|
||||
} from "../utils/actor-filter";
|
||||
|
||||
/**
|
||||
* Extracts the trigger timestamp from the GitHub webhook payload.
|
||||
@@ -166,6 +170,35 @@ export function isBodySafeToUse(
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters comments by actor username based on include/exclude patterns
|
||||
* @param comments - Array of comments to filter
|
||||
* @param includeActors - Comma-separated actors to include
|
||||
* @param excludeActors - Comma-separated actors to exclude
|
||||
* @returns Filtered array of comments
|
||||
*/
|
||||
export function filterCommentsByActor<T extends { author: { login: string } }>(
|
||||
comments: T[],
|
||||
includeActors: string = "",
|
||||
excludeActors: string = "",
|
||||
): T[] {
|
||||
const includeParsed = parseActorFilter(includeActors);
|
||||
const excludeParsed = parseActorFilter(excludeActors);
|
||||
|
||||
// No filters = return all
|
||||
if (includeParsed.length === 0 && excludeParsed.length === 0) {
|
||||
return comments;
|
||||
}
|
||||
|
||||
return comments.filter((comment) =>
|
||||
shouldIncludeCommentByActor(
|
||||
comment.author.login,
|
||||
includeParsed,
|
||||
excludeParsed,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
type FetchDataParams = {
|
||||
octokits: Octokits;
|
||||
repository: string;
|
||||
@@ -174,6 +207,8 @@ type FetchDataParams = {
|
||||
triggerUsername?: string;
|
||||
triggerTime?: string;
|
||||
originalTitle?: string;
|
||||
includeCommentsByActor?: string;
|
||||
excludeCommentsByActor?: string;
|
||||
};
|
||||
|
||||
export type GitHubFileWithSHA = GitHubFile & {
|
||||
@@ -198,6 +233,8 @@ export async function fetchGitHubData({
|
||||
triggerUsername,
|
||||
triggerTime,
|
||||
originalTitle,
|
||||
includeCommentsByActor,
|
||||
excludeCommentsByActor,
|
||||
}: FetchDataParams): Promise<FetchDataResult> {
|
||||
const [owner, repo] = repository.split("/");
|
||||
if (!owner || !repo) {
|
||||
@@ -225,9 +262,13 @@ export async function fetchGitHubData({
|
||||
const pullRequest = prResult.repository.pullRequest;
|
||||
contextData = pullRequest;
|
||||
changedFiles = pullRequest.files.nodes || [];
|
||||
comments = filterCommentsToTriggerTime(
|
||||
pullRequest.comments?.nodes || [],
|
||||
triggerTime,
|
||||
comments = filterCommentsByActor(
|
||||
filterCommentsToTriggerTime(
|
||||
pullRequest.comments?.nodes || [],
|
||||
triggerTime,
|
||||
),
|
||||
includeCommentsByActor,
|
||||
excludeCommentsByActor,
|
||||
);
|
||||
reviewData = pullRequest.reviews || [];
|
||||
|
||||
@@ -248,9 +289,13 @@ export async function fetchGitHubData({
|
||||
|
||||
if (issueResult.repository.issue) {
|
||||
contextData = issueResult.repository.issue;
|
||||
comments = filterCommentsToTriggerTime(
|
||||
contextData?.comments?.nodes || [],
|
||||
triggerTime,
|
||||
comments = filterCommentsByActor(
|
||||
filterCommentsToTriggerTime(
|
||||
contextData?.comments?.nodes || [],
|
||||
triggerTime,
|
||||
),
|
||||
includeCommentsByActor,
|
||||
excludeCommentsByActor,
|
||||
);
|
||||
|
||||
console.log(`Successfully fetched issue #${prNumber} data`);
|
||||
@@ -318,7 +363,27 @@ export async function fetchGitHubData({
|
||||
body: r.body,
|
||||
}));
|
||||
|
||||
// Filter review comments to trigger time
|
||||
// Filter review comments to trigger time and by actor
|
||||
if (reviewData && reviewData.nodes) {
|
||||
// Filter reviews by actor
|
||||
reviewData.nodes = filterCommentsByActor(
|
||||
reviewData.nodes,
|
||||
includeCommentsByActor,
|
||||
excludeCommentsByActor,
|
||||
);
|
||||
|
||||
// Also filter inline review comments within each review
|
||||
reviewData.nodes.forEach((review) => {
|
||||
if (review.comments?.nodes) {
|
||||
review.comments.nodes = filterCommentsByActor(
|
||||
review.comments.nodes,
|
||||
includeCommentsByActor,
|
||||
excludeCommentsByActor,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const allReviewComments =
|
||||
reviewData?.nodes?.flatMap((r) => r.comments?.nodes ?? []) ?? [];
|
||||
const filteredReviewComments = filterCommentsToTriggerTime(
|
||||
|
||||
65
src/github/utils/actor-filter.ts
Normal file
65
src/github/utils/actor-filter.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Parses actor filter string into array of patterns
|
||||
* @param filterString - Comma-separated actor names (e.g., "user1,user2,*[bot]")
|
||||
* @returns Array of actor patterns
|
||||
*/
|
||||
export function parseActorFilter(filterString: string): string[] {
|
||||
if (!filterString.trim()) return [];
|
||||
return filterString
|
||||
.split(",")
|
||||
.map((actor) => actor.trim())
|
||||
.filter((actor) => actor.length > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an actor matches a pattern
|
||||
* Supports wildcards: "*[bot]" matches all bots, "dependabot[bot]" matches specific
|
||||
* @param actor - Actor username to check
|
||||
* @param pattern - Pattern to match against
|
||||
* @returns true if actor matches pattern
|
||||
*/
|
||||
export function actorMatchesPattern(actor: string, pattern: string): boolean {
|
||||
// Exact match
|
||||
if (actor === pattern) return true;
|
||||
|
||||
// Wildcard bot pattern: "*[bot]" matches any username ending with [bot]
|
||||
if (pattern === "*[bot]" && actor.endsWith("[bot]")) return true;
|
||||
|
||||
// No match
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a comment should be included based on actor filters
|
||||
* @param actor - Comment author username
|
||||
* @param includeActors - Array of actors to include (empty = include all)
|
||||
* @param excludeActors - Array of actors to exclude (empty = exclude none)
|
||||
* @returns true if comment should be included
|
||||
*/
|
||||
export function shouldIncludeCommentByActor(
|
||||
actor: string,
|
||||
includeActors: string[],
|
||||
excludeActors: string[],
|
||||
): boolean {
|
||||
// Check exclusion first (exclusion takes priority)
|
||||
if (excludeActors.length > 0) {
|
||||
for (const pattern of excludeActors) {
|
||||
if (actorMatchesPattern(actor, pattern)) {
|
||||
return false; // Excluded
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check inclusion
|
||||
if (includeActors.length > 0) {
|
||||
for (const pattern of includeActors) {
|
||||
if (actorMatchesPattern(actor, pattern)) {
|
||||
return true; // Explicitly included
|
||||
}
|
||||
}
|
||||
return false; // Not in include list
|
||||
}
|
||||
|
||||
// No filters or passed all checks
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user