-
Notifications
You must be signed in to change notification settings - Fork 1
/
git_repo.go
150 lines (137 loc) · 3.74 KB
/
git_repo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package gits
import (
"fmt"
"io"
"strings"
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/util"
"gopkg.in/AlecAivazis/survey.v1"
)
type CreateRepoData struct {
Organisation string
RepoName string
FullName string
PrivateRepo bool
User *auth.UserAuth
GitProvider GitProvider
}
type GitRepositoryOptions struct {
ServerURL string
Username string
ApiToken string
}
func (d *CreateRepoData) CreateRepository() (*GitRepository, error) {
return d.GitProvider.CreateRepository(d.Organisation, d.RepoName, d.PrivateRepo)
}
func PickNewGitRepository(out io.Writer, batchMode bool, authConfigSvc auth.AuthConfigService, defaultRepoName string, repoOptions GitRepositoryOptions) (*CreateRepoData, error) {
config := authConfigSvc.Config()
var err error
var server *auth.AuthServer
if repoOptions.ServerURL != "" {
server = config.GetOrCreateServer(repoOptions.ServerURL)
} else {
server, err = config.PickServer("Which git provider?")
if err != nil {
return nil, err
}
}
fmt.Fprintf(out, "Using git provider %s\n", util.ColorInfo(server.Description()))
url := server.URL
var userAuth *auth.UserAuth
if repoOptions.Username != "" {
userAuth = config.GetOrCreateUserAuth(url, repoOptions.Username)
} else {
if batchMode {
if len(server.Users) == 0 {
return nil, fmt.Errorf("Server %s has no user auths defined!", url)
}
var ua *auth.UserAuth
if server.CurrentUser != "" {
ua = config.FindUserAuth(url, server.CurrentUser)
}
if ua == nil {
ua = server.Users[0]
}
userAuth = ua
} else {
userAuth, err = config.PickServerUserAuth(server, "git user name?")
if err != nil {
return nil, err
}
}
}
if userAuth.IsInvalid() && repoOptions.ApiToken != "" {
userAuth.ApiToken = repoOptions.ApiToken
}
if userAuth.IsInvalid() {
PrintCreateRepositoryGenerateAccessToken(server, out)
// TODO could we guess this based on the users ~/.git for github?
defaultUserName := ""
err = config.EditUserAuth(userAuth, defaultUserName, true)
if err != nil {
return nil, err
}
// TODO lets verify the auth works
err = authConfigSvc.SaveUserAuth(url, userAuth)
if err != nil {
return nil, fmt.Errorf("Failed to store git auth configuration %s", err)
}
if userAuth.IsInvalid() {
return nil, fmt.Errorf("You did not properly define the user authentication!")
}
}
gitUsername := userAuth.Username
fmt.Fprintf(out, "\n\nAbout to create a repository on server %s with user %s\n", util.ColorInfo(url), util.ColorInfo(gitUsername))
provider, err := CreateProvider(server, userAuth)
if err != nil {
return nil, err
}
org, err := PickOrganisation(provider, gitUsername)
if err != nil {
return nil, err
}
owner := org
if org == "" {
owner = gitUsername
}
repoName := ""
if batchMode {
repoName = defaultRepoName
if repoName == "" {
repoName = "dummy"
}
} else {
prompt := &survey.Input{
Message: "Enter the new repository name: ",
Default: defaultRepoName,
}
validator := func(val interface{}) error {
str, ok := val.(string)
if !ok {
return fmt.Errorf("Expected string value!")
}
if strings.TrimSpace(str) == "" {
return fmt.Errorf("Repository name is required")
}
return provider.ValidateRepositoryName(owner, str)
}
err = survey.AskOne(prompt, &repoName, validator)
if err != nil {
return nil, err
}
if repoName == "" {
return nil, fmt.Errorf("No repository name specified!")
}
}
fullName := GitRepoName(org, repoName)
fmt.Fprintf(out, "\n\nCreating repository %s\n", util.ColorInfo(fullName))
privateRepo := false
return &CreateRepoData{
Organisation: org,
RepoName: repoName,
FullName: fullName,
PrivateRepo: privateRepo,
User: userAuth,
GitProvider: provider,
}, err
}