Fix filtering out the bot activity.

This commit is contained in:
2025-12-11 09:31:55 +00:00
parent 319143132b
commit 7ff6df70ee
5 changed files with 161 additions and 90 deletions
+13 -2
View File
@@ -192,19 +192,30 @@ func (c *Config) GetTeamForUser(username string) *TeamConfig {
return nil
}
// IsBot checks if a username matches bot patterns
// IsBot checks if a username matches bot patterns (hardcoded defaults + user-defined)
func (c *Config) IsBot(username string) bool {
if c.Options.IncludeBots {
return false
}
lower := strings.ToLower(username)
for _, pattern := range c.Options.BotPatterns {
// Check hardcoded default patterns first
for _, pattern := range DefaultBotPatterns() {
pattern = strings.ToLower(pattern)
if matchPattern(lower, pattern) {
return true
}
}
// Check user-defined additional patterns
for _, pattern := range c.Options.AdditionalBotPatterns {
pattern = strings.ToLower(pattern)
if matchPattern(lower, pattern) {
return true
}
}
return false
}
+36 -7
View File
@@ -615,15 +615,10 @@ func TestConfig_GetTeamForUser(t *testing.T) {
func TestConfig_IsBot(t *testing.T) {
t.Parallel()
// Bot patterns are now hardcoded, so we just need IncludeBots: false
cfg := &Config{
Options: OptionsConfig{
IncludeBots: false,
BotPatterns: []string{
"*[bot]",
"dependabot*",
"renovate*",
"github-actions*",
},
},
}
@@ -652,6 +647,16 @@ func TestConfig_IsBot(t *testing.T) {
username: "github-actions[bot]",
expected: true,
},
{
name: "codecov bot (hardcoded)",
username: "codecov[bot]",
expected: true,
},
{
name: "snyk bot (hardcoded)",
username: "snyk-bot",
expected: true,
},
{
name: "regular user",
username: "alice",
@@ -674,19 +679,43 @@ func TestConfig_IsBot(t *testing.T) {
}
}
func TestConfig_IsBot_AdditionalPatterns(t *testing.T) {
t.Parallel()
cfg := &Config{
Options: OptionsConfig{
IncludeBots: false,
AdditionalBotPatterns: []string{"my-custom-bot", "ci-*"},
},
}
// Custom patterns should work
assert.True(t, cfg.IsBot("my-custom-bot"))
assert.True(t, cfg.IsBot("ci-runner"))
assert.True(t, cfg.IsBot("ci-bot"))
// Hardcoded patterns should still work
assert.True(t, cfg.IsBot("dependabot[bot]"))
assert.True(t, cfg.IsBot("renovate[bot]"))
// Regular users should not match
assert.False(t, cfg.IsBot("alice"))
}
func TestConfig_IsBot_IncludeBots(t *testing.T) {
t.Parallel()
cfg := &Config{
Options: OptionsConfig{
IncludeBots: true,
BotPatterns: []string{"*[bot]"},
},
}
// When IncludeBots is true, nothing should be considered a bot
// (even hardcoded patterns are bypassed)
assert.False(t, cfg.IsBot("my-app[bot]"))
assert.False(t, cfg.IsBot("dependabot"))
assert.False(t, cfg.IsBot("renovate[bot]"))
}
func TestMatchPattern(t *testing.T) {
+28 -16
View File
@@ -139,12 +139,29 @@ type CacheConfig struct {
// OptionsConfig holds advanced options
type OptionsConfig struct {
ConcurrentRequests int `yaml:"concurrent_requests"`
IncludeBots bool `yaml:"include_bots"`
BotPatterns []string `yaml:"bot_patterns"`
CloneDirectory string `yaml:"clone_directory"` // Directory for local git clones
UseLocalGit bool `yaml:"use_local_git"` // Use local git for commits (faster)
UserAliases []UserAlias `yaml:"user_aliases,omitempty"` // Manual email/name to login mappings
ConcurrentRequests int `yaml:"concurrent_requests"`
IncludeBots bool `yaml:"include_bots"`
AdditionalBotPatterns []string `yaml:"additional_bot_patterns"` // User-defined patterns (added to hardcoded defaults)
CloneDirectory string `yaml:"clone_directory"` // Directory for local git clones
UseLocalGit bool `yaml:"use_local_git"` // Use local git for commits (faster)
UserAliases []UserAlias `yaml:"user_aliases,omitempty"` // Manual email/name to login mappings
}
// DefaultBotPatterns returns the hardcoded bot patterns that are always applied
// These cannot be overridden by users to ensure consistent bot filtering
func DefaultBotPatterns() []string {
return []string{
"*[bot]", // GitHub App bots: dependabot[bot], renovate[bot], etc.
"dependabot*", // Dependabot variants
"renovate*", // Renovate bot variants
"github-actions*", // GitHub Actions
"codecov*", // Codecov bot
"snyk*", // Snyk security bot
"greenkeeper*", // Greenkeeper (legacy)
"imgbot*", // Image optimization bot
"allcontributors*", // All Contributors bot
"semantic-release*", // Semantic release bot
}
}
// UserAlias maps git emails or names to a GitHub login
@@ -198,16 +215,11 @@ func DefaultConfig() *Config {
TTL: "24h",
},
Options: OptionsConfig{
ConcurrentRequests: 5,
IncludeBots: false,
BotPatterns: []string{
"*[bot]",
"dependabot*",
"renovate*",
"github-actions*",
},
CloneDirectory: "./.repos",
UseLocalGit: true, // Default to faster local git analysis
ConcurrentRequests: 5,
IncludeBots: false,
AdditionalBotPatterns: []string{}, // Users can add custom patterns here
CloneDirectory: "./.repos",
UseLocalGit: true, // Default to faster local git analysis
},
}
}