forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
196 lines (167 loc) · 4.58 KB
/
provider.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package gits
import (
"fmt"
"github.com/jenkins-x/jx/pkg/auth"
"gopkg.in/AlecAivazis/survey.v1"
"sort"
"strings"
)
type GitProvider interface {
ListOrganisations() ([]GitOrganisation, error)
ListRepositories(org string) ([]*GitRepository, error)
CreateRepository(org string, name string, private bool) (*GitRepository, error)
DeleteRepository(org string, name string) error
ForkRepository(originalOrg string, name string, destinationOrg string) (*GitRepository, error)
RenameRepository(org string, name string, newName string) (*GitRepository, error)
ValidateRepositoryName(org string, name string) error
CreatePullRequest(data *GitPullRequestArguments) (*GitPullRequest, error)
CreateWebHook(data *GitWebHookArguments) error
IsGitHub() bool
// returns the path relative to the Jenkins URL to trigger webhooks on this kind of repository
//
// e.g. for GitHub its /github-webhook/
// other examples include:
//
// * gitlab: /gitlab/notify_commit
// https://github.com/elvanja/jenkins-gitlab-hook-plugin#notify-commit-hook
//
// * git plugin
// /git/notifyCommit?url=
// http://kohsuke.org/2011/12/01/polling-must-die-triggering-jenkins-builds-from-a-git-hook/
//
// * generic webhook
// /generic-webhook-trigger/invoke?token=abc123
// https://wiki.jenkins.io/display/JENKINS/Generic+Webhook+Trigger+Plugin
JenkinsWebHookPath(gitURL string, secret string) string
Label() string
}
type GitOrganisation struct {
Login string
}
type GitRepository struct {
Name string
AllowMergeCommit bool
HTMLURL string
CloneURL string
SSHURL string
}
type GitPullRequest struct {
URL string
}
type GitPullRequestArguments struct {
Owner string
Repo string
Title string
Body string
Head string
Base string
}
type GitWebHookArguments struct {
Owner string
Repo string
URL string
Secret string
}
func CreateProvider(server *auth.AuthServer, user *auth.UserAuth) (GitProvider, error) {
switch server.Kind {
case "gitea":
return NewGiteaProvider(server, user)
default:
return NewGitHubProvider(server, user)
}
}
func ProviderAccessTokenURL(kind string, url string) string {
switch kind {
case "gitea":
return GiteaAccessTokenURL(url)
default:
return GitHubAccessTokenURL(url)
}
}
// PickOrganisation picks an organisations login if there is one available
func PickOrganisation(provider GitProvider, userName string) (string, error) {
answer := ""
orgs, err := provider.ListOrganisations()
if err != nil {
return answer, err
}
if len(orgs) == 0 {
return answer, nil
}
if len(orgs) == 1 {
return orgs[0].Login, nil
}
orgNames := []string{userName}
for _, o := range orgs {
name := o.Login
if name != "" {
orgNames = append(orgNames, name)
}
}
sort.Strings(orgNames)
orgName := ""
prompt := &survey.Select{
Message: "Which organisation do you want to use?",
Options: orgNames,
Default: userName,
}
err = survey.AskOne(prompt, &orgName, nil)
if err != nil {
return "", err
}
if orgName == userName {
return "", nil
}
return orgName, nil
}
func PickRepositories(provider GitProvider, owner string, message string, selectAll bool, filter string) ([]*GitRepository, error) {
answer := []*GitRepository{}
repos, err := provider.ListRepositories(owner)
if err != nil {
return answer, err
}
repoMap := map[string]*GitRepository{}
allRepoNames := []string{}
for _, repo := range repos {
n := repo.Name
if n != "" && (filter == "" || strings.Contains(n, filter)) {
allRepoNames = append(allRepoNames, n)
repoMap[n] = repo
}
}
if len(allRepoNames) == 0 {
return answer, fmt.Errorf("No matching repositories could be found!")
}
sort.Strings(allRepoNames)
prompt := &survey.MultiSelect{
Message: message,
Options: allRepoNames,
}
if selectAll {
prompt.Default = allRepoNames
}
repoNames := []string{}
err = survey.AskOne(prompt, &repoNames, nil)
for _, n := range repoNames {
repo := repoMap[n]
if repo != nil {
answer = append(answer, repo)
}
}
return answer, err
}
func (i *GitRepositoryInfo) PickOrCreateProvider(authConfigSvc auth.AuthConfigService, message string) (GitProvider, error) {
config := authConfigSvc.Config()
server := config.GetOrCreateServer(i.Host)
userAuth, err := config.PickServerUserAuth(server, message)
if err != nil {
return nil, err
}
return i.CreateProviderForUser(server, &userAuth)
}
func (i *GitRepositoryInfo) CreateProviderForUser(server *auth.AuthServer, user *auth.UserAuth) (GitProvider, error) {
if i.Host == GitHubHost {
return NewGitHubProvider(server, user)
}
return nil, fmt.Errorf("Git provider not supported for host %s", i.Host)
}