Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
373 changes: 141 additions & 232 deletions cmd/init.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func showWelcomeMessage() {
fmt.Println()
cyan.Println("Initialize your project with:")
fmt.Println(" codacy-cli init --repository-token YOUR_TOKEN")
fmt.Println(" codacy-cli init --codacy-api-token YOUR_TOKEN")
fmt.Println()
fmt.Println("Or run without a token to use local configuration:")
fmt.Println(" codacy-cli init")
Expand Down
15 changes: 15 additions & 0 deletions domain/patternConfiguration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package domain

type ParameterConfiguration struct {

Check notice on line 3 in domain/patternConfiguration.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

domain/patternConfiguration.go#L3

exported type ParameterConfiguration should have comment or be unexported
Name string `json:"name"`
Value string `json:"value"`
}

type PatternDefinition struct {

Check notice on line 8 in domain/patternConfiguration.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

domain/patternConfiguration.go#L8

exported type PatternDefinition should have comment or be unexported
Id string `json:"id"`

Check notice on line 9 in domain/patternConfiguration.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

domain/patternConfiguration.go#L9

struct field Id should be ID
}

type PatternConfiguration struct {

Check notice on line 12 in domain/patternConfiguration.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

domain/patternConfiguration.go#L12

exported type PatternConfiguration should have comment or be unexported
PatternDefinition PatternDefinition `json:"patternDefinition"`
Parameters []ParameterConfiguration
}
31 changes: 31 additions & 0 deletions plugins/tool-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,34 @@ func getDownloadURL(urlTemplate string, fileName string, version string, mappedA

return buf.String()
}

// GetSupportedTools returns a map of supported tool names based on the tools folder
func GetSupportedTools() (map[string]struct{}, error) {
supportedTools := make(map[string]struct{})

// Read all directories in the tools folder
entries, err := toolsFS.ReadDir("tools")
if err != nil {
return nil, fmt.Errorf("failed to read tools directory: %w", err)
}

// For each directory, check if it has a plugin.yaml file
for _, entry := range entries {
if !entry.IsDir() {
continue
}

toolName := entry.Name()
pluginPath := filepath.Join("tools", toolName, "plugin.yaml")

// Check if plugin.yaml exists
_, err := toolsFS.ReadFile(pluginPath)
if err != nil {
continue // Skip if no plugin.yaml
}

supportedTools[toolName] = struct{}{}
}

return supportedTools, nil
}
42 changes: 42 additions & 0 deletions plugins/tool-utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,45 @@ func TestProcessToolsWithDownload(t *testing.T) {
}
assert.Contains(t, trivyInfo.DownloadURL, expectedArch)
}

func TestGetSupportedTools(t *testing.T) {
tests := []struct {
name string
expectedTools []string
expectedError bool
}{
{
name: "should return supported tools",
expectedTools: []string{
"eslint",
"pmd",
"pylint",
"trivy",
},
expectedError: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
supportedTools, err := GetSupportedTools()

if tt.expectedError {
assert.Error(t, err)
return
}

assert.NoError(t, err)
assert.NotNil(t, supportedTools)

// Check that all expected tools are supported
for _, expectedTool := range tt.expectedTools {
_, exists := supportedTools[expectedTool]
assert.True(t, exists, "tool %s should be supported", expectedTool)
}

// Check that we have exactly the expected number of tools
assert.Equal(t, len(tt.expectedTools), len(supportedTools), "number of supported tools should match")
})
}
}
11 changes: 6 additions & 5 deletions tools/eslintConfigCreator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tools

import (
"codacy/cli-v2/domain"
"encoding/json"
"fmt"
"strings"
Expand All @@ -18,14 +19,14 @@
}
}

