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