|
| 1 | +package lizard |
| 2 | + |
| 3 | +import ( |
| 4 | + "codacy/cli-v2/domain" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "gopkg.in/yaml.v3" |
| 11 | +) |
| 12 | + |
| 13 | +// CreateLizardConfig generates a Lizard configuration file content based on the API configuration |
| 14 | +func CreateLizardConfig(patterns []domain.PatternDefinition) (string, error) { |
| 15 | + patternInfo := make(map[string]map[string]interface{}) |
| 16 | + |
| 17 | + for _, pattern := range patterns { |
| 18 | + metricType := getMetricTypeFromPatternId(pattern.Id) |
| 19 | + if metricType == "" { |
| 20 | + fmt.Printf("Warning: Invalid pattern ID format: %s\n", pattern.Id) |
| 21 | + continue |
| 22 | + } |
| 23 | + |
| 24 | + threshold := getThresholdFromParams(pattern.Parameters) |
| 25 | + if threshold != 0 { |
| 26 | + // Create a unique key for this pattern that includes the severity |
| 27 | + patternKey := pattern.Id |
| 28 | + patternInfo[patternKey] = map[string]interface{}{ |
| 29 | + "id": pattern.Id, |
| 30 | + "category": pattern.Category, |
| 31 | + "level": pattern.Level, |
| 32 | + "severityLevel": pattern.SeverityLevel, |
| 33 | + "title": pattern.Title, |
| 34 | + "description": pattern.Description, |
| 35 | + "explanation": pattern.Explanation, |
| 36 | + "timeToFix": pattern.TimeToFix, |
| 37 | + "threshold": threshold, |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + config := map[string]interface{}{ |
| 43 | + "patterns": patternInfo, |
| 44 | + } |
| 45 | + |
| 46 | + yamlData, err := yaml.Marshal(config) |
| 47 | + if err != nil { |
| 48 | + return "", fmt.Errorf("failed to marshal YAML: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + return string(yamlData), nil |
| 52 | +} |
| 53 | + |
| 54 | +// getThresholdFromParams extracts the threshold value from the parameters |
| 55 | +func getThresholdFromParams(params []domain.ParameterConfiguration) int { |
| 56 | + for _, param := range params { |
| 57 | + if param.Name == "threshold" { |
| 58 | + if param.Value != "" { |
| 59 | + threshold, err := strconv.Atoi(param.Value) |
| 60 | + if err == nil { |
| 61 | + return threshold |
| 62 | + } |
| 63 | + } else if param.Default != "" { |
| 64 | + threshold, err := strconv.Atoi(param.Default) |
| 65 | + if err == nil { |
| 66 | + return threshold |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + return 0 |
| 72 | +} |
| 73 | + |
| 74 | +// getMetricTypeFromPatternId extracts the metric type from the pattern ID |
| 75 | +func getMetricTypeFromPatternId(patternID string) string { |
| 76 | + // Pattern IDs are in the format "Lizard_metric-severity" |
| 77 | + parts := strings.Split(patternID, "_") |
| 78 | + if len(parts) != 2 { |
| 79 | + fmt.Printf("Warning: Invalid pattern ID format: %s\n", patternID) |
| 80 | + return "" |
| 81 | + } |
| 82 | + |
| 83 | + // Extract the metric type from the second part |
| 84 | + // For patterns like "file-nloc-critical", we need to get "file-nloc" |
| 85 | + metricParts := strings.Split(parts[1], "-") |
| 86 | + if len(metricParts) < 2 { |
| 87 | + fmt.Printf("Warning: Invalid metric format: %s\n", parts[1]) |
| 88 | + return "" |
| 89 | + } |
| 90 | + |
| 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 | + } |
| 97 | + |
| 98 | + // Validating that the metric type is one of the known types |
| 99 | + switch metricType { |
| 100 | + case "ccn": |
| 101 | + return "ccn" |
| 102 | + case "nloc": |
| 103 | + return "nloc" |
| 104 | + case "file-nloc": |
| 105 | + return "file-nloc" |
| 106 | + case "parameter-count": |
| 107 | + return "parameter-count" |
| 108 | + default: |
| 109 | + fmt.Printf("Warning: Unknown metric type: %s\n", metricType) |
| 110 | + return "" |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// ReadConfig reads and parses the Lizard configuration file |
| 115 | +func ReadConfig(configPath string) ([]domain.PatternDefinition, error) { |
| 116 | + data, err := os.ReadFile(configPath) |
| 117 | + if err != nil { |
| 118 | + return nil, fmt.Errorf("failed to read config file: %w", err) |
| 119 | + } |
| 120 | + |
| 121 | + var config map[string]map[string]map[string]interface{} |
| 122 | + if err := yaml.Unmarshal(data, &config); err != nil { |
| 123 | + return nil, fmt.Errorf("failed to parse config file: %w", err) |
| 124 | + } |
| 125 | + |
| 126 | + var patterns []domain.PatternDefinition |
| 127 | + for _, pattern := range config["patterns"] { |
| 128 | + threshold, ok := pattern["threshold"].(int) |
| 129 | + if !ok { |
| 130 | + continue |
| 131 | + } |
| 132 | + |
| 133 | + patterns = append(patterns, domain.PatternDefinition{ |
| 134 | + Id: pattern["id"].(string), |
| 135 | + Category: pattern["category"].(string), |
| 136 | + Level: pattern["level"].(string), |
| 137 | + SeverityLevel: pattern["severityLevel"].(string), |
| 138 | + Title: pattern["title"].(string), |
| 139 | + Description: pattern["description"].(string), |
| 140 | + Explanation: pattern["explanation"].(string), |
| 141 | + TimeToFix: pattern["timeToFix"].(int), |
| 142 | + Parameters: []domain.ParameterConfiguration{ |
| 143 | + { |
| 144 | + Name: "threshold", |
| 145 | + Value: fmt.Sprintf("%d", threshold), |
| 146 | + Default: fmt.Sprintf("%d", threshold), |
| 147 | + }, |
| 148 | + }, |
| 149 | + }) |
| 150 | + } |
| 151 | + |
| 152 | + return patterns, nil |
| 153 | +} |
0 commit comments