Skip to content

Commit ed511ef

Browse files
code review changes applied
1 parent b8b9eae commit ed511ef

15 files changed

+384
-304
lines changed

.codacy/cli-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
mode: local

.codacy/codacy.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
runtimes:
2+
3+
4+
tools:
5+
- eslint@
6+
- trivy@
7+
8+
- pmd@

cmd/init.go

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@ import (
1818

1919
const CodacyApiBase = "https://app.codacy.com"
2020

21-
var codacyRepositoryToken string
2221
var codacyApiToken string
2322
var remoteProvider string
24-
var remoteOrganizationName string
25-
var remoteRepositoryName string
23+
var organization string
24+
var remoteRepo string
2625

2726
func init() {
28-
initCmd.Flags().StringVar(&codacyApiToken, "codacy-api-token", "", "optional codacy api token, if defined configurations will be fetched from codacy")
27+
initCmd.Flags().StringVar(&codacyApiToken, "api-token", "", "optional codacy api token, if defined configurations will be fetched from codacy")
2928
initCmd.Flags().StringVar(&remoteProvider, "provider", "", "optional provider (gh/bb/gl), if defined configurations will be fetched from codacy")
30-
initCmd.Flags().StringVar(&remoteOrganizationName, "organization", "", "optional remote organization name, if defined configurations will be fetched from codacy")
31-
initCmd.Flags().StringVar(&remoteRepositoryName, "repository", "", "optional remote repository name, if defined configurations will be fetched from codacy")
29+
initCmd.Flags().StringVar(&organization, "organization", "", "optional remote organization name, if defined configurations will be fetched from codacy")
30+
initCmd.Flags().StringVar(&remoteRepo, "repository", "", "optional remote repository name, if defined configurations will be fetched from codacy")
3231
rootCmd.AddCommand(initCmd)
3332
}
3433

@@ -50,8 +49,7 @@ var initCmd = &cobra.Command{
5049
log.Fatal(err)
5150
}
5251
} else {
53-
token := domain.NewApiToken(codacyApiToken)
54-
err := buildRepositoryConfigurationFiles(token)
52+
err := buildRepositoryConfigurationFiles(codacyApiToken)
5553
if err != nil {
5654
log.Fatal(err)
5755
}
@@ -102,13 +100,13 @@ func configFileTemplate(tools []tools.Tool) string {
102100

103101
for _, tool := range tools {
104102
switch tool.Uuid {
105-
case string(ESLint):
103+
case ESLint:
106104
eslintVersion = tool.Version
107-
case string(Trivy):
105+
case Trivy:
108106
trivyVersion = tool.Version
109-
case string(PyLint):
107+
case PyLint:
110108
pylintVersion = tool.Version
111-
case string(PMD):
109+
case PMD:
112110
pmdVersion = tool.Version
113111
}
114112
}
@@ -136,24 +134,14 @@ func cliConfigFileTemplate(cliLocalMode bool) string {
136134
return fmt.Sprintf(`mode: %s`, cliModeString)
137135
}
138136

139-
func buildRepositoryConfigurationFiles(token domain.Token) error {
140-
fmt.Println("Building repository configuration files ...")
141-
switch token := token.(type) {
142-
case domain.ApiToken:
143-
return buildRepositoryConfigurationFilesFromApiToken(token)
144-
default:
145-
return fmt.Errorf("unknown token type: %T", token)
146-
}
147-
}
148-
149-
func buildRepositoryConfigurationFilesFromApiToken(token domain.ApiToken) error {
137+
func buildRepositoryConfigurationFiles(token string) error {
150138
fmt.Println("Fetching repository configuration from codacy using api token ...")
151139

152140
client := &http.Client{
153141
Timeout: 10 * time.Second,
154142
}
155143

156-
apiTools, err := tools.GetRepositoryTools(token, remoteProvider, remoteOrganizationName, remoteRepositoryName)
144+
apiTools, err := tools.GetRepositoryTools(CodacyApiBase, token, remoteProvider, organization, remoteRepo)
157145
if err != nil {
158146
return err
159147
}
@@ -167,11 +155,10 @@ func buildRepositoryConfigurationFilesFromApiToken(token domain.ApiToken) error
167155
url := fmt.Sprintf("%s/api/v3/analysis/organizations/%s/%s/repositories/%s/tools/%s/patterns?enabled=true",
168156
CodacyApiBase,
169157
remoteProvider,
170-
remoteOrganizationName,
171-
remoteRepositoryName,
158+
organization,
159+
remoteRepo,
172160
tool.Uuid)
173161

174-
fmt.Printf("url: %q\n", url)
175162
// Create a new GET request
176163
req, err := http.NewRequest("GET", url, nil)
177164
if err != nil {
@@ -180,7 +167,7 @@ func buildRepositoryConfigurationFilesFromApiToken(token domain.ApiToken) error
180167
}
181168

182169
// Set the headers
183-
req.Header.Set("api-token", token.Value())
170+
req.Header.Set("api-token", token)
184171

185172
// Send the request
186173
resp, err := client.Do(req)
@@ -212,17 +199,12 @@ func buildRepositoryConfigurationFilesFromApiToken(token domain.ApiToken) error
212199
var apiToolConfigurations []domain.PatternConfiguration
213200
err = json.Unmarshal(objmap["data"], &apiToolConfigurations)
214201

215-
for _, toolConfiguration := range apiToolConfigurations {
216-
fmt.Println("tool configuration", toolConfiguration)
217-
}
218-
219202
if err != nil {
220203
fmt.Println("Error unmarshaling tool configurations:", err)
221204
return err
222205
}
223206

224207
createToolFileConfigurations(tool, apiToolConfigurations)
225-
// TODO: Process the response and create configuration files for each tool
226208
}
227209

228210
return nil
@@ -231,7 +213,7 @@ func buildRepositoryConfigurationFilesFromApiToken(token domain.ApiToken) error
231213
// map tool uuid to tool name
232214
func createToolFileConfigurations(tool tools.Tool, patternConfiguration []domain.PatternConfiguration) error {
233215
switch tool.Uuid {
234-
case string(ESLint):
216+
case ESLint:
235217
if len(patternConfiguration) > 0 {
236218
eslintConfigurationString := tools.CreateEslintConfig(patternConfiguration)
237219

@@ -253,20 +235,20 @@ func createToolFileConfigurations(tool tools.Tool, patternConfiguration []domain
253235
}
254236
fmt.Println("Default ESLint configuration created")
255237
}
256-
case string(Trivy):
238+
case Trivy:
257239
if len(patternConfiguration) > 0 {
258-
return createTrivyConfigFile(patternConfiguration)
240+
createTrivyConfigFile(patternConfiguration)
259241
} else {
260-
return createDefaultTrivyConfigFile()
242+
createDefaultTrivyConfigFile()
261243
}
262-
263-
case string(PMD):
244+
fmt.Println("Trivy configuration created based on Codacy settings")
245+
case PMD:
264246
if len(patternConfiguration) > 0 {
265-
return createPMDConfigFile(patternConfiguration)
247+
createPMDConfigFile(patternConfiguration)
266248
} else {
267-
return createDefaultPMDConfigFile()
249+
createDefaultPMDConfigFile()
268250
}
269-
251+
fmt.Println("PMD configuration created based on Codacy settings")
270252
}
271253
return nil
272254
}
@@ -310,11 +292,9 @@ func createDefaultEslintConfigFile() error {
310292
return os.WriteFile("eslint.config.mjs", []byte(content), 0644)
311293
}
312294

313-
type ToolUiid string
314-
315295
const (
316-
ESLint ToolUiid = "f8b29663-2cb2-498d-b923-a10c6a8c05cd"
317-
Trivy ToolUiid = "2fd7fbe0-33f9-4ab3-ab73-e9b62404e2cb"
318-
PMD ToolUiid = "9ed24812-b6ee-4a58-9004-0ed183c45b8f"
319-
PyLint ToolUiid = "31677b6d-4ae0-4f56-8041-606a8d7a8e61"
296+
ESLint string = "f8b29663-2cb2-498d-b923-a10c6a8c05cd"
297+
Trivy string = "2fd7fbe0-33f9-4ab3-ab73-e9b62404e2cb"
298+
PMD string = "9ed24812-b6ee-4a58-9004-0ed183c45b8f"
299+
PyLint string = "31677b6d-4ae0-4f56-8041-606a8d7a8e61"
320300
)

domain/patternConfiguration.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ type ParameterConfiguration struct {
66
}
77

88
type PatternDefinition struct {
9-
Id string `json:"id"`
10-
Parameters []ParameterConfiguration `json:"parameters"`
9+
Id string `json:"id"`
1110
}
1211

1312
type PatternConfiguration struct {

domain/token.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

domain/toolName.go

Lines changed: 0 additions & 9 deletions
This file was deleted.

eslint.config.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default [
2+
{
3+
rules: {
4+
"@angular-eslint/component-class-suffix": "error",
5+
"@angular-eslint/no-async-lifecycle-method": "error",
6+
"@angular-eslint/no-attribute-decorator": "error",
7+
"unicorn/better-regex": "error",
8+
}
9+
}
10+
];

pmd-ruleset.xml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ruleset name="Default PMD Ruleset"
3+
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
6+
7+
<description>Default PMD ruleset with commonly used rules</description>
8+
9+
<!-- Java Best Practices -->
10+
<rule ref="category/java/bestpractices.xml/AvoidReassigningParameters"/>
11+
<rule ref="category/java/bestpractices.xml/CheckResultSet"/>
12+
<rule ref="category/java/bestpractices.xml/JUnitTestsShouldIncludeAssert"/>
13+
<rule ref="category/java/bestpractices.xml/OneDeclarationPerLine"/>
14+
<rule ref="category/java/bestpractices.xml/SwitchStmtsShouldHaveDefault"/>
15+
<rule ref="category/java/bestpractices.xml/UnusedImports"/>
16+
<rule ref="category/java/bestpractices.xml/UnusedLocalVariable"/>
17+
<rule ref="category/java/bestpractices.xml/UnusedPrivateField"/>
18+
<rule ref="category/java/bestpractices.xml/UnusedPrivateMethod"/>
19+
20+
<!-- Java Code Style -->
21+
<rule ref="category/java/codestyle.xml/ClassNamingConventions">
22+
<properties>
23+
<property name="classPattern" value="[A-Z][a-zA-Z0-9]*"/>
24+
<property name="abstractClassPattern" value="Abstract[A-Z][a-zA-Z0-9]*"/>
25+
<property name="interfacePattern" value="[A-Z][a-zA-Z0-9]*"/>
26+
<property name="enumPattern" value="[A-Z][a-zA-Z0-9]*"/>
27+
<property name="annotationPattern" value="[A-Z][a-zA-Z0-9]*"/>
28+
</properties>
29+
</rule>
30+
<rule ref="category/java/codestyle.xml/MethodNamingConventions">
31+
<properties>
32+
<property name="methodPattern" value="[a-z][a-zA-Z0-9]*"/>
33+
<property name="staticPattern" value="[a-z][a-zA-Z0-9]*"/>
34+
<property name="nativePattern" value="[a-z][a-zA-Z0-9]*"/>
35+
</properties>
36+
</rule>
37+
<rule ref="category/java/codestyle.xml/DefaultPackage"/>
38+
<rule ref="category/java/codestyle.xml/DontImportJavaLang"/>
39+
<rule ref="category/java/codestyle.xml/ExtendsObject"/>
40+
<rule ref="category/java/codestyle.xml/FieldDeclarationsShouldBeAtStartOfClass"/>
41+
<rule ref="category/java/codestyle.xml/GenericsNaming"/>
42+
<rule ref="category/java/codestyle.xml/NoPackage"/>
43+
<rule ref="category/java/codestyle.xml/PackageCase"/>
44+
45+
<!-- Java Design -->
46+
<rule ref="category/java/design.xml/AvoidThrowingNullPointerException"/>
47+
<rule ref="category/java/design.xml/AvoidThrowingRawExceptionTypes"/>
48+
<rule ref="category/java/design.xml/CollapsibleIfStatements"/>
49+
<rule ref="category/java/design.xml/ExcessiveClassLength">
50+
<properties>
51+
<property name="minimum" value="1000"/>
52+
</properties>
53+
</rule>
54+
<rule ref="category/java/design.xml/ExcessiveMethodLength">
55+
<properties>
56+
<property name="minimum" value="50"/>
57+
</properties>
58+
</rule>
59+
<rule ref="category/java/design.xml/ExcessiveParameterList">
60+
<properties>
61+
<property name="minimum" value="10"/>
62+
</properties>
63+
</rule>
64+
<rule ref="category/java/design.xml/SimplifyBooleanReturns"/>
65+
<rule ref="category/java/design.xml/SimplifyBooleanExpressions"/>
66+
67+
<!-- Java Error Prone -->
68+
<rule ref="category/java/errorprone.xml/AvoidBranchingStatementAsLastInLoop"/>
69+
<rule ref="category/java/errorprone.xml/AvoidDecimalLiteralsInBigDecimalConstructor"/>
70+
<rule ref="category/java/errorprone.xml/AvoidMultipleUnaryOperators"/>
71+
<rule ref="category/java/errorprone.xml/AvoidCallingFinalize"/>
72+
<rule ref="category/java/errorprone.xml/EmptyIfStmt"/>
73+
<rule ref="category/java/errorprone.xml/EmptyTryBlock"/>
74+
<rule ref="category/java/errorprone.xml/EmptyFinallyBlock"/>
75+
<rule ref="category/java/errorprone.xml/EmptyWhileStmt"/>
76+
<rule ref="category/java/errorprone.xml/CompareObjectsWithEquals"/>
77+
<rule ref="category/java/errorprone.xml/UseEqualsToCompareStrings"/>
78+
79+
<!-- Java Performance -->
80+
<rule ref="category/java/performance.xml/BooleanInstantiation"/>
81+
<rule ref="category/java/performance.xml/StringInstantiation"/>
82+
<rule ref="category/java/performance.xml/StringToString"/>
83+
<rule ref="category/java/performance.xml/UseStringBufferLength"/>
84+
85+
<!-- Java Documentation -->
86+
<rule ref="category/java/documentation.xml/UncommentedEmptyMethodBody"/>
87+
88+
<!-- JavaScript Best Practices -->
89+
<rule ref="category/ecmascript/bestpractices.xml/AvoidWithStatement"/>
90+
<rule ref="category/ecmascript/bestpractices.xml/ConsistentReturn"/>
91+
<rule ref="category/ecmascript/bestpractices.xml/UseBaseWithParseInt"/>
92+
93+
<!-- JSP Design -->
94+
<rule ref="category/jsp/design.xml/NoInlineScript"/>
95+
<rule ref="category/jsp/design.xml/NoInlineStyleInformation"/>
96+
<rule ref="category/jsp/design.xml/NoScriptlets"/>
97+
98+
<!-- XML Error Prone -->
99+
<rule ref="category/xml/errorprone.xml/MistypedCDATASection"/>
100+
101+
</ruleset>

tools/eslintConfigCreator.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ func CreateEslintConfig(configuration []domain.PatternConfiguration) string {
2727

2828
for _, patternConfiguration := range configuration {
2929
rule := strings.TrimPrefix(patternConfiguration.PatternDefinition.Id, "ESLint8_")
30-
fmt.Println("Rule:", rule)
3130

3231
const tempstring = "TEMPORARYSTRING"
3332
rule = strings.ReplaceAll(rule, "__", tempstring)

0 commit comments

Comments
 (0)