From ac8a4ccb55d60f2d476a9798c31794dddad0b75a Mon Sep 17 00:00:00 2001 From: Toma Puljak Date: Thu, 17 Oct 2024 10:55:41 +0000 Subject: [PATCH 1/2] feat: update project config git provider Signed-off-by: Toma Puljak --- pkg/cmd/gitprovider/delete.go | 5 ++- pkg/cmd/gitprovider/update.go | 5 ++- pkg/cmd/projectconfig/update.go | 44 +++++++++++++++---- pkg/cmd/workspace/create.go | 5 ++- pkg/cmd/workspace/util/creation_data.go | 5 ++- .../workspace/selection/gitproviderconfig.go | 27 +++++++++--- 6 files changed, 72 insertions(+), 19 deletions(-) diff --git a/pkg/cmd/gitprovider/delete.go b/pkg/cmd/gitprovider/delete.go index 2d6c18d178..4082fb7860 100644 --- a/pkg/cmd/gitprovider/delete.go +++ b/pkg/cmd/gitprovider/delete.go @@ -46,7 +46,10 @@ var gitProviderDeleteCmd = &cobra.Command{ return nil } - selectedGitProvider := selection.GetGitProviderConfigFromPrompt(gitProviders, false, "Remove") + selectedGitProvider := selection.GetGitProviderConfigFromPrompt(selection.GetGitProviderConfigParams{ + GitProviderConfigs: gitProviders, + ActionVerb: "Remove", + }) if selectedGitProvider == nil { return nil diff --git a/pkg/cmd/gitprovider/update.go b/pkg/cmd/gitprovider/update.go index bc0d82122d..b8fa535541 100644 --- a/pkg/cmd/gitprovider/update.go +++ b/pkg/cmd/gitprovider/update.go @@ -36,7 +36,10 @@ var gitProviderUpdateCmd = &cobra.Command{ return nil } - selectedGitProvider := selection.GetGitProviderConfigFromPrompt(gitProviders, false, "Update") + selectedGitProvider := selection.GetGitProviderConfigFromPrompt(selection.GetGitProviderConfigParams{ + GitProviderConfigs: gitProviders, + ActionVerb: "Update", + }) if selectedGitProvider == nil { return nil diff --git a/pkg/cmd/projectconfig/update.go b/pkg/cmd/projectconfig/update.go index 19a9f4b021..e0e9163ac0 100644 --- a/pkg/cmd/projectconfig/update.go +++ b/pkg/cmd/projectconfig/update.go @@ -6,6 +6,7 @@ package projectconfig import ( "context" "net/http" + "net/url" apiclient_util "github.com/daytonaio/daytona/internal/util/apiclient" "github.com/daytonaio/daytona/pkg/apiclient" @@ -59,8 +60,9 @@ var projectConfigUpdateCmd = &cobra.Command{ Url: projectConfig.RepositoryUrl, }, }, - BuildConfig: projectConfig.BuildConfig, - EnvVars: projectConfig.EnvVars, + BuildConfig: projectConfig.BuildConfig, + EnvVars: projectConfig.EnvVars, + GitProviderConfigId: projectConfig.GitProviderConfigId, }, } @@ -74,18 +76,42 @@ var projectConfigUpdateCmd = &cobra.Command{ projectDefaults.DevcontainerFilePath = projectConfig.BuildConfig.Devcontainer.FilePath } - create.ProjectsConfigurationChanged, err = create.RunProjectConfiguration(&createDto, *projectDefaults) + _, err = create.RunProjectConfiguration(&createDto, *projectDefaults) if err != nil { return err } + eligibleGitProviders, res, err := apiClient.GitProviderAPI.ListGitProvidersForUrl(ctx, url.QueryEscape(projectConfig.RepositoryUrl)).Execute() + if err != nil { + return apiclient_util.HandleErrorResponse(res, err) + } + + if len(eligibleGitProviders) > 0 { + selectedGitProvider := selection.GetGitProviderConfigFromPrompt(selection.GetGitProviderConfigParams{ + GitProviderConfigs: eligibleGitProviders, + ActionVerb: "Use", + WithNoneOption: true, + }) + + if selectedGitProvider == nil { + return nil + } + + if selectedGitProvider.Id == selection.NoneGitProviderConfigIdentifier { + createDto[0].GitProviderConfigId = nil + } else { + createDto[0].GitProviderConfigId = &selectedGitProvider.Id + } + } + newProjectConfig := apiclient.CreateProjectConfigDTO{ - Name: projectConfig.Name, - BuildConfig: createDto[0].BuildConfig, - Image: createDto[0].Image, - User: createDto[0].User, - RepositoryUrl: createDto[0].Source.Repository.Url, - EnvVars: createDto[0].EnvVars, + Name: projectConfig.Name, + BuildConfig: createDto[0].BuildConfig, + Image: createDto[0].Image, + User: createDto[0].User, + RepositoryUrl: createDto[0].Source.Repository.Url, + EnvVars: createDto[0].EnvVars, + GitProviderConfigId: createDto[0].GitProviderConfigId, } res, err = apiClient.ProjectConfigAPI.SetProjectConfig(ctx).ProjectConfig(newProjectConfig).Execute() diff --git a/pkg/cmd/workspace/create.go b/pkg/cmd/workspace/create.go index 3ea177feb4..e169138b8b 100644 --- a/pkg/cmd/workspace/create.go +++ b/pkg/cmd/workspace/create.go @@ -422,7 +422,10 @@ func processGitURL(ctx context.Context, repoUrl string, apiClient *apiclient.API if len(gitProviderConfigs) == 1 { projectConfigurationFlags.GitProviderConfig = &gitProviderConfigs[0].Id } else if len(gitProviderConfigs) > 1 { - gp := selection.GetGitProviderConfigFromPrompt(gitProviderConfigs, false, "Use") + gp := selection.GetGitProviderConfigFromPrompt(selection.GetGitProviderConfigParams{ + GitProviderConfigs: gitProviderConfigs, + ActionVerb: "Use", + }) projectConfigurationFlags.GitProviderConfig = &gp.Id } diff --git a/pkg/cmd/workspace/util/creation_data.go b/pkg/cmd/workspace/util/creation_data.go index 1bb5bbbd0e..1655acc48f 100644 --- a/pkg/cmd/workspace/util/creation_data.go +++ b/pkg/cmd/workspace/util/creation_data.go @@ -146,7 +146,10 @@ func GetProjectsCreationDataFromPrompt(config ProjectsDataPromptConfig) ([]apicl if len(gitProviderConfigs) == 1 { gitProviderConfigId = gitProviderConfigs[0].Id } else if len(gitProviderConfigs) > 1 { - gp := selection.GetGitProviderConfigFromPrompt(gitProviderConfigs, false, "Use") + gp := selection.GetGitProviderConfigFromPrompt(selection.GetGitProviderConfigParams{ + GitProviderConfigs: gitProviderConfigs, + ActionVerb: "Use", + }) gitProviderConfigId = gp.Id } else { gitProviderConfigId = "" diff --git a/pkg/views/workspace/selection/gitproviderconfig.go b/pkg/views/workspace/selection/gitproviderconfig.go index d22abc7b1b..e03b30f705 100644 --- a/pkg/views/workspace/selection/gitproviderconfig.go +++ b/pkg/views/workspace/selection/gitproviderconfig.go @@ -17,19 +17,27 @@ import ( ) var NewGitProviderConfigIdentifier = "" +var NoneGitProviderConfigIdentifier = "" -func GetGitProviderConfigFromPrompt(gitProviderConfigs []apiclient.GitProvider, withNewGitProviderConfig bool, actionVerb string) *apiclient.GitProvider { +type GetGitProviderConfigParams struct { + GitProviderConfigs []apiclient.GitProvider + WithNewGitProviderConfig bool + WithNoneOption bool + ActionVerb string +} + +func GetGitProviderConfigFromPrompt(params GetGitProviderConfigParams) *apiclient.GitProvider { choiceChan := make(chan *apiclient.GitProvider) - go selectGitProviderConfigPrompt(gitProviderConfigs, withNewGitProviderConfig, actionVerb, choiceChan) + go selectGitProviderConfigPrompt(params, choiceChan) return <-choiceChan } -func selectGitProviderConfigPrompt(gitProviderConfigs []apiclient.GitProvider, withNewGitProviderConfig bool, actionVerb string, choiceChan chan<- *apiclient.GitProvider) { +func selectGitProviderConfigPrompt(params GetGitProviderConfigParams, choiceChan chan<- *apiclient.GitProvider) { items := []list.Item{} supportedGitProviders := config.GetSupportedGitProviders() - for _, gp := range gitProviderConfigs { + for _, gp := range params.GitProviderConfigs { title := fmt.Sprintf("%s (%s)", gp.ProviderId, gp.Alias) for _, provider := range supportedGitProviders { @@ -48,13 +56,20 @@ func selectGitProviderConfigPrompt(gitProviderConfigs []apiclient.GitProvider, w items = append(items, newItem) } - if withNewGitProviderConfig { + if params.WithNewGitProviderConfig { newItem := item[apiclient.GitProvider]{title: "+ Add a new Git provider", desc: "", choiceProperty: apiclient.GitProvider{ Id: NewGitProviderConfigIdentifier, }} items = append(items, newItem) } + if params.WithNoneOption { + newItem := item[apiclient.GitProvider]{title: "- None", desc: "", choiceProperty: apiclient.GitProvider{ + Id: NoneGitProviderConfigIdentifier, + }} + items = append(items, newItem) + } + d := list.NewDefaultDelegate() d.Styles.SelectedTitle = lipgloss.NewStyle(). @@ -74,7 +89,7 @@ func selectGitProviderConfigPrompt(gitProviderConfigs []apiclient.GitProvider, w l.FilterInput.PromptStyle = lipgloss.NewStyle().Foreground(views.Green) l.FilterInput.TextStyle = lipgloss.NewStyle().Foreground(views.Green) - title := "Select a Git Provider Config To " + actionVerb + title := "Select a Git Provider Config To " + params.ActionVerb l.Title = views.GetStyledMainTitle(title) l.Styles.Title = titleStyle From 6773a95d3f848be03376aa2fd62ad258bdef7392 Mon Sep 17 00:00:00 2001 From: Toma Puljak Date: Thu, 17 Oct 2024 15:16:28 +0000 Subject: [PATCH 2/2] fix: review change Signed-off-by: Toma Puljak --- pkg/cmd/projectconfig/update.go | 7 ++++--- pkg/cmd/workspace/util/creation_data.go | 3 +++ pkg/views/workspace/selection/gitproviderconfig.go | 13 ++++++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/projectconfig/update.go b/pkg/cmd/projectconfig/update.go index e0e9163ac0..11a52dd52c 100644 --- a/pkg/cmd/projectconfig/update.go +++ b/pkg/cmd/projectconfig/update.go @@ -88,9 +88,10 @@ var projectConfigUpdateCmd = &cobra.Command{ if len(eligibleGitProviders) > 0 { selectedGitProvider := selection.GetGitProviderConfigFromPrompt(selection.GetGitProviderConfigParams{ - GitProviderConfigs: eligibleGitProviders, - ActionVerb: "Use", - WithNoneOption: true, + GitProviderConfigs: eligibleGitProviders, + ActionVerb: "Use", + WithNoneOption: true, + PreselectedGitProviderId: projectConfig.GitProviderConfigId, }) if selectedGitProvider == nil { diff --git a/pkg/cmd/workspace/util/creation_data.go b/pkg/cmd/workspace/util/creation_data.go index 1655acc48f..ac1edeb3a6 100644 --- a/pkg/cmd/workspace/util/creation_data.go +++ b/pkg/cmd/workspace/util/creation_data.go @@ -150,6 +150,9 @@ func GetProjectsCreationDataFromPrompt(config ProjectsDataPromptConfig) ([]apicl GitProviderConfigs: gitProviderConfigs, ActionVerb: "Use", }) + if gp == nil { + return nil, common.ErrCtrlCAbort + } gitProviderConfigId = gp.Id } else { gitProviderConfigId = "" diff --git a/pkg/views/workspace/selection/gitproviderconfig.go b/pkg/views/workspace/selection/gitproviderconfig.go index e03b30f705..7b285fab72 100644 --- a/pkg/views/workspace/selection/gitproviderconfig.go +++ b/pkg/views/workspace/selection/gitproviderconfig.go @@ -24,6 +24,7 @@ type GetGitProviderConfigParams struct { WithNewGitProviderConfig bool WithNoneOption bool ActionVerb string + PreselectedGitProviderId *string } func GetGitProviderConfigFromPrompt(params GetGitProviderConfigParams) *apiclient.GitProvider { @@ -34,10 +35,11 @@ func GetGitProviderConfigFromPrompt(params GetGitProviderConfigParams) *apiclien func selectGitProviderConfigPrompt(params GetGitProviderConfigParams, choiceChan chan<- *apiclient.GitProvider) { items := []list.Item{} + selectedProviderIndex := -1 supportedGitProviders := config.GetSupportedGitProviders() - for _, gp := range params.GitProviderConfigs { + for i, gp := range params.GitProviderConfigs { title := fmt.Sprintf("%s (%s)", gp.ProviderId, gp.Alias) for _, provider := range supportedGitProviders { @@ -46,6 +48,11 @@ func selectGitProviderConfigPrompt(params GetGitProviderConfigParams, choiceChan } } + if params.PreselectedGitProviderId != nil && *params.PreselectedGitProviderId == gp.Id { + title = fmt.Sprintf("%s (currently used)", title) + selectedProviderIndex = i + } + desc := gp.Id if gp.BaseApiUrl != nil && *gp.BaseApiUrl != "" { @@ -83,6 +90,10 @@ func selectGitProviderConfigPrompt(params GetGitProviderConfigParams, choiceChan l := list.New(items, d, 0, 0) + if selectedProviderIndex != -1 { + l.Select(selectedProviderIndex) + } + l.Styles.FilterPrompt = lipgloss.NewStyle().Foreground(views.Green) l.Styles.FilterCursor = lipgloss.NewStyle().Foreground(views.Green)