Skip to content

Commit dd841e6

Browse files
Merge pull request #58 from codacy/api-token-pluto-1379
breaking: support use of codacy api token - PLUTO-1379
2 parents 93219d2 + eff63cf commit dd841e6

13 files changed

+556
-466
lines changed

cmd/init.go

Lines changed: 141 additions & 232 deletions
Large diffs are not rendered by default.

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func showWelcomeMessage() {
4848
fmt.Println()
4949
cyan.Println("Initialize your project with:")
5050
fmt.Println(" codacy-cli init --repository-token YOUR_TOKEN")
51+
fmt.Println(" codacy-cli init --codacy-api-token YOUR_TOKEN")
5152
fmt.Println()
5253
fmt.Println("Or run without a token to use local configuration:")
5354
fmt.Println(" codacy-cli init")

domain/patternConfiguration.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package domain
2+
3+
type ParameterConfiguration struct {
4+
Name string `json:"name"`
5+
Value string `json:"value"`
6+
}
7+
8+
type PatternDefinition struct {
9+
Id string `json:"id"`
10+
}
11+
12+
type PatternConfiguration struct {
13+
PatternDefinition PatternDefinition `json:"patternDefinition"`
14+
Parameters []ParameterConfiguration
15+
}

plugins/tool-utils.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,3 +294,34 @@ func getDownloadURL(urlTemplate string, fileName string, version string, mappedA
294294

295295
return buf.String()
296296
}
297+
298+
// GetSupportedTools returns a map of supported tool names based on the tools folder
299+
func GetSupportedTools() (map[string]struct{}, error) {
300+
supportedTools := make(map[string]struct{})
301+
302+
// Read all directories in the tools folder
303+
entries, err := toolsFS.ReadDir("tools")
304+
if err != nil {
305+
return nil, fmt.Errorf("failed to read tools directory: %w", err)
306+
}
307+
308+
// For each directory, check if it has a plugin.yaml file
309+
for _, entry := range entries {
310+
if !entry.IsDir() {
311+
continue
312+
}
313+
314+
toolName := entry.Name()
315+
pluginPath := filepath.Join("tools", toolName, "plugin.yaml")
316+
317+
// Check if plugin.yaml exists
318+
_, err := toolsFS.ReadFile(pluginPath)
319+
if err != nil {
320+
continue // Skip if no plugin.yaml
321+
}
322+
323+
supportedTools[toolName] = struct{}{}
324+
}
325+
326+
return supportedTools, nil
327+
}

plugins/tool-utils_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,45 @@ func TestProcessToolsWithDownload(t *testing.T) {
152152
}
153153
assert.Contains(t, trivyInfo.DownloadURL, expectedArch)
154154
}
155+
156+
func TestGetSupportedTools(t *testing.T) {
157+
tests := []struct {
158+
name string
159+
expectedTools []string
160+
expectedError bool
161+
}{
162+
{
163+
name: "should return supported tools",
164+
expectedTools: []string{
165+
"eslint",
166+
"pmd",
167+
"pylint",
168+
"trivy",
169+
},
170+
expectedError: false,
171+
},
172+
}
173+
174+
for _, tt := range tests {
175+
t.Run(tt.name, func(t *testing.T) {
176+
supportedTools, err := GetSupportedTools()
177+
178+
if tt.expectedError {
179+
assert.Error(t, err)
180+
return
181+
}
182+
183+
assert.NoError(t, err)
184+
assert.NotNil(t, supportedTools)
185+
186+
// Check that all expected tools are supported
187+
for _, expectedTool := range tt.expectedTools {
188+
_, exists := supportedTools[expectedTool]
189+
assert.True(t, exists, "tool %s should be supported", expectedTool)
190+
}
191+
192+
// Check that we have exactly the expected number of tools
193+
assert.Equal(t, len(tt.expectedTools), len(supportedTools), "number of supported tools should match")
194+
})
195+
}
196+
}

tools/eslintConfigCreator.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tools
22

33
import (
4+
"codacy/cli-v2/domain"
45
"encoding/json"
56
"fmt"
67
"strings"
@@ -18,14 +19,14 @@ func quoteWhenIsNotJson(value string) string {
1819
}
1920
}
2021

