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
20 changes: 20 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
"fmt"
"github.com/Tomas-vilte/MateCommit/internal/cli/command/config"
"github.com/Tomas-vilte/MateCommit/internal/cli/command/handler"
"github.com/Tomas-vilte/MateCommit/internal/cli/command/pr"
"github.com/Tomas-vilte/MateCommit/internal/cli/command/suggest"
"github.com/Tomas-vilte/MateCommit/internal/cli/registry"
cfg "github.com/Tomas-vilte/MateCommit/internal/config"
"github.com/Tomas-vilte/MateCommit/internal/i18n"
"github.com/Tomas-vilte/MateCommit/internal/infrastructure/ai/gemini"
"github.com/Tomas-vilte/MateCommit/internal/infrastructure/factory"
"github.com/Tomas-vilte/MateCommit/internal/infrastructure/git"
"github.com/Tomas-vilte/MateCommit/internal/infrastructure/tickets/jira"
"github.com/Tomas-vilte/MateCommit/internal/services"
Expand Down Expand Up @@ -57,6 +59,11 @@
log.Fatalf("Error initializing AI service: %v", err)
}

aiSummarizer, err := gemini.NewGeminiPRSummarizer(context.Background(), cfgApp, translations)
if err != nil {
log.Fatalf("Error al crear el servicio: %v", err)
}

Check warning on line 65 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L62-L65

Added lines #L62 - L65 were not covered by tests

ticketService := jira.NewJiraService(cfgApp, &http.Client{})

commitService := services.NewCommitService(gitService, aiProvider, ticketService, cfgApp)
Expand All @@ -65,6 +72,15 @@

registerCommand := registry.NewRegistry(cfgApp, translations)

prServiceFactory := factory.NewPrServiceFactory(cfgApp, translations, aiSummarizer)
prService, err := prServiceFactory.CreatePRService()
if err != nil {
log.Printf("Warning: %v", err)
log.Println("Algunos comandos estan desactivados, configura el vcs")
}

Check warning on line 80 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L75-L80

Added lines #L75 - L80 were not covered by tests

prCommand := pr.NewSummarizeCommand(prService)

Check warning on line 83 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L82-L83

Added lines #L82 - L83 were not covered by tests
if err := registerCommand.Register("suggest", suggest.NewSuggestCommandFactory(commitService, commitHandler)); err != nil {
log.Fatalf("Error al registrar el comando 'suggest': %v", err)
}
Expand All @@ -73,6 +89,10 @@
log.Fatalf("Error al registrar el comando 'config': %v", err)
}

if err := registerCommand.Register("summarize-pr", prCommand); err != nil {
log.Fatalf("Error al registrar el comando 'summarize-pr': %v", err)
}

Check warning on line 94 in cmd/main.go

View check run for this annotation

Codecov / codecov/patch

cmd/main.go#L92-L94

Added lines #L92 - L94 were not covered by tests

return &cli.Command{
Name: "mate-commit",
Usage: translations.GetMessage("app_usage", 0, nil),
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/command/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
c.newSetTicketCommand(t, cfg),
c.newSetAIActiveCommand(t, cfg),
c.newSetAIModelCommand(t, cfg),
c.newSetActiveVCSCommand(t, cfg),
c.newSetVCSConfigCommand(t, cfg),

Check warning on line 30 in internal/cli/command/config/config.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/command/config/config.go#L29-L30

Added lines #L29 - L30 were not covered by tests
},
}
}
48 changes: 48 additions & 0 deletions internal/cli/command/config/set_vcs_active.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package config

import (
"context"
"fmt"
"github.com/Tomas-vilte/MateCommit/internal/config"
"github.com/Tomas-vilte/MateCommit/internal/i18n"
"github.com/urfave/cli/v3"
)

func (c *ConfigCommandFactory) newSetActiveVCSCommand(t *i18n.Translations, cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "set-active-vcs",
Usage: t.GetMessage("vcs_summary.config_set_active_vcs_usage", 0, nil),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "provider",
Aliases: []string{"p"},
Usage: t.GetMessage("vcs_summary.config_set_active_vcs_provider_usage", 0, nil),
Required: true,
},
},
Action: func(ctx context.Context, command *cli.Command) error {
provider := command.String("provider")

if _, exists := cfg.VCSConfigs[provider]; !exists {
msg := t.GetMessage("error.vcs_provider_not_configured", 0, map[string]interface{}{
"Provider": provider,
})
return fmt.Errorf("%s", msg)
}

cfg.ActiveVCSProvider = provider

if err := config.SaveConfig(cfg); err != nil {
msg := t.GetMessage("config_save.error_saving_config", 0, map[string]interface{}{
"Error": err.Error(),
})
return fmt.Errorf("%s", msg)
}

fmt.Println(t.GetMessage("vcs_summary.config_active_vcs_updated", 0, map[string]interface{}{
"Provider": provider,
}))
return nil
},
}
}
96 changes: 96 additions & 0 deletions internal/cli/command/config/set_vcs_active_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package config

import (
"context"
"github.com/Tomas-vilte/MateCommit/internal/config"
"github.com/stretchr/testify/assert"
"os"
"testing"
)

