-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrules.go
107 lines (95 loc) · 2.74 KB
/
rules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import "fmt"
type ReviewRule struct {
ID string // Unique identifier (e.g., "ERR001")
Dimension string // High-level category
Category string // Specific category
Name string // Human-readable name
Description string // Detailed description
Severity string // Default severity level
Examples CodeExample // Good and bad examples
Metadata RuleMetadata // Additional rule information
}
type RuleMetadata struct {
Category string
Impact string
AutoFixable bool
OutdatedRate float64 // Track effectiveness using BitsAI-CR's metric
}
// RuleStore manages our taxonomy of review rules.
type RuleStore struct {
rules map[string]ReviewRule
// Indexes for efficient lookup
dimensionIndex map[string][]string // Dimension -> Rule IDs
categoryIndex map[string][]string // Category -> Rule IDs
}
func (rs *RuleStore) AddRule(rule ReviewRule) error {
// Validate rule structure
if err := rs.validateRule(rule); err != nil {
return fmt.Errorf("invalid rule %s: %w", rule.ID, err)
}
// Add to main store and indexes
rs.rules[rule.ID] = rule
rs.dimensionIndex[rule.Dimension] = append(
rs.dimensionIndex[rule.Dimension],
rule.ID,
)
rs.categoryIndex[rule.Category] = append(
rs.categoryIndex[rule.Category],
rule.ID,
)
return nil
}
// validateRule ensures a review rule meets our requirements before being added
// to the store.
func (rs *RuleStore) validateRule(rule ReviewRule) error {
// Check required fields
if rule.ID == "" {
return fmt.Errorf("rule ID is required")
}
if rule.Dimension == "" {
return fmt.Errorf("dimension is required")
}
if rule.Category == "" {
return fmt.Errorf("category is required")
}
if rule.Description == "" {
return fmt.Errorf("description is required")
}
// Validate dimension is one of our known dimensions
validDimensions := map[string]bool{
"Code Defect": true,
"Security Vulnerability": true,
"Maintainability and Readability": true,
"Performance Issue": true,
"Other": true,
}
if !validDimensions[rule.Dimension] {
return fmt.Errorf("invalid dimension: %s", rule.Dimension)
}
// Ensure we don't have duplicate IDs
if _, exists := rs.rules[rule.ID]; exists {
return fmt.Errorf("duplicate rule ID: %s", rule.ID)
}
return nil
}
// FormatRuleContent creates a standardized text representation of a rule for embedding.
func FormatRuleContent(rule ReviewRule) string {
return fmt.Sprintf(`Rule: %s
Category: %s
Dimension: %s
Description: %s
Good Example:
%s
Bad Example:
%s
Explanation: %s`,
rule.Name,
rule.Category,
rule.Dimension,
rule.Description,
rule.Examples.Good,
rule.Examples.Bad,
rule.Examples.Explanation,
)
}