export.go

  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/pkg/errors"
 14	"github.com/shurcooL/githubv4"
 15
 16	"github.com/MichaelMure/git-bug/bridge/core"
 17	"github.com/MichaelMure/git-bug/bug"
 18	"github.com/MichaelMure/git-bug/cache"
 19	"github.com/MichaelMure/git-bug/util/git"
 20)
 21
 22var (
 23	ErrMissingIdentityToken = errors.New("missing identity token")
 24)
 25
 26// githubImporter implement the Importer interface
 27type githubExporter struct {
 28	conf core.Configuration
 29
 30	// number of exported bugs
 31	exportedBugs int
 32
 33	// export only bugs taged with one of these origins
 34	onlyOrigins []string
 35
 36	// cache identities clients
 37	identityClient map[string]*githubv4.Client
 38
 39	// map identity with their tokens
 40	identityToken map[string]string
 41
 42	// github repository ID
 43	repositoryID string
 44
 45	// cache identifiers used to speed up exporting operations
 46	// cleared for each bug
 47	cachedIDs map[string]string
 48
 49	// cache labels used to speed up exporting labels events
 50	cachedLabels map[string]string
 51}
 52
 53// Init .
 54func (ge *githubExporter) Init(conf core.Configuration) error {
 55	ge.conf = conf
 56	//TODO: initialize with multiple tokens
 57	ge.identityToken = make(map[string]string)
 58	ge.identityClient = make(map[string]*githubv4.Client)
 59	ge.cachedIDs = make(map[string]string)
 60	ge.cachedLabels = make(map[string]string)
 61	return nil
 62}
 63
 64func (ge *githubExporter) allowOrigin(origin string) bool {
 65	if ge.onlyOrigins == nil {
 66		return true
 67	}
 68
 69	for _, o := range ge.onlyOrigins {
 70		if origin == o {
 71			return true
 72		}
 73	}
 74
 75	return false
 76}
 77
 78// getIdentityClient return an identity github api v4 client
 79// if no client were found it will initilize it from the known tokens map and cache it for next it use
 80func (ge *githubExporter) getIdentityClient(id string) (*githubv4.Client, error) {
 81	client, ok := ge.identityClient[id]
 82	if ok {
 83		return client, nil
 84	}
 85
 86	// get token
 87	token, ok := ge.identityToken[id]
 88	if !ok {
 89		return nil, ErrMissingIdentityToken
 90	}
 91
 92	// create client
 93	client = buildClient(token)
 94	// cache client
 95	ge.identityClient[id] = client
 96
 97	return client, nil
 98}
 99
