Skip to content

Commit 562099c

Browse files
committed
refactor config creator
1 parent 7a69fc8 commit 562099c

File tree

3 files changed

+43
-114
lines changed

3 files changed

+43
-114
lines changed

cmd/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ func createToolFileConfigurations(tool domain.Tool, patternConfiguration []domai
347347
}
348348

349349
func createPMDConfigFile(config []domain.PatternConfiguration, toolsConfigDir string) error {
350-
pmdConfigurationString := tools.CreatePmdConfig(config)
350+
pmdConfigurationString := tools.CreatePmd6Config(config)
351351
return os.WriteFile(filepath.Join(toolsConfigDir, "ruleset.xml"), []byte(pmdConfigurationString), utils.DefaultFilePerms)
352352
}
353353

tools/pmdConfigCreator.go

Lines changed: 35 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -33,52 +33,61 @@ type RuleSet struct {
3333
}
3434

3535
// DeprecatedReferences maps deprecated pattern IDs to their new versions
36-
var DeprecatedReferences = map[string]string{
37-
// Add deprecated pattern mappings here
38-
// Example: "rulesets_java_design_ExcessiveClassLength": "category_java_design_ExcessiveClassLength",
36+
var DeprecatedReferences = map[string]string{}
37+
38+
// Add PMD headers
39+
var (
40+
pmd6Header = `<?xml version="1.0"?>
41+
<ruleset name="Codacy PMD Ruleset"
42+
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
43+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44+
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
45+
<description>Codacy PMD Ruleset</description>`
46+
47+
pmd7Header = `<?xml version="1.0" encoding="UTF-8"?>
48+
<ruleset name="Codacy PMD 7 Ruleset"
49+
xmlns="https://pmd.github.io/ruleset/2.0.0"
50+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
51+
xsi:schemaLocation="https://pmd.github.io/ruleset/2.0.0 https://pmd.github.io/schemas/pmd-7.0.0.xsd">
52+
<description>Codacy PMD 7 Ruleset</description>`
53+
)
54+
55+
// CreatePmd6Config creates a PMD 6 configuration from the provided tool configuration
56+
func CreatePmd6Config(configuration []domain.PatternConfiguration) string {
57+
return createPmdConfigGeneric(configuration, pmd6Header, convertPatternIDToPMD, false)
58+
}
59+
60+
// CreatePmd7Config creates a PMD 7 configuration from the provided tool configuration
61+
func CreatePmd7Config(configuration []domain.PatternConfiguration) string {
62+
return createPmdConfigGeneric(configuration, pmd7Header, convertPatternIDToPMD7, true)
3963
}
4064

4165
// prefixPatternID adds the appropriate prefix to a pattern ID
4266
func prefixPatternID(patternID string) string {
4367
parts := strings.Split(patternID, "_")
44-
45-
// Handle different pattern ID formats
4668
switch len(parts) {
4769
case 2:
48-
// Format: "patternCategory_patternName"
4970
return fmt.Sprintf("category_java_%s_%s", parts[0], parts[1])
5071
case 3:
51-
// Format: "langAlias_patternCategory_patternName"
5272
return fmt.Sprintf("category_%s_%s_%s", parts[0], parts[1], parts[2])
5373
case 4:
54-
// Format: "root_langAlias_patternCategory_patternName"
5574
return fmt.Sprintf("%s_%s_%s_%s", parts[0], parts[1], parts[2], parts[3])
5675
default:
57-
// Return as is if format is unknown
5876
return patternID
5977
}
6078
}
6179

62-
// convertPatternIDToPMD converts a Codacy pattern ID to PMD format
80+
// convertPatternIDToPMD converts a Codacy pattern ID to PMD 6 format
6381
func convertPatternIDToPMD(patternID string) (string, error) {
64-
// Check if this is a deprecated pattern
6582
if newID, ok := DeprecatedReferences[patternID]; ok {
6683
patternID = newID
6784
}
6885

69-
// Handle both formats:
70-
// 1. "java/design/NPathComplexity"
71-
// 2. "PMD_category_java_design_NPathComplexity"
72-
// 3. "PMD_category_apex_security_ApexSharingViolations"
73-
// 4. "PMD_category_plsql_errorprone_TO_TIMESTAMPWithoutDateFormat"
74-
7586
var parts []string
7687
if strings.Contains(patternID, "/") {
7788
parts = strings.Split(patternID, "/")
7889
} else {
79-
// Remove PMD_ prefix if present
8090
id := strings.TrimPrefix(patternID, "PMD_")
81-
// Split by underscore and remove "category" if present
8291
parts = strings.Split(id, "_")
8392
if parts[0] == "category" {
8493
parts = parts[1:]
@@ -89,30 +98,20 @@ func convertPatternIDToPMD(patternID string) (string, error) {
8998
return "", fmt.Errorf("invalid pattern ID format: %s", patternID)
9099
}
91100

92-
// Extract language, category, and rule
93-
language := parts[0] // java, apex, etc.
94-
category := parts[1] // design, security, etc.
95-
rule := parts[2] // rule name
96-
97-
// If there are more parts, combine them with the rule name
98-
if len(parts) > 3 {
99-
rule = strings.Join(parts[2:], "_")
100-
}
101+
language := parts[0]
102+
category := parts[1]
103+
rule := strings.Join(parts[2:], "_")
101104

102105
return fmt.Sprintf("category/%s/%s.xml/%s", language, category, rule), nil
103106
}
104107

105108
// convertPatternIDToPMD7 converts a Codacy pattern ID to PMD 7 format
106109
func convertPatternIDToPMD7(patternID string) (string, error) {
107-
// Strip deprecated mappings
108110
if newID, ok := DeprecatedReferences[patternID]; ok {
109111
patternID = newID
110112
}
111113

112-
// Remove "PMD7_" prefix if present
113114
patternID = strings.TrimPrefix(patternID, "PMD7_")
114-
115-
// Now handle patternIDs like: category_java_bestpractices_UnusedLocalVariable
116115
parts := strings.Split(patternID, "_")
117116

118117
if len(parts) < 4 || parts[0] != "category" {
@@ -126,48 +125,6 @@ func convertPatternIDToPMD7(patternID string) (string, error) {
126125
return fmt.Sprintf("category/%s/%s.xml/%s", lang, category, rule), nil
127126
}
128127

129-
// generateRuleXML generates XML for a single rule
130-
func generateRuleXML(rule Rule) (string, error) {
131-
pmdRef, err := convertPatternIDToPMD(rule.PatternID)
132-
if err != nil {
133-
return "", err
134-
}
135-
136-
if len(rule.Parameters) == 0 {
137-
return fmt.Sprintf(` <rule ref="%s"/>`, pmdRef), nil
138-
}
139-
140-
// Generate rule with parameters
141-
var params strings.Builder
142-
for _, param := range rule.Parameters {
143-
// Skip enabled and version parameters, but include all others
144-
if param.Name != "enabled" && param.Name != "version" {
145-
params.WriteString(fmt.Sprintf(`
146-
<property name="%s" value="%s"/>`, param.Name, param.Value))
147-
}
148-
}
149-
150-
// If no parameters left after filtering, just output the rule without properties
151-
if params.Len() == 0 {
152-
return fmt.Sprintf(` <rule ref="%s"/>`, pmdRef), nil
153-
}
154-
155-
result := fmt.Sprintf(` <rule ref="%s">
156-
<properties>%s
157-
</properties>
158-
</rule>`, pmdRef, params.String())
159-
160-
return result, nil
161-
}
162-
163-
// Add PMD 7 header
164-
var pmd7Header = `<?xml version="1.0" encoding="UTF-8"?>
165-
<ruleset name="Codacy PMD 7 Ruleset"
166-
xmlns="https://pmd.github.io/ruleset/2.0.0"
167-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
168-
xsi:schemaLocation="https://pmd.github.io/ruleset/2.0.0 https://pmd.github.io/schemas/pmd-7.0.0.xsd">
169-
<description>Codacy PMD 7 Ruleset</description>`
170-
171128
// createPmdConfigGeneric abstracts the config creation for both PMD and PMD7
172129
func createPmdConfigGeneric(
173130
configuration []domain.PatternConfiguration,
@@ -176,7 +133,6 @@ func createPmdConfigGeneric(
176133
isPMD7 bool,
177134
) string {
178135
if len(configuration) == 0 {
179-
// Fallback empty ruleset for PMD 7, or default for PMD 6
180136
if header == pmd7Header {
181137
return header + `</ruleset>`
182138
}
@@ -199,21 +155,15 @@ func createPmdConfigGeneric(
199155
value = param.Default
200156
}
201157
if value != "" {
202-
params = append(params, Parameter{
203-
Name: param.Name,
204-
Value: value,
205-
})
158+
params = append(params, Parameter{Name: param.Name, Value: value})
206159
}
207160
}
208161
}
209162

210163
patternID := pattern.PatternDefinition.Id
211-
if !isPMD7 {
212-
if !strings.HasPrefix(patternID, "PMD_") && !strings.Contains(patternID, "/") {
213-
patternID = prefixPatternID(patternID)
214-
}
164+
if !isPMD7 && !strings.HasPrefix(patternID, "PMD_") && !strings.Contains(patternID, "/") {
165+
patternID = prefixPatternID(patternID)
215166
}
216-
// For PMD7, do not alter the patternID here; let convertPatternIDToPMD7 handle it.
217167

218168
rules = append(rules, Rule{
219169
PatternID: patternID,
@@ -222,7 +172,6 @@ func createPmdConfigGeneric(
222172
})
223173
}
224174

225-
// Build the XML
226175
var rulesetXML strings.Builder
227176
rulesetXML.WriteString(header)
228177
if !strings.HasSuffix(header, "\n") {
@@ -239,7 +188,6 @@ func createPmdConfigGeneric(
239188
if err != nil {
240189
continue
241190
}
242-
243191
if seen[ref] {
244192
continue
245193
}
@@ -252,12 +200,9 @@ func createPmdConfigGeneric(
252200

253201
rulesetXML.WriteString(fmt.Sprintf(` <rule ref="%s">
254202
<properties>`, ref))
255-
256203
for _, p := range rule.Parameters {
257-
rulesetXML.WriteString(fmt.Sprintf(`
258-
<property name="%s" value="%s"/>`, p.Name, p.Value))
204+
rulesetXML.WriteString(fmt.Sprintf("\n <property name=\"%s\" value=\"%s\"/>", p.Name, p.Value))
259205
}
260-
261206
rulesetXML.WriteString(`
262207
</properties>
263208
</rule>
@@ -267,19 +212,3 @@ func createPmdConfigGeneric(
267212
rulesetXML.WriteString("</ruleset>")
268213
return rulesetXML.String()
269214
}
270-
271-
// CreatePmdConfig creates a PMD 6 configuration from the provided tool configuration
272-
func CreatePmdConfig(configuration []domain.PatternConfiguration) string {
273-
return createPmdConfigGeneric(configuration, `<?xml version="1.0"?>
274-
<ruleset name="Codacy PMD Ruleset"
275-
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
276-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
277-
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
278-
<description>Codacy PMD Ruleset</description>
279-
280-
`, convertPatternIDToPMD, false)
281-
}
282-
283-
func CreatePmd7Config(configuration []domain.PatternConfiguration) string {
284-
return createPmdConfigGeneric(configuration, pmd7Header, convertPatternIDToPMD7, true)
285-
}

tools/pmdConfigCreator_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestCreatePmdConfig(t *testing.T) {
8080
}
8181

8282
// Generate PMD config
83-
generatedConfig := CreatePmdConfig(config)
83+
generatedConfig := CreatePmd6Config(config)
8484

8585
// Read expected ruleset
8686
expectedRulesetPath := filepath.Join("testdata", "repositories", "pmd", "expected-ruleset.xml")
@@ -125,7 +125,7 @@ func TestCreatePmdConfigWithDisabledRules(t *testing.T) {
125125
},
126126
}
127127

128-
obtainedConfig := CreatePmdConfig(config)
128+
obtainedConfig := CreatePmd6Config(config)
129129

130130
var ruleset PMDRuleset
131131
err := xml.Unmarshal([]byte(obtainedConfig), &ruleset)
@@ -171,7 +171,7 @@ func TestCreatePmdConfigEmpty(t *testing.T) {
171171

172172
config := []domain.PatternConfiguration{}
173173

174-
obtainedConfig := CreatePmdConfig(config)
174+
obtainedConfig := CreatePmd6Config(config)
175175

176176
assert.Contains(t, obtainedConfig, `name="Default PMD Ruleset"`, "XML should contain the correct ruleset name")
177177
assert.Contains(t, obtainedConfig, `xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"`, "XML should contain the correct xmlns")
@@ -207,7 +207,7 @@ func TestCreatePmdConfigEmptyParameterValues(t *testing.T) {
207207
},
208208
}
209209

210-
obtainedConfig := CreatePmdConfig(config)
210+
obtainedConfig := CreatePmd6Config(config)
211211

212212
var ruleset PMDRuleset
213213
err := xml.Unmarshal([]byte(obtainedConfig), &ruleset)
@@ -266,7 +266,7 @@ func TestEmptyParametersAreSkipped(t *testing.T) {
266266
},
267267
}
268268

269-
obtainedConfig := CreatePmdConfig(config)
269+
obtainedConfig := CreatePmd6Config(config)
270270

271271
// Should find this exact pattern in the generated XML
272272
expectedPattern := `<rule ref="category/pom/errorprone.xml/InvalidDependencyTypes"/>`
@@ -292,7 +292,7 @@ func TestNonEmptyParameterValue(t *testing.T) {
292292
},
293293
}
294294

295-
obtainedConfig := CreatePmdConfig(config)
295+
obtainedConfig := CreatePmd6Config(config)
296296

297297
var ruleset PMDRuleset
298298
err := xml.Unmarshal([]byte(obtainedConfig), &ruleset)
@@ -338,7 +338,7 @@ func TestExactJsonStructure(t *testing.T) {
338338
},
339339
}
340340

341-
obtainedConfig := CreatePmdConfig(config)
341+
obtainedConfig := CreatePmd6Config(config)
342342

343343
var ruleset PMDRuleset
344344
err := xml.Unmarshal([]byte(obtainedConfig), &ruleset)

0 commit comments

Comments
 (0)