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 identities 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 // incr exported bugs
205 ge.exportedBugs++
206
207 hash, err := createOp.Hash()
208 if err != nil {
209 return errors.Wrap(err, "comment hash")
210 }
211
212 // mark bug creation operation as exported
213 if err := markOperationAsExported(b, hash, id, url); err != nil {
214 return errors.Wrap(err, "marking operation as exported")
215 }
216
217 // commit operation to avoid creating multiple issues with multiple pushes
218 if err := b.CommitAsNeeded(); err != nil {
219 return errors.Wrap(err, "bug commit")
220 }
221
222 // cache bug github ID and URL
223 bugGithubID = id
224 bugGithubURL = url
225 }
226
227 // get createOp hash
228 hash, err := createOp.Hash()
229 if err != nil {
230 return err
231 }
232
233 bugCreationHash = hash.String()
234
235 // cache operation github id
236 ge.cachedIDs[bugCreationHash] = bugGithubID
237
238 for _, op := range snapshot.Operations[1:] {
239 // ignore SetMetadata operations
240 if _, ok := op.(*bug.SetMetadataOperation); ok {
241 continue
242 }
243
244 // get operation hash
245 hash, err := op.Hash()
246 if err != nil {
247 return errors.Wrap(err, "operation hash")
248 }
249
250 // ignore imported (or exported) operations from github
251 // cache the ID of already exported or imported issues and events from Github
252 if id, ok := op.GetMetadata(keyGithubId); ok {
253 ge.cachedIDs[hash.String()] = id
254 continue
255 }
256
257 opAuthor := op.GetAuthor()
258 client, err := ge.getIdentityClient(opAuthor.Id())
259 if err != nil {
260 // don't export operation
261 continue
262 }
263
264 var id, url string
265 switch op.(type) {
266 case *bug.AddCommentOperation:
267 opr := op.(*bug.AddCommentOperation)
268
269 // send operation to github
270 id, url, err = addCommentGithubIssue(client, bugGithubID, opr.Message)
271 if err != nil {
272 return errors.Wrap(err, "adding comment")
273 }
274
275 hash, err = opr.Hash()
276 if err != nil {
277 return errors.Wrap(err, "comment hash")
278 }
279
280 case *bug.EditCommentOperation:
281
282 opr := op.(*bug.EditCommentOperation)
283 targetHash := opr.Target.String()
284
285 // Since github doesn't consider the issue body as a comment
286 if targetHash == bugCreationHash {
287
288 // case bug creation operation: we need to edit the Github issue
289 if err := updateGithubIssueBody(client, bugGithubID, opr.Message); err != nil {
290 return errors.Wrap(err, "editing issue")
291 }
292
293 id = bugGithubID
294 url = bugGithubURL
295
296 } else {
297
298 // case comment edition operation: we need to edit the Github comment
299 commentID, ok := ge.cachedIDs[targetHash]
300 if !ok {
301 panic("unexpected error: comment id not found")
302 }
303
304 eid, eurl, err := editCommentGithubIssue(client, commentID, opr.Message)
305 if err != nil {
306 return errors.Wrap(err, "editing comment")
307 }
308
309 // use comment id/url instead of issue id/url
310 id = eid
311 url = eurl
312 }
313
314 hash, err = opr.Hash()
315 if err != nil {
316 return errors.Wrap(err, "comment hash")
317 }
318
319 case *bug.SetStatusOperation:
320 opr := op.(*bug.SetStatusOperation)
321 if err := updateGithubIssueStatus(client, bugGithubID, opr.Status); err != nil {
322 return errors.Wrap(err, "editing status")
323 }
324
325 hash, err = opr.Hash()
326 if err != nil {
327 return errors.Wrap(err, "set status operation hash")
328 }
329
330 id = bugGithubID
331 url = bugGithubURL
332
333 case *bug.SetTitleOperation:
334 opr := op.(*bug.SetTitleOperation)
335 if err := updateGithubIssueTitle(client, bugGithubID, opr.Title); err != nil {
336 return errors.Wrap(err, "editing title")
337 }
338
339 hash, err = opr.Hash()
340 if err != nil {
341 return errors.Wrap(err, "set title operation hash")
342 }
343
344 id = bugGithubID
345 url = bugGithubURL
346
347 case *bug.LabelChangeOperation:
348 opr := op.(*bug.LabelChangeOperation)
349 if err := ge.updateGithubIssueLabels(client, bugGithubID, opr.Added, opr.Removed); err != nil {
350 return errors.Wrap(err, "updating labels")
351 }
352
353 hash, err = opr.Hash()
354 if err != nil {
355 return errors.Wrap(err, "label change operation hash")
356 }
357
358 id = bugGithubID
359 url = bugGithubURL
360
361 default:
362 panic("unhandled operation type case")
363 }
364
365 // mark operation as exported
366 if err := markOperationAsExported(b, hash, id, url); err != nil {
367 return errors.Wrap(err, "marking operation as exported")
368 }
369
370 if err := b.CommitAsNeeded(); err != nil {
371 return errors.Wrap(err, "bug commit")
372 }
373 }
374
375 return nil
376}
377
378// getRepositoryNodeID request github api v3 to get repository node id
379func getRepositoryNodeID(owner, project, token string) (string, error) {
380 url := fmt.Sprintf("%s/repos/%s/%s", githubV3Url, owner, project)
381
382 client := &http.Client{
383 Timeout: defaultTimeout,
384 }
385
386 req, err := http.NewRequest("GET", url, nil)
387 if err != nil {
388 return "", err
389 }
390
391 // need the token for private repositories
392 req.Header.Set("Authorization", fmt.Sprintf("token %s", token))
393
394 resp, err := client.Do(req)
395 if err != nil {
396 return "", err
397 }
398
399 if resp.StatusCode != http.StatusOK {
400 return "", fmt.Errorf("error retrieving repository node id %v", resp.StatusCode)
401 }
402
403 aux := struct {
404 NodeID string `json:"node_id"`
405 }{}
406
407 data, _ := ioutil.ReadAll(resp.Body)
408 defer resp.Body.Close()
409
410 err = json.Unmarshal(data, &aux)
411 if err != nil {
412 return "", err
413 }
414
415 return aux.NodeID, nil
416}
417
418func markOperationAsExported(b *cache.BugCache, target git.Hash, githubID, githubURL string) error {
419 _, err := b.SetMetadata(
420 target,
421 map[string]string{
422 keyGithubId: githubID,
423 keyGithubUrl: githubURL,
424 },
425 )
426
427 return err
428}
429
430// get label from github
431func (ge *githubExporter) getGithubLabelID(gc *githubv4.Client, label string) (string, error) {
432 q := &labelQuery{}
433 variables := map[string]interface{}{
434 "label": githubv4.String(label),
435 "owner": githubv4.String(ge.conf[keyOwner]),
436 "name": githubv4.String(ge.conf[keyProject]),
437 }
438
439 parentCtx := context.Background()
440 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
441 defer cancel()
442
443 if err := gc.Query(ctx, q, variables); err != nil {
444 return "", err
445 }
446
447 if q.Repository.Label.ID == "" {
448 return "", fmt.Errorf("label not found")
449 }
450
451 return q.Repository.Label.ID, nil
452}
453
454func (ge *githubExporter) createGithubLabel(gc *githubv4.Client, label, labelColor string) (string, error) {
455 url := fmt.Sprintf("%s/repos/%s/%s/labels", githubV3Url, ge.conf[keyOwner], ge.conf[keyProject])
456
457 client := &http.Client{
458 Timeout: defaultTimeout,
459 }
460
461 params := struct {
462 Name string `json:"name"`
463 Color string `json:"color"`
464 Description string `json:"description"`
465 }{
466 Name: label,
467 Color: labelColor,
468 }
469
470 data, err := json.Marshal(params)
471 if err != nil {
472 return "", err
473 }
474
475 req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
476 if err != nil {
477 return "", err
478 }
479
480 // need the token for private repositories
481 req.Header.Set("Authorization", fmt.Sprintf("token %s", ge.conf[keyToken]))
482
483 resp, err := client.Do(req)
484 if err != nil {
485 return "", err
486 }
487
488 if resp.StatusCode != http.StatusCreated {
489 return "", fmt.Errorf("error creating label: response status %v", resp.StatusCode)
490 }
491
492 aux := struct {
493 ID int `json:"id"`
494 NodeID string `json:"node_id"`
495 Color string `json:"color"`
496 }{}
497
498 data, _ = ioutil.ReadAll(resp.Body)
499 defer resp.Body.Close()
500
501 err = json.Unmarshal(data, &aux)
502 if err != nil {
503 return "", err
504 }
505
506 return aux.NodeID, nil
507}
508
509// create github label using api v4
510func (ge *githubExporter) createGithubLabelV4(gc *githubv4.Client, label, labelColor string) (string, error) {
511 m := createLabelMutation{}
512 input := createLabelInput{
513 RepositoryID: ge.repositoryID,
514 Name: githubv4.String(label),
515 Color: githubv4.String(labelColor),
516 }
517
518 parentCtx := context.Background()
519 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
520 defer cancel()
521
522 if err := gc.Mutate(ctx, &m, input, nil); err != nil {
523 return "", err
524 }
525
526 return m.CreateLabel.Label.ID, nil
527}
528
529func (ge *githubExporter) getOrCreateGithubLabelID(gc *githubv4.Client, repositoryID string, label bug.Label) (string, error) {
530 // try to get label id
531 labelID, err := ge.getGithubLabelID(gc, string(label))
532 if err == nil {
533 return labelID, nil
534 }
535
536 // hex color
537 rgba := label.RGBA()
538 hexColor := fmt.Sprintf("%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B)
539
540 // create label and return id
541 labelID, err = ge.createGithubLabel(gc, string(label), hexColor)
542 if err != nil {
543 return "", err
544 }
545
546 return labelID, nil
547}
548
549func (ge *githubExporter) getLabelsIDs(gc *githubv4.Client, repositoryID string, labels []bug.Label) ([]githubv4.ID, error) {
550 ids := make([]githubv4.ID, 0, len(labels))
551 var err error
552
553 // check labels ids
554 for _, label := range labels {
555 id, ok := ge.cachedLabels[string(label)]
556 if !ok {
557 // try to query label id
558 id, err = ge.getOrCreateGithubLabelID(gc, repositoryID, label)
559 if err != nil {
560 return nil, errors.Wrap(err, "get or create github label")
561 }
562
563 // cache label id
564 ge.cachedLabels[string(label)] = id
565 }
566
567 ids = append(ids, githubv4.ID(id))
568 }
569
570 return ids, nil
571}
572
573// create a github issue and return it ID
574func createGithubIssue(gc *githubv4.Client, repositoryID, title, body string) (string, string, error) {
575 m := &createIssueMutation{}
576 input := githubv4.CreateIssueInput{
577 RepositoryID: repositoryID,
578 Title: githubv4.String(title),
579 Body: (*githubv4.String)(&body),
580 }
581
582 parentCtx := context.Background()
583 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
584 defer cancel()
585
586 if err := gc.Mutate(ctx, m, input, nil); err != nil {
587 return "", "", err
588 }
589
590 issue := m.CreateIssue.Issue
591 return issue.ID, issue.URL, nil
592}
593
594// add a comment to an issue and return it ID
595func addCommentGithubIssue(gc *githubv4.Client, subjectID string, body string) (string, string, error) {
596 m := &addCommentToIssueMutation{}
597 input := githubv4.AddCommentInput{
598 SubjectID: subjectID,
599 Body: githubv4.String(body),
600 }
601
602 parentCtx := context.Background()
603 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
604 defer cancel()
605
606 if err := gc.Mutate(ctx, m, input, nil); err != nil {
607 return "", "", err
608 }
609
610 node := m.AddComment.CommentEdge.Node
611 return node.ID, node.URL, nil
612}
613
614func editCommentGithubIssue(gc *githubv4.Client, commentID, body string) (string, string, error) {
615 m := &updateIssueCommentMutation{}
616 input := githubv4.UpdateIssueCommentInput{
617 ID: commentID,
618 Body: githubv4.String(body),
619 }
620
621 parentCtx := context.Background()
622 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
623 defer cancel()
624
625 if err := gc.Mutate(ctx, m, input, nil); err != nil {
626 return "", "", err
627 }
628
629 return commentID, m.UpdateIssueComment.IssueComment.URL, nil
630}
631
632func updateGithubIssueStatus(gc *githubv4.Client, id string, status bug.Status) error {
633 m := &updateIssueMutation{}
634
635 // set state
636 state := githubv4.IssueStateClosed
637 if status == bug.OpenStatus {
638 state = githubv4.IssueStateOpen
639 }
640
641 input := githubv4.UpdateIssueInput{
642 ID: id,
643 State: &state,
644 }
645
646 parentCtx := context.Background()
647 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
648 defer cancel()
649
650 if err := gc.Mutate(ctx, m, input, nil); err != nil {
651 return err
652 }
653
654 return nil
655}
656
657func updateGithubIssueBody(gc *githubv4.Client, id string, body string) error {
658 m := &updateIssueMutation{}
659 input := githubv4.UpdateIssueInput{
660 ID: id,
661 Body: (*githubv4.String)(&body),
662 }
663
664 parentCtx := context.Background()
665 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
666 defer cancel()
667
668 if err := gc.Mutate(ctx, m, input, nil); err != nil {
669 return err
670 }
671
672 return nil
673}
674
675func updateGithubIssueTitle(gc *githubv4.Client, id, title string) error {
676 m := &updateIssueMutation{}
677 input := githubv4.UpdateIssueInput{
678 ID: id,
679 Title: (*githubv4.String)(&title),
680 }
681
682 parentCtx := context.Background()
683 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
684 defer cancel()
685
686 if err := gc.Mutate(ctx, m, input, nil); err != nil {
687 return err
688 }
689
690 return nil
691}
692
693// update github issue labels
694func (ge *githubExporter) updateGithubIssueLabels(gc *githubv4.Client, labelableID string, added, removed []bug.Label) error {
695 addedIDs, err := ge.getLabelsIDs(gc, labelableID, added)
696 if err != nil {
697 return errors.Wrap(err, "getting added labels ids")
698 }
699
700 m := &addLabelsToLabelableMutation{}
701 inputAdd := githubv4.AddLabelsToLabelableInput{
702 LabelableID: labelableID,
703 LabelIDs: addedIDs,
704 }
705
706 parentCtx := context.Background()
707 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
708
709 // add labels
710 if err := gc.Mutate(ctx, m, inputAdd, nil); err != nil {
711 cancel()
712 return err
713 }
714 cancel()
715
716 if len(removed) > 0 {
717 removedIDs, err := ge.getLabelsIDs(gc, labelableID, removed)
718 if err != nil {
719 return errors.Wrap(err, "getting added labels ids")
720 }
721
722 m2 := &removeLabelsFromLabelableMutation{}
723 inputRemove := githubv4.RemoveLabelsFromLabelableInput{
724 LabelableID: labelableID,
725 LabelIDs: removedIDs,
726 }
727
728 ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
729 defer cancel()
730
731 // remove label labels
732 if err := gc.Mutate(ctx, m2, inputRemove, nil); err != nil {
733 return err
734 }
735
736 }
737
738 return nil
739}