Skip to content

Commit 87ce0a8

Browse files
committed
[PLUTO-1431] Revive config
1 parent f0b8976 commit 87ce0a8

File tree

6 files changed

+91
-13
lines changed

6 files changed

+91
-13
lines changed

.codacy/codacy.yaml

Lines changed: 2 additions & 3 deletions

cmd/configsetup/setup.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"codacy/cli-v2/tools"
1616
"codacy/cli-v2/tools/lizard"
1717
"codacy/cli-v2/tools/pylint"
18+
reviveTool "codacy/cli-v2/tools/revive"
1819
"codacy/cli-v2/utils"
1920
)
2021

@@ -374,6 +375,11 @@ func createToolFileConfigurations(tool domain.Tool, patternConfiguration []domai
374375
return fmt.Errorf("failed to create Lizard config: %v", err)
375376
}
376377
fmt.Println("Lizard configuration created based on Codacy settings")
378+
case domain.Revive:
379+
if err := createReviveConfigFile(patternConfiguration, toolsConfigDir); err != nil {
380+
return fmt.Errorf("failed to write revive config: %v", err)
381+
}
382+
fmt.Println("Revive configuration created based on Codacy settings")
377383
}
378384
return nil
379385
}
@@ -466,6 +472,11 @@ func createLizardConfigFile(toolsConfigDir string, patternConfiguration []domain
466472
return nil
467473
}
468474