func CreateEslintConfig(configuration ToolConfiguration) string {
func CreateEslintConfig(configuration []domain.PatternConfiguration) string {

Check failure on line 22 in tools/eslintConfigCreator.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tools/eslintConfigCreator.go#L22

Method CreateEslintConfig has a cyclomatic complexity of 11 (limit is 10)

Check notice on line 22 in tools/eslintConfigCreator.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tools/eslintConfigCreator.go#L22

exported function CreateEslintConfig should have comment or be unexported
result := `export default [
{
rules: {
`

for _, patternConfiguration := range configuration.PatternsConfiguration {
rule := strings.TrimPrefix(patternConfiguration.PatternId, "ESLint8_")
for _, patternConfiguration := range configuration {
rule := strings.TrimPrefix(patternConfiguration.PatternDefinition.Id, "ESLint8_")

const tempstring = "TEMPORARYSTRING"
rule = strings.ReplaceAll(rule, "__", tempstring)
Expand All @@ -34,15 +35,15 @@

parametersString := ""

for _, parameter := range patternConfiguration.ParameterConfigurations {
for _, parameter := range patternConfiguration.Parameters {
if parameter.Name == "unnamedParam" {
parametersString += quoteWhenIsNotJson(parameter.Value)
}
}

// build named parameters json object
namedParametersString := ""
for _, parameter := range patternConfiguration.ParameterConfigurations {
for _, parameter := range patternConfiguration.Parameters {

if parameter.Name != "unnamedParam" {
if len(namedParametersString) == 0 {
Expand Down
83 changes: 42 additions & 41 deletions tools/eslintConfigCreator_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package tools

import (
"codacy/cli-v2/domain"
"testing"

"github.com/stretchr/testify/assert"
)

func testConfig(t *testing.T, configuration ToolConfiguration, expected string) {
func testConfig(t *testing.T, configuration []domain.PatternConfiguration, expected string) {
actual := CreateEslintConfig(configuration)
assert.Equal(t, expected, actual)
}

func TestCreateEslintConfigEmptyConfig(t *testing.T) {
testConfig(t,
ToolConfiguration{},
[]domain.PatternConfiguration{},
`export default [
{
rules: {
Expand All @@ -24,10 +25,10 @@ func TestCreateEslintConfigEmptyConfig(t *testing.T) {

func TestCreateEslintConfigConfig1(t *testing.T) {
testConfig(t,
ToolConfiguration{
PatternsConfiguration: []PatternConfiguration{
{
PatternId: "ESLint8_semi",
[]domain.PatternConfiguration{
{
PatternDefinition: domain.PatternDefinition{
Id: "ESLint8_semi",
},
},
},
Expand All @@ -42,15 +43,15 @@ func TestCreateEslintConfigConfig1(t *testing.T) {

func TestCreateEslintConfigUnnamedParam(t *testing.T) {
testConfig(t,
ToolConfiguration{
PatternsConfiguration: []PatternConfiguration{
{
PatternId: "ESLint8_semi",
ParameterConfigurations: []PatternParameterConfiguration{
{
Name: "unnamedParam",
Value: "never",
},
[]domain.PatternConfiguration{
{
PatternDefinition: domain.PatternDefinition{
Id: "ESLint8_semi",
},
Parameters: []domain.ParameterConfiguration{
{
Name: "unnamedParam",
Value: "never",
},
},
},
Expand All @@ -66,15 +67,15 @@ func TestCreateEslintConfigUnnamedParam(t *testing.T) {

func TestCreateEslintConfigNamedParam(t *testing.T) {
testConfig(t,
ToolConfiguration{
PatternsConfiguration: []PatternConfiguration{
{
PatternId: "consistent-return",
ParameterConfigurations: []PatternParameterConfiguration{
{
Name: "treatUndefinedAsUnspecified",
Value: "false",
},
[]domain.PatternConfiguration{
{
PatternDefinition: domain.PatternDefinition{
Id: "consistent-return",
},
Parameters: []domain.ParameterConfiguration{
{
Name: "treatUndefinedAsUnspecified",
Value: "false",
},
},
},
Expand All @@ -90,19 +91,19 @@ func TestCreateEslintConfigNamedParam(t *testing.T) {

func TestCreateEslintConfigUnnamedAndNamedParam(t *testing.T) {
testConfig(t,
ToolConfiguration{
PatternsConfiguration: []PatternConfiguration{
{
PatternId: "consistent-return",
ParameterConfigurations: []PatternParameterConfiguration{
{
Name: "treatUndefinedAsUnspecified",
Value: "false",
},
{
Name: "unnamedParam",
Value: "foo",
},
[]domain.PatternConfiguration{
{
PatternDefinition: domain.PatternDefinition{
Id: "consistent-return",
},
Parameters: []domain.ParameterConfiguration{
{
Name: "treatUndefinedAsUnspecified",
Value: "false",
},
{
Name: "unnamedParam",
Value: "foo",
},
},
},
Expand All @@ -118,10 +119,10 @@ func TestCreateEslintConfigUnnamedAndNamedParam(t *testing.T) {

func TestCreateEslintConfigSupportPlugins(t *testing.T) {
testConfig(t,
ToolConfiguration{
PatternsConfiguration: []PatternConfiguration{
{
PatternId: "plugin/consistent-return",
[]domain.PatternConfiguration{
{
PatternDefinition: domain.PatternDefinition{
Id: "plugin/consistent-return",
},
},
},
Expand Down
Loading