Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update project config git provider #1263

Merged
merged 2 commits into from
Oct 18, 2024
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
5 changes: 4 additions & 1 deletion pkg/cmd/gitprovider/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/gitprovider/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
45 changes: 36 additions & 9 deletions pkg/cmd/projectconfig/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
},
}

Expand All @@ -74,18 +76,43 @@ 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,
PreselectedGitProviderId: projectConfig.GitProviderConfigId,
})

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()
Expand Down
5 changes: 4 additions & 1 deletion pkg/cmd/workspace/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
8 changes: 7 additions & 1 deletion pkg/cmd/workspace/util/creation_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,13 @@ 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",
})
if gp == nil {
return nil, common.ErrCtrlCAbort
}
gitProviderConfigId = gp.Id
} else {
gitProviderConfigId = ""
Expand Down
38 changes: 32 additions & 6 deletions pkg/views/workspace/selection/gitproviderconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,29 @@ import (
)

var NewGitProviderConfigIdentifier = "<NEW_GIT_PROVIDER_CONFIG>"
var NoneGitProviderConfigIdentifier = "<NONE>"

type GetGitProviderConfigParams struct {
GitProviderConfigs []apiclient.GitProvider
WithNewGitProviderConfig bool
WithNoneOption bool
ActionVerb string
PreselectedGitProviderId *string
}

func GetGitProviderConfigFromPrompt(gitProviderConfigs []apiclient.GitProvider, withNewGitProviderConfig bool, actionVerb string) *apiclient.GitProvider {
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{}
selectedProviderIndex := -1

supportedGitProviders := config.GetSupportedGitProviders()

for _, gp := range gitProviderConfigs {
for i, gp := range params.GitProviderConfigs {
title := fmt.Sprintf("%s (%s)", gp.ProviderId, gp.Alias)

for _, provider := range supportedGitProviders {
Expand All @@ -38,6 +48,11 @@ func selectGitProviderConfigPrompt(gitProviderConfigs []apiclient.GitProvider, w
}
}

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 != "" {
Expand All @@ -48,13 +63,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().
Expand All @@ -68,13 +90,17 @@ func selectGitProviderConfigPrompt(gitProviderConfigs []apiclient.GitProvider, w

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)

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
Expand Down
Loading