-
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
-
376
-
377
-
378
-
379
-
380
-
381
-
382
-
383
-
384
-
385
-
386
-
387
-
388
-
389
-
390
-
391
-
392
-
393
-
394
-
395
-
396
-
397
-
398
-
399
-
400
-
401
-
402
-
403
-
404
-
405
-
406
-
407
-
408
-
409
-
410
-
411
-
412
-
413
-
414
-
415
-
416
-
417
-
418
-
419
-
420
-
421
-
422
-
423
-
424
-
425
-
426
-
427
-
428
-
429
-
430
-
431
-
432
-
433
-
434
-
435
-
436
-
437
-
438
-
439
-
440
-
441
-
442
-
443
-
444
-
445
-
446
-
447
-
448
-
449
-
450
-
451
-
452
-
453
-
454
-
455
-
456
-
457
-
458
-
459
-
460
-
461
-
462
-
463
-
464
-
465
-
466
-
467
-
468
-
469
-
470
-
471
-
472
-
473
-
474
-
475
-
476
-
477
-
478
-
479
-
480
-
481
-
482
-
483
-
484
-
485
-
486
-
487
-
488
-
489
-
490
-
491
-
492
-
493
-
494
-
495
-
496
-
497
-
498
-
499
-
500
-
501
-
502
-
503
-
504
-
505
-
506
-
507
-
508
-
509
-
510
-
511
-
512
-
513
-
514
-
515
-
516
-
517
-
518
-
519
-
520
-
521
-
522
-
523
-
524
-
525
-
526
-
527
-
528
-
529
-
530
-
531
-
532
-
533
-
534
-
535
-
536
-
537
-
538
-
539
-
540
-
541
-
542
-
543
-
544
-
545
-
546
-
547
-
548
-
549
-
550
-
551
-
552
-
553
-
554
-
555
-
556
-
557
-
558
-
559
-
560
-
561
-
562
-
563
-
564
-
565
-
566
-
567
-
568
-
569
-
570
-
571
-
572
-
573
-
574
-
575
-
576
-
577
-
578
-
579
-
580
-
581
-
582
-
583
-
584
-
585
-
586
-
587
-
588
-
589
-
590
-
591
-
592
-
593
-
594
-
595
-
596
-
597
-
598
-
599
-
600
-
601
-
602
-
603
-
604
-
605
-
606
-
607
-
608
-
609
-
610
-
611
-
612
-
613
-
614
-
615
-
616
-
617
-
618
-
619
-
620
-
621
-
622
-
623
-
624
-
625
-
626
-
627
-
628
-
629
-
630
-
631
-
632
-
633
-
634
-
635
-
636
-
637
-
638
-
639
-
640
-
641
-
642
-
643
-
644
-
645
-
646
-
647
-
648
-
649
-
650
-
651
-
652
-
653
-
654
-
655
-
656
-
657
-
658
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package migrations
import (
"context"
"encoding/xml"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"code.gitea.io/gitea/modules/log"
base "code.gitea.io/gitea/modules/migration"
"code.gitea.io/gitea/modules/proxy"
"code.gitea.io/gitea/modules/structs"
)
var (
_ base.Downloader = &CodebaseDownloader{}
_ base.DownloaderFactory = &CodebaseDownloaderFactory{}
)
func init() {
RegisterDownloaderFactory(&CodebaseDownloaderFactory{})
}
// CodebaseDownloaderFactory defines a downloader factory
type CodebaseDownloaderFactory struct{}
// New returns a downloader related to this factory according MigrateOptions
func (f *CodebaseDownloaderFactory) New(ctx context.Context, opts base.MigrateOptions) (base.Downloader, error) {
u, err := url.Parse(opts.CloneAddr)
if err != nil {
return nil, err
}
u.User = nil
fields := strings.Split(strings.Trim(u.Path, "/"), "/")
if len(fields) != 2 {
return nil, fmt.Errorf("invalid path: %s", u.Path)
}
project := fields[0]
repoName := strings.TrimSuffix(fields[1], ".git")
log.Trace("Create Codebase downloader. BaseURL: %v RepoName: %s", u, repoName)
return NewCodebaseDownloader(ctx, u, project, repoName, opts.AuthUsername, opts.AuthPassword), nil
}
// GitServiceType returns the type of git service
func (f *CodebaseDownloaderFactory) GitServiceType() structs.GitServiceType {
return structs.CodebaseService
}
type codebaseUser struct {
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
// CodebaseDownloader implements a Downloader interface to get repository information
// from Codebase
type CodebaseDownloader struct {
base.NullDownloader
ctx context.Context
client *http.Client
baseURL *url.URL
projectURL *url.URL
project string
repoName string
maxIssueIndex int64
userMap map[int64]*codebaseUser
commitMap map[string]string
}
// SetContext set context
func (d *CodebaseDownloader) SetContext(ctx context.Context) {
d.ctx = ctx
}
// NewCodebaseDownloader creates a new downloader
func NewCodebaseDownloader(ctx context.Context, projectURL *url.URL, project, repoName, username, password string) *CodebaseDownloader {
baseURL, _ := url.Parse("https://api3.codebasehq.com")
downloader := &CodebaseDownloader{
ctx: ctx,
baseURL: baseURL,
projectURL: projectURL,
project: project,
repoName: repoName,
client: &http.Client{
Transport: &http.Transport{
Proxy: func(req *http.Request) (*url.URL, error) {
if len(username) > 0 && len(password) > 0 {
req.SetBasicAuth(username, password)
}
return proxy.Proxy()(req)
},
},
},
userMap: make(map[int64]*codebaseUser),
commitMap: make(map[string]string),
}
return downloader
}
// FormatCloneURL add authentication into remote URLs
func (d *CodebaseDownloader) FormatCloneURL(opts base.MigrateOptions, remoteAddr string) (string, error) {
return opts.CloneAddr, nil
}
func (d *CodebaseDownloader) callAPI(endpoint string, parameter map[string]string, result interface{}) error {
u, err := d.baseURL.Parse(endpoint)
if err != nil {
return err
}
if parameter != nil {
query := u.Query()
for k, v := range parameter {
query.Set(k, v)
}
u.RawQuery = query.Encode()
}
req, err := http.NewRequestWithContext(d.ctx, "GET", u.String(), nil)
if err != nil {
return err
}
req.Header.Add("Accept", "application/xml")
resp, err := d.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return xml.NewDecoder(resp.Body).Decode(&result)
}
// GetRepoInfo returns repository information
// https://support.codebasehq.com/kb/projects
func (d *CodebaseDownloader) GetRepoInfo() (*base.Repository, error) {
var rawRepository struct {
XMLName xml.Name `xml:"repository"`
Name string `xml:"name"`
Description string `xml:"description"`
Permalink string `xml:"permalink"`
CloneURL string `xml:"clone-url"`
Source string `xml:"source"`
}
err := d.callAPI(
fmt.Sprintf("/%s/%s", d.project, d.repoName),
nil,
&rawRepository,
)
if err != nil {
return nil, err
}
return &base.Repository{
Name: rawRepository.Name,
Description: rawRepository.Description,
CloneURL: rawRepository.CloneURL,
OriginalURL: d.projectURL.String(),
}, nil
}
// GetMilestones returns milestones
// https://support.codebasehq.com/kb/tickets-and-milestones/milestones
func (d *CodebaseDownloader) GetMilestones() ([]*base.Milestone, error) {
var rawMilestones struct {
XMLName xml.Name `xml:"ticketing-milestone"`
Type string `xml:"type,attr"`
TicketingMilestone []struct {
Text string `xml:",chardata"`
ID struct {
Value int64 `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"id"`
Identifier string `xml:"identifier"`
Name string `xml:"name"`
Deadline struct {
Value string `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"deadline"`
Description string `xml:"description"`
Status string `xml:"status"`
} `xml:"ticketing-milestone"`
}
err := d.callAPI(
fmt.Sprintf("/%s/milestones", d.project),
nil,
&rawMilestones,
)
if err != nil {
return nil, err
}
milestones := make([]*base.Milestone, 0, len(rawMilestones.TicketingMilestone))
for _, milestone := range rawMilestones.TicketingMilestone {
var deadline *time.Time
if len(milestone.Deadline.Value) > 0 {
if val, err := time.Parse("2006-01-02", milestone.Deadline.Value); err == nil {
deadline = &val
}
}
closed := deadline
state := "closed"
if milestone.Status == "active" {
closed = nil
state = ""
}
milestones = append(milestones, &base.Milestone{
Title: milestone.Name,
Deadline: deadline,
Closed: closed,
State: state,
})
}
return milestones, nil
}
// GetLabels returns labels
// https://support.codebasehq.com/kb/tickets-and-milestones/statuses-priorities-and-categories
func (d *CodebaseDownloader) GetLabels() ([]*base.Label, error) {
var rawTypes struct {
XMLName xml.Name `xml:"ticketing-types"`
Type string `xml:"type,attr"`
TicketingType []struct {
ID struct {
Value int64 `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"id"`
Name string `xml:"name"`
} `xml:"ticketing-type"`
}
err := d.callAPI(
fmt.Sprintf("/%s/tickets/types", d.project),
nil,
&rawTypes,
)
if err != nil {
return nil, err
}
labels := make([]*base.Label, 0, len(rawTypes.TicketingType))
for _, label := range rawTypes.TicketingType {
labels = append(labels, &base.Label{
Name: label.Name,
Color: "ffffff",
})
}
return labels, nil
}
type codebaseIssueContext struct {
foreignID int64
localID int64
Comments []*base.Comment
}
func (c codebaseIssueContext) LocalID() int64 {
return c.localID
}
func (c codebaseIssueContext) ForeignID() int64 {
return c.foreignID
}
// GetIssues returns issues, limits are not supported
// https://support.codebasehq.com/kb/tickets-and-milestones
// https://support.codebasehq.com/kb/tickets-and-milestones/updating-tickets
func (d *CodebaseDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, error) {
var rawIssues struct {
XMLName xml.Name `xml:"tickets"`
Type string `xml:"type,attr"`
Ticket []struct {
TicketID struct {
Value int64 `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"ticket-id"`
Summary string `xml:"summary"`
TicketType string `xml:"ticket-type"`
ReporterID struct {
Value int64 `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"reporter-id"`
Reporter string `xml:"reporter"`
Type struct {
Name string `xml:"name"`
} `xml:"type"`
Status struct {
TreatAsClosed struct {
Value bool `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"treat-as-closed"`
} `xml:"status"`
Milestone struct {
Name string `xml:"name"`
} `xml:"milestone"`
UpdatedAt struct {
Value time.Time `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"updated-at"`
CreatedAt struct {
Value time.Time `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"created-at"`
} `xml:"ticket"`
}
err := d.callAPI(
fmt.Sprintf("/%s/tickets", d.project),
nil,
&rawIssues,
)
if err != nil {
return nil, false, err
}
issues := make([]*base.Issue, 0, len(rawIssues.Ticket))
for _, issue := range rawIssues.Ticket {
var notes struct {
XMLName xml.Name `xml:"ticket-notes"`
Type string `xml:"type,attr"`
TicketNote []struct {
Content string `xml:"content"`
CreatedAt struct {
Value time.Time `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"created-at"`
UpdatedAt struct {
Value time.Time `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"updated-at"`
ID struct {
Value int64 `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"id"`
UserID struct {
Value int64 `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"user-id"`
} `xml:"ticket-note"`
}
err := d.callAPI(
fmt.Sprintf("/%s/tickets/%d/notes", d.project, issue.TicketID.Value),
nil,
¬es,
)
if err != nil {
return nil, false, err
}
comments := make([]*base.Comment, 0, len(notes.TicketNote))
for _, note := range notes.TicketNote {
if len(note.Content) == 0 {
continue
}
poster := d.tryGetUser(note.UserID.Value)
comments = append(comments, &base.Comment{
IssueIndex: issue.TicketID.Value,
Index: note.ID.Value,
PosterID: poster.ID,
PosterName: poster.Name,
PosterEmail: poster.Email,
Content: note.Content,
Created: note.CreatedAt.Value,
Updated: note.UpdatedAt.Value,
})
}
if len(comments) == 0 {
comments = append(comments, &base.Comment{})
}
state := "open"
if issue.Status.TreatAsClosed.Value {
state = "closed"
}
poster := d.tryGetUser(issue.ReporterID.Value)
issues = append(issues, &base.Issue{
Title: issue.Summary,
Number: issue.TicketID.Value,
PosterName: poster.Name,
PosterEmail: poster.Email,
Content: comments[0].Content,
Milestone: issue.Milestone.Name,
State: state,
Created: issue.CreatedAt.Value,
Updated: issue.UpdatedAt.Value,
Labels: []*base.Label{
{Name: issue.Type.Name},
},
Context: codebaseIssueContext{
foreignID: issue.TicketID.Value,
localID: issue.TicketID.Value,
Comments: comments[1:],
},
})
if d.maxIssueIndex < issue.TicketID.Value {
d.maxIssueIndex = issue.TicketID.Value
}
}
return issues, true, nil
}
// GetComments returns comments
func (d *CodebaseDownloader) GetComments(opts base.GetCommentOptions) ([]*base.Comment, bool, error) {
context, ok := opts.Context.(codebaseIssueContext)
if !ok {
return nil, false, fmt.Errorf("unexpected comment context: %+v", opts.Context)
}
return context.Comments, true, nil
}
// GetPullRequests returns pull requests
// https://support.codebasehq.com/kb/repositories/merge-requests
func (d *CodebaseDownloader) GetPullRequests(page, perPage int) ([]*base.PullRequest, bool, error) {
var rawMergeRequests struct {
XMLName xml.Name `xml:"merge-requests"`
Type string `xml:"type,attr"`
MergeRequest []struct {
ID struct {
Value int64 `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"id"`
} `xml:"merge-request"`
}
err := d.callAPI(
fmt.Sprintf("/%s/%s/merge_requests", d.project, d.repoName),
map[string]string{
"query": `"Target Project" is "` + d.repoName + `"`,
"offset": strconv.Itoa((page - 1) * perPage),
"count": strconv.Itoa(perPage),
},
&rawMergeRequests,
)
if err != nil {
return nil, false, err
}
pullRequests := make([]*base.PullRequest, 0, len(rawMergeRequests.MergeRequest))
for i, mr := range rawMergeRequests.MergeRequest {
var rawMergeRequest struct {
XMLName xml.Name `xml:"merge-request"`
ID struct {
Value int64 `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"id"`
SourceRef string `xml:"source-ref"`
TargetRef string `xml:"target-ref"`
Subject string `xml:"subject"`
Status string `xml:"status"`
UserID struct {
Value int64 `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"user-id"`
CreatedAt struct {
Value time.Time `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"created-at"`
UpdatedAt struct {
Value time.Time `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"updated-at"`
Comments struct {
Type string `xml:"type,attr"`
Comment []struct {
Content string `xml:"content"`
ID struct {
Value int64 `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"id"`
UserID struct {
Value int64 `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"user-id"`
Action struct {
Value string `xml:",chardata"`
Nil string `xml:"nil,attr"`
} `xml:"action"`
CreatedAt struct {
Value time.Time `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"created-at"`
} `xml:"comment"`
} `xml:"comments"`
}
err := d.callAPI(
fmt.Sprintf("/%s/%s/merge_requests/%d", d.project, d.repoName, mr.ID.Value),
nil,
&rawMergeRequest,
)
if err != nil {
return nil, false, err
}
number := d.maxIssueIndex + int64(i) + 1
state := "open"
merged := false
var closeTime *time.Time
var mergedTime *time.Time
if rawMergeRequest.Status != "new" {
state = "closed"
closeTime = &rawMergeRequest.UpdatedAt.Value
}
comments := make([]*base.Comment, 0, len(rawMergeRequest.Comments.Comment))
for _, comment := range rawMergeRequest.Comments.Comment {
if len(comment.Content) == 0 {
if comment.Action.Value == "merging" {
merged = true
mergedTime = &comment.CreatedAt.Value
}
continue
}
poster := d.tryGetUser(comment.UserID.Value)
comments = append(comments, &base.Comment{
IssueIndex: number,
Index: comment.ID.Value,
PosterID: poster.ID,
PosterName: poster.Name,
PosterEmail: poster.Email,
Content: comment.Content,
Created: comment.CreatedAt.Value,
Updated: comment.CreatedAt.Value,
})
}
if len(comments) == 0 {
comments = append(comments, &base.Comment{})
}
poster := d.tryGetUser(rawMergeRequest.UserID.Value)
pullRequests = append(pullRequests, &base.PullRequest{
Title: rawMergeRequest.Subject,
Number: number,
PosterName: poster.Name,
PosterEmail: poster.Email,
Content: comments[0].Content,
State: state,
Created: rawMergeRequest.CreatedAt.Value,
Updated: rawMergeRequest.UpdatedAt.Value,
Closed: closeTime,
Merged: merged,
MergedTime: mergedTime,
Head: base.PullRequestBranch{
Ref: rawMergeRequest.SourceRef,
SHA: d.getHeadCommit(rawMergeRequest.SourceRef),
RepoName: d.repoName,
},
Base: base.PullRequestBranch{
Ref: rawMergeRequest.TargetRef,
SHA: d.getHeadCommit(rawMergeRequest.TargetRef),
RepoName: d.repoName,
},
Context: codebaseIssueContext{
foreignID: rawMergeRequest.ID.Value,
localID: number,
Comments: comments[1:],
},
})
}
return pullRequests, true, nil
}
// GetReviews returns pull requests reviews
func (d *CodebaseDownloader) GetReviews(context base.IssueContext) ([]*base.Review, error) {
return []*base.Review{}, nil
}
// GetTopics return repository topics
func (d *CodebaseDownloader) GetTopics() ([]string, error) {
return []string{}, nil
}
func (d *CodebaseDownloader) tryGetUser(userID int64) *codebaseUser {
if len(d.userMap) == 0 {
var rawUsers struct {
XMLName xml.Name `xml:"users"`
Type string `xml:"type,attr"`
User []struct {
EmailAddress string `xml:"email-address"`
ID struct {
Value int64 `xml:",chardata"`
Type string `xml:"type,attr"`
} `xml:"id"`
LastName string `xml:"last-name"`
FirstName string `xml:"first-name"`
Username string `xml:"username"`
} `xml:"user"`
}
err := d.callAPI(
"/users",
nil,
&rawUsers,
)
if err == nil {
for _, user := range rawUsers.User {
d.userMap[user.ID.Value] = &codebaseUser{
Name: user.Username,
Email: user.EmailAddress,
}
}
}
}
user, ok := d.userMap[userID]
if !ok {
user = &codebaseUser{
Name: fmt.Sprintf("User %d", userID),
}
d.userMap[userID] = user
}
return user
}
func (d *CodebaseDownloader) getHeadCommit(ref string) string {
commitRef, ok := d.commitMap[ref]
if !ok {
var rawCommits struct {
XMLName xml.Name `xml:"commits"`
Type string `xml:"type,attr"`
Commit []struct {
Ref string `xml:"ref"`
} `xml:"commit"`
}
err := d.callAPI(
fmt.Sprintf("/%s/%s/commits/%s", d.project, d.repoName, ref),
nil,
&rawCommits,
)
if err == nil && len(rawCommits.Commit) > 0 {
commitRef = rawCommits.Commit[0].Ref
d.commitMap[ref] = commitRef
}
}
return commitRef
}