Skip to content

Commit bb92304

Browse files
refactor upload to use codacyclient, use domain types and run lizard like other tools
1 parent e3db698 commit bb92304

File tree

6 files changed

+59
-158
lines changed

6 files changed

+59
-158
lines changed

cmd/analyze.go

Lines changed: 15 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,21 @@ package cmd
22

33
import (
44
"codacy/cli-v2/config"
5+
"codacy/cli-v2/domain"
56
"codacy/cli-v2/plugins"
67
"codacy/cli-v2/tools"
78
"codacy/cli-v2/tools/lizard"
89
"encoding/json"
910
"fmt"
10-
"io"
1111
"log"
12-
"net/http"
1312
"os"
1413
"path/filepath"
1514
"strings"
1615

1716
"codacy/cli-v2/utils"
1817

18+
codacyclient "codacy/cli-v2/codacy-client"
19+
1920
"github.com/spf13/cobra"
2021
"gopkg.in/yaml.v3"
2122
)
@@ -210,21 +211,6 @@ type CodacyIssue struct {
210211
Category string `json:"category"`
211212
}
212213

213-
type Tool struct {
214-
UUID string `json:"uuid"`
215-
ShortName string `json:"shortName"`
216-
Prefix string `json:"prefix"`
217-
}
218-
219-
type Pattern struct {
220-
UUID string `json:"uuid"`
221-
ID string `json:"id"`
222-
Name string `json:"name"`
223-
Category string `json:"category"`
224-
Description string `json:"description"`
225-
Level string `json:"level"`
226-
}
227-
228214
func init() {
229215
analyzeCmd.Flags().StringVarP(&outputFile, "output", "o", "", "Output file for analysis results")
230216
analyzeCmd.Flags().StringVarP(&toolsToAnalyzeParam, "tool", "t", "", "Which tool to run analysis with. If not specified, all configured tools will be run")
@@ -233,66 +219,24 @@ func init() {
233219
rootCmd.AddCommand(analyzeCmd)
234220
}
235221

236-
func loadsToolAndPatterns(toolName string) (Tool, []Pattern) {
237-
var toolsURL = "https://app.codacy.com/api/v3/tools"
238-
239-
req, err := http.NewRequest("GET", toolsURL, nil)
222+
func loadsToolAndPatterns(toolName string) (domain.Tool, []domain.PatternConfiguration) {
223+
var toolsResponse, err = codacyclient.GetToolsVersions()
240224
if err != nil {
241-
fmt.Printf("Error creating request: %v\n", err)
242-
panic("panic")
243-
}
244-
req.Header.Set("Content-Type", "application/json")
245-
246-
resp, err := http.DefaultClient.Do(req)
247-
if err != nil {
248-
fmt.Printf("Error fetching patterns: %v\n", err)
249-
panic("panic")
250-
}
251-
defer resp.Body.Close()
252-
253-
body, _ := io.ReadAll(resp.Body)
254-
255-
var toolsResponse struct {
256-
Data []Tool `json:"data"`
225+
fmt.Println("Error:", err)
226+
return domain.Tool{}, []domain.PatternConfiguration{}
257227
}
258-
json.Unmarshal(body, &toolsResponse)
259-
var tool Tool
260-
for _, t := range toolsResponse.Data {
261-
if t.ShortName == toolName {
228+
var tool domain.Tool
229+
for _, t := range toolsResponse {
230+
if t.Name == toolName {
262231
tool = t
263232
break
264233
}
265234
}
266-
var patterns []Pattern
267-
var hasNext bool = true
268-
cursor := ""
269-
client := &http.Client{}
270-
271-
for hasNext {
272-
baseURL := fmt.Sprintf("https://app.codacy.com/api/v3/tools/%s/patterns?limit=1000%s", tool.UUID, cursor)
273-
req, _ := http.NewRequest("GET", baseURL, nil)
274-
req.Header.Set("Content-Type", "application/json")
275-
276-
resp, err := client.Do(req)
277-
if err != nil {
278-
fmt.Println("Error:", err)
279-
break
280-
}
281-
defer resp.Body.Close()
282-
body, _ := io.ReadAll(resp.Body)
283-
284-
var patternsResponse struct {
285-
Data []Pattern `json:"data"`
286-
Pagination struct {
287-
Cursor string `json:"cursor"`
288-
} `json:"pagination"`
289-
}
290-
json.Unmarshal(body, &patternsResponse)
291-
patterns = append(patterns, patternsResponse.Data...)
292-
hasNext = patternsResponse.Pagination.Cursor != ""
293-
if hasNext {
294-
cursor = "&cursor=" + patternsResponse.Pagination.Cursor
295-
}
235+
var patterns []domain.PatternConfiguration
236+
patterns, err = codacyclient.GetDefaultToolPatternsConfig(domain.InitFlags{}, tool.Uuid)
237+
if err != nil {
238+
fmt.Println("Error:", err)
239+
return domain.Tool{}, []domain.PatternConfiguration{}
296240
}
297241
return tool, patterns
298242
}

cmd/upload.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"bytes"
5+
"codacy/cli-v2/domain"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -206,8 +207,18 @@ func resultsFinalWithAPIToken(commitUUID string, apiToken string, provider strin
206207
fmt.Println("Response Body:", string(body))
207208
}
208209

209-
func getPatternByID(patterns []Pattern, patternID string) *Pattern {
210+
func getPatternByID(patterns []domain.PatternConfiguration, patternID string) *domain.SarifPatternConfiguration {
211+
var sarifPatterns []domain.SarifPatternConfiguration
210212
for _, p := range patterns {
213+
sarifPatterns = append(sarifPatterns, domain.SarifPatternConfiguration{
214+
UUID: p.PatternDefinition.Id,
215+
ID: p.PatternDefinition.Id,
216+
Category: p.PatternDefinition.Category,
217+
Description: p.PatternDefinition.Description,
218+
Level: p.PatternDefinition.Level,
219+
})
220+
}
221+
for _, p := range sarifPatterns {
211222
if strings.EqualFold(p.ID, patternID) {
212223
return &p
213224
}

domain/patternConfiguration.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,12 @@ type PatternResponse struct {
3636
IsCustom bool `json:"isCustom"`
3737
Parameters []ParameterConfiguration `json:"parameters"`
3838
}
39+
40+
type SarifPatternConfiguration struct {
41+
UUID string `json:"uuid"`
42+
ID string `json:"id"`
43+
Name string `json:"name"`
44+
Category string `json:"category"`
45+
Description string `json:"description"`
46+
Level string `json:"level"`
47+
}

domain/tool.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ type ToolsResponse struct {
77

88
// Tool represents a tool in the Codacy API
99
type Tool struct {
10-
Uuid string `json:"uuid"`
11-
Name string `json:"name"`
12-
Version string `json:"version"`
13-
Settings struct {
10+
Uuid string `json:"uuid"`
11+
Name string `json:"name"`
12+
Version string `json:"version"`
13+
ShortName string `json:"shortName"`
14+
Prefix string `json:"prefix"`
15+
Settings struct {
1416
Enabled bool `json:"isEnabled"`
1517
HasConfigurationFile bool `json:"hasConfigurationFile"`
1618
UsesConfigurationFile bool `json:"usesConfigurationFile"`

tools/lizard/test/expected.sarif

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,46 @@
1010
"informationUri": "https://github.com/terryyin/lizard",
1111
"rules": [
1212
{
13-
"id": "Lizard_ccn-critical",
13+
"id": "Lizard_ccn-medium",
1414
"shortDescription": {
15-
"text": "Method has extremely high cyclomatic complexity"
15+
"text": "Checks if the cyclomatic complexity of a function or logic block exceeds the medium threshold (default is 8)."
1616
},
1717
"properties": {
1818
"tags": [
19-
"critical"
19+
"warning"
2020
]
2121
}
2222
},
2323
{
24-
"id": "Lizard_ccn-minor",
24+
"id": "Lizard_file-nloc-medium",
2525
"shortDescription": {
26-
"text": "Method has high cyclomatic complexity"
26+
"text": "This rule checks if the number of lines of code (excluding comments) in a file exceeds a medium threshold, typically 500 lines."
2727
},
2828
"properties": {
2929
"tags": [
30-
"minor"
30+
"warning"
3131
]
3232
}
3333
},
3434
{
3535
"id": "Lizard_nloc-medium",
3636
"shortDescription": {
37-
"text": "Method has too many lines of code"
37+
"text": "Checks if the number of lines of code (excluding comments) in a function exceeds a medium threshold (default 50 lines)."
3838
},
3939
"properties": {
4040
"tags": [
41-
"medium"
41+
"warning"
4242
]
4343
}
4444
},
4545
{
46-
"id": "Lizard_nloc-minor",
46+
"id": "Lizard_parameter-count-medium",
4747
"shortDescription": {
48-
"text": "Method has too many lines of code"
48+
"text": "This rule checks the number of parameters passed to a function and raises an issue if it exceeds a medium threshold, which by default is 8 parameters."
4949
},
5050
"properties": {
5151
"tags": [
52-
"minor"
52+
"warning"
5353
]
5454
}
5555
}
@@ -59,9 +59,9 @@
5959
"results": [
6060
{
6161
"ruleId": "Lizard_nloc-medium",
62-
"level": "medium",
62+
"level": "warning",
6363
"message": {
64-
"text": "Method complex_analysis has 65 lines of code (limit is 25)"
64+
"text": "Method complex_analysis has 65 lines of code (limit is 50)"
6565
},
6666
"locations": [
6767
{
@@ -78,10 +78,10 @@
7878
]
7979
},
8080
{
81-
"ruleId": "Lizard_ccn-critical",
82-
"level": "critical",
81+
"ruleId": "Lizard_ccn-medium",
82+
"level": "warning",
8383
"message": {
84-
"text": "Method complex_analysis has a cyclomatic complexity of 31 (limit is 30)"
84+
"text": "Method complex_analysis has a cyclomatic complexity of 31 (limit is 8)"
8585
},
8686
"locations": [
8787
{

tools/lizard/test/lizardRunner_test.go

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package test
22

33
import (
4-
"codacy/cli-v2/domain"
54
"codacy/cli-v2/tools/lizard"
65
"os"
76
"path/filepath"
@@ -31,70 +30,6 @@ func TestRunLizardWithSarifOutput(t *testing.T) {
3130
// Construct the path to the test file
3231
complexPyPath := filepath.Join(currentDir, "complex.py")
3332

34-
// Create test patterns
35-
patterns := []domain.PatternDefinition{
36-
{
37-
Id: "Lizard_nloc-minor",
38-
Category: "CodeStyle",
39-
Level: "Code",
40-
SeverityLevel: "Minor",
41-
Title: "Method Too Long (Minor)",
42-
Description: "Method has too many lines of code",
43-
Parameters: []domain.ParameterConfiguration{
44-
{
45-
Name: "threshold",
46-
Value: "15",
47-
Default: "15",
48-
},
49-
},
50-
},
51-
{
52-
Id: "Lizard_nloc-medium",
53-
Category: "CodeStyle",
54-
Level: "Code",
55-
SeverityLevel: "Medium",
56-
Title: "Method Too Long (Medium)",
57-
Description: "Method has too many lines of code",
58-
Parameters: []domain.ParameterConfiguration{
59-
{
60-
Name: "threshold",
61-
Value: "25",
62-
Default: "25",
63-
},
64-
},
65-
},
66-
{
67-
Id: "Lizard_ccn-minor",
68-
Category: "CodeStyle",
69-
Level: "Code",
70-
SeverityLevel: "Minor",
71-
Title: "Cyclomatic Complexity (Minor)",
72-
Description: "Method has high cyclomatic complexity",
73-
Parameters: []domain.ParameterConfiguration{
74-
{
75-
Name: "threshold",
76-
Value: "3",
77-
Default: "3",
78-
},
79-
},
80-
},
81-
{
82-
Id: "Lizard_ccn-critical",
83-
Category: "CodeStyle",
84-
Level: "Code",
85-
SeverityLevel: "Critical",
86-
Title: "Cyclomatic Complexity (Critical)",
87-
Description: "Method has extremely high cyclomatic complexity",
88-
Parameters: []domain.ParameterConfiguration{
89-
{
90-
Name: "threshold",
91-
Value: "30",
92-
Default: "30",
93-
},
94-
},
95-
},
96-
}
97-
9833
// Create a temporary output file
9934
outputFile := filepath.Join(currentDir, "output.sarif")
10035
defer os.Remove(outputFile)
@@ -107,7 +42,7 @@ func TestRunLizardWithSarifOutput(t *testing.T) {
10742
expectedOutput := strings.TrimSpace(string(expectedData))
10843

10944
// Run Lizard with SARIF output
110-
err = lizard.RunLizard(currentDir, lizardBinary, []string{complexPyPath}, outputFile, "sarif", patterns)
45+
err = lizard.RunLizard(currentDir, lizardBinary, []string{complexPyPath}, outputFile, "sarif")
11146
if err != nil {
11247
t.Fatalf("RunLizard failed: %v", err)
11348
}

0 commit comments

Comments
 (0)