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

test(git provider): add tests for git providers creation from URL #2119

Merged
merged 13 commits into from Oct 31, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions pkg/auth/server.go
Expand Up @@ -50,3 +50,16 @@ func (s *AuthServer) GetUsernames() []string {
sort.Strings(answer)
return answer
}

func (s *AuthServer) HasUserAuths() bool {
return len(s.Users) > 0
}

func (s *AuthServer) CurrentAuth() *UserAuth {
for _, user := range s.Users {
if user.Username == s.CurrentUser {
return user
}
}
return nil
}
22 changes: 11 additions & 11 deletions pkg/gits/provider.go
Expand Up @@ -322,12 +322,18 @@ func (i *GitRepositoryInfo) CreateProvider(authConfigSvc auth.AuthConfigService,
func CreateProviderForURL(authConfigSvc auth.AuthConfigService, gitKind string, hostUrl string, git Gitter, batchMode bool, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) (GitProvider, error) {
config := authConfigSvc.Config()
server := config.GetOrCreateServer(hostUrl)
url := server.URL
if gitKind != "" {
server.Kind = gitKind
}
userAuths := authConfigSvc.Config().FindUserAuths(url)
if len(userAuths) == 0 {

var userAuth *auth.UserAuth
if server != nil {
userAuth = server.CurrentAuth()
}

if userAuth != nil && !userAuth.IsInvalid() {
return CreateProvider(server, userAuth, git)
} else {
kind := server.Kind
if kind != "" {
userAuth := auth.CreateAuthUserFromEnvironment(strings.ToUpper(kind))
Expand All @@ -340,16 +346,11 @@ func CreateProviderForURL(authConfigSvc auth.AuthConfigService, gitKind string,
return CreateProvider(server, &userAuth, git)
}
}
if len(userAuths) > 0 {
// TODO use default user???
auth := userAuths[0]
return CreateProvider(server, auth, git)
}
auth, err := createUserForServer(batchMode, authConfigSvc, server, git, in, out, errOut)
userAuth, err := createUserForServer(batchMode, authConfigSvc, server, git, in, out, errOut)
if err != nil {
return nil, err
}
return CreateProvider(server, auth, git)
return CreateProvider(server, userAuth, git)
}

func createUserForServer(batchMode bool, authConfigSvc auth.AuthConfigService, server *auth.AuthServer,
Expand All @@ -361,7 +362,6 @@ func createUserForServer(batchMode bool, authConfigSvc auth.AuthConfigService, s
return nil
}

// TODO could we guess this based on the users ~/.git for github?
defaultUserName := ""
err := authConfigSvc.Config().EditUserAuth(server.Label(), userAuth, defaultUserName, false, batchMode, f, in, out, errOut)
if err != nil {
Expand Down