Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions commentfilter/ci_fix_attempt.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ func CIFixAttemptMarker(failures []models.CheckRunFailure, commitSHA string) str
// CountCIFixAttempts counts the number of ci-fix-attempt markers in
// the given PR comments from the bot.
func CountCIFixAttempts(comments []models.PRComment, botUsername string) int {
normBot := normalizeUsername(botUsername)
normBot := NormalizeUsername(botUsername)
count := 0
for _, c := range comments {
if normalizeUsername(c.Author.Username) != normBot {
if NormalizeUsername(c.Author.Username) != normBot {
continue
}
if ciFixAttemptRe.MatchString(c.Body) {
Expand Down
44 changes: 31 additions & 13 deletions commentfilter/commentfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ func Filter(comments []models.PRComment, cfg Config) []models.PRComment {
}

byID := buildLookup(comments)
normBot := normalizeUsername(cfg.BotUsername)
normBot := NormalizeUsername(cfg.BotUsername)
ignoreRe := botIgnoreDirectiveRe(normBot)

result := make([]models.PRComment, 0, len(comments))
for _, c := range comments {
norm := normalizeUsername(c.Author.Username)
norm := NormalizeUsername(c.Author.Username)

// Keep bot's own comments (needed for address detection).
if norm == normBot {
Expand Down Expand Up @@ -138,11 +138,11 @@ func Filter(comments []models.PRComment, cfg Config) []models.PRComment {
// already replied to.
func HasNewActionable(comments []models.PRComment, cfg Config) bool {
filtered := Filter(comments, cfg)
normBot := normalizeUsername(cfg.BotUsername)
normBot := NormalizeUsername(cfg.BotUsername)
botRepliedTo := BotRepliedTo(filtered, normBot)

for _, c := range filtered {
if normalizeUsername(c.Author.Username) == normBot {
if NormalizeUsername(c.Author.Username) == normBot {
continue
}
if !botRepliedTo[c.ID] {
Expand All @@ -169,7 +169,7 @@ func BotRepliedTo(comments []models.PRComment, normBot string) map[int64]bool {
threadBotLatest := make(map[int64]time.Time)

for _, c := range comments {
if normalizeUsername(c.Author.Username) != normBot {
if NormalizeUsername(c.Author.Username) != normBot {
continue
}
// Review comment reply: threaded via InReplyTo.
Expand All @@ -188,7 +188,7 @@ func BotRepliedTo(comments []models.PRComment, normBot string) map[int64]bool {
// Second pass: in flat review threads, mark non-bot follow-up
// comments as replied-to when the bot replied at or after them.
for _, c := range comments {
if normalizeUsername(c.Author.Username) == normBot {
if NormalizeUsername(c.Author.Username) == normBot {
continue
}
if c.IsReviewComment && c.InReplyTo != 0 {
Expand All @@ -201,9 +201,9 @@ func BotRepliedTo(comments []models.PRComment, normBot string) map[int64]bool {
return replied
}

// normalizeUsername strips the GitHub [bot] suffix and lowercases
// NormalizeUsername strips the GitHub [bot] suffix and lowercases
// for case-insensitive comparison.
func normalizeUsername(s string) string {
func NormalizeUsername(s string) string {
return strings.ToLower(strings.TrimSuffix(s, "[bot]"))
}

Expand All @@ -217,7 +217,7 @@ func buildLookup(comments []models.PRComment) map[int64]models.PRComment {

func isIgnored(normUsername string, ignored []string) bool {
for _, u := range ignored {
if normalizeUsername(u) == normUsername {
if NormalizeUsername(u) == normUsername {
return true
}
}
Expand All @@ -226,7 +226,7 @@ func isIgnored(normUsername string, ignored []string) bool {

func isKnownBot(normUsername string, knownBots []string) bool {
for _, b := range knownBots {
if normalizeUsername(b) == normUsername {
if NormalizeUsername(b) == normUsername {
return true
}
}
Expand Down Expand Up @@ -261,6 +261,24 @@ func botIgnoreDirectiveRe(normBot string) *regexp.Regexp {
return regexp.MustCompile(pattern)
}

// BotCommandRe builds a compiled regexp matching @<botUsername> <command>.
// The structure mirrors [botIgnoreDirectiveRe]: case-insensitive,
// optional [bot] suffix, horizontal whitespace only, word boundary.
// Callers that check multiple comments should compile once and reuse.
func BotCommandRe(botUsername, command string) *regexp.Regexp {
normBot := NormalizeUsername(botUsername)
pattern := `(?i)(^|[^[:alnum:]_])@` + regexp.QuoteMeta(normBot) + `(\[bot\])?[ \t]+` + regexp.QuoteMeta(command) + `\b`
return regexp.MustCompile(pattern)
}

// IsSyncCommand reports whether body contains an @<botUsername> sync
// directive. The match is case-insensitive and accepts an optional
// [bot] suffix on the username. Recompiles the regex on each call;
// for checking multiple bodies, use [BotCommandRe] once and reuse.
func IsSyncCommand(body, botUsername string) bool {
return BotCommandRe(botUsername, "sync").MatchString(body)
}

// shouldSkipBotReply returns true if a known bot is replying to our
// bot's comment, or if the parent comment is missing (defensive
// skip — the parent may have been our bot's comment).
Expand All @@ -269,7 +287,7 @@ func shouldSkipBotReply(c models.PRComment, byID map[int64]models.PRComment, nor
if !found {
return true
}
return normalizeUsername(parent.Author.Username) == normBot
return NormalizeUsername(parent.Author.Username) == normBot
}

// threadDepth counts how many times our bot appears in a comment's
Expand Down Expand Up @@ -301,7 +319,7 @@ func reviewThreadBotCount(c models.PRComment, normBot string, byID map[int64]mod

count := 0
for _, sibling := range byID {
if normalizeUsername(sibling.Author.Username) == normBot && sibling.InReplyTo == c.InReplyTo {
if NormalizeUsername(sibling.Author.Username) == normBot && sibling.InReplyTo == c.InReplyTo {
count++
}
}
Expand All @@ -327,7 +345,7 @@ func parentChainBotCount(commentID int64, normBot string, byID map[int64]models.
break
}

if normalizeUsername(c.Author.Username) == normBot {
if NormalizeUsername(c.Author.Username) == normBot {
depth++
}

Expand Down
112 changes: 112 additions & 0 deletions commentfilter/commentfilter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -935,3 +935,115 @@ func assertIDs(t *testing.T, got, want []int64) {
}
}
}

// --- IsSyncCommand ---

func TestIsSyncCommand(t *testing.T) {
tests := []struct {
name string
body string
botUsername string
want bool
}{
{
name: "basic match",
body: "@ai-bot sync",
botUsername: "ai-bot",
want: true,
},
{
name: "at start with trailing text",
body: "@ai-bot sync\n\nPlease pull main.",
botUsername: "ai-bot",
want: true,
},
{
name: "at end of comment",
body: "CI is broken, can you\n@ai-bot sync",
botUsername: "ai-bot",
want: true,
},
{
name: "inline in comment",
body: "Hey @ai-bot sync please",
botUsername: "ai-bot",
want: true,
},
{
name: "case insensitive",
body: "@AI-BOT Sync",
botUsername: "ai-bot",
want: true,
},
{
name: "with [bot] suffix",
body: "@ai-bot[bot] sync",
botUsername: "ai-bot",
want: true,
},
{
name: "config username has [bot] suffix",
body: "@my-bot sync",
botUsername: "my-bot[bot]",
want: true,
},
{
name: "word boundary prevents syncing",
body: "@ai-bot syncing main",
botUsername: "ai-bot",
want: false,
},
{
name: "newline between mention and sync",
body: "@ai-bot\nsync",
botUsername: "ai-bot",
want: false,
},
{
name: "mention inside larger token",
body: "contact-foo@ai-bot sync",
botUsername: "ai-bot",
want: false,
},
{
name: "no match without mention",
body: "sync main into this branch",
botUsername: "ai-bot",
want: false,
},
{
name: "empty body",
body: "",
botUsername: "ai-bot",
want: false,
},
{
name: "wrong bot username",
body: "@other-bot sync",
botUsername: "ai-bot",
want: false,
},
{
name: "tab between mention and sync",
body: "@ai-bot\tsync",
botUsername: "ai-bot",
want: true,
},
{
name: "multiple spaces between mention and sync",
body: "@ai-bot sync",
botUsername: "ai-bot",
want: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := commentfilter.IsSyncCommand(tt.body, tt.botUsername)
if got != tt.want {
t.Errorf("IsSyncCommand(%q, %q) = %v, want %v",
tt.body, tt.botUsername, got, tt.want)
}
})
}
}
4 changes: 2 additions & 2 deletions executor/costcomment.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (p *Pipeline) postOrUpdateCostComment(
}
body := formatCostComment([]costEntry{{Label: label, Cost: cost}})

if err := p.git.PostIssueComment(owner, repo, prNumber, body); err != nil {
if _, err := p.git.PostIssueComment(owner, repo, prNumber, body); err != nil {
logger.Warn("Failed to post cost comment",
zap.String("owner", owner), zap.String("repo", repo),
zap.Int("pr_number", prNumber), zap.Error(err))
Expand Down Expand Up @@ -254,7 +254,7 @@ func (p *Pipeline) postCostCrossReference(logger *zap.Logger, ref *costCrossRef)
if findCostCrossRef(comments) != nil {
continue
}
if err := p.git.PostIssueComment(pr.owner, pr.repo, pr.number, body); err != nil {
if _, err := p.git.PostIssueComment(pr.owner, pr.repo, pr.number, body); err != nil {
logger.Warn("Failed to post cost cross-reference",
zap.String("repo", pr.repo), zap.Error(err))
}
Expand Down
6 changes: 3 additions & 3 deletions executor/costcomment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,11 @@ func (s *costCommentGitStub) ListIssueComments(owner, repo string, prNumber int)
return []models.IssueComment{}, nil
}

func (s *costCommentGitStub) PostIssueComment(owner, repo string, prNumber int, body string) error {
func (s *costCommentGitStub) PostIssueComment(owner, repo string, prNumber int, body string) (int64, error) {
if s.postFunc != nil {
return s.postFunc(owner, repo, prNumber, body)
return 0, s.postFunc(owner, repo, prNumber, body)
}
return nil
return 0, nil
}

func TestPostCostCrossReference_PostsOnSecondaryPRs(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,11 @@ type GitService interface {
commentID int64, body string) error

// PostIssueComment posts a top-level comment on a PR (via the
// issues endpoint). Used for replying to conversation comments,
// which do not support threading.
// issues endpoint) and returns the created comment's ID. Used
// for replying to conversation comments and bot command
// acknowledgments.
PostIssueComment(owner, repo string, prNumber int,
body string) error
body string) (int64, error)

// ListIssueComments returns all top-level comments on a PR.
// Used to find existing bot comments for update-in-place.
Expand Down
6 changes: 3 additions & 3 deletions executor/executortest/stubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type StubGitService struct {
GetPRForBranchFunc func(owner, repo, head string) (*models.PRDetails, error)
GetPRCommentsFunc func(owner, repo string, number int, since time.Time) ([]models.PRComment, error)
ReplyToCommentFunc func(owner, repo string, prNumber int, commentID int64, body string) error
PostIssueCommentFunc func(owner, repo string, prNumber int, body string) error
PostIssueCommentFunc func(owner, repo string, prNumber int, body string) (int64, error)
ListIssueCommentsFunc func(owner, repo string, prNumber int) ([]models.IssueComment, error)
UpdateIssueCommentFunc func(owner, repo string, commentID int64, body string) error
MergeBaseFunc func(dir, branch, fetchURL string) ([]string, error)
Expand Down Expand Up @@ -169,11 +169,11 @@ func (s *StubGitService) ReplyToComment(owner, repo string, prNumber int, commen
return nil
}

func (s *StubGitService) PostIssueComment(owner, repo string, prNumber int, body string) error {
func (s *StubGitService) PostIssueComment(owner, repo string, prNumber int, body string) (int64, error) {
if s.PostIssueCommentFunc != nil {
return s.PostIssueCommentFunc(owner, repo, prNumber, body)
}
return nil
return 0, nil
}

func (s *StubGitService) ListIssueComments(owner, repo string, prNumber int) ([]models.IssueComment, error) {
Expand Down
8 changes: 4 additions & 4 deletions executor/feedback.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ func (p *Pipeline) replyToCommentsOnRepo(
} else {
contextual := conversationReplyBody(c, replyBody)
markedBody := fmt.Sprintf("%s\n%s", contextual, commentfilter.AddressedMarker(c.ID))
if err := p.git.PostIssueComment(owner, repo, pr.Number, markedBody); err != nil {
if _, err := p.git.PostIssueComment(owner, repo, pr.Number, markedBody); err != nil {
logger.Warn("Failed to reply to conversation comment",
zap.Int64("comment_id", c.ID),
zap.Error(err))
Expand Down Expand Up @@ -1002,7 +1002,7 @@ func (p *Pipeline) replyToComments(
} else {
contextual := conversationReplyBody(c, replyBody)
markedBody := fmt.Sprintf("%s\n%s", contextual, commentfilter.AddressedMarker(c.ID))
if err := p.git.PostIssueComment(
if _, err := p.git.PostIssueComment(
settings.Repos[0].Owner, settings.Repos[0].Repo, prDetails.Number, markedBody); err != nil {
logger.Warn("Failed to reply to conversation comment",
zap.Int64("comment_id", c.ID),
Expand Down Expand Up @@ -1050,7 +1050,7 @@ func (p *Pipeline) replyUnableToAddress(
} else {
contextual := conversationReplyBody(c, replyBody)
markedBody := fmt.Sprintf("%s\n%s", contextual, commentfilter.AddressedMarker(c.ID))
if err := p.git.PostIssueComment(
if _, err := p.git.PostIssueComment(
settings.Repos[0].Owner, settings.Repos[0].Repo, prDetails.Number, markedBody); err != nil {
logger.Warn("Failed to reply to conversation comment",
zap.Int64("comment_id", c.ID),
Expand Down Expand Up @@ -1230,7 +1230,7 @@ func (p *Pipeline) postCIFixMarker(
return
}
marker := commentfilter.CIFixAttemptMarker(ciFailures, commitSHA)
if err := p.git.PostIssueComment(owner, repo, prNumber, marker); err != nil {
if _, err := p.git.PostIssueComment(owner, repo, prNumber, marker); err != nil {
logger.Warn("Failed to post CI fix attempt marker", zap.Error(err))
}
}
Expand Down
Loading
Loading