Skip to content

Commit 83238e7

Browse files
Semgrep config initilaization (#76) [Pluto-1391]
Support semgrep configuration from the cloud. The collection of semgrep patterns definitions is built-in into the CLI.
1 parent 31460f1 commit 83238e7

File tree

8 files changed

+114301
-18
lines changed

8 files changed

+114301
-18
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ codacy-cli
3131

3232
#Ignore cursor AI rules
3333
.cursor/rules/codacy.mdc
34+
35+
#Macos
36+
.DS_Store

cmd/init.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,12 @@ func configFileTemplate(tools []tools.Tool) string {
141141
needsDart := false
142142
// Default versions
143143
defaultVersions := map[string]string{
144-
ESLint: "9.3.0",
145-
Trivy: "0.59.1",
146-
PyLint: "3.3.6",
147-
PMD: "6.55.0",
144+
ESLint: "9.3.0",
145+
Trivy: "0.59.1",
146+
PyLint: "3.3.6",
147+
PMD: "6.55.0",
148148
DartAnalyzer: "3.7.2",
149+
Semgrep: "1.78.0",
149150
}
150151

151152
// Build map of enabled tools with their versions
@@ -200,6 +201,7 @@ func configFileTemplate(tools []tools.Tool) string {
200201
PyLint: "pylint",
201202
PMD: "pmd",
202203
DartAnalyzer: "dartanalyzer",
204+
Semgrep: "semgrep",
203205
}
204206

205207
for uuid, name := range uuidToName {
@@ -214,6 +216,7 @@ func configFileTemplate(tools []tools.Tool) string {
214216
sb.WriteString(fmt.Sprintf(" - pylint@%s\n", defaultVersions[PyLint]))
215217
sb.WriteString(fmt.Sprintf(" - pmd@%s\n", defaultVersions[PMD]))
216218
sb.WriteString(fmt.Sprintf(" - dartanalyzer@%s\n", defaultVersions[DartAnalyzer]))
219+
sb.WriteString(fmt.Sprintf(" - semgrep@%s\n", defaultVersions[Semgrep]))
217220
}
218221

219222
return sb.String()
@@ -266,7 +269,8 @@ func buildRepositoryConfigurationFiles(token string) error {
266269

267270
// Only generate config files for tools not using their own config file
268271
for _, tool := range configuredToolsWithUI {
269-
url := fmt.Sprintf("%s/api/v3/analysis/organizations/%s/%s/repositories/%s/tools/%s/patterns?enabled=true",
272+
273+
url := fmt.Sprintf("%s/api/v3/analysis/organizations/%s/%s/repositories/%s/tools/%s/patterns?enabled=true&limit=1000",
270274
CodacyApiBase,
271275
initFlags.provider,
272276
initFlags.organization,
@@ -389,13 +393,6 @@ func createToolFileConfigurations(tool tools.Tool, patternConfiguration []domain
389393
}
390394
}
391395
fmt.Println("Pylint configuration created based on Codacy settings")
392-
case DartAnalyzer:
393-
if len(patternConfiguration) > 0 {
394-
err := createDartAnalyzerConfigFile(patternConfiguration, toolsConfigDir)
395-
if err != nil {
396-
return fmt.Errorf("failed to create Dart Analyzer config: %v", err)
397-
}
398-
}
399396
}
400397
return nil
401398
}
@@ -456,6 +453,24 @@ func createDefaultEslintConfigFile(toolsConfigDir string) error {
456453
return os.WriteFile(filepath.Join(toolsConfigDir, "eslint.config.mjs"), []byte(content), utils.DefaultFilePerms)
457454
}
458455

456+
// SemgrepRulesFile represents the structure of the rules.yaml file
457+
type SemgrepRulesFile struct {
458+
Rules []map[string]interface{} `yaml:"rules"`
459+
}
460+
461+
// createSemgrepConfigFile creates a semgrep.yaml configuration file based on the API configuration
462+
func createSemgrepConfigFile(config []domain.PatternConfiguration, toolsConfigDir string) error {
463+
// Use the refactored function from tools package
464+
configData, err := tools.GetSemgrepConfig(config)
465+
466+
if err != nil {
467+
return fmt.Errorf("failed to create Semgrep config: %v", err)
468+
}
469+
470+
// Write to file
471+
return os.WriteFile(filepath.Join(toolsConfigDir, "semgrep.yaml"), configData, utils.DefaultFilePerms)
472+
}
473+
459474
// cleanConfigDirectory removes all previous configuration files in the tools-configs directory
460475
func cleanConfigDirectory(toolsConfigDir string) error {
461476
// Check if directory exists
@@ -489,4 +504,5 @@ const (
489504
PMD string = "9ed24812-b6ee-4a58-9004-0ed183c45b8f"
490505
PyLint string = "31677b6d-4ae0-4f56-8041-606a8d7a8e61"
491506
DartAnalyzer string = "d203d615-6cf1-41f9-be5f-e2f660f7850f"
507+
Semgrep string = "6792c561-236d-41b7-ba5e-9d6bee0d548b"
492508
)

domain/patternConfiguration.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,22 @@ type ParameterConfiguration struct {
66
}
77

88
type PatternDefinition struct {
9-
Id string `json:"id"`
10-
Category string `json:"category"`
11-
Level string `json:"level"`
9+
Id string `json:"id"`
10+
Category string `json:"category"`
11+
Level string `json:"level"`
12+
SeverityLevel string `json:"severityLevel"`
13+
Enabled bool `json:"enabled"`
14+
Parameters []ParameterConfiguration `json:"parameters"`
15+
Title string `json:"title"`
16+
Description string `json:"description"`
17+
Explanation string `json:"explanation"`
18+
Languages []string `json:"languages"`
19+
TimeToFix int `json:"timeToFix"`
1220
}
1321

1422
type PatternConfiguration struct {
1523
PatternDefinition PatternDefinition `json:"patternDefinition"`
1624
Parameters []ParameterConfiguration
25+
Enabled bool `json:"enabled"`
26+
IsCustom bool `json:"isCustom"`
1727
}

0 commit comments

Comments
 (0)