export.go

  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{}{
431		"label": githubv4.String(label),
432		"owner": githubv4.String(ge.conf[keyOwner]),
433		"name":  githubv4.String(ge.conf[keyProject]),
434	}
435
436	parentCtx := context.Background()
437	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
438	defer cancel()
439
440	if err := gc.Query(ctx, q, variables); err != nil {
441		return "", err
442	}
443
444	if q.Repository.Label.ID == "" {
445		return "", fmt.Errorf("label not found")
446	}
447
448	return q.Repository.Label.ID, nil
449}
450
451func (ge *githubExporter) createGithubLabel(gc *githubv4.Client, label, labelColor string) (string, error) {
452	url := fmt.Sprintf("%s/repos/%s/%s/labels", githubV3Url, ge.conf[keyOwner], ge.conf[keyProject])
453
454	client := &http.Client{
455		Timeout: defaultTimeout,
456	}
457
458	params := struct {
459		Name        string `json:"name"`
460		Color       string `json:"color"`
461		Description string `json:"description"`
462	}{
463		Name:  label,
464		Color: labelColor,
465	}
466
467	data, err := json.Marshal(params)
468	if err != nil {
469		return "", err
470	}
471
472	req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
473	if err != nil {
474		return "", err
475	}
476
477	// need the token for private repositories
478	req.Header.Set("Authorization", fmt.Sprintf("token %s", ge.conf[keyToken]))
479
480	resp, err := client.Do(req)
481	if err != nil {
482		return "", err
483	}
484
485	if resp.StatusCode != http.StatusCreated {
486		return "", fmt.Errorf("error creating label: response status %v", resp.StatusCode)
487	}
488
489	aux := struct {
490		ID     int    `json:"id"`
491		NodeID string `json:"node_id"`
492		Color  string `json:"color"`
493	}{}
494
495	data, _ = ioutil.ReadAll(resp.Body)
496	defer resp.Body.Close()
497
498	err = json.Unmarshal(data, &aux)
499	if err != nil {
500		return "", err
501	}
502
503	return aux.NodeID, nil
504}
505
506// create github label using api v4
507func (ge *githubExporter) createGithubLabelV4(gc *githubv4.Client, label, labelColor string) (string, error) {
508	m := createLabelMutation{}
509	input := createLabelInput{
510		RepositoryID: ge.repositoryID,
511		Name:         githubv4.String(label),
512		Color:        githubv4.String(labelColor),
513	}
514
515	parentCtx := context.Background()
516	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
517	defer cancel()
518
519	if err := gc.Mutate(ctx, &m, input, nil); err != nil {
520		return "", err
521	}
522
523	return m.CreateLabel.Label.ID, nil
524}
525
526func (ge *githubExporter) getOrCreateGithubLabelID(gc *githubv4.Client, repositoryID string, label bug.Label) (string, error) {
527	// try to get label id
528	labelID, err := ge.getGithubLabelID(gc, string(label))
529	if err == nil {
530		return labelID, nil
531	}
532
533	// hex color
534	rgba := label.RGBA()
535	hexColor := fmt.Sprintf("%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B)
536
537	// create label and return id
538	labelID, err = ge.createGithubLabel(gc, string(label), hexColor)
539	if err != nil {
540		return "", err
541	}
542
543	return labelID, nil
544}
545
546func (ge *githubExporter) getLabelsIDs(gc *githubv4.Client, repositoryID string, labels []bug.Label) ([]githubv4.ID, error) {
547	ids := make([]githubv4.ID, 0, len(labels))
548	var err error
549
550	// check labels ids
551	for _, label := range labels {
552		id, ok := ge.cachedLabels[string(label)]
553		if !ok {
554			// try to query label id
555			id, err = ge.getOrCreateGithubLabelID(gc, repositoryID, label)
556			if err != nil {
557				return nil, errors.Wrap(err, "get or create github label")
558			}
559
560			// cache label id
561			ge.cachedLabels[string(label)] = id
562		}
563
564		ids = append(ids, githubv4.ID(id))
565	}
566
567	return ids, nil
568}
569
570// create a github issue and return it ID
571func createGithubIssue(gc *githubv4.Client, repositoryID, title, body string) (string, string, error) {
572	m := &createIssueMutation{}
573	input := githubv4.CreateIssueInput{
574		RepositoryID: repositoryID,
575		Title:        githubv4.String(title),
576		Body:         (*githubv4.String)(&body),
577	}
578
579	parentCtx := context.Background()
580	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
581	defer cancel()
582
583	if err := gc.Mutate(ctx, m, input, nil); err != nil {
584		return "", "", err
585	}
586
587	issue := m.CreateIssue.Issue
588	return issue.ID, issue.URL, nil
589}
590
591// add a comment to an issue and return it ID
592func addCommentGithubIssue(gc *githubv4.Client, subjectID string, body string) (string, string, error) {
593	m := &addCommentToIssueMutation{}
594	input := githubv4.AddCommentInput{
595		SubjectID: subjectID,
596		Body:      githubv4.String(body),
597	}
598
599	parentCtx := context.Background()
600	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
601	defer cancel()
602
603	if err := gc.Mutate(ctx, m, input, nil); err != nil {
604		return "", "", err
605	}
606
607	node := m.AddComment.CommentEdge.Node
608	return node.ID, node.URL, nil
609}
610
611func editCommentGithubIssue(gc *githubv4.Client, commentID, body string) (string, string, error) {
612	m := &updateIssueCommentMutation{}
613	input := githubv4.UpdateIssueCommentInput{
614		ID:   commentID,
615		Body: githubv4.String(body),
616	}
617
618	parentCtx := context.Background()
619	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
620	defer cancel()
621
622	if err := gc.Mutate(ctx, m, input, nil); err != nil {
623		return "", "", err
624	}
625
626	return commentID, m.UpdateIssueComment.IssueComment.URL, nil
627}
628
629func updateGithubIssueStatus(gc *githubv4.Client, id string, status bug.Status) error {
630	m := &updateIssueMutation{}
631
632	// set state
633	state := githubv4.IssueStateClosed
634	if status == bug.OpenStatus {
635		state = githubv4.IssueStateOpen
636	}
637
638	input := githubv4.UpdateIssueInput{
639		ID:    id,
640		State: &state,
641	}
642
643	parentCtx := context.Background()
644	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
645	defer cancel()
646
647	if err := gc.Mutate(ctx, m, input, nil); err != nil {
648		return err
649	}
650
651	return nil
652}
653
654func updateGithubIssueBody(gc *githubv4.Client, id string, body string) error {
655	m := &updateIssueMutation{}
656	input := githubv4.UpdateIssueInput{
657		ID:   id,
658		Body: (*githubv4.String)(&body),
659	}
660
661	parentCtx := context.Background()
662	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
663	defer cancel()
664
665	if err := gc.Mutate(ctx, m, input, nil); err != nil {
666		return err
667	}
668
669	return nil
670}
671
672func updateGithubIssueTitle(gc *githubv4.Client, id, title string) error {
673	m := &updateIssueMutation{}
674	input := githubv4.UpdateIssueInput{
675		ID:    id,
676		Title: (*githubv4.String)(&title),
677	}
678
679	parentCtx := context.Background()
680	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
681	defer cancel()
682
683	if err := gc.Mutate(ctx, m, input, nil); err != nil {
684		return err
685	}
686
687	return nil
688}
689
690// update github issue labels
691func (ge *githubExporter) updateGithubIssueLabels(gc *githubv4.Client, labelableID string, added, removed []bug.Label) error {
692	addedIDs, err := ge.getLabelsIDs(gc, labelableID, added)
693	if err != nil {
694		return errors.Wrap(err, "getting added labels ids")
695	}
696
697	m := &addLabelsToLabelableMutation{}
698	inputAdd := githubv4.AddLabelsToLabelableInput{
699		LabelableID: labelableID,
700		LabelIDs:    addedIDs,
701	}
702
703	parentCtx := context.Background()
704	ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
705
706	// add labels
707	if err := gc.Mutate(ctx, m, inputAdd, nil); err != nil {
708		cancel()
709		return err
710	}
711	cancel()
712
713	if len(removed) > 0 {
714		removedIDs, err := ge.getLabelsIDs(gc, labelableID, removed)
715		if err != nil {
716			return errors.Wrap(err, "getting added labels ids")
717		}
718
719		m2 := &removeLabelsFromLabelableMutation{}
720		inputRemove := githubv4.RemoveLabelsFromLabelableInput{
721			LabelableID: labelableID,
722			LabelIDs:    removedIDs,
723		}
724
725		ctx, cancel := context.WithTimeout(parentCtx, defaultTimeout)
726		defer cancel()
727
728		// remove label labels
729		if err := gc.Mutate(ctx, m2, inputRemove, nil); err != nil {
730			return err
731		}
732
733	}
734
735	return nil
736}