1package gitlab
2
3import (
4 "fmt"
5 "strconv"
6 "time"
7
8 "github.com/pkg/errors"
9 "github.com/xanzy/go-gitlab"
10
11 "github.com/MichaelMure/git-bug/bridge/core"
12 "github.com/MichaelMure/git-bug/bug"
13 "github.com/MichaelMure/git-bug/cache"
14 "github.com/MichaelMure/git-bug/util/git"
15)
16
17var (
18 ErrMissingIdentityToken = errors.New("missing identity token")
19)
20
21// gitlabExporter implement the Exporter interface
22type gitlabExporter struct {
23 conf core.Configuration
24
25 // cache identities clients
26 identityClient map[string]*gitlab.Client
27
28 // map identities with their tokens
29 identityToken map[string]string
30
31 // gitlab repository ID
32 repositoryID string
33
34 // cache identifiers used to speed up exporting operations
35 // cleared for each bug
36 cachedOperationIDs map[string]string
37
38 // cache labels used to speed up exporting labels events
39 cachedLabels map[string]string
40}
41
42// Init .
43func (ge *gitlabExporter) Init(conf core.Configuration) error {
44 ge.conf = conf
45 //TODO: initialize with multiple tokens
46 ge.identityToken = make(map[string]string)
47 ge.identityClient = make(map[string]*gitlab.Client)
48 ge.cachedOperationIDs = make(map[string]string)
49 ge.cachedLabels = make(map[string]string)
50
51 return nil
52}
53
54// getIdentityClient return a gitlab v4 API client configured with the access token of the given identity.
55// if no client were found it will initialize it from the known tokens map and cache it for next use
56func (ge *gitlabExporter) getIdentityClient(id string) (*gitlab.Client, error) {
57 client, ok := ge.identityClient[id]
58 if ok {
59 return client, nil
60 }
61
62 // get token
63 token, ok := ge.identityToken[id]
64 if !ok {
65 return nil, ErrMissingIdentityToken
66 }
67
68 // create client
69 client = buildClient(token)
70 // cache client
71 ge.identityClient[id] = client
72
73 return client, nil
74}
75
76// ExportAll export all event made by the current user to Gitlab
77func (ge *gitlabExporter) ExportAll(repo *cache.RepoCache, since time.Time) (<-chan core.ExportResult, error) {
78 out := make(chan core.ExportResult)
79
80 user, err := repo.GetUserIdentity()
81 if err != nil {
82 return nil, err
83 }
84
85 ge.identityToken[user.Id()] = ge.conf[keyToken]
86
87 // get repository node id
88 ge.repositoryID = ge.conf[keyProjectID]
89
90 if err != nil {
91 return nil, err
92 }
93
94 go func() {
95 defer close(out)
96
97 var allIdentitiesIds []string
98 for id := range ge.identityToken {
99 allIdentitiesIds = append(allIdentitiesIds, id)
100 }
101
102 allBugsIds := repo.AllBugsIds()
103
104 for _, id := range allBugsIds {
105 b, err := repo.ResolveBug(id)
106 if err != nil {
107 out <- core.NewExportError(err, id)
108 return
109 }
110
111 snapshot := b.Snapshot()
112
113 // ignore issues created before since date
114 // TODO: compare the Lamport time instead of using the unix time
115 if snapshot.CreatedAt.Before(since) {
116 out <- core.NewExportNothing(b.Id(), "bug created before the since date")
117 continue
118 }
119
120 if snapshot.HasAnyActor(allIdentitiesIds...) {
121 // try to export the bug and it associated events
122 ge.exportBug(b, since, out)
123 } else {
124 out <- core.NewExportNothing(id, "not an actor")
125 }
126 }
127 }()
128
129 return out, nil
130}
131
132// exportBug publish bugs and related events
133func (ge *gitlabExporter) exportBug(b *cache.BugCache, since time.Time, out chan<- core.ExportResult) {
134 snapshot := b.Snapshot()
135
136 var err error
137 var bugGitlabID int
138 var bugGitlabIDString string
139 var bugGitlabURL string
140 var bugCreationHash string
141 //labels := make([]string, 0)
142
143 // Special case:
144 // if a user try to export a bug that is not already exported to Gitlab (or imported
145 // from Gitlab) and we do not have the token of the bug author, there is nothing we can do.
146
147 // skip bug if origin is not allowed
148 origin, ok := snapshot.GetCreateMetadata(core.KeyOrigin)
149 if ok && origin != target {
150 out <- core.NewExportNothing(b.Id(), fmt.Sprintf("issue tagged with origin: %s", origin))
151 return
152 }
153
154 // first operation is always createOp
155 createOp := snapshot.Operations[0].(*bug.CreateOperation)
156 author := snapshot.Author
157
158 // get gitlab bug ID
159 gitlabID, ok := snapshot.GetCreateMetadata(keyGitlabId)
160 if ok {
161 gitlabURL, ok := snapshot.GetCreateMetadata(keyGitlabUrl)
162 if !ok {
163 // if we find gitlab ID, gitlab URL must be found too
164 err := fmt.Errorf("expected to find gitlab issue URL")
165 out <- core.NewExportError(err, b.Id())
166 }
167
168 //FIXME:
169 // ignore issue comming from other repositories
170
171 out <- core.NewExportNothing(b.Id(), "bug already exported")
172 // will be used to mark operation related to a bug as exported
173 bugGitlabIDString = gitlabID
174 bugGitlabID, err = strconv.Atoi(bugGitlabIDString)
175 if err != nil {
176 panic("unexpected gitlab id format")
177 }
178
179 bugGitlabURL = gitlabURL
180
181 } else {
182 // check that we have a token for operation author
183 client, err := ge.getIdentityClient(author.Id())
184 if err != nil {
185 // if bug is still not exported and we do not have the author stop the execution
186 out <- core.NewExportNothing(b.Id(), fmt.Sprintf("missing author token"))
187 return
188 }
189
190 // create bug
191 id, url, err := createGitlabIssue(client, ge.repositoryID, createOp.Title, createOp.Message)
192 if err != nil {
193 err := errors.Wrap(err, "exporting gitlab issue")
194 out <- core.NewExportError(err, b.Id())
195 return
196 }
197
198 idString := strconv.Itoa(id)
199
200 out <- core.NewExportBug(b.Id())
201
202 hash, err := createOp.Hash()
203 if err != nil {
204 err := errors.Wrap(err, "comment hash")
205 out <- core.NewExportError(err, b.Id())
206 return
207 }
208
209 // mark bug creation operation as exported
210 if err := markOperationAsExported(b, hash, idString, url); err != nil {
211 err := errors.Wrap(err, "marking operation as exported")
212 out <- core.NewExportError(err, b.Id())
213 return
214 }
215
216 // commit operation to avoid creating multiple issues with multiple pushes
217 if err := b.CommitAsNeeded(); err != nil {
218 err := errors.Wrap(err, "bug commit")
219 out <- core.NewExportError(err, b.Id())
220 return
221 }
222
223 // cache bug gitlab ID and URL
224 bugGitlabID = id
225 bugGitlabIDString = idString
226 bugGitlabURL = url
227 }
228
229 // get createOp hash
230 hash, err := createOp.Hash()
231 if err != nil {
232 out <- core.NewExportError(err, b.Id())
233 return
234 }
235
236 bugCreationHash = hash.String()
237
238 // cache operation gitlab id
239 ge.cachedOperationIDs[bugCreationHash] = bugGitlabIDString
240
241 for _, op := range snapshot.Operations[1:] {
242 // ignore SetMetadata operations
243 if _, ok := op.(*bug.SetMetadataOperation); ok {
244 continue
245 }
246
247 // get operation hash
248 hash, err := op.Hash()
249 if err != nil {
250 err := errors.Wrap(err, "operation hash")
251 out <- core.NewExportError(err, b.Id())
252 return
253 }
254
255 // ignore operations already existing in gitlab (due to import or export)
256 // cache the ID of already exported or imported issues and events from Gitlab
257 if id, ok := op.GetMetadata(keyGitlabId); ok {
258 ge.cachedOperationIDs[hash.String()] = id
259 out <- core.NewExportNothing(hash.String(), "already exported operation")
260 continue
261 }
262
263 opAuthor := op.GetAuthor()
264 client, err := ge.getIdentityClient(opAuthor.Id())
265 if err != nil {
266 out <- core.NewExportNothing(hash.String(), "missing operation author token")
267 continue
268 }
269
270 var id int
271 var idString, url string
272 switch op.(type) {
273 case *bug.AddCommentOperation:
274 opr := op.(*bug.AddCommentOperation)
275
276 // send operation to gitlab
277 id, err = addCommentGitlabIssue(client, ge.repositoryID, bugGitlabID, opr.Message)
278 if err != nil {
279 err := errors.Wrap(err, "adding comment")
280 out <- core.NewExportError(err, b.Id())
281 return
282 }
283
284 idString = strconv.Itoa(id)
285 out <- core.NewExportComment(hash.String())
286
287 // cache comment id
288 ge.cachedOperationIDs[hash.String()] = idString
289
290 case *bug.EditCommentOperation:
291
292 opr := op.(*bug.EditCommentOperation)
293 targetHash := opr.Target.String()
294
295 // Since gitlab doesn't consider the issue body as a comment
296 if targetHash == bugCreationHash {
297
298 // case bug creation operation: we need to edit the Gitlab issue
299 if err := updateGitlabIssueBody(client, ge.repositoryID, id, opr.Message); err != nil {
300 err := errors.Wrap(err, "editing issue")
301 out <- core.NewExportError(err, b.Id())
302 return
303 }
304
305 out <- core.NewExportCommentEdition(hash.String())
306
307 id = bugGitlabID
308 url = bugGitlabURL
309
310 } else {
311
312 // case comment edition operation: we need to edit the Gitlab comment
313 _, ok := ge.cachedOperationIDs[targetHash]
314 if !ok {
315 panic("unexpected error: comment id not found")
316 }
317
318 err := editCommentGitlabIssue(client, ge.repositoryID, id, id, opr.Message)
319 if err != nil {
320 err := errors.Wrap(err, "editing comment")
321 out <- core.NewExportError(err, b.Id())
322 return
323 }
324
325 out <- core.NewExportCommentEdition(hash.String())
326
327 // use comment id/url instead of issue id/url
328 //id = eid
329 //url = eurl
330 }
331
332 case *bug.SetStatusOperation:
333 opr := op.(*bug.SetStatusOperation)
334 if err := updateGitlabIssueStatus(client, idString, id, opr.Status); err != nil {
335 err := errors.Wrap(err, "editing status")
336 out <- core.NewExportError(err, b.Id())
337 return
338 }
339
340 out <- core.NewExportStatusChange(hash.String())
341
342 id = bugGitlabID
343 url = bugGitlabURL
344
345 case *bug.SetTitleOperation:
346 opr := op.(*bug.SetTitleOperation)
347 if err := updateGitlabIssueTitle(client, ge.repositoryID, id, opr.Title); err != nil {
348 err := errors.Wrap(err, "editing title")
349 out <- core.NewExportError(err, b.Id())
350 return
351 }
352
353 out <- core.NewExportTitleEdition(hash.String())
354
355 id = bugGitlabID
356 url = bugGitlabURL
357
358 case *bug.LabelChangeOperation:
359 _ = op.(*bug.LabelChangeOperation)
360 if err := updateGitlabIssueLabels(client, ge.repositoryID, bugGitlabID, []string{}); err != nil {
361 err := errors.Wrap(err, "updating labels")
362 out <- core.NewExportError(err, b.Id())
363 return
364 }
365
366 out <- core.NewExportLabelChange(hash.String())
367
368 id = bugGitlabID
369 url = bugGitlabURL
370
371 default:
372 panic("unhandled operation type case")
373 }
374
375 // mark operation as exported
376 if err := markOperationAsExported(b, hash, idString, url); err != nil {
377 err := errors.Wrap(err, "marking operation as exported")
378 out <- core.NewExportError(err, b.Id())
379 return
380 }
381
382 // commit at each operation export to avoid exporting same events multiple times
383 if err := b.CommitAsNeeded(); err != nil {
384 err := errors.Wrap(err, "bug commit")
385 out <- core.NewExportError(err, b.Id())
386 return
387 }
388 }
389}
390
391func markOperationAsExported(b *cache.BugCache, target git.Hash, gitlabID, gitlabURL string) error {
392 _, err := b.SetMetadata(
393 target,
394 map[string]string{
395 keyGitlabId: gitlabID,
396 keyGitlabUrl: gitlabURL,
397 },
398 )
399
400 return err
401}
402
403func (ge *gitlabExporter) getGitlabLabelID(label string) (string, error) {
404 id, ok := ge.cachedLabels[label]
405 if !ok {
406 return "", fmt.Errorf("non cached label")
407 }
408
409 return id, nil
410}
411
412// get label from gitlab
413func (ge *gitlabExporter) loadLabelsFromGitlab(gc *gitlab.Client) error {
414 labels, _, err := gc.Labels.ListLabels(
415 ge.repositoryID,
416 &gitlab.ListLabelsOptions{
417 Page: 0,
418 },
419 )
420
421 if err != nil {
422 return err
423 }
424
425 for _, label := range labels {
426 ge.cachedLabels[label.Name] = strconv.Itoa(label.ID)
427 }
428
429 for page := 2; len(labels) != 0; page++ {
430
431 labels, _, err = gc.Labels.ListLabels(
432 ge.repositoryID,
433 &gitlab.ListLabelsOptions{
434 Page: page,
435 },
436 )
437
438 if err != nil {
439 return err
440 }
441
442 for _, label := range labels {
443 ge.cachedLabels[label.Name] = strconv.Itoa(label.ID)
444 }
445 }
446
447 return nil
448}
449
450func (ge *gitlabExporter) createGitlabLabel(gc *gitlab.Client, label bug.Label) error {
451 client := buildClient(ge.conf[keyToken])
452
453 // RGBA to hex color
454 rgba := label.RGBA()
455 hexColor := fmt.Sprintf("%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B)
456 name := label.String()
457
458 _, _, err := client.Labels.CreateLabel(ge.repositoryID, &gitlab.CreateLabelOptions{
459 Name: &name,
460 Color: &hexColor,
461 })
462
463 return err
464}
465
466// create a gitlab. issue and return it ID
467func createGitlabIssue(gc *gitlab.Client, repositoryID, title, body string) (int, string, error) {
468 issue, _, err := gc.Issues.CreateIssue(repositoryID, &gitlab.CreateIssueOptions{
469 Title: &title,
470 Description: &body,
471 })
472
473 if err != nil {
474 return 0, "", err
475 }
476
477 return issue.IID, issue.WebURL, nil
478}
479
480// add a comment to an issue and return it ID
481func addCommentGitlabIssue(gc *gitlab.Client, repositoryID string, issueID int, body string) (int, error) {
482 note, _, err := gc.Notes.CreateIssueNote(repositoryID, issueID, &gitlab.CreateIssueNoteOptions{
483 Body: &body,
484 })
485
486 if err != nil {
487 return 0, err
488 }
489
490 return note.ID, nil
491}
492
493func editCommentGitlabIssue(gc *gitlab.Client, repositoryID string, issueID, noteID int, body string) error {
494 _, _, err := gc.Notes.UpdateIssueNote(repositoryID, issueID, noteID, &gitlab.UpdateIssueNoteOptions{
495 Body: &body,
496 })
497
498 return err
499}
500
501func updateGitlabIssueStatus(gc *gitlab.Client, repositoryID string, issueID int, status bug.Status) error {
502 var state string
503
504 switch status {
505 case bug.OpenStatus:
506 state = "reopen"
507 case bug.ClosedStatus:
508 state = "close"
509 default:
510 panic("unknown bug state")
511 }
512
513 _, _, err := gc.Issues.UpdateIssue(repositoryID, issueID, &gitlab.UpdateIssueOptions{
514 StateEvent: &state,
515 })
516
517 return err
518}
519
520func updateGitlabIssueBody(gc *gitlab.Client, repositoryID string, issueID int, body string) error {
521 _, _, err := gc.Issues.UpdateIssue(repositoryID, issueID, &gitlab.UpdateIssueOptions{
522 Description: &body,
523 })
524
525 return err
526}
527
528func updateGitlabIssueTitle(gc *gitlab.Client, repositoryID string, issueID int, title string) error {
529 _, _, err := gc.Issues.UpdateIssue(repositoryID, issueID, &gitlab.UpdateIssueOptions{
530 Title: &title,
531 })
532
533 return err
534}
535
536// update gitlab. issue labels
537func updateGitlabIssueLabels(gc *gitlab.Client, repositoryID string, issueID int, labels []string) error {
538 _, _, err := gc.Issues.UpdateIssue(repositoryID, issueID, &gitlab.UpdateIssueOptions{
539 Labels: labels,
540 })
541
542 return err
543}