Skip to content

Commit 541c44f

Browse files
wip: semgrep patterns empty
1 parent 90d4e4f commit 541c44f

File tree

7 files changed

+2286
-19
lines changed

7 files changed

+2286
-19
lines changed

.codacy/codacy.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
runtimes:
22
3-
43
tools:
5-
4+
5+
66
7-
87
9-

cmd/init.go

Lines changed: 46 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,20 @@ func createToolFileConfigurations(tool tools.Tool, patternConfiguration []domain
380384
}
381385
}
382386
fmt.Println("Pylint configuration created based on Codacy settings")
387+
case Semgrep:
388+
fmt.Printf("Pattern configuration for Semgrep: %+v\n", patternConfiguration)
389+
if len(patternConfiguration) > 0 {
390+
err := createSemgrepConfigFile(patternConfiguration, toolsConfigDir)
391+
if err != nil {
392+
return fmt.Errorf("failed to create Semgrep config: %v", err)
393+
}
394+
} else {
395+
err := createDefaultSemgrepConfigFile(toolsConfigDir)
396+
if err != nil {
397+
return fmt.Errorf("failed to create default Semgrep config: %v", err)
398+
}
399+
}
400+
fmt.Println("Semgrep configuration created based on Codacy settings")
383401
}
384402
return nil
385403
}
@@ -434,6 +452,20 @@ func createDefaultEslintConfigFile(toolsConfigDir string) error {
434452
return os.WriteFile(filepath.Join(toolsConfigDir, "eslint.config.mjs"), []byte(content), utils.DefaultFilePerms)
435453
}
436454

455+
// createSemgrepConfigFile creates a semgrep.yaml configuration file based on the API configuration
456+
func createSemgrepConfigFile(config []domain.PatternConfiguration, toolsConfigDir string) error {
457+
semgrepConfigurationString := tools.CreateSemgrepConfig(config)
458+
return os.WriteFile(filepath.Join(toolsConfigDir, "semgrep.yaml"), []byte(semgrepConfigurationString), utils.DefaultFilePerms)
459+
}
460+
461+
// createDefaultSemgrepConfigFile creates a default semgrep.yaml configuration file
462+
func createDefaultSemgrepConfigFile(toolsConfigDir string) error {
463+
// Use empty tool configuration to get default settings
464+
emptyConfig := []domain.PatternConfiguration{}
465+
content := tools.CreateSemgrepConfig(emptyConfig)
466+
return os.WriteFile(filepath.Join(toolsConfigDir, "semgrep.yaml"), []byte(content), utils.DefaultFilePerms)
467+
}
468+
437469
// cleanConfigDirectory removes all previous configuration files in the tools-configs directory
438470
func cleanConfigDirectory(toolsConfigDir string) error {
439471
// Check if directory exists
@@ -462,8 +494,9 @@ func cleanConfigDirectory(toolsConfigDir string) error {
462494
}
463495

464496
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"
497+
ESLint string = "f8b29663-2cb2-498d-b923-a10c6a8c05cd"
498+
Trivy string = "2fd7fbe0-33f9-4ab3-ab73-e9b62404e2cb"
499+
PMD string = "9ed24812-b6ee-4a58-9004-0ed183c45b8f"
500+
PyLint string = "31677b6d-4ae0-4f56-8041-606a8d7a8e61"
501+
Semgrep string = "6792c561-236d-41b7-ba5e-9d6bee0d548b"
469502
)

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)