1package github
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "fmt"
8 "io/ioutil"
9 "net/http"
10 "time"
11
12 "github.com/pkg/errors"
13 "github.com/shurcooL/githubv4"
14
15 "github.com/MichaelMure/git-bug/bridge/core"
16 "github.com/MichaelMure/git-bug/bug"
17 "github.com/MichaelMure/git-bug/cache"
18 "github.com/MichaelMure/git-bug/util/git"
19)
20
21var (
22 ErrMissingIdentityToken = errors.New("missing identity token")
23)
24
25// githubImporter implement the Importer interface
26type githubExporter struct {
27 conf core.Configuration
28
29 // number of exported bugs
30 exportedBugs int
31
32 // export only bugs taged with one of these origins
33 onlyOrigins []string
34
35 // cache identities clients
36 identityClient map[string]*githubv4.Client
37
38 // map identity with their tokens
39 identityToken map[string]string
40
41 // github repository ID
42 repositoryID string
43
44 // cache identifiers used to speed up exporting operations
45 // cleared for each bug
46 cachedIDs map[string]string
47
48 // cache labels used to speed up exporting labels events
49 cachedLabels map[string]string
50}
51
52// Init .
53func (ge *githubExporter) Init(conf core.Configuration) error {
54 ge.conf = conf
55 //TODO: initialize with multiple tokens
56 ge.identityToken = make(map[string]string)
57 ge.identityClient = make(map[string]*githubv4.Client)
58 ge.cachedIDs = make(map[string]string)
59 ge.cachedLabels = make(map[string]string)
60 return nil
61}
62
63func (ge *githubExporter) allowOrigin(origin string) bool {
64 if ge.onlyOrigins == nil {
65 return true
66 }
67
68 for _, o := range ge.onlyOrigins {
69 if origin == o {
70 return true
71 }
72 }
73
74 return false
75}
76
77// getIdentityClient return an identity github api v4 client
78// if no client were found it will initilize it from the known tokens map and cache it for next it use
79func (ge *githubExporter) getIdentityClient(id string) (*githubv4.Client, error) {
80 client, ok := ge.identityClient[id]
81 if ok {
82 return client, nil
83 }
84
85 // get token
86 token, ok := ge.identityToken[id]
87 if !ok {
88 return nil, ErrMissingIdentityToken
89 }
90
91 // create client
92 client = buildClient(token)
93 // cache client
94 ge.identityClient[id] = client
95
96 return client, nil
97}
98
99// ExportAll export all event made by the current user to Github
100func (ge *githubExporter) ExportAll(repo *cache.RepoCache, since time.Time) error {
101 user, err := repo.GetUserIdentity()
102 if err != nil {
103 return err
104 }
105
106 ge.identityToken[user.Id()] = ge.conf[keyToken]
107
108 // get repository node id
109 ge.repositoryID, err = getRepositoryNodeID(
110 ge.conf[keyOwner],
111 ge.conf[keyProject],
112 ge.conf[keyToken],
113 )
114
115 if err != nil {
116 return err
117 }
118
119 allBugsIds := repo.AllBugsIds()
120bugLoop:
121 for _, id := range allBugsIds {
122 b, err := repo.ResolveBug(id)
123 if err != nil {
124 return err
125 }
126
127 snapshot := b.Snapshot()
128
129 // ignore issues created before since date
130 if snapshot.CreatedAt.Before(since) {
131 continue
132 }
133
134 for _, p := range snapshot.Participants {
135 // if we have a token for one of the participants
136 for userId := range ge.identityToken {
137 if p.Id() == userId {
138 // try to export the bug and it associated events
139 if err := ge.exportBug(b, since); err != nil {
140 return err
141 }
142
143 continue bugLoop
144 }
145 }
146 }
147 }
148
149 return nil
150}
151
152// exportBug publish bugs and related events
153func (ge *githubExporter) exportBug(b *cache.BugCache, since time.Time) error {
154 snapshot := b.Snapshot()
155
156 var bugGithubID string
157 var bugGithubURL string
158 var bugCreationHash string
159
160 // Special case:
161 // if a user try to export a bug that is not already exported to Github (or imported
162 // from Github) and he is not the author of the bug. There is nothing we can do.
163
164 // first operation is always createOp
165 createOp := snapshot.Operations[0].(*bug.CreateOperation)
166 author := createOp.GetAuthor()
167
168 // skip bug if origin is not allowed
169 origin, ok := createOp.GetMetadata(keyOrigin)
170 if ok && !ge.allowOrigin(origin) {
171 // TODO print a warn ?
172 return nil
173 }
174
175 // get github bug ID
176 githubID, ok := createOp.GetMetadata(keyGithubId)
177 if ok {
178 githubURL, ok := createOp.GetMetadata(keyGithubUrl)
179 if !ok {
180 // if we find github ID, github URL must be found too
181 panic("expected to find github issue URL")
182 }
183 // will be used to mark operation related to a bug as exported
184 bugGithubID = githubID
185 bugGithubURL = githubURL
186
187 } else {
188 // check that we have a token for operation author
189 client, err := ge.getIdentityClient(author.Id())
190 if err != nil {
191 // if bug is still not exported and we do not have the author stop the execution
192
193 fmt.Println("warning: skipping issue due to missing token for bug creator")
194 // this is not an error, don't export bug
195 return nil
196 }
197
198 // create bug
199 id, url, err := createGithubIssue(client, ge.repositoryID, createOp.Title, createOp.Message)
200 if err != nil {
201 return errors.Wrap(err, "exporting github issue")
202 }
203
204 hash, err := createOp.Hash()
205 if err != nil {
206 return errors.Wrap(err, "comment hash")
207 }
208
209 // mark bug creation operation as exported
210 if err := markOperationAsExported(b, hash, id, url); err != nil {
211 return errors.Wrap(err, "marking operation as exported")
212 }
213
214 // commit operation to avoid creating multiple issues with multiple pushes
215 if err := b.CommitAsNeeded(); err != nil {
216 return errors.Wrap(err, "bug commit")
217 }
218
219 // cache bug github ID and URL
220 bugGithubID = id
221 bugGithubURL = url
222 }
223
224 // get createOp hash
225 hash, err := createOp.Hash()
226 if err != nil {
227 return err
228 }
229
230 bugCreationHash = hash.String()
231
232 // cache operation github id
233 ge.cachedIDs[bugCreationHash] = bugGithubID
234
235 for _, op := range snapshot.Operations[1:] {
236 // ignore SetMetadata operations
237 if _, ok := op.(*bug.SetMetadataOperation); ok {
238 continue
239 }
240
241 // get operation hash
242 hash, err := op.Hash()
243 if err != nil {
244 return errors.Wrap(err, "operation hash")
245 }
246
247 // ignore imported (or exported) operations from github
248 // cache the ID of already exported or imported issues and events from Github
249 if id, ok := op.GetMetadata(keyGithubId); ok {
250 ge.cachedIDs[hash.String()] = id
251 continue
252 }
253
254 opAuthor := op.GetAuthor()
255 client, err := ge.getIdentityClient(opAuthor.Id())
256 if err != nil {
257 // don't export operation
258 continue
259 }
260
261 var id, url string
262 switch op.(type) {
263 case *bug.AddCommentOperation:
264 opr := op.(*bug.AddCommentOperation)
265
266 // send operation to github
267 id, url, err = addCommentGithubIssue(client, bugGithubID, opr.Message)
268 if err != nil {
269 return errors.Wrap(err, "adding comment")
270 }
271
272 hash, err = opr.Hash()
273 if err != nil {
274 return errors.Wrap(err, "comment hash")
275 }
276
277 case *bug.EditCommentOperation:
278
279 opr := op.(*bug.EditCommentOperation)
280 targetHash := opr.Target.String()
281
282 // Since github doesn't consider the issue body as a comment
283 if targetHash == bugCreationHash {
284
285 // case bug creation operation: we need to edit the Github issue
286 if err := updateGithubIssueBody(client, bugGithubID, opr.Message); err != nil {
287 return errors.Wrap(err, "editing issue")
288 }
289
290 id = bugGithubID
291 url = bugGithubURL
292
293 } else {
294
295 // case comment edition operation: we need to edit the Github comment
296 commentID, ok := ge.cachedIDs[targetHash]
297 if !ok {
298 panic("unexpected error: comment id not found")
299 }
300
301 eid, eurl, err := editCommentGithubIssue(client, commentID, opr.Message)
302 if err != nil {
303 return errors.Wrap(err, "editing comment")
304 }
305
306 // use comment id/url instead of issue id/url
307 id = eid
308 url = eurl
309 }
310
311 hash, err = opr.Hash()
312 if err != nil {
313 return errors.Wrap(err, "comment hash")
314 }
315
316 case *bug.SetStatusOperation:
317 opr := op.(*bug.SetStatusOperation)
318 if err := updateGithubIssueStatus(client, bugGithubID, opr.Status); err != nil {
319 return errors.Wrap(err, "editing status")
320 }
321
322 hash, err = opr.Hash()
323 if err != nil {
324 return errors.Wrap(err, "set status operation hash")
325 }
326
327 id = bugGithubID
328 url = bugGithubURL
329
330 case *bug.SetTitleOperation:
331 opr := op.(*bug.SetTitleOperation)
332 if err := updateGithubIssueTitle(client, bugGithubID, opr.Title); err != nil {
333 return errors.Wrap(err, "editing title")
334 }
335
336 hash, err = opr.Hash()
337 if err != nil {
338 return errors.Wrap(err, "set title operation hash")
339 }
340
341 id = bugGithubID
342 url = bugGithubURL
343
344 case *bug.LabelChangeOperation:
345 opr := op.(*bug.LabelChangeOperation)
346 if err := ge.updateGithubIssueLabels(client, bugGithubID, opr.Added, opr.Removed); err != nil {
347 return errors.Wrap(err, "updating labels")
348 }
349
350 hash, err = opr.Hash()
351 if err != nil {
352 return errors.Wrap(err, "label change operation hash")
353 }
354
355 id = bugGithubID
356 url = bugGithubURL
357
358 default:
359 panic("unhandled operation type case")
360 }
361
362 // mark operation as exported
363 if err := markOperationAsExported(b, hash, id, url); err != nil {
364 return errors.Wrap(err, "marking operation as exported")
365 }
366
367 if err := b.CommitAsNeeded(); err != nil {
368 return errors.Wrap(err, "bug commit")
369 }
370 }
371
372 return nil
373}
374
375// getRepositoryNodeID request github api v3 to get repository node id
376func getRepositoryNodeID(owner, project, token string) (string, error) {
377 url := fmt.Sprintf("%s/repos/%s/%s", githubV3Url, owner, project)
378
379 client := &http.Client{
380 Timeout: defaultTimeout,
381 }
382
383 req, err := http.NewRequest("GET", url, nil)
384 if err != nil {
385 return "", err
386 }
387
388 // need the token for private repositories
389 req.Header.Set("Authorization", fmt.Sprintf("token %s", token))
390
391 resp, err := client.Do(req)
392 if err != nil {
393 return "", err
394 }
395
396 if resp.StatusCode != http.StatusOK {
397 return "", fmt.Errorf("error retrieving repository node id %v", resp.StatusCode)
398 }
399
400 aux := struct {
401 NodeID string `json:"node_id"`
402 }{}
403
404 data, _ := ioutil.ReadAll(resp.Body)
405 defer resp.Body.Close()
406
407 err = json.Unmarshal(data, &aux)
408 if err != nil {
409 return "", err
410 }
411
412 return aux.NodeID, nil
413}
414
415func markOperationAsExported(b *cache.BugCache, target git.Hash, githubID, githubURL string) error {
416 _, err := b.SetMetadata(
417 target,
418 map[string]string{
419 keyGithubId: githubID,
420 keyGithubUrl: githubURL,
421 },
422 )
423
424 return err
425}
426
427// get label from github
428func (ge *githubExporter) getGithubLabelID(gc *githubv4.Client, label string) (string, error) {
429 q := labelQuery{}
430 variables := map[string]interface{}{"name": label}
431
432 parentCtx := context.Background()
433 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
434 defer cancel()
435
436 if err := gc.Query(ctx, &q, variables); err != nil {
437 return "", err
438 }
439
440 return q.Repository.Label.ID, nil
441}
442
443func (ge *githubExporter) createGithubLabel(gc *githubv4.Client, label, labelColor string) (string, error) {
444 url := fmt.Sprintf("%s/repos/%s/%s/labels", githubV3Url, ge.conf[keyOwner], ge.conf[keyProject])
445
446 client := &http.Client{
447 Timeout: defaultTimeout,
448 }
449
450 params := struct {
451 Name string `json:"name"`
452 Color string `json:"color"`
453 Description string `json:"description"`
454 }{
455 Name: label,
456 Color: labelColor,
457 }
458
459 data, err := json.Marshal(params)
460 if err != nil {
461 return "", err
462 }
463
464 req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
465 if err != nil {
466 return "", err
467 }
468
469 // need the token for private repositories
470 req.Header.Set("Authorization", fmt.Sprintf("token %s", ge.conf[keyToken]))
471
472 resp, err := client.Do(req)
473 if err != nil {
474 return "", err
475 }
476
477 if resp.StatusCode != http.StatusCreated {
478 //d, _ := ioutil.ReadAll(resp.Body)
479 //fmt.Println(string(d))
480 return "", fmt.Errorf("error creating label: response status %v", resp.StatusCode)
481 }
482
483 aux := struct {
484 ID int `json:"id"`
485 NodeID string `json:"node_id"`
486 Color string `json:"color"`
487 }{}
488
489 data, _ = ioutil.ReadAll(resp.Body)
490 defer resp.Body.Close()
491
492 err = json.Unmarshal(data, &aux)
493 if err != nil {
494 return "", err
495 }
496
497 fmt.Println("ezzz", aux.NodeID)
498 return aux.NodeID, nil
499}
500
501// create github label using api v4
502func (ge *githubExporter) createGithubLabelV4(gc *githubv4.Client, label, labelColor string) (string, error) {
503 m := &createLabelMutation{}
504 input := createLabelInput{
505 RepositoryID: ge.repositoryID,
506 Name: githubv4.String(label),
507 Color: githubv4.String(labelColor),
508 }
509
510 parentCtx := context.Background()
511 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
512 defer cancel()
513
514 if err := gc.Mutate(ctx, m, input, nil); err != nil {
515 return "", err
516 }
517
518 return m.CreateLabel.Label.ID, nil
519}
520
521func (ge *githubExporter) getOrCreateGithubLabelID(gc *githubv4.Client, repositoryID string, label bug.Label) (string, error) {
522 // try to get label id
523 labelID, err := ge.getGithubLabelID(gc, string(label))
524 if err == nil {
525 return labelID, nil
526 }
527
528 // hex color
529 rgba := label.RGBA()
530 hexColor := fmt.Sprintf("%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B)
531
532 fmt.Println("creating color", label, hexColor)
533
534 // create label and return id
535 labelID, err = ge.createGithubLabel(gc, string(label), hexColor)
536 if err != nil {
537 return "", err
538 }
539
540 return labelID, nil
541}
542
543func (ge *githubExporter) getLabelsIDs(gc *githubv4.Client, repositoryID string, labels []bug.Label) ([]githubv4.ID, error) {
544 ids := make([]githubv4.ID, 0, len(labels))
545 var err error
546
547 // check labels ids
548 for _, label := range labels {
549 id, ok := ge.cachedLabels[string(label)]
550 if !ok {
551 // try to query label id
552 id, err = ge.getOrCreateGithubLabelID(gc, repositoryID, label)
553 if err != nil {
554 return nil, errors.Wrap(err, "get or create github label")
555 }
556
557 // cache label id
558 ge.cachedLabels[string(label)] = id
559 }
560
561 ids = append(ids, githubv4.ID(id))
562 }
563
564 return ids, nil
565}
566
567// create a github issue and return it ID
568func createGithubIssue(gc *githubv4.Client, repositoryID, title, body string) (string, string, error) {
569 m := &createIssueMutation{}
570 input := githubv4.CreateIssueInput{
571 RepositoryID: repositoryID,
572 Title: githubv4.String(title),
573 Body: (*githubv4.String)(&body),
574 }
575
576 parentCtx := context.Background()
577 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
578 defer cancel()
579
580 if err := gc.Mutate(ctx, m, input, nil); err != nil {
581 return "", "", err
582 }
583
584 issue := m.CreateIssue.Issue
585 return issue.ID, issue.URL, nil
586}
587
588// add a comment to an issue and return it ID
589func addCommentGithubIssue(gc *githubv4.Client, subjectID string, body string) (string, string, error) {
590 m := &addCommentToIssueMutation{}
591 input := githubv4.AddCommentInput{
592 SubjectID: subjectID,
593 Body: githubv4.String(body),
594 }
595
596 parentCtx := context.Background()
597 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
598 defer cancel()
599
600 if err := gc.Mutate(ctx, m, input, nil); err != nil {
601 return "", "", err
602 }
603
604 node := m.AddComment.CommentEdge.Node
605 return node.ID, node.URL, nil
606}
607
608func editCommentGithubIssue(gc *githubv4.Client, commentID, body string) (string, string, error) {
609 m := &updateIssueCommentMutation{}
610 input := githubv4.UpdateIssueCommentInput{
611 ID: commentID,
612 Body: githubv4.String(body),
613 }
614
615 parentCtx := context.Background()
616 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
617 defer cancel()
618
619 if err := gc.Mutate(ctx, m, input, nil); err != nil {
620 return "", "", err
621 }
622
623 comment := m.IssueComment
624 return commentID, comment.URL, nil
625}
626
627func updateGithubIssueStatus(gc *githubv4.Client, id string, status bug.Status) error {
628 m := &updateIssueMutation{}
629
630 // set state
631 state := githubv4.IssueStateClosed
632 if status == bug.OpenStatus {
633 state = githubv4.IssueStateOpen
634 }
635
636 input := githubv4.UpdateIssueInput{
637 ID: id,
638 State: &state,
639 }
640
641 parentCtx := context.Background()
642 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
643 defer cancel()
644
645 if err := gc.Mutate(ctx, m, input, nil); err != nil {
646 return err
647 }
648
649 return nil
650}
651
652func updateGithubIssueBody(gc *githubv4.Client, id string, body string) error {
653 m := &updateIssueMutation{}
654 input := githubv4.UpdateIssueInput{
655 ID: id,
656 Body: (*githubv4.String)(&body),
657 }
658
659 parentCtx := context.Background()
660 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
661 defer cancel()
662
663 if err := gc.Mutate(ctx, m, input, nil); err != nil {
664 return err
665 }
666
667 return nil
668}
669
670func updateGithubIssueTitle(gc *githubv4.Client, id, title string) error {
671 m := &updateIssueMutation{}
672 input := githubv4.UpdateIssueInput{
673 ID: id,
674 Title: (*githubv4.String)(&title),
675 }
676
677 parentCtx := context.Background()
678 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
679 defer cancel()
680
681 if err := gc.Mutate(ctx, m, input, nil); err != nil {
682 return err
683 }
684
685 return nil
686}
687
688// update github issue labels
689func (ge *githubExporter) updateGithubIssueLabels(gc *githubv4.Client, labelableID string, added, removed []bug.Label) error {
690
691 addedIDs, err := ge.getLabelsIDs(gc, labelableID, added)
692 if err != nil {
693 return errors.Wrap(err, "getting added labels ids")
694 }
695
696 m := &addLabelsToLabelableMutation{}
697 inputAdd := githubv4.AddLabelsToLabelableInput{
698 LabelableID: labelableID,
699 LabelIDs: addedIDs,
700 }
701
702 parentCtx := context.Background()
703 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
704
705 // add labels
706 if err := gc.Mutate(ctx, m, inputAdd, nil); err != nil {
707 cancel()
708 return err
709 }
710
711 cancel()
712 removedIDs, err := ge.getLabelsIDs(gc, labelableID, added)
713 if err != nil {
714 return errors.Wrap(err, "getting added labels ids")
715 }
716
717 m2 := &removeLabelsFromLabelableMutation{}
718 inputRemove := githubv4.RemoveLabelsFromLabelableInput{
719 LabelableID: labelableID,
720 LabelIDs: removedIDs,
721 }
722
723 ctx2, cancel2 := context.WithTimeout(parentCtx, defaultTimeout)
724 defer cancel2()
725
726 // remove label labels
727 if err := gc.Mutate(ctx2, m2, inputRemove, nil); err != nil {
728 return err
729 }
730
731 return nil
732}