-
Notifications
You must be signed in to change notification settings - Fork 10
feature: parse apiv2 patterns jsonData into pylintrc type file - PLUTO-1358 #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| package pylint | ||
|
|
||
| import ( | ||
| "codacy/cli-v2/domain" | ||
| "fmt" | ||
| "log" | ||
| "strings" | ||
| ) | ||
|
|
||
| // getDefaultParametersForPatterns returns a map of pattern IDs to their default parameters | ||
| func getDefaultParametersForPatterns(patternIDs []string) map[string][]domain.ParameterConfiguration { | ||
| defaultParams := make(map[string][]domain.ParameterConfiguration) | ||
|
|
||
| for _, patternID := range patternIDs { | ||
| if params, exists := PatternDefaultParameters[patternID]; exists { | ||
| defaultParams[patternID] = params | ||
| } | ||
| } | ||
|
|
||
| return defaultParams | ||
| } | ||
|
|
||
| // writePylintRCHeader writes the common header sections to the RC content | ||
| func writePylintRCHeader(rcContent *strings.Builder) { | ||
| rcContent.WriteString("[MASTER]\n") | ||
| rcContent.WriteString("ignore=CVS\n") | ||
| rcContent.WriteString("persistent=yes\n") | ||
| rcContent.WriteString("load-plugins=\n\n") | ||
| rcContent.WriteString("[MESSAGES CONTROL]\n") | ||
| rcContent.WriteString("disable=all\n") | ||
| } | ||
|
|
||
| // writeEnabledPatterns writes the enabled patterns section to the RC content | ||
| func writeEnabledPatterns(rcContent *strings.Builder, patternIDs []string) { | ||
| if len(patternIDs) > 0 { | ||
| rcContent.WriteString(fmt.Sprintf("enable=%s\n", strings.Join(patternIDs, ","))) | ||
| } | ||
| rcContent.WriteString("\n") | ||
| } | ||
|
|
||
| // writeParametersBySection writes the parameters grouped by section to the RC content | ||
| func writeParametersBySection(rcContent *strings.Builder, groupedParams map[string][]domain.ParameterConfiguration) { | ||
| for sectionName, params := range groupedParams { | ||
| rcContent.WriteString(fmt.Sprintf("[%s]\n", sectionName)) | ||
| for _, param := range params { | ||
| rcContent.WriteString(fmt.Sprintf("%s=%s\n", param.Name, param.Value)) | ||
| } | ||
| rcContent.WriteString("\n") | ||
| } | ||
| } | ||
|
|
||
| // groupParametersByPatterns groups parameters from patterns into sections | ||
| func groupParametersByPatterns(patterns []domain.PatternConfiguration) map[string][]domain.ParameterConfiguration { | ||
| groupedParams := make(map[string][]domain.ParameterConfiguration) | ||
|
|
||
| for _, pattern := range patterns { | ||
| patternID := extractPatternId(pattern.PatternDefinition.Id) | ||
| params := pattern.Parameters | ||
|
|
||
| // If no parameters, check defaults | ||
| if len(params) == 0 { | ||
| if defaultParams, exists := PatternDefaultParameters[patternID]; exists { | ||
| params = defaultParams | ||
| } | ||
| } | ||
|
|
||
| // Add parameters to their respective sections | ||
| for _, param := range params { | ||
| sectionName := GetParameterSection(param.Name) | ||
| if sectionName == nil { | ||
| log.Printf("Parameter %s has no section name", param.Name) | ||
| continue | ||
| } | ||
|
|
||
| // Check if parameter already exists in section | ||
| exists := false | ||
| for _, existingParam := range groupedParams[*sectionName] { | ||
| if existingParam.Name == param.Name { | ||
| exists = true | ||
| break | ||
| } | ||
| } | ||
|
|
||
| // Only add if not already present | ||
| if !exists { | ||
| groupedParams[*sectionName] = append(groupedParams[*sectionName], param) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return groupedParams | ||
| } | ||
|
|
||
| func GeneratePylintRCDefault() string { | ||
| var rcContent strings.Builder | ||
|
|
||
| writePylintRCHeader(&rcContent) | ||
| writeEnabledPatterns(&rcContent, DefaultPatterns) | ||
|
|
||
| // Get default parameters for enabled patterns | ||
| defaultParams := getDefaultParametersForPatterns(DefaultPatterns) | ||
|
|
||
| // Convert default parameters to pattern configurations | ||
| var patterns []domain.PatternConfiguration | ||
| for patternID, params := range defaultParams { | ||
| patterns = append(patterns, domain.PatternConfiguration{ | ||
| PatternDefinition: domain.PatternDefinition{ | ||
| Id: "PyLintPython3_" + patternID, | ||
| }, | ||
| Parameters: params, | ||
| }) | ||
| } | ||
|
|
||
| // Group and write parameters | ||
| groupedParams := groupParametersByPatterns(patterns) | ||
| writeParametersBySection(&rcContent, groupedParams) | ||
|
|
||
| return rcContent.String() | ||
| } | ||
|
|
||
| // GeneratePylintRC generates a pylintrc file content with the specified patterns enabled | ||
| func GeneratePylintRC(config []domain.PatternConfiguration) string { | ||
| var rcContent strings.Builder | ||
|
|
||
| writePylintRCHeader(&rcContent) | ||
|
|
||
| // Collect enabled pattern IDs | ||
| var enabledPatternsIds []string | ||
|
|
||
| for _, pattern := range config { | ||
| patternID := extractPatternId(pattern.PatternDefinition.Id) | ||
| enabledPatternsIds = append(enabledPatternsIds, patternID) | ||
| } | ||
|
|
||
| writeEnabledPatterns(&rcContent, enabledPatternsIds) | ||
|
|
||
| // Group and write parameters | ||
| groupedParams := groupParametersByPatterns(config) | ||
| writeParametersBySection(&rcContent, groupedParams) | ||
|
|
||
| return rcContent.String() | ||
| } | ||
|
|
||
| // extractPatternId returns the part of the pattern ID after the underscore | ||
| // For example: "PyLintPython3_C0301" -> "C0301" | ||
| func extractPatternId(fullID string) string { | ||
| parts := strings.Split(fullID, "_") | ||
| if len(parts) > 1 { | ||
| return parts[1] | ||
| } | ||
| return fullID | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package pylint | ||
|
|
||
| // DefaultPatterns contains the list of default Pylint patterns | ||
| var DefaultPatterns = []string{ | ||
| "C0123", | ||
| "C0200", | ||
| "C0303", | ||
| "E0100", | ||
| "E0101", | ||
| "E0102", | ||
| "E0103", | ||
| "E0104", | ||
| "E0105", | ||
| "E0106", | ||
| "E0107", | ||
| "E0108", | ||
| "E0110", | ||
| "E0112", | ||
| "E0113", | ||
| "E0114", | ||
| "E0115", | ||
| "E0116", | ||
| "E0117", | ||
| "E0202", | ||
| "E0203", | ||
| "E0211", | ||
| "E0236", | ||
| "E0238", | ||
| "E0239", | ||
| "E0240", | ||
| "E0241", | ||
| "E0301", | ||
| "E0302", | ||
| "E0601", | ||
| "E0603", | ||
| "E0604", | ||
| "E0701", | ||
| "E0702", | ||
| "E0703", | ||
| "E0704", | ||
| "E0710", | ||
| "E0711", | ||
| "E0712", | ||
| "E1003", | ||
| "E1102", | ||
| "E1111", | ||
| "E1120", | ||
| "E1121", | ||
| "E1123", | ||
| "E1124", | ||
| "E1125", | ||
| "E1126", | ||
| "E1127", | ||
| "E1132", | ||
| "E1200", | ||
| "E1201", | ||
| "E1205", | ||
| "E1206", | ||
| "E1300", | ||
| "E1301", | ||
| "E1302", | ||
| "E1303", | ||
| "E1304", | ||
| "E1305", | ||
| "E1306", | ||
| "R0201", | ||
| "R0202", | ||
| "R0203", | ||
| "W0101", | ||
| "W0102", | ||
| "W0104", | ||
| "W0105", | ||
| "W0106", | ||
| "W0107", | ||
| "W0108", | ||
| "W0109", | ||
| "W0120", | ||
| "W0122", | ||
| "W0124", | ||
| "W0150", | ||
| "W0199", | ||
| "W0221", | ||
| "W0222", | ||
| "W0233", | ||
| "W0404", | ||
| "W0410", | ||
| "W0601", | ||
| "W0602", | ||
| "W0604", | ||
| "W0611", | ||
| "W0612", | ||
| "W0622", | ||
| "W0623", | ||
| "W0702", | ||
| "W0705", | ||
| "W0711", | ||
| "W1300", | ||
| "W1301", | ||
| "W1302", | ||
| "W1303", | ||
| "W1305", | ||
| "W1306", | ||
| "W1307", | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package pylint | ||
|
|
||
| import "codacy/cli-v2/domain" | ||
|
|
||
| // PatternDefaultParameters contains the default parameters for Pylint patterns | ||
| var PatternDefaultParameters = map[string][]domain.ParameterConfiguration{ | ||
| "R0914": { | ||
| { | ||
| Name: "max-locals", | ||
| Value: "15", | ||
| }, | ||
| }, | ||
| "C0301": { | ||
| { | ||
| Name: "max-line-length", | ||
| Value: "120", | ||
| }, | ||
| }, | ||
| "C0102": { | ||
| { | ||
| Name: "bad-names", | ||
| Value: "foo,bar,baz,toto,tutu,tata", | ||
| }, | ||
| }, | ||
| "C0103": { | ||
| { | ||
| Name: "argument-rgx", | ||
| Value: "[a-z_][a-z0-9_]{2,30}$", | ||
| }, | ||
| { | ||
| Name: "attr-rgx", | ||
| Value: "[a-z_][a-z0-9_]{2,30}$", | ||
| }, | ||
| { | ||
| Name: "class-rgx", | ||
| Value: "[A-Z_][a-zA-Z0-9]+$", | ||
| }, | ||
| { | ||
| Name: "const-rgx", | ||
| Value: "(([A-Z_][A-Z0-9_]*)|(__.*__))$", | ||
| }, | ||
| { | ||
| Name: "function-rgx", | ||
| Value: "[a-z_][a-z0-9_]{2,30}$", | ||
| }, | ||
| { | ||
| Name: "method-rgx", | ||
| Value: "[a-z_][a-z0-9_]{2,30}$", | ||
| }, | ||
| { | ||
| Name: "module-rgx", | ||
| Value: "(([a-z_][a-z0-9_]*)|([A-Z][a-zA-Z0-9]+))$", | ||
| }, | ||
| { | ||
| Name: "variable-rgx", | ||
| Value: "[a-z_][a-z0-9_]{2,30}$", | ||
| }, | ||
| { | ||
| Name: "inlinevar-rgx", | ||
| Value: "[A-Za-z_][A-Za-z0-9_]*$", | ||
| }, | ||
| { | ||
| Name: "class-attribute-rgx", | ||
| Value: "([A-Za-z_][A-Za-z0-9_]{2,30}|(__.*__))$", | ||
| }, | ||
| }, | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is the thing that we can change to get from the API, but I am fine moving forward with this on this scope