Skip to content

Commit 1b285ea

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

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,21 +2,22 @@ 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
reviveTool "codacy/cli-v2/tools/revive"
910
"encoding/json"
1011
"fmt"
11-
"io"
1212
"log"
13-
"net/http"
1413
"os"
1514
"path/filepath"
1615
"strings"
1716

1817
"codacy/cli-v2/utils"
1918

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

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

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

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)