-
Notifications
You must be signed in to change notification settings - Fork 788
/
gerrit.go
375 lines (295 loc) · 11 KB
/
gerrit.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package gits
import (
"context"
"fmt"
"net/url"
"os"
"time"
gerrit "github.com/andygrunwald/go-gerrit"
"github.com/google/go-github/github"
"github.com/jenkins-x/jx/v2/pkg/auth"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/pkg/errors"
)
type GerritProvider struct {
Client *gerrit.Client
Username string
Context context.Context
Server auth.AuthServer
User auth.UserAuth
Git Gitter
}
func NewGerritProvider(server *auth.AuthServer, user *auth.UserAuth, git Gitter) (GitProvider, error) {
ctx := context.Background()
provider := GerritProvider{
Server: *server,
User: *user,
Context: ctx,
Username: user.Username,
Git: git,
}
client, err := gerrit.NewClient(server.URL, nil)
if err != nil {
return nil, err
}
client.Authentication.SetBasicAuth(user.Username, user.ApiToken)
provider.Client = client
return &provider, nil
}
// We have to do this because url.Escape is not idempotent, so we unescape the URL
// to ensure it's not encoded, then we re-encode it.
func buildEncodedProjectName(org, name string) string {
var fullName string
if org != "" {
fullName = fmt.Sprintf("%s/%s", org, name)
} else {
fullName = fmt.Sprintf("%s", name)
}
fullNamePathUnescaped, err := url.PathUnescape(fullName)
if err != nil {
return ""
}
fullNamePathEscaped := url.PathEscape(fullNamePathUnescaped)
return fullNamePathEscaped
}
func (p *GerritProvider) projectInfoToGitRepository(project *gerrit.ProjectInfo) *GitRepository {
return &GitRepository{
Name: project.Name,
CloneURL: fmt.Sprintf("%s/%s", p.Server.URL, project.Name),
SSHURL: fmt.Sprintf("%s:%s", p.Server.URL, project.Name),
}
}
func (p *GerritProvider) ListRepositories(org string) ([]*GitRepository, error) {
options := &gerrit.ProjectOptions{
Description: true,
Prefix: url.PathEscape(org),
}
gerritProjects, _, err := p.Client.Projects.ListProjects(options)
if err != nil {
return nil, err
}
repos := []*GitRepository{}
for name, project := range *gerritProjects {
project.Name = name
repo := p.projectInfoToGitRepository(&project)
repos = append(repos, repo)
}
return repos, nil
}
func (p *GerritProvider) CreateRepository(org string, name string, private bool) (*GitRepository, error) {
input := &gerrit.ProjectInput{
SubmitType: "INHERIT",
Description: "Created automatically by Jenkins X.",
PermissionsOnly: private,
}
fullNamePathEscaped := buildEncodedProjectName(org, name)
project, _, err := p.Client.Projects.CreateProject(fullNamePathEscaped, input)
if err != nil {
return nil, err
}
repo := p.projectInfoToGitRepository(project)
return repo, nil
}
func (p *GerritProvider) GetRepository(org string, name string) (*GitRepository, error) {
fullName := buildEncodedProjectName(org, name)
project, _, err := p.Client.Projects.GetProject(fullName)
if err != nil {
return nil, err
}
return p.projectInfoToGitRepository(project), nil
}
func (p *GerritProvider) DeleteRepository(org string, name string) error {
return nil
}
func (p *GerritProvider) ForkRepository(originalOrg string, name string, destinationOrg string) (*GitRepository, error) {
return nil, nil
}
func (p *GerritProvider) RenameRepository(org string, name string, newName string) (*GitRepository, error) {
return nil, nil
}
func (p *GerritProvider) ValidateRepositoryName(org string, name string) error {
return nil
}
func (p *GerritProvider) CreatePullRequest(data *GitPullRequestArguments) (*GitPullRequest, error) {
return nil, nil
}
// UpdatePullRequest updates pull request with number using data
func (p *GerritProvider) UpdatePullRequest(data *GitPullRequestArguments, number int) (*GitPullRequest, error) {
return nil, errors.Errorf("Not yet implemented for gerrit")
}
func (p *GerritProvider) UpdatePullRequestStatus(pr *GitPullRequest) error {
return nil
}
func (p *GerritProvider) GetPullRequest(owner string, repo *GitRepository, number int) (*GitPullRequest, error) {
return nil, nil
}
// ListOpenPullRequests lists the open pull requests
func (p *GerritProvider) ListOpenPullRequests(owner string, repo string) ([]*GitPullRequest, error) {
return nil, nil
}
func (p *GerritProvider) GetPullRequestCommits(owner string, repo *GitRepository, number int) ([]*GitCommit, error) {
return nil, nil
}
func (p *GerritProvider) PullRequestLastCommitStatus(pr *GitPullRequest) (string, error) {
return "", nil
}
func (p *GerritProvider) ListCommitStatus(org string, repo string, sha string) ([]*GitRepoStatus, error) {
return nil, nil
}
// UpdateCommitStatus updates the status of a specified commit in a specified repo.
func (p *GerritProvider) UpdateCommitStatus(org, repo, sha string, status *GitRepoStatus) (*GitRepoStatus, error) {
return nil, nil
}
func (p *GerritProvider) MergePullRequest(pr *GitPullRequest, message string) error {
return nil
}
func (p *GerritProvider) CreateWebHook(data *GitWebHookArguments) error {
return nil
}
// UpdateWebHook update a webhook with the data specified.
func (p *GerritProvider) UpdateWebHook(data *GitWebHookArguments) error {
return nil
}
// ListWebHooks lists all webhooks for the specified repo.
func (p *GerritProvider) ListWebHooks(org, repo string) ([]*GitWebHookArguments, error) {
return nil, nil
}
// ListOrganisations lists all organizations the configured user has access to.
func (p *GerritProvider) ListOrganisations() ([]GitOrganisation, error) {
return nil, nil
}
func (p *GerritProvider) IsGitHub() bool {
return false
}
func (p *GerritProvider) IsGitea() bool {
return false
}
func (p *GerritProvider) IsBitbucketCloud() bool {
return false
}
func (p *GerritProvider) IsBitbucketServer() bool {
return false
}
func (p *GerritProvider) IsGerrit() bool {
return true
}
func (p *GerritProvider) Kind() string {
return "gerrit"
}
func (p *GerritProvider) GetIssue(org string, name string, number int) (*GitIssue, error) {
log.Logger().Warn("Gerrit does not support issue tracking")
return nil, nil
}
func (p *GerritProvider) IssueURL(org string, name string, number int, isPull bool) string {
log.Logger().Warn("Gerrit does not support issue tracking")
return ""
}
func (p *GerritProvider) SearchIssues(org string, name string, query string) ([]*GitIssue, error) {
log.Logger().Warn("Gerrit does not support issue tracking")
return nil, nil
}
func (p *GerritProvider) SearchIssuesClosedSince(org string, name string, t time.Time) ([]*GitIssue, error) {
log.Logger().Warn("Gerrit does not support issue tracking")
return nil, nil
}
func (p *GerritProvider) CreateIssue(owner string, repo string, issue *GitIssue) (*GitIssue, error) {
log.Logger().Warn("Gerrit does not support issue tracking")
return nil, nil
}
func (p *GerritProvider) HasIssues() bool {
log.Logger().Warn("Gerrit does not support issue tracking")
return false
}
func (p *GerritProvider) AddPRComment(pr *GitPullRequest, comment string) error {
return nil
}
func (p *GerritProvider) CreateIssueComment(owner string, repo string, number int, comment string) error {
log.Logger().Warn("Gerrit does not support issue tracking")
return nil
}
func (p *GerritProvider) UpdateRelease(owner string, repo string, tag string, releaseInfo *GitRelease) error {
return nil
}
// UpdateReleaseStatus is not supported for this git provider
func (p *GerritProvider) UpdateReleaseStatus(owner string, repo string, tag string, releaseInfo *GitRelease) error {
return nil
}
func (p *GerritProvider) ListReleases(org string, name string) ([]*GitRelease, error) {
return nil, nil
}
// GetRelease returns the release info for org, repo name and tag
func (p *GerritProvider) GetRelease(org string, name string, tag string) (*GitRelease, error) {
return nil, nil
}
func (p *GerritProvider) JenkinsWebHookPath(gitURL string, secret string) string {
return ""
}
func (p *GerritProvider) Label() string {
return ""
}
func (p *GerritProvider) ServerURL() string {
return ""
}
func (p *GerritProvider) BranchArchiveURL(org string, name string, branch string) string {
return ""
}
func (p *GerritProvider) CurrentUsername() string {
return ""
}
func (p *GerritProvider) UserAuth() auth.UserAuth {
return auth.UserAuth{}
}
func (p *GerritProvider) UserInfo(username string) *GitUser {
return nil
}
func (p *GerritProvider) AddCollaborator(user string, organisation string, repo string) error {
log.Logger().Infof("Automatically adding the pipeline user as a collaborator is currently not implemented for gerrit. Please add user: %v as a collaborator to this project.", user)
return nil
}
func (p *GerritProvider) ListInvitations() ([]*github.RepositoryInvitation, *github.Response, error) {
log.Logger().Infof("Automatically adding the pipeline user as a collaborator is currently not implemented for gerrit.")
return []*github.RepositoryInvitation{}, &github.Response{}, nil
}
func (p *GerritProvider) AcceptInvitation(ID int64) (*github.Response, error) {
log.Logger().Infof("Automatically adding the pipeline user as a collaborator is currently not implemented for gerrit.")
return &github.Response{}, nil
}
func (p *GerritProvider) GetContent(org string, name string, path string, ref string) (*GitFileContent, error) {
return nil, fmt.Errorf("Getting content not supported on gerrit")
}
// ShouldForkForPullReques treturns true if we should create a personal fork of this repository
// before creating a pull request
func (p *GerritProvider) ShouldForkForPullRequest(originalOwner string, repoName string, username string) bool {
return originalOwner != username
}
func (p *GerritProvider) ListCommits(owner, repo string, opt *ListCommitsArguments) ([]*GitCommit, error) {
return nil, fmt.Errorf("Listing commits not supported on gerrit")
}
// AddLabelsToIssue adds labels to issues or pullrequests
func (p *GerritProvider) AddLabelsToIssue(owner, repo string, number int, labels []string) error {
return fmt.Errorf("Getting content not supported on gerrit")
}
// GetLatestRelease fetches the latest release from the git provider for org and name
func (p *GerritProvider) GetLatestRelease(org string, name string) (*GitRelease, error) {
return nil, nil
}
// UploadReleaseAsset will upload an asset to org/repo to a release with id, giving it a name, it will return the release asset from the git provider
func (p *GerritProvider) UploadReleaseAsset(org string, repo string, id int64, name string, asset *os.File) (*GitReleaseAsset, error) {
return nil, nil
}
// GetBranch returns the branch information for an owner/repo, including the commit at the tip
func (p *GerritProvider) GetBranch(owner string, repo string, branch string) (*GitBranch, error) {
return nil, nil
}
// GetProjects returns all the git projects in owner/repo
func (p *GerritProvider) GetProjects(owner string, repo string) ([]GitProject, error) {
return nil, nil
}
//ConfigureFeatures sets specific features as enabled or disabled for owner/repo
func (p *GerritProvider) ConfigureFeatures(owner string, repo string, issues *bool, projects *bool, wikis *bool) (*GitRepository, error) {
return nil, nil
}
// IsWikiEnabled returns true if a wiki is enabled for owner/repo
func (p *GerritProvider) IsWikiEnabled(owner string, repo string) (bool, error) {
return false, nil
}