475+
func createReviveConfigFile(config []domain.PatternConfiguration, toolsConfigDir string) error {
476+
reviveConfigurationString := reviveTool.GenerateReviveConfig(config)
477+
return os.WriteFile(filepath.Join(toolsConfigDir, "revive.toml"), []byte(reviveConfigurationString), utils.DefaultFilePerms)
478+
}
479+
469480
// buildDefaultConfigurationFiles creates default configuration files for all tools
470481
func BuildDefaultConfigurationFiles(toolsConfigDir string, flags domain.InitFlags) error {
471482
for uuid := range domain.SupportedToolsMetadata {
@@ -502,6 +513,10 @@ func BuildDefaultConfigurationFiles(toolsConfigDir string, flags domain.InitFlag
502513
if err := createLizardConfigFile(toolsConfigDir, patternsConfig); err != nil {
503514
return fmt.Errorf("failed to create default Lizard configuration: %w", err)
504515
}
516+
case domain.Revive:
517+
if err := createReviveConfigFile(patternsConfig, toolsConfigDir); err != nil {
518+
return fmt.Errorf("failed to create default Revive configuration: %w", err)
519+
}
505520
case domain.PMD7, domain.ESLint9:
506521
continue
507522
}

domain/tool.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
DartAnalyzer string = "d203d615-6cf1-41f9-be5f-e2f660f7850f"
2828
Semgrep string = "6792c561-236d-41b7-ba5e-9d6bee0d548b"
2929
Lizard string = "76348462-84b3-409a-90d3-955e90abfb87"
30+
Revive string = "bd81d1f4-1406-402d-9181-1274ee09f1aa"
3031
)
3132

3233
type ToolInfo struct {
@@ -45,4 +46,5 @@ var SupportedToolsMetadata = map[string]ToolInfo{
4546
DartAnalyzer: {Name: "dartanalyzer", Priority: 0},
4647
Lizard: {Name: "lizard", Priority: 0},
4748
Semgrep: {Name: "semgrep", Priority: 0},
49+
Revive: {Name: "revive", Priority: 0},
4850
}

plugins/tools/revive/plugin.yaml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ binaries:
1212
- name: revive
1313
path: revive
1414
formatters:
15-
- name: stylish
16-
flag: "-formatter stylish"
17-
- name: json
18-
flag: "-formatter json"
15+
- name: sarif
16+
flag: "-formatter sarif"
1917
output_options:
2018
file_flag: "-o"
2119
analysis_options:
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package tools
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"codacy/cli-v2/domain"
8+
)
9+
10+
// GenerateReviveConfig generates a TOML config for revive based on enabled patterns and their parameters
11+
func GenerateReviveConfig(patterns []domain.PatternConfiguration) string {
12+
var sb strings.Builder
13+
sb.WriteString("[revive]\n")
14+
sb.WriteString("ignoreGeneratedHeader = true\n")
15+
sb.WriteString("severity = \"warning\"\n")
16+
sb.WriteString("confidence = 0.8\n")
17+
sb.WriteString("errorCode = 0\n")
18+
sb.WriteString("warningCode = 0\n\n")
19+
20+
enabledRules := make([]string, 0)
21+
for _, pattern := range patterns {
22+
if pattern.Enabled {
23+
ruleName := strings.TrimPrefix(pattern.PatternDefinition.Id, "Revive_")
24+
enabledRules = append(enabledRules, fmt.Sprintf("\"%s\"", ruleName))
25+
}
26+
}
27+
if len(enabledRules) > 0 {
28+
sb.WriteString(fmt.Sprintf("rules = [%s]\n\n", strings.Join(enabledRules, ", ")))
29+
}
30+
31+
for _, pattern := range patterns {
32+
if pattern.Enabled {
33+
ruleName := strings.TrimPrefix(pattern.PatternDefinition.Id, "Revive_")
34+
sb.WriteString(fmt.Sprintf("[rule.%s]\n", ruleName))
35+
if len(pattern.Parameters) > 0 {
36+
sb.WriteString("arguments = [")
37+
args := make([]string, 0)
38+
for _, param := range pattern.Parameters {
39+
// TOML: string values should be quoted
40+
args = append(args, fmt.Sprintf("\"%s\"", param.Value))
41+
}
42+
sb.WriteString(strings.Join(args, ", "))
43+
sb.WriteString("]\n")
44+
}
45+
sb.WriteString("\n")
46+
}
47+
}
48+
49+
return sb.String()
50+
}

tools/revive/reviveRunner.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@ import (
55
"log"
66
"os"
77
"os/exec"
8+
9+
"codacy/cli-v2/config"
10+
parenttools "codacy/cli-v2/tools"
811
)
912

1013
// RunRevive executes revive analysis on the specified files or directory
1114
func RunRevive(workDirectory string, binary string, files []string, outputFile string, outputFormat string) error {
1215
cmdArgs := []string{}
1316

17+
// Check if a config file exists in the expected location and use it if present
18+
if configFile, exists := parenttools.ConfigFileExists(config.Config, "revive.toml"); exists {
19+
cmdArgs = append(cmdArgs, "-config", configFile)
20+
}
21+
1422
// Add output format if specified
1523
if outputFormat != "" {
1624
cmdArgs = append(cmdArgs, "-formatter", outputFormat)
1725
}
1826

19-
// Add output file if specified
20-
if outputFile != "" {
21-
cmdArgs = append(cmdArgs, "-o", outputFile)
22-
}
23-
2427
// Add files to analyze - if no files specified, analyze current directory
2528
if len(files) > 0 {
2629
cmdArgs = append(cmdArgs, files...)
@@ -30,9 +33,20 @@ func RunRevive(workDirectory string, binary string, files []string, outputFile s
3033

3134
cmd := exec.Command(binary, cmdArgs...)
3235
cmd.Dir = workDirectory
33-
cmd.Stdout = os.Stdout
3436
cmd.Stderr = os.Stderr
3537

38+
// Handle output file redirection
39+
if outputFile != "" {
40+
outputWriter, err := os.Create(outputFile)
41+
if err != nil {
42+
return fmt.Errorf("failed to create output file: %w", err)
43+
}
44+
defer outputWriter.Close()
45+
cmd.Stdout = outputWriter
46+
} else {
47+
cmd.Stdout = os.Stdout
48+
}
49+
3650
if err := cmd.Run(); err != nil {
3751
if _, ok := err.(*exec.ExitError); !ok {
3852
log.Printf("[REVIVE] Error running revive: %v", err)

0 commit comments

Comments
 (0)