Skip to content

Commit a84eb7c

Browse files
Merge pull request #123 from codacy/refactor-client-package
refactore: using client package for API requests
2 parents 44f78ac + 24bc3ab commit a84eb7c

File tree

14 files changed

+242
-284
lines changed

14 files changed

+242
-284
lines changed

cmd/init.go

Lines changed: 12 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,15 @@ import (
88
"codacy/cli-v2/tools/lizard"
99
"codacy/cli-v2/tools/pylint"
1010
"codacy/cli-v2/utils"
11-
"encoding/json"
12-
"errors"
1311
"fmt"
14-
"io"
1512
"log"
16-
"net/http"
1713
"os"
1814
"path/filepath"
1915
"strings"
20-
"time"
2116

2217
"github.com/spf13/cobra"
2318
)
2419

25-
const CodacyApiBase = "https://app.codacy.com"
26-
2720
var initFlags domain.InitFlags
2821

2922
func init() {
@@ -55,7 +48,7 @@ var initCmd = &cobra.Command{
5548
if cliLocalMode {
5649
fmt.Println()
5750
fmt.Println("ℹ️ No project token was specified, fetching codacy default configurations")
58-
noTools := []tools.Tool{}
51+
noTools := []domain.Tool{}
5952
err := createConfigurationFiles(noTools, cliLocalMode)
6053
if err != nil {
6154
log.Fatal(err)
@@ -97,7 +90,7 @@ func createGitIgnoreFile() error {
9790
return nil
9891
}
9992

100-
func createConfigurationFiles(tools []tools.Tool, cliLocalMode bool) error {
93+
func createConfigurationFiles(tools []domain.Tool, cliLocalMode bool) error {
10194
configFile, err := os.Create(config.Config.ProjectConfigFile())
10295
if err != nil {
10396
return fmt.Errorf("failed to create project config file: %w", err)
@@ -125,7 +118,7 @@ func createConfigurationFiles(tools []tools.Tool, cliLocalMode bool) error {
125118
return nil
126119
}
127120

128-
func configFileTemplate(tools []tools.Tool) string {
121+
func configFileTemplate(tools []domain.Tool) string {
129122
// Maps to track which tools are enabled
130123
toolsMap := make(map[string]bool)
131124
toolVersions := make(map[string]string)
@@ -255,11 +248,7 @@ func buildRepositoryConfigurationFiles(token string) error {
255248
return fmt.Errorf("failed to clean configuration directory: %w", err)
256249
}
257250

258-
client := &http.Client{
259-
Timeout: 10 * time.Second,
260-
}
261-
262-
apiTools, err := tools.GetRepositoryTools(CodacyApiBase, token, initFlags.Provider, initFlags.Organization, initFlags.Repository)
251+
apiTools, err := tools.GetRepositoryTools(initFlags)
263252
if err != nil {
264253
return err
265254
}
@@ -276,7 +265,7 @@ func buildRepositoryConfigurationFiles(token string) error {
276265
}
277266

278267
// Generate languages configuration based on API tools response
279-
if err := tools.CreateLanguagesConfigFile(apiTools, toolsConfigDir, uuidToName, token, initFlags.Provider, initFlags.Organization, initFlags.Repository); err != nil {
268+
if err := tools.CreateLanguagesConfigFile(apiTools, toolsConfigDir, uuidToName, initFlags); err != nil {
280269
return fmt.Errorf("failed to create languages configuration file: %w", err)
281270
}
282271

@@ -292,52 +281,7 @@ func buildRepositoryConfigurationFiles(token string) error {
292281
// Only generate config files for tools not using their own config file
293282
for _, tool := range configuredToolsWithUI {
294283

295-
url := fmt.Sprintf("%s/api/v3/analysis/organizations/%s/%s/repositories/%s/tools/%s/patterns?enabled=true&limit=1000",
296-
CodacyApiBase,
297-
initFlags.Provider,
298-
initFlags.Organization,
299-
initFlags.Repository,
300-
tool.Uuid)
301-
302-
// Create a new GET request
303-
req, err := http.NewRequest("GET", url, nil)
304-
if err != nil {
305-
fmt.Println("Error:", err)
306-
return err
307-
}
308-
309-
// Set the headers
310-
req.Header.Set("api-token", token)
311-
312-
// Send the request
313-
resp, err := client.Do(req)
314-
if err != nil {
315-
fmt.Println("Error:", err)
316-
return err
317-
}
318-
defer resp.Body.Close()
319-
320-
if resp.StatusCode >= 400 {
321-
return errors.New("failed to get repository's configuration from Codacy API")
322-
}
323-
324-
// Read the response body
325-
body, err := io.ReadAll(resp.Body)
326-
if err != nil {
327-
fmt.Println("Error:", err)
328-
return err
329-
}
330-
331-
var objmap map[string]json.RawMessage
332-
err = json.Unmarshal(body, &objmap)
333-
334-
if err != nil {
335-
fmt.Println("Error unmarshaling response:", err)
336-
return err
337-
}
338-
339-
var apiToolConfigurations []domain.PatternConfiguration
340-
err = json.Unmarshal(objmap["data"], &apiToolConfigurations)
284+
apiToolConfigurations, err := codacyclient.GetRepositoryToolPatterns(initFlags, tool.Uuid)
341285

342286
if err != nil {
343287
fmt.Println("Error unmarshaling tool configurations:", err)
@@ -351,7 +295,7 @@ func buildRepositoryConfigurationFiles(token string) error {
351295
}
352296

353297
// map tool uuid to tool name
354-
func createToolFileConfigurations(tool tools.Tool, patternConfiguration []domain.PatternConfiguration) error {
298+
func createToolFileConfigurations(tool domain.Tool, patternConfiguration []domain.PatternConfiguration) error {
355299
toolsConfigDir := config.Config.ToolsConfigDirectory()
356300
switch tool.Uuid {
357301
case ESLint:
@@ -544,17 +488,19 @@ func createLizardConfigFile(toolsConfigDir string, patternConfiguration []domain
544488
var patterns []domain.PatternDefinition
545489

546490
if len(patternConfiguration) == 0 {
547-
var err error
548-
patterns, err = tools.FetchDefaultEnabledPatterns(Lizard)
491+
patternsConfig, err := codacyclient.GetDefaultToolPatternsConfig(initFlags, Lizard)
549492
if err != nil {
550493
return err
551494
}
495+
patterns = make([]domain.PatternDefinition, len(patternsConfig))
496+
for i, pattern := range patternsConfig {
497+
patterns[i] = pattern.PatternDefinition
498+
}
552499
} else {
553500
patterns = make([]domain.PatternDefinition, len(patternConfiguration))
554501
for i, pattern := range patternConfiguration {
555502
patterns[i] = pattern.PatternDefinition
556503
}
557-
558504
}
559505

560506
content, err := lizard.CreateLizardConfig(patterns)

cmd/init_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package cmd
22

33
import (
44
"codacy/cli-v2/config"
5-
"codacy/cli-v2/tools"
5+
"codacy/cli-v2/domain"
66
"codacy/cli-v2/utils"
77
"os"
88
"path/filepath"
@@ -14,13 +14,13 @@ import (
1414
func TestConfigFileTemplate(t *testing.T) {
1515
tests := []struct {
1616
name string
17-
tools []tools.Tool
17+
tools []domain.Tool
1818
expected []string
1919
notExpected []string
2020
}{
2121
{
2222
name: "empty tools list uses defaults",
23-
tools: []tools.Tool{},
23+
tools: []domain.Tool{},
2424
expected: []string{
2525
2626
@@ -33,7 +33,7 @@ func TestConfigFileTemplate(t *testing.T) {
3333
},
3434
{
3535
name: "only eslint enabled",
36-
tools: []tools.Tool{
36+
tools: []domain.Tool{
3737
{
3838
Uuid: ESLint,
3939
Name: "eslint",
@@ -53,7 +53,7 @@ func TestConfigFileTemplate(t *testing.T) {
5353
},
5454
{
5555
name: "only pylint enabled",
56-
tools: []tools.Tool{
56+
tools: []domain.Tool{
5757
{
5858
Uuid: PyLint,
5959
Name: "pylint",
@@ -73,7 +73,7 @@ func TestConfigFileTemplate(t *testing.T) {
7373
},
7474
{
7575
name: "eslint and trivy enabled",
76-
tools: []tools.Tool{
76+
tools: []domain.Tool{
7777
{
7878
Uuid: ESLint,
7979
Name: "eslint",
@@ -98,7 +98,7 @@ func TestConfigFileTemplate(t *testing.T) {
9898
},
9999
{
100100
name: "all tools enabled",
101-
tools: []tools.Tool{
101+
tools: []domain.Tool{
102102
{
103103
Uuid: ESLint,
104104
Name: "eslint",
@@ -216,7 +216,7 @@ func TestInitCommand_NoToken(t *testing.T) {
216216

217217
cliLocalMode := len(initFlags.ApiToken) == 0
218218
if cliLocalMode {
219-
noTools := []tools.Tool{}
219+
noTools := []domain.Tool{}
220220
err := createConfigurationFiles(noTools, cliLocalMode)
221221
assert.NoError(t, err, "createConfigurationFiles should not return an error")
222222
if err := buildDefaultConfigurationFiles(toolsConfigDir); err != nil {

0 commit comments

Comments
 (0)