func TestSetActiveVCSCommand(t *testing.T) {
t.Run("should successfully set active VCS provider", func(t *testing.T) {
// Arrange
cfg, translations, tmpConfigPath, cleanup := setupConfigTest(t)
defer cleanup()

cfg.VCSConfigs = map[string]config.VCSConfig{
"github": {Provider: "github"},
"gitlab": {Provider: "gitlab"},
}
cfg.ActiveVCSProvider = "gitlab"
assert.NoError(t, config.SaveConfig(cfg))

factory := NewConfigCommandFactory()
cmd := factory.newSetActiveVCSCommand(translations, cfg)

app := cmd
ctx := context.Background()

// Act
err := app.Run(ctx, []string{"set-active-vcs", "--provider", "github"})

// Assert
assert.NoError(t, err)
loadedCfg, err := config.LoadConfig(tmpConfigPath)
assert.NoError(t, err)
assert.Equal(t, "github", loadedCfg.ActiveVCSProvider)
})

t.Run("should fail with non-existent provider", func(t *testing.T) {
// Arrange
cfg, translations, tmpConfigPath, cleanup := setupConfigTest(t)
defer cleanup()

cfg.VCSConfigs = map[string]config.VCSConfig{
"github": {Provider: "github"},
}
cfg.ActiveVCSProvider = "github"
assert.NoError(t, config.SaveConfig(cfg))

factory := NewConfigCommandFactory()
cmd := factory.newSetActiveVCSCommand(translations, cfg)

app := cmd
ctx := context.Background()

// Act
err := app.Run(ctx, []string{"set-active-vcs", "--provider", "bitbucket"})

// Assert
assert.Error(t, err)
assert.Contains(t, err.Error(), translations.GetMessage("error.vcs_provider_not_configured", 0, map[string]interface{}{
"Provider": "bitbucket",
}))
loadedCfg, err := config.LoadConfig(tmpConfigPath)
assert.NoError(t, err)
assert.Equal(t, "github", loadedCfg.ActiveVCSProvider)
})

t.Run("config save error", func(t *testing.T) {
// Arrange
cfg, translations, tmpConfigPath, cleanup := setupConfigTest(t)
defer cleanup()

err := os.Mkdir(tmpConfigPath, 0755)
assert.NoError(t, err)

cfg.VCSConfigs = map[string]config.VCSConfig{
"github": {Provider: "github"},
}

factory := NewConfigCommandFactory()
cmd := factory.newSetActiveVCSCommand(translations, cfg)

app := cmd
ctx := context.Background()

// Act
err = app.Run(ctx, []string{"set-active-vcs", "--provider", "github"})

// Assert
assert.Error(t, err)
assert.Contains(t, err.Error(), "error al guardar la configuración")
assert.Equal(t, "github", cfg.ActiveVCSProvider)
})
}
78 changes: 78 additions & 0 deletions internal/cli/command/config/set_vcs_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package config

import (
"context"
"fmt"
"github.com/Tomas-vilte/MateCommit/internal/config"
"github.com/Tomas-vilte/MateCommit/internal/i18n"
"github.com/urfave/cli/v3"
)

func (c *ConfigCommandFactory) newSetVCSConfigCommand(t *i18n.Translations, cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "set-vcs",
Usage: t.GetMessage("vcs_summary.config_set_vcs_usage", 0, nil),
Flags: []cli.Flag{
&cli.StringFlag{
Name: "provider",
Aliases: []string{"p"},
Usage: t.GetMessage("vcs_summary.config_set_vcs_provider_usage", 0, nil),
Required: true,
},
&cli.StringFlag{
Name: "token",
Aliases: []string{"t"},
Usage: t.GetMessage("vcs_summary.config_set_vcs_token_usage", 0, nil),
Required: false,
},
&cli.StringFlag{
Name: "owner",
Aliases: []string{"o"},
Usage: t.GetMessage("vcs_summary.config_set_vcs_owner_usage", 0, nil),
Required: false,
},
&cli.StringFlag{
Name: "repo",
Aliases: []string{"r"},
Usage: t.GetMessage("vcs_summary.config_set_vcs_repo_usage", 0, nil),
Required: false,
},
},
Action: func(ctx context.Context, command *cli.Command) error {
provider := command.String("provider")

if cfg.VCSConfigs == nil {
cfg.VCSConfigs = make(map[string]config.VCSConfig)
}

vcsConfig, exists := cfg.VCSConfigs[provider]
if !exists {
vcsConfig = config.VCSConfig{Provider: provider}
}

if token := command.String("token"); token != "" {
vcsConfig.Token = token
}
if owner := command.String("owner"); owner != "" {
vcsConfig.Owner = owner
}
if repo := command.String("repo"); repo != "" {
vcsConfig.Repo = repo
}

cfg.VCSConfigs[provider] = vcsConfig

if err := config.SaveConfig(cfg); err != nil {
msg := t.GetMessage("config_save.error_saving_config", 0, map[string]interface{}{
"Error": err.Error(),
})
return fmt.Errorf("%s", msg)
}

fmt.Println(t.GetMessage("vcs_summary.config_vcs_updated", 0, map[string]interface{}{
"Provider": provider,
}))
return nil
},
}
}
Loading
Loading