100// ExportAll export all event made by the current user to Github
101func (ge *githubExporter) ExportAll(repo *cache.RepoCache, since time.Time) error {
102	user, err := repo.GetUserIdentity()
103	if err != nil {
104		return err
105	}
106
107	ge.identityToken[user.Id()] = ge.conf[keyToken]
108
109	// get repository node id
110	ge.repositoryID, err = getRepositoryNodeID(
111		ge.conf[keyOwner],
112		ge.conf[keyProject],
113		ge.conf[keyToken],
114	)
115
116	if err != nil {
117		return err
118	}
119
120	allBugsIds := repo.AllBugsIds()
121bugLoop:
122	for _, id := range allBugsIds {
123		b, err := repo.ResolveBug(id)
124		if err != nil {
125			return err
126		}
127
128		snapshot := b.Snapshot()
129
130		// ignore issues created before since date
131		if snapshot.CreatedAt.Before(since) {
132			continue
133		}
134
135		for _, p := range snapshot.Participants {
136			// if we have a token for one of the participants
137			for userId := range ge.identityToken {
138				if p.Id() == userId {
139					// try to export the bug and it associated events
140					if err := ge.exportBug(b, since); err != nil {
141						return err
142					}
143
144					continue bugLoop
145				}
146			}
147		}
148	}
149
150	return nil
151}
152
153// exportBug publish bugs and related events
154func (ge *githubExporter) exportBug(b *cache.BugCache, since time.Time) error {
155	snapshot := b.Snapshot()
156
157	var bugGithubID string
158	var bugGithubURL string
159	var bugCreationHash string
160
161	// Special case:
162	// if a user try to export a bug that is not already exported to Github (or imported
163	// from Github) and he is not the author of the bug. There is nothing we can do.
164
165	// first operation is always createOp
166	createOp := snapshot.Operations[0].(*bug.CreateOperation)
167	author := createOp.GetAuthor()
168
169	// skip bug if origin is not allowed
170	origin, ok := createOp.GetMetadata(keyOrigin)
171	if ok && !ge.allowOrigin(origin) {
172		// TODO print a warn ?
173		return nil
174	}
175
176	// get github bug ID
177	githubID, ok := createOp.GetMetadata(keyGithubId)
178	if ok {
179		githubURL, ok := createOp.GetMetadata(keyGithubUrl)
180		if !ok {
181			// if we find github ID, github URL must be found too
182			panic("expected to find github issue URL")
183		}
184		// will be used to mark operation related to a bug as exported
185		bugGithubID = githubID
186		bugGithubURL = githubURL
187
188	} else {
189		// check that we have a token for operation author
190		client, err := ge.getIdentityClient(author.Id())
191		if err != nil {
192			// if bug is still not exported and we do not have the author stop the execution
193
194			fmt.Println("warning: skipping issue due to missing token for bug creator")
195			// this is not an error, don't export bug
196			return nil
197		}
198
199		// create bug
200		id, url, err := createGithubIssue(client, ge.repositoryID, createOp.Title, createOp.Message)
201		if err != nil {
202			return errors.Wrap(err, "exporting github issue")
203		}
204
205		hash, err := createOp.Hash()
206		if err != nil {
207			return errors.Wrap(err, "comment hash")
208		}
209
210		// mark bug creation operation as exported
211		if err := markOperationAsExported(b, hash, id, url); err != nil {
212			return errors.Wrap(err, "marking operation as exported")
213		}
214
215		// commit operation to avoid creating multiple issues with multiple pushes
216		if err := b.CommitAsNeeded(); err != nil {
217			return errors.Wrap(err, "bug commit")
218		}
219
220		// cache bug github ID and URL
221		bugGithubID = id
222		bugGithubURL = url
223	}
224
225	// get createOp hash
226	hash, err := createOp.Hash()
227	if err != nil {
228		return err
229	}
230
231	bugCreationHash = hash.String()
232
233	// cache operation github id
234	ge.cachedIDs[bugCreationHash] = bugGithubID
235
236	for _, op := range snapshot.Operations[1:] {
237		// ignore SetMetadata operations
238		if _, ok := op.(*bug.SetMetadataOperation); ok {
239			continue
240		}
241
242		// get operation hash
243		hash, err := op.Hash()
244		if err != nil {
245			return errors.Wrap(err, "operation hash")
246		}
247
248		// ignore imported (or exported) operations from github
249		// cache the ID of already exported or imported issues and events from Github
250		if id, ok := op.GetMetadata(keyGithubId); ok {
251			ge.cachedIDs[hash.String()] = id
252			continue
253		}
254
255		opAuthor := op.GetAuthor()
256		client, err := ge.getIdentityClient(opAuthor.Id())
257		if err != nil {
258			// don't export operation
259			continue
260		}
261
262		var id, url string
263		switch op.(type) {
264		case *bug.AddCommentOperation:
265			opr := op.(*bug.AddCommentOperation)
266
267			// send operation to github
268			id, url, err = addCommentGithubIssue(client, bugGithubID, opr.Message)
269			if err != nil {
270				return errors.Wrap(err, "adding comment")
271			}
272
273			hash, err = opr.Hash()
274			if err != nil {
275				return errors.Wrap(err, "comment hash")
276			}
277
278		case *bug.EditCommentOperation:
279
280			opr := op.(*bug.EditCommentOperation)
281			targetHash := opr.Target.String()
282
283			// Since github doesn't consider the issue body as a comment
284			if targetHash == bugCreationHash {
285
286				// case bug creation operation: we need to edit the Github issue
287				if err := updateGithubIssueBody(client, bugGithubID, opr.Message); err != nil {
288					return errors.Wrap(err, "editing issue")
289				}
290
291				id = bugGithubID
292				url = bugGithubURL
293
294			} else {
295
296				// case comment edition operation: we need to edit the Github comment
297				commentID, ok := ge.cachedIDs[targetHash]
298				if !ok {
299					panic("unexpected error: comment id not found")
300				}
301
302				eid, eurl, err := editCommentGithubIssue(client, commentID, opr.Message)
303				if err != nil {
304					return errors.Wrap(err, "editing comment")
305				}
306
307				// use comment id/url instead of issue id/url
308				id = eid
309				url = eurl
310			}
311
312			hash, err = opr.Hash()
313			if err != nil {
314				return errors.Wrap(err, "comment hash")
315			}
316
317		case *bug.SetStatusOperation:
318			opr := op.(*bug.SetStatusOperation)
319			if err := updateGithubIssueStatus(client, bugGithubID, opr.Status); err != nil {
320				return errors.Wrap(err, "editing status")
321			}
322
323			hash, err = opr.Hash()
324			if err != nil {
325				return errors.Wrap(err, "set status operation hash")
326			}
327
328			id = bugGithubID
329			url = bugGithubURL
330
331		case *bug.SetTitleOperation:
332			opr := op.(*bug.SetTitleOperation)
333			if err := updateGithubIssueTitle(client, bugGithubID, opr.Title); err != nil {
334				return errors.Wrap(err, "editing title")
335			}
336
337			hash, err = opr.Hash()
338			if err != nil {
339				return errors.Wrap(err, "set title operation hash")
340			}
341
342			id = bugGithubID
343			url = bugGithubURL
344
345		case *bug.LabelChangeOperation:
346			opr := op.(*bug.LabelChangeOperation)
347			if err := ge.updateGithubIssueLabels(client, bugGithubID, opr.Added, opr.Removed); err != nil {
348				return errors.Wrap(err, "updating labels")
349			}
350
351			hash, err = opr.Hash()
352			if err != nil {
353				return errors.Wrap(err, "label change operation hash")
354			}
355
356			id = bugGithubID
357			url = bugGithubURL
358
359		default:
360			panic("unhandled operation type case")
361		}
362
363		// mark operation as exported
364		if err := markOperationAsExported(b, hash, id, url); err != nil {
365			return errors.Wrap(err, "marking operation as exported")
366		}
367
368		if err := b.CommitAsNeeded(); err != nil {
369			return errors.Wrap(err, "bug commit")
370		}
371	}
372
373	return nil
374}
375
376// getRepositoryNodeID request github api v3 to get repository node id
377func getRepositoryNodeID(owner, project, token string) (string, error) {
378	url := fmt.Sprintf("%s/repos/%s/%s", githubV3Url, owner, project)
379
380	client := &http.Client{
381		Timeout: defaultTimeout,
382	}
383
384	req, err := http.NewRequest("GET", url, nil)
385	if err != nil {
386		return "", err
387	}
388
389	// need the token for private repositories
390	req.Header.Set("Authorization", fmt.Sprintf("token %s", token))
391
392	resp, err := client.Do(req)
393	if err != nil {
394		return "", err
395	}
396
397	if resp.StatusCode != http.StatusOK {
398		return "", fmt.Errorf("error retrieving repository node id %v", resp.StatusCode)
399	}
400
401	aux := struct {
402		NodeID string `json:"node_id"`
403	}{}
404
405	data, _ := ioutil.ReadAll(resp.Body)
406	defer resp.Body.Close()
407
408	err = json.Unmarshal(data, &aux)
409	if err != nil {
410		return "", err
411	}
412
413	return aux.NodeID, nil
414}
415
416func markOperationAsExported(b *cache.BugCache, target git.Hash, githubID, githubURL string) error {
417	_, err := b.SetMetadata(
418		target,
419		map[string]string{
420			keyGithubId:  githubID,
421			keyGithubUrl: githubURL,
422		},
423	)
424
425	return err
426}
427
428// get label from github
429func (ge *githubExporter) getGithubLabelID(gc *githubv4.Client, label string) (string, error) {
430	q := labelQuery{}
431	variables := map[string]interface{}{"name": label}
432	if err := gc.Query(context.TODO(), &q, variables); err != nil {
433		return "", err
434	}
435
436	return q.Repository.Label.ID, nil
437}
438
439func (ge *githubExporter) createGithubLabel(gc *githubv4.Client, label, labelColor string) (string, error) {
440	url := fmt.Sprintf("%s/repos/%s/%s/labels", githubV3Url, ge.conf[keyOwner], ge.conf[keyProject])
441
442	client := &http.Client{
443		Timeout: defaultTimeout,
444	}
445
446	req, err := http.NewRequest("POST", url, nil)
447	if err != nil {
448		return "", err
449	}
450
451	// need the token for private repositories
452	req.Header.Set("Authorization", fmt.Sprintf("token %s", ge.conf[keyToken]))
453
454	resp, err := client.Do(req)
455	if err != nil {
456		return "", err
457	}
458
459	if resp.StatusCode != http.StatusCreated {
460		return "", fmt.Errorf("error creating label: response status %v", resp.StatusCode)
461	}
462
463	aux := struct {
464		ID     string `json:"id"`
465		NodeID string `json:"node_id"`
466		Color  string `json:"color"`
467	}{}
468
469	data, _ := ioutil.ReadAll(resp.Body)
470	defer resp.Body.Close()
471
472	err = json.Unmarshal(data, &aux)
473	if err != nil {
474		return "", err
475	}
476
477	return aux.NodeID, nil
478}
479
480// create github label using api v4
481func (ge *githubExporter) createGithubLabelV4(gc *githubv4.Client, label, labelColor string) (string, error) {
482	m := &createLabelMutation{}
483	input := createLabelInput{
484		RepositoryID: ge.repositoryID,
485		Name:         githubv4.String(label),
486		Color:        githubv4.String(labelColor),
487	}
488
489	if err := gc.Mutate(context.TODO(), m, input, nil); err != nil {
490		return "", err
491	}
492
493	return m.CreateLabel.Label.ID, 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(gc *githubv4.Client, repositoryID, label string) (string, error) {
507	// try to get label id
508	labelID, err := ge.getGithubLabelID(gc, 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(gc, label, color)
519	if err != nil {
520		return "", err
521	}
522
523	return labelID, nil
524}
525
526func (ge *githubExporter) getLabelsIDs(gc *githubv4.Client, 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(gc, repositoryID, label)
538			if err != nil {
539				return nil, errors.Wrap(err, "get or create github label")
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(gc, labelableID, added)
652	if err != nil {
653		return errors.Wrap(err, "getting added labels ids")
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(gc, labelableID, added)
668	if err != nil {
669		return errors.Wrap(err, "getting added labels ids")
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}