Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 12 additions & 66 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,15 @@
"codacy/cli-v2/tools/lizard"
"codacy/cli-v2/tools/pylint"
"codacy/cli-v2/utils"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"time"

"github.com/spf13/cobra"
)

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

var initFlags domain.InitFlags

func init() {
Expand Down Expand Up @@ -55,7 +48,7 @@
if cliLocalMode {
fmt.Println()
fmt.Println("ℹ️ No project token was specified, fetching codacy default configurations")
noTools := []tools.Tool{}
noTools := []domain.Tool{}
err := createConfigurationFiles(noTools, cliLocalMode)
if err != nil {
log.Fatal(err)
Expand Down Expand Up @@ -97,7 +90,7 @@
return nil
}

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

func configFileTemplate(tools []tools.Tool) string {
func configFileTemplate(tools []domain.Tool) string {

Check warning on line 121 in cmd/init.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/init.go#L121

Method configFileTemplate has 81 lines of code (limit is 50)

Check failure on line 121 in cmd/init.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/init.go#L121

Method configFileTemplate has a cyclomatic complexity of 16 (limit is 10)
// Maps to track which tools are enabled
toolsMap := make(map[string]bool)
toolVersions := make(map[string]string)
Expand Down Expand Up @@ -255,11 +248,7 @@
return fmt.Errorf("failed to clean configuration directory: %w", err)
}

client := &http.Client{
Timeout: 10 * time.Second,
}

apiTools, err := tools.GetRepositoryTools(CodacyApiBase, token, initFlags.Provider, initFlags.Organization, initFlags.Repository)
apiTools, err := tools.GetRepositoryTools(initFlags)
if err != nil {
return err
}
Expand All @@ -276,7 +265,7 @@
}

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

Expand All @@ -292,52 +281,7 @@
// Only generate config files for tools not using their own config file
for _, tool := range configuredToolsWithUI {

url := fmt.Sprintf("%s/api/v3/analysis/organizations/%s/%s/repositories/%s/tools/%s/patterns?enabled=true&limit=1000",
CodacyApiBase,
initFlags.Provider,
initFlags.Organization,
initFlags.Repository,
tool.Uuid)

// Create a new GET request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println("Error:", err)
return err
}

// Set the headers
req.Header.Set("api-token", token)

// Send the request
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error:", err)
return err
}
defer resp.Body.Close()

if resp.StatusCode >= 400 {
return errors.New("failed to get repository's configuration from Codacy API")
}

// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error:", err)
return err
}

var objmap map[string]json.RawMessage
err = json.Unmarshal(body, &objmap)

if err != nil {
fmt.Println("Error unmarshaling response:", err)
return err
}

var apiToolConfigurations []domain.PatternConfiguration
err = json.Unmarshal(objmap["data"], &apiToolConfigurations)
apiToolConfigurations, err := codacyclient.GetRepositoryToolPatterns(initFlags, tool.Uuid)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏


if err != nil {
fmt.Println("Error unmarshaling tool configurations:", err)
Expand All @@ -351,7 +295,7 @@
}

// map tool uuid to tool name
func createToolFileConfigurations(tool tools.Tool, patternConfiguration []domain.PatternConfiguration) error {
func createToolFileConfigurations(tool domain.Tool, patternConfiguration []domain.PatternConfiguration) error {

Check warning on line 298 in cmd/init.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/init.go#L298

Method createToolFileConfigurations has 83 lines of code (limit is 50)

Check failure on line 298 in cmd/init.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/init.go#L298

Method createToolFileConfigurations has a cyclomatic complexity of 25 (limit is 10)
toolsConfigDir := config.Config.ToolsConfigDirectory()
switch tool.Uuid {
case ESLint:
Expand Down Expand Up @@ -544,17 +488,19 @@
var patterns []domain.PatternDefinition

if len(patternConfiguration) == 0 {
var err error
patterns, err = tools.FetchDefaultEnabledPatterns(Lizard)
patternsConfig, err := codacyclient.GetDefaultToolPatternsConfig(initFlags, Lizard)
if err != nil {
return err
}
patterns = make([]domain.PatternDefinition, len(patternsConfig))
for i, pattern := range patternsConfig {
patterns[i] = pattern.PatternDefinition
}
} else {
patterns = make([]domain.PatternDefinition, len(patternConfiguration))
for i, pattern := range patternConfiguration {
patterns[i] = pattern.PatternDefinition
}

}

content, err := lizard.CreateLizardConfig(patterns)
Expand Down
16 changes: 8 additions & 8 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"codacy/cli-v2/config"
"codacy/cli-v2/tools"
"codacy/cli-v2/domain"
"codacy/cli-v2/utils"
"os"
"path/filepath"
Expand All @@ -14,13 +14,13 @@ import (
func TestConfigFileTemplate(t *testing.T) {
tests := []struct {
name string
tools []tools.Tool
tools []domain.Tool
expected []string
notExpected []string
}{
{
name: "empty tools list uses defaults",
tools: []tools.Tool{},
tools: []domain.Tool{},
expected: []string{
"node@22.2.0",
"python@3.11.11",
Expand All @@ -33,7 +33,7 @@ func TestConfigFileTemplate(t *testing.T) {
},
{
name: "only eslint enabled",
tools: []tools.Tool{
tools: []domain.Tool{
{
Uuid: ESLint,
Name: "eslint",
Expand All @@ -53,7 +53,7 @@ func TestConfigFileTemplate(t *testing.T) {
},
{
name: "only pylint enabled",
tools: []tools.Tool{
tools: []domain.Tool{
{
Uuid: PyLint,
Name: "pylint",
Expand All @@ -73,7 +73,7 @@ func TestConfigFileTemplate(t *testing.T) {
},
{
name: "eslint and trivy enabled",
tools: []tools.Tool{
tools: []domain.Tool{
{
Uuid: ESLint,
Name: "eslint",
Expand All @@ -98,7 +98,7 @@ func TestConfigFileTemplate(t *testing.T) {
},
{
name: "all tools enabled",
tools: []tools.Tool{
tools: []domain.Tool{
{
Uuid: ESLint,
Name: "eslint",
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestInitCommand_NoToken(t *testing.T) {

cliLocalMode := len(initFlags.ApiToken) == 0
if cliLocalMode {
noTools := []tools.Tool{}
noTools := []domain.Tool{}
err := createConfigurationFiles(noTools, cliLocalMode)
assert.NoError(t, err, "createConfigurationFiles should not return an error")
if err := buildDefaultConfigurationFiles(toolsConfigDir); err != nil {
Expand Down
Loading
Loading