Skip to content

Commit 5f001ac

Browse files
fix lizard configuration parser
1 parent 6603bab commit 5f001ac

File tree

7 files changed

+38
-16
lines changed

7 files changed

+38
-16
lines changed

codacy-client/client.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
const timeout = 10 * time.Second
1414

15+
// CodacyApiBase is the base URL for the Codacy API
1516
var CodacyApiBase = "https://app.codacy.com"
1617

1718
func getRequest(url string, apiToken string) ([]byte, error) {
@@ -113,17 +114,17 @@ func parsePatternConfigurations(response []byte) ([]domain.PatternConfiguration,
113114
return nil, "", fmt.Errorf("failed to unmarshal response: %w", err)
114115
}
115116

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

121-
patternConfigurations := make([]domain.PatternConfiguration, len(patterns))
122-
for i, pattern := range patterns {
122+
patternConfigurations := make([]domain.PatternConfiguration, len(patternResponses))
123+
for i, patternResp := range patternResponses {
123124
patternConfigurations[i] = domain.PatternConfiguration{
124-
PatternDefinition: pattern,
125-
Parameters: pattern.Parameters,
126-
Enabled: pattern.Enabled,
125+
PatternDefinition: patternResp.PatternDefinition,
126+
Parameters: patternResp.Parameters,
127+
Enabled: patternResp.Enabled,
127128
}
128129
}
129130

@@ -137,21 +138,26 @@ func parsePatternConfigurations(response []byte) ([]domain.PatternConfiguration,
137138
return patternConfigurations, pagination.Cursor, nil
138139
}
139140

141+
// GetDefaultToolPatternsConfig fetches the default patterns for a tool
140142
func GetDefaultToolPatternsConfig(initFlags domain.InitFlags, toolUUID string) ([]domain.PatternConfiguration, error) {
141143
baseURL := fmt.Sprintf("%s/api/v3/tools/%s/patterns?enabled=true", CodacyApiBase, toolUUID)
142144
return getAllPages(baseURL, initFlags, parsePatternConfigurations)
143145
}
144146

147+
// GetRepositoryToolPatterns fetches the patterns for a tool in a repository
145148
func GetRepositoryToolPatterns(initFlags domain.InitFlags, toolUUID string) ([]domain.PatternConfiguration, error) {
146149
baseURL := fmt.Sprintf("%s/api/v3/analysis/organizations/%s/%s/repositories/%s/tools/%s/patterns?enabled=true",
147150
CodacyApiBase,
148151
initFlags.Provider,
149152
initFlags.Organization,
150153
initFlags.Repository,
151154
toolUUID)
152-
return getAllPages(baseURL, initFlags, parsePatternConfigurations)
155+
156+
result, err := getAllPages(baseURL, initFlags, parsePatternConfigurations)
157+
return result, err
153158
}
154159

160+
// GetRepositoryTools fetches the tools for a repository
155161
func GetRepositoryTools(initFlags domain.InitFlags) ([]domain.Tool, error) {
156162
baseURL := fmt.Sprintf("%s/api/v3/analysis/organizations/%s/%s/repositories/%s/tools",
157163
CodacyApiBase,
@@ -175,6 +181,7 @@ func GetRepositoryTools(initFlags domain.InitFlags) ([]domain.Tool, error) {
175181
return toolsResponse.Data, nil
176182
}
177183

184+
// GetToolsVersions fetches the tools versions
178185
func GetToolsVersions() ([]domain.Tool, error) {
179186
baseURL := fmt.Sprintf("%s/api/v3/tools", CodacyApiBase)
180187

@@ -192,6 +199,7 @@ func GetToolsVersions() ([]domain.Tool, error) {
192199
return toolsResponse.Data, nil
193200
}
194201

202+
// GetRepositoryLanguages fetches the languages for a repository
195203
func GetRepositoryLanguages(initFlags domain.InitFlags) ([]domain.Language, error) {
196204
baseURL := fmt.Sprintf("%s/api/v3/organizations/%s/%s/repositories/%s/settings/languages",
197205
CodacyApiBase,

domain/initFlags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package domain
22

3+
// InitFlags represents the flags for the init command
34
type InitFlags struct {
45
ApiToken string
56
Provider string

domain/language.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package domain
22

3+
// Language represents a language in the Codacy API
34
type Language struct {
45
Name string `json:"name"`
56
CodacyDefaults []string `json:"codacyDefaults"`
@@ -8,6 +9,7 @@ type Language struct {
89
Detected bool `json:"detected"`
910
}
1011

12+
// LanguagesResponse represents the structure of the languages response
1113
type LanguagesResponse struct {
1214
Languages []Language `json:"languages"`
1315
}

domain/pagination.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package domain
22

3+
// Pagination represents the structure of the pagination response
34
type Pagination struct {
45
Cursor string `json:"cursor"`
56
Limit int `json:"limit"`

domain/patternConfiguration.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package domain
22

3+
// ParameterConfiguration represents the structure of a parameter in the Codacy API
34
type ParameterConfiguration struct {
45
Name string `json:"name"`
56
Value string `json:"value,omitempty"`
67
Default string `json:"default,omitempty"`
78
}
89

10+
// PatternDefinition represents the structure of a pattern in the Codacy API
911
type PatternDefinition struct {
1012
Id string `json:"id"`
1113
Category string `json:"category"`
@@ -20,8 +22,17 @@ type PatternDefinition struct {
2022
TimeToFix int `json:"timeToFix"`
2123
}
2224

25+
// PatternConfiguration represents the structure of a pattern in the Codacy API
2326
type PatternConfiguration struct {
2427
PatternDefinition PatternDefinition `json:"patternDefinition"`
2528
Parameters []ParameterConfiguration
2629
Enabled bool `json:"enabled"`
2730
}
31+
32+
// PatternResponse represents the structure of a pattern in the API response
33+
type PatternResponse struct {
34+
PatternDefinition PatternDefinition `json:"patternDefinition"`
35+
Enabled bool `json:"enabled"`
36+
IsCustom bool `json:"isCustom"`
37+
Parameters []ParameterConfiguration `json:"parameters"`
38+
}

domain/tool.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package domain
22

3+
// ToolsResponse represents the structure of the tools response
34
type ToolsResponse struct {
45
Data []Tool `json:"data"`
56
}
67

8+
// Tool represents a tool in the Codacy API
79
type Tool struct {
810
Uuid string `json:"uuid"`
911
Name string `json:"name"`

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)