21-
func CreateEslintConfig(configuration ToolConfiguration) string {
22+
func CreateEslintConfig(configuration []domain.PatternConfiguration) string {
2223
result := `export default [
2324
{
2425
rules: {
2526
`
2627

27-
for _, patternConfiguration := range configuration.PatternsConfiguration {
28-
rule := strings.TrimPrefix(patternConfiguration.PatternId, "ESLint8_")
28+
for _, patternConfiguration := range configuration {
29+
rule := strings.TrimPrefix(patternConfiguration.PatternDefinition.Id, "ESLint8_")
2930

3031
const tempstring = "TEMPORARYSTRING"
3132
rule = strings.ReplaceAll(rule, "__", tempstring)
@@ -34,15 +35,15 @@ func CreateEslintConfig(configuration ToolConfiguration) string {
3435

3536
parametersString := ""
3637

37-
for _, parameter := range patternConfiguration.ParameterConfigurations {
38+
for _, parameter := range patternConfiguration.Parameters {
3839
if parameter.Name == "unnamedParam" {
3940
parametersString += quoteWhenIsNotJson(parameter.Value)
4041
}
4142
}
4243

4344
// build named parameters json object
4445
namedParametersString := ""
45-
for _, parameter := range patternConfiguration.ParameterConfigurations {
46+
for _, parameter := range patternConfiguration.Parameters {
4647

4748
if parameter.Name != "unnamedParam" {
4849
if len(namedParametersString) == 0 {

tools/eslintConfigCreator_test.go

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
package tools
22

33
import (
4+
"codacy/cli-v2/domain"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
78
)
89

9-
func testConfig(t *testing.T, configuration ToolConfiguration, expected string) {
10+
func testConfig(t *testing.T, configuration []domain.PatternConfiguration, expected string) {
1011
actual := CreateEslintConfig(configuration)
1112
assert.Equal(t, expected, actual)
1213
}
1314

1415
func TestCreateEslintConfigEmptyConfig(t *testing.T) {
1516
testConfig(t,
16-
ToolConfiguration{},
17+
[]domain.PatternConfiguration{},
1718
`export default [
1819
{
1920
rules: {
@@ -24,10 +25,10 @@ func TestCreateEslintConfigEmptyConfig(t *testing.T) {
2425

2526
func TestCreateEslintConfigConfig1(t *testing.T) {
2627
testConfig(t,
27-
ToolConfiguration{
28-
PatternsConfiguration: []PatternConfiguration{
29-
{
30-
PatternId: "ESLint8_semi",
28+
[]domain.PatternConfiguration{
29+
{
30+
PatternDefinition: domain.PatternDefinition{
31+
Id: "ESLint8_semi",
3132
},
3233
},
3334
},
@@ -42,15 +43,15 @@ func TestCreateEslintConfigConfig1(t *testing.T) {
4243

4344
func TestCreateEslintConfigUnnamedParam(t *testing.T) {
4445
testConfig(t,
45-
ToolConfiguration{
46-
PatternsConfiguration: []PatternConfiguration{
47-
{
48-
PatternId: "ESLint8_semi",
49-
ParameterConfigurations: []PatternParameterConfiguration{
50-
{
51-
Name: "unnamedParam",
52-
Value: "never",
53-
},
46+
[]domain.PatternConfiguration{
47+
{
48+
PatternDefinition: domain.PatternDefinition{
49+
Id: "ESLint8_semi",
50+
},
51+
Parameters: []domain.ParameterConfiguration{
52+
{
53+
Name: "unnamedParam",
54+
Value: "never",
5455
},
5556
},
5657
},
@@ -66,15 +67,15 @@ func TestCreateEslintConfigUnnamedParam(t *testing.T) {
6667

6768
func TestCreateEslintConfigNamedParam(t *testing.T) {
6869
testConfig(t,
69-
ToolConfiguration{
70-
PatternsConfiguration: []PatternConfiguration{
71-
{
72-
PatternId: "consistent-return",
73-
ParameterConfigurations: []PatternParameterConfiguration{
74-
{
75-
Name: "treatUndefinedAsUnspecified",
76-
Value: "false",
77-
},
70+
[]domain.PatternConfiguration{
71+
{
72+
PatternDefinition: domain.PatternDefinition{
73+
Id: "consistent-return",
74+
},
75+
Parameters: []domain.ParameterConfiguration{
76+
{
77+
Name: "treatUndefinedAsUnspecified",
78+
Value: "false",
7879
},
7980
},
8081
},
@@ -90,19 +91,19 @@ func TestCreateEslintConfigNamedParam(t *testing.T) {
9091

9192
func TestCreateEslintConfigUnnamedAndNamedParam(t *testing.T) {
9293
testConfig(t,
93-
ToolConfiguration{
94-
PatternsConfiguration: []PatternConfiguration{
95-
{
96-
PatternId: "consistent-return",
97-
ParameterConfigurations: []PatternParameterConfiguration{
98-
{
99-
Name: "treatUndefinedAsUnspecified",
100-
Value: "false",
101-
},
102-
{
103-
Name: "unnamedParam",
104-
Value: "foo",
105-
},
94+
[]domain.PatternConfiguration{
95+
{
96+
PatternDefinition: domain.PatternDefinition{
97+
Id: "consistent-return",
98+
},
99+
Parameters: []domain.ParameterConfiguration{
100+
{
101+
Name: "treatUndefinedAsUnspecified",
102+
Value: "false",
103+
},
104+
{
105+
Name: "unnamedParam",
106+
Value: "foo",
106107
},
107108
},
108109
},
@@ -118,10 +119,10 @@ func TestCreateEslintConfigUnnamedAndNamedParam(t *testing.T) {
118119

119120
func TestCreateEslintConfigSupportPlugins(t *testing.T) {
120121
testConfig(t,
121-
ToolConfiguration{
122-
PatternsConfiguration: []PatternConfiguration{
123-
{
124-
PatternId: "plugin/consistent-return",
122+
[]domain.PatternConfiguration{
123+
{
124+
PatternDefinition: domain.PatternDefinition{
125+
Id: "plugin/consistent-return",
125126
},
126127
},
127128
},

0 commit comments

Comments
 (0)