Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion .codacy/cli-config.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
mode: local
mode: remote
2 changes: 1 addition & 1 deletion .codacy/codacy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ runtimes:
- [email protected]
- [email protected]
tools:
- eslint@9.3.0
- eslint@8.57.0
- [email protected]
- [email protected]
- [email protected]
124 changes: 118 additions & 6 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
const CodacyApiBase = "https://app.codacy.com"

var codacyRepositoryToken string
var codacyApiToken string

Check notice on line 21 in cmd/init.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/init.go#L21

var codacyApiToken should be codacyAPIToken

func init() {
initCmd.Flags().StringVar(&codacyRepositoryToken, "repository-token", "", "optional codacy repository token, if defined configurations will be fetched from codacy")
initCmd.Flags().StringVar(&codacyApiToken, "codacy-api-token", "", "optional codacy api token, if defined configurations will be fetched from codacy")
Copy link
Contributor

Choose a reason for hiding this comment

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

We can simplify it

Suggested change
initCmd.Flags().StringVar(&codacyApiToken, "codacy-api-token", "", "optional codacy api token, if defined configurations will be fetched from codacy")
initCmd.Flags().StringVar(&codacyApiToken, "api-token", "", "optional codacy api token, if defined configurations will be fetched from codacy")

rootCmd.AddCommand(initCmd)
}

Expand All @@ -29,10 +31,9 @@
Short: "Bootstraps project configuration",
Long: "Bootstraps project configuration, creates codacy configuration file",
Run: func(cmd *cobra.Command, args []string) {

config.Config.CreateLocalCodacyDir()

cliLocalMode := len(codacyRepositoryToken) == 0
cliLocalMode := len(codacyRepositoryToken) == 0 && len(codacyApiToken) == 0

if cliLocalMode {
fmt.Println()
Expand All @@ -51,7 +52,15 @@
if err != nil {
log.Fatal(err)
}
err = buildRepositoryConfigurationFiles(codacyRepositoryToken)

var token Token
if len(codacyApiToken) > 0 {
token = ApiToken{value: codacyApiToken}
} else {
token = ProjectToken{value: codacyRepositoryToken}
}

err = buildRepositoryConfigurationFiles(token)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -138,9 +147,112 @@
return fmt.Sprintf(`mode: %s`, cliModeString)
}

func buildRepositoryConfigurationFiles(token string) error {
func buildRepositoryConfigurationFiles(token Token) error {
fmt.Println("Building repository configuration files ...")
fmt.Println("Fetching repository configuration from codacy ...")
switch token := token.(type) {
case ProjectToken:
return buildRepositoryConfigurationFilesFromRepositoryToken(token)
case ApiToken:
return buildRepositoryConfigurationFilesFromApiToken(token)
default:
return fmt.Errorf("unknown token type: %T", token)
}
}

func buildRepositoryConfigurationFilesFromApiToken(token ApiToken) error {

Check warning on line 162 in cmd/init.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/init.go#L162

Method buildRepositoryConfigurationFilesFromApiToken has 68 lines of code (limit is 50)

Check failure on line 162 in cmd/init.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/init.go#L162

Method buildRepositoryConfigurationFilesFromApiToken has a cyclomatic complexity of 12 (limit is 10)

Check notice on line 162 in cmd/init.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/init.go#L162

func buildRepositoryConfigurationFilesFromApiToken should be buildRepositoryConfigurationFilesFromAPIToken
fmt.Println("Fetching repository configuration from codacy using api token ...")

// Ask for provider
var provider string
for {
fmt.Print("Enter provider (gh/bb/gl): ")
fmt.Scanln(&provider)
if provider == "gh" || provider == "bb" || provider == "gl" {
break
}
fmt.Println("Invalid provider. Please enter 'gh', 'bb', or 'gl'")
}

// Ask for organization name
var organizationName string
fmt.Print("Enter remote organization name: ")
fmt.Scanln(&organizationName)

// Ask for repository name
var repositoryName string
fmt.Print("Enter remote repository name: ")
fmt.Scanln(&repositoryName)

var supportedTools = []string{
"f8b29663-2cb2-498d-b923-a10c6a8c05cd", // ESLint
"2fd7fbe0-33f9-4ab3-ab73-e9b62404e2cb", // Trivy
"31677b6d-4ae0-4f56-8041-606a8d7a8e61", // Pylint
"9ed24812-b6ee-4a58-9004-0ed183c45b8f", // PMD
}

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

for _, toolUuid := range supportedTools {

Check notice on line 197 in cmd/init.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/init.go#L197

range var toolUuid should be toolUUID
url := fmt.Sprintf("%s/api/v3/analysis/organizations/%s/%s/repositories/%s/tools/%s/patterns",
CodacyApiBase,
provider,
organizationName,
repositoryName,
toolUuid)

// 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.Value())

// 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 []CodacyToolConfiguration
err = json.Unmarshal(objmap["toolConfiguration"], &apiToolConfigurations)
if err != nil {
fmt.Println("Error unmarshaling tool configurations:", err)
return err
}

// TODO: Process the response and create configuration files for each tool
Copy link
Contributor

Choose a reason for hiding this comment

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

leftovers delete

}

return nil
}

func buildRepositoryConfigurationFilesFromRepositoryToken(token ProjectToken) error {

Check warning on line 254 in cmd/init.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/init.go#L254

Method buildRepositoryConfigurationFilesFromRepositoryToken has 89 lines of code (limit is 50)

Check failure on line 254 in cmd/init.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/init.go#L254

Method buildRepositoryConfigurationFilesFromRepositoryToken has a cyclomatic complexity of 17 (limit is 10)
fmt.Println("Fetching repository configuration from codacy using repository token ...")

// API call to fetch settings
url := CodacyApiBase + "/2.0/project/analysis/configuration"
Expand All @@ -157,7 +269,7 @@
}

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

// Send the request
resp, err := client.Do(req)
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func showWelcomeMessage() {
fmt.Println()
cyan.Println("Initialize your project with:")
fmt.Println(" codacy-cli init --repository-token YOUR_TOKEN")
fmt.Println(" codacy-cli init --codacy-api-token YOUR_TOKEN")
fmt.Println()
fmt.Println("Or run without a token to use local configuration:")
fmt.Println(" codacy-cli init")
Expand Down
21 changes: 21 additions & 0 deletions cmd/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

type Token interface {

Check notice on line 3 in cmd/token.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/token.go#L3

exported type Token should have comment or be unexported
Value() string
}

type ProjectToken struct {

Check notice on line 7 in cmd/token.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/token.go#L7

exported type ProjectToken should have comment or be unexported
value string
}

func (t ProjectToken) Value() string {

Check notice on line 11 in cmd/token.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/token.go#L11

exported method ProjectToken.Value should have comment or be unexported
return t.value
}

type ApiToken struct {

Check notice on line 15 in cmd/token.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/token.go#L15

exported type ApiToken should have comment or be unexported

Check notice on line 15 in cmd/token.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/token.go#L15

type ApiToken should be APIToken
value string
}

func (t ApiToken) Value() string {

Check notice on line 19 in cmd/token.go

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

cmd/token.go#L19

exported method ApiToken.Value should have comment or be unexported
return t.value
}
Loading