Skip to content

Commit 7329f45

Browse files
fix lizard configuration parser
1 parent b866947 commit 7329f45

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

codacy-client/client.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,17 @@ func parsePatternConfigurations(response []byte) ([]domain.PatternConfiguration,
113113
return nil, "", fmt.Errorf("failed to unmarshal response: %w", err)
114114
}
115115

116-
var patterns []domain.PatternDefinition
117-
if err := json.Unmarshal(objmap["data"], &patterns); err != nil {
116+
var patternResponses []domain.PatternResponse
117+
if err := json.Unmarshal(objmap["data"], &patternResponses); err != nil {
118118
return nil, "", fmt.Errorf("failed to unmarshal patterns: %w", err)
119119
}
120120

121-
patternConfigurations := make([]domain.PatternConfiguration, len(patterns))
122-
for i, pattern := range patterns {
121+
patternConfigurations := make([]domain.PatternConfiguration, len(patternResponses))
122+
for i, patternResp := range patternResponses {
123123
patternConfigurations[i] = domain.PatternConfiguration{
124-
PatternDefinition: pattern,
125-
Parameters: pattern.Parameters,
126-
Enabled: pattern.Enabled,
124+
PatternDefinition: patternResp.PatternDefinition,
125+
Parameters: patternResp.Parameters,
126+
Enabled: patternResp.Enabled,
127127
}
128128
}
129129

@@ -149,7 +149,9 @@ func GetRepositoryToolPatterns(initFlags domain.InitFlags, toolUUID string) ([]d
149149
initFlags.Organization,
150150
initFlags.Repository,
151151
toolUUID)
152-
return getAllPages(baseURL, initFlags, parsePatternConfigurations)
152+
153+
result, err := getAllPages(baseURL, initFlags, parsePatternConfigurations)
154+
return result, err
153155
}
154156

155157
func GetRepositoryTools(initFlags domain.InitFlags) ([]domain.Tool, error) {

domain/patternConfiguration.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ type PatternConfiguration struct {
2525
Parameters []ParameterConfiguration
2626
Enabled bool `json:"enabled"`
2727
}
28+
29+
// PatternResponse represents the structure of a pattern in the API response
30+
type PatternResponse struct {
31+
PatternDefinition PatternDefinition `json:"patternDefinition"`
32+
Enabled bool `json:"enabled"`
33+
IsCustom bool `json:"isCustom"`
34+
Parameters []ParameterConfiguration `json:"parameters"`
35+
}

tools/lizard/lizardConfigCreator.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,23 @@ func getThresholdFromParams(params []domain.ParameterConfiguration) int {
7474
// getMetricTypeFromPatternId extracts the metric type from the pattern ID
7575
func getMetricTypeFromPatternId(patternID string) string {
7676
// Pattern IDs are in the format "Lizard_metric-severity"
77+
7778
parts := strings.Split(patternID, "_")
7879
if len(parts) != 2 {
7980
fmt.Printf("Warning: Invalid pattern ID format: %s\n", patternID)
8081
return ""
8182
}
8283

83-
// Extract the metric type from the second part
84-
// For patterns like "file-nloc-critical", we need to get "file-nloc"
84+
// Extract the metric parts from the second part
8585
metricParts := strings.Split(parts[1], "-")
8686
if len(metricParts) < 2 {
8787
fmt.Printf("Warning: Invalid metric format: %s\n", parts[1])
8888
return ""
8989
}
9090

91-
// Handle compound metric types
92-
metricType := metricParts[0]
93-
if len(metricParts) > 2 {
94-
// For patterns like "file-nloc-critical", combine "file" and "nloc"
95-
metricType = metricParts[0] + "-" + metricParts[1]
96-
}
91+
// The last part is always the severity (medium, critical, etc.)
92+
// Everything before that is the metric type
93+
metricType := strings.Join(metricParts[:len(metricParts)-1], "-")
9794

9895
// Validating that the metric type is one of the known types
9996
switch metricType {

0 commit comments

Comments
 (0)