Skip to content

Commit 835a54c

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 396a734 commit 835a54c

File tree

8 files changed

+114320
-17
lines changed

8 files changed

+114320
-17
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ go.work.sum
2727
# Codacy CLI
2828
cli-v2
2929
codacy-cli
30+
31+
#Macos
32+
.DS_Store

cmd/init.go

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,11 @@ func configFileTemplate(tools []tools.Tool) string {
141141

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",
148+
Semgrep: "1.78.0",
148149
}
149150

150151
// Build map of enabled tools with their versions
@@ -188,10 +189,11 @@ func configFileTemplate(tools []tools.Tool) string {
188189
if len(tools) > 0 {
189190
// Add only the tools that are in the API response (enabled tools)
190191
uuidToName := map[string]string{
191-
ESLint: "eslint",
192-
Trivy: "trivy",
193-
PyLint: "pylint",
194-
PMD: "pmd",
192+
ESLint: "eslint",
193+
Trivy: "trivy",
194+
PyLint: "pylint",
195+
PMD: "pmd",
196+
Semgrep: "semgrep",
195197
}
196198

197199
for uuid, name := range uuidToName {
@@ -205,6 +207,7 @@ func configFileTemplate(tools []tools.Tool) string {
205207
sb.WriteString(fmt.Sprintf(" - trivy@%s\n", defaultVersions[Trivy]))
206208
sb.WriteString(fmt.Sprintf(" - pylint@%s\n", defaultVersions[PyLint]))
207209
sb.WriteString(fmt.Sprintf(" - pmd@%s\n", defaultVersions[PMD]))
210+
sb.WriteString(fmt.Sprintf(" - semgrep@%s\n", defaultVersions[Semgrep]))
208211
}
209212

210213
return sb.String()
@@ -257,7 +260,8 @@ func buildRepositoryConfigurationFiles(token string) error {
257260

258261
// Only generate config files for tools not using their own config file
259262
for _, tool := range configuredToolsWithUI {
260-
url := fmt.Sprintf("%s/api/v3/analysis/organizations/%s/%s/repositories/%s/tools/%s/patterns?enabled=true",
263+
264+
url := fmt.Sprintf("%s/api/v3/analysis/organizations/%s/%s/repositories/%s/tools/%s/patterns?enabled=true&limit=1000",
261265
CodacyApiBase,
262266
initFlags.provider,
263267
initFlags.organization,
@@ -380,6 +384,17 @@ func createToolFileConfigurations(tool tools.Tool, patternConfiguration []domain
380384
}
381385
}
382386
fmt.Println("Pylint configuration created based on Codacy settings")
387+
case Semgrep:
388+
if len(patternConfiguration) > 0 {
389+
err := createSemgrepConfigFile(patternConfiguration, toolsConfigDir)
390+
if err != nil {
391+
return fmt.Errorf("failed to create Semgrep config: %v", err)
392+
}
393+
fmt.Println("Semgrep configuration created based on Codacy settings")
394+
} else {
395+
// In case of no patterns, we run semgrep with default config
396+
return nil
397+
}
383398
}
384399
return nil
385400
}
@@ -434,6 +449,24 @@ func createDefaultEslintConfigFile(toolsConfigDir string) error {
434449
return os.WriteFile(filepath.Join(toolsConfigDir, "eslint.config.mjs"), []byte(content), utils.DefaultFilePerms)
435450
}
436451

452+
// SemgrepRulesFile represents the structure of the rules.yaml file
453+
type SemgrepRulesFile struct {
454+
Rules []map[string]interface{} `yaml:"rules"`
455+
}
456+
457+
// createSemgrepConfigFile creates a semgrep.yaml configuration file based on the API configuration
458+
func createSemgrepConfigFile(config []domain.PatternConfiguration, toolsConfigDir string) error {
459+
// Use the refactored function from tools package
460+
configData, err := tools.GetSemgrepConfig(config)
461+
462+
if err != nil {
463+
return fmt.Errorf("failed to create Semgrep config: %v", err)
464+
}
465+
466+
// Write to file
467+
return os.WriteFile(filepath.Join(toolsConfigDir, "semgrep.yaml"), configData, utils.DefaultFilePerms)
468+
}
469+
437470
// cleanConfigDirectory removes all previous configuration files in the tools-configs directory
438471
func cleanConfigDirectory(toolsConfigDir string) error {
439472
// Check if directory exists
@@ -462,8 +495,9 @@ func cleanConfigDirectory(toolsConfigDir string) error {
462495
}
463496

464497
const (
465-
ESLint string = "f8b29663-2cb2-498d-b923-a10c6a8c05cd"
466-
Trivy string = "2fd7fbe0-33f9-4ab3-ab73-e9b62404e2cb"
467-
PMD string = "9ed24812-b6ee-4a58-9004-0ed183c45b8f"
468-
PyLint string = "31677b6d-4ae0-4f56-8041-606a8d7a8e61"
498+
ESLint string = "f8b29663-2cb2-498d-b923-a10c6a8c05cd"
499+
Trivy string = "2fd7fbe0-33f9-4ab3-ab73-e9b62404e2cb"
500+
PMD string = "9ed24812-b6ee-4a58-9004-0ed183c45b8f"
501+
PyLint string = "31677b6d-4ae0-4f56-8041-606a8d7a8e61"
502+
Semgrep string = "6792c561-236d-41b7-ba5e-9d6bee0d548b"
469503
)

domain/patternConfiguration.go

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

88
type PatternDefinition struct {
9-
Id string `json:"id"`
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"`
1020
}
1121

1222
type PatternConfiguration struct {
1323
PatternDefinition PatternDefinition `json:"patternDefinition"`
1424
Parameters []ParameterConfiguration
25+
Enabled bool `json:"enabled"`
26+
IsCustom bool `json:"isCustom"`
1527
}

0 commit comments

Comments
 (0)