export.go

  1package gitlab
  2
  3import (
  4	"context"
  5	"fmt"
  6	"strconv"
  7	"time"
  8
  9	"github.com/pkg/errors"
 10	"github.com/xanzy/go-gitlab"
 11
 12	"github.com/MichaelMure/git-bug/bridge/core"
 13	"github.com/MichaelMure/git-bug/bridge/core/auth"
 14	"github.com/MichaelMure/git-bug/bug"
 15	"github.com/MichaelMure/git-bug/cache"
 16	"github.com/MichaelMure/git-bug/entity"
 17	"github.com/MichaelMure/git-bug/repository"
 18)
 19
 20var (
 21	ErrMissingIdentityToken = errors.New("missing identity token")
 22)
 23
 24// gitlabExporter implement the Exporter interface
 25type gitlabExporter struct {
 26	conf core.Configuration
 27
 28	// cache identities clients
 29	identityClient map[entity.Id]*gitlab.Client
 30
 31	// gitlab repository ID
 32	repositoryID string
 33
 34	// cache identifiers used to speed up exporting operations
 35	// cleared for each bug
 36	cachedOperationIDs map[string]string
 37}
 38
 39// Init .
 40func (ge *gitlabExporter) Init(repo *cache.RepoCache, conf core.Configuration) error {
 41	ge.conf = conf
 42	ge.identityClient = make(map[entity.Id]*gitlab.Client)
 43	ge.cachedOperationIDs = make(map[string]string)
 44
 45	// get repository node id
 46	ge.repositoryID = ge.conf[keyProjectID]
 47
 48	// preload all clients
 49	err := ge.cacheAllClient(repo)
 50	if err != nil {
 51		return err
 52	}
 53
 54	return nil
 55}
 56
 57func (ge *gitlabExporter) cacheAllClient(repo repository.RepoConfig) error {
 58	creds, err := auth.List(repo, auth.WithTarget(target), auth.WithKind(auth.KindToken))
 59	if err != nil {
 60		return err
 61	}
 62
 63	for _, cred := range creds {
 64		if _, ok := ge.identityClient[cred.UserId()]; !ok {
 65			client := buildClient(creds[0].(*auth.Token))
 66			ge.identityClient[cred.UserId()] = client
 67		}
 68	}
 69
 70	return nil
 71}
 72
 73// getIdentityClient return a gitlab v4 API client configured with the access token of the given identity.
 74func (ge *gitlabExporter) getIdentityClient(userId entity.Id) (*gitlab.Client, error) {
 75	client, ok := ge.identityClient[userId]
 76	if ok {
 77		return client, nil
 78	}
 79
 80	return nil, ErrMissingIdentityToken
 81}
 82
 83// ExportAll export all event made by the current user to Gitlab
 84func (ge *gitlabExporter) ExportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ExportResult, error) {
 85	out := make(chan core.ExportResult)
 86
 87	go func() {
 88		defer close(out)
 89
 90		allIdentitiesIds := make([]entity.Id, 0, len(ge.identityClient))
 91		for id := range ge.identityClient {
 92			allIdentitiesIds = append(allIdentitiesIds, id)
 93		}
 94
 95		allBugsIds := repo.AllBugsIds()
 96
 97		for _, id := range allBugsIds {
 98			select {
 99			case <-ctx.Done():
100				return
101			default:
102				b, err := repo.ResolveBug(id)
103				if err != nil {
104					out <- core.NewExportError(err, id)
105					return
106				}
107
108				snapshot := b.Snapshot()
109
110				// ignore issues created before since date
111				// TODO: compare the Lamport time instead of using the unix time
112				if snapshot.CreatedAt.Before(since) {
113					out <- core.NewExportNothing(b.Id(), "bug created before the since date")
114					continue
115				}
116
117				if snapshot.HasAnyActor(allIdentitiesIds...) {
118					// try to export the bug and it associated events
119					ge.exportBug(ctx, b, since, out)
120				}
121			}
122		}
123	}()
124
125	return out, nil
126}
127
128// exportBug publish bugs and related events
129func (ge *gitlabExporter) exportBug(ctx context.Context, b *cache.BugCache, since time.Time, out chan<- core.ExportResult) {
130	snapshot := b.Snapshot()
131
132	var bugUpdated bool
133	var err error
134	var bugGitlabID int
135	var bugGitlabIDString string
136	var bugCreationId string
137
138	// Special case:
139	// if a user try to export a bug that is not already exported to Gitlab (or imported
140	// from Gitlab) and we do not have the token of the bug author, there is nothing we can do.
141
142	// skip bug if origin is not allowed
143	origin, ok := snapshot.GetCreateMetadata(core.MetaKeyOrigin)
144	if ok && origin != target {
145		out <- core.NewExportNothing(b.Id(), fmt.Sprintf("issue tagged with origin: %s", origin))
146		return
147	}
148
149	// first operation is always createOp
150	createOp := snapshot.Operations[0].(*bug.CreateOperation)
151	author := snapshot.Author
152
153	// get gitlab bug ID
154	gitlabID, ok := snapshot.GetCreateMetadata(metaKeyGitlabId)
155	if ok {
156		projectID, ok := snapshot.GetCreateMetadata(metaKeyGitlabProject)
157		if !ok {
158			err := fmt.Errorf("expected to find gitlab project id")
159			out <- core.NewExportError(err, b.Id())
160			return
161		}
162
163		if projectID != ge.conf[keyProjectID] {
164			out <- core.NewExportNothing(b.Id(), "skipping issue imported from another repository")
165			return
166		}
167
168		// will be used to mark operation related to a bug as exported
169		bugGitlabIDString = gitlabID
170		bugGitlabID, err = strconv.Atoi(bugGitlabIDString)
171		if err != nil {
172			out <- core.NewExportError(fmt.Errorf("unexpected gitlab id format: %s", bugGitlabIDString), b.Id())
173			return
174		}
175
176	} else {
177		// check that we have a token for operation author
178		client, err := ge.getIdentityClient(author.Id())
179		if err != nil {
180			// if bug is still not exported and we do not have the author stop the execution
181			out <- core.NewExportNothing(b.Id(), fmt.Sprintf("missing author token"))
182			return
183		}
184
185		// create bug
186		_, id, url, err := createGitlabIssue(ctx, client, ge.repositoryID, createOp.Title, createOp.Message)
187		if err != nil {
188			err := errors.Wrap(err, "exporting gitlab issue")
189			out <- core.NewExportError(err, b.Id())
190			return
191		}
192
193		idString := strconv.Itoa(id)
194		out <- core.NewExportBug(b.Id())
195
196		_, err = b.SetMetadata(
197			createOp.Id(),
198			map[string]string{
199				metaKeyGitlabId:      idString,
200				metaKeyGitlabUrl:     url,
201				metaKeyGitlabProject: ge.repositoryID,
202			},
203		)
204		if err != nil {
205			err := errors.Wrap(err, "marking operation as exported")
206			out <- core.NewExportError(err, b.Id())
207			return
208		}
209
210		// commit operation to avoid creating multiple issues with multiple pushes
211		if err := b.CommitAsNeeded(); err != nil {
212			err := errors.Wrap(err, "bug commit")
213			out <- core.NewExportError(err, b.Id())
214			return
215		}
216
217		// cache bug gitlab ID and URL
218		bugGitlabID = id
219		bugGitlabIDString = idString
220	}
221
222	bugCreationId = createOp.Id().String()
223	// cache operation gitlab id
224	ge.cachedOperationIDs[bugCreationId] = bugGitlabIDString
225
226	labelSet := make(map[string]struct{})
227	for _, op := range snapshot.Operations[1:] {
228		// ignore SetMetadata operations
229		if _, ok := op.(*bug.SetMetadataOperation); ok {
230			continue
231		}
232
233		// ignore operations already existing in gitlab (due to import or export)
234		// cache the ID of already exported or imported issues and events from Gitlab
235		if id, ok := op.GetMetadata(metaKeyGitlabId); ok {
236			ge.cachedOperationIDs[op.Id().String()] = id
237			continue
238		}
239
240		opAuthor := op.GetAuthor()
241		client, err := ge.getIdentityClient(opAuthor.Id())
242		if err != nil {
243			continue
244		}
245
246		var id int
247		var idString, url string
248		switch op := op.(type) {
249		case *bug.AddCommentOperation:
250
251			// send operation to gitlab
252			id, err = addCommentGitlabIssue(ctx, client, ge.repositoryID, bugGitlabID, op.Message)
253			if err != nil {
254				err := errors.Wrap(err, "adding comment")
255				out <- core.NewExportError(err, b.Id())
256				return
257			}
258
259			out <- core.NewExportComment(op.Id())
260
261			idString = strconv.Itoa(id)
262			// cache comment id
263			ge.cachedOperationIDs[op.Id().String()] = idString
264
265		case *bug.EditCommentOperation:
266			targetId := op.Target.String()
267
268			// Since gitlab doesn't consider the issue body as a comment
269			if targetId == bugCreationId {
270
271				// case bug creation operation: we need to edit the Gitlab issue
272				if err := updateGitlabIssueBody(ctx, client, ge.repositoryID, bugGitlabID, op.Message); err != nil {
273					err := errors.Wrap(err, "editing issue")
274					out <- core.NewExportError(err, b.Id())
275					return
276				}
277
278				out <- core.NewExportCommentEdition(op.Id())
279				id = bugGitlabID
280
281			} else {
282
283				// case comment edition operation: we need to edit the Gitlab comment
284				commentID, ok := ge.cachedOperationIDs[targetId]
285				if !ok {
286					out <- core.NewExportError(fmt.Errorf("unexpected error: comment id not found"), op.Target)
287					return
288				}
289
290				commentIDint, err := strconv.Atoi(commentID)
291				if err != nil {
292					out <- core.NewExportError(fmt.Errorf("unexpected comment id format"), op.Target)
293					return
294				}
295
296				if err := editCommentGitlabIssue(ctx, client, ge.repositoryID, bugGitlabID, commentIDint, op.Message); err != nil {
297					err := errors.Wrap(err, "editing comment")
298					out <- core.NewExportError(err, b.Id())
299					return
300				}
301
302				out <- core.NewExportCommentEdition(op.Id())
303				id = commentIDint
304			}
305
306		case *bug.SetStatusOperation:
307			if err := updateGitlabIssueStatus(ctx, client, ge.repositoryID, bugGitlabID, op.Status); err != nil {
308				err := errors.Wrap(err, "editing status")
309				out <- core.NewExportError(err, b.Id())
310				return
311			}
312
313			out <- core.NewExportStatusChange(op.Id())
314			id = bugGitlabID
315
316		case *bug.SetTitleOperation:
317			if err := updateGitlabIssueTitle(ctx, client, ge.repositoryID, bugGitlabID, op.Title); err != nil {
318				err := errors.Wrap(err, "editing title")
319				out <- core.NewExportError(err, b.Id())
320				return
321			}
322
323			out <- core.NewExportTitleEdition(op.Id())
324			id = bugGitlabID
325
326		case *bug.LabelChangeOperation:
327			// we need to set the actual list of labels at each label change operation
328			// because gitlab update issue requests need directly the latest list of the verison
329
330			for _, label := range op.Added {
331				labelSet[label.String()] = struct{}{}
332			}
333
334			for _, label := range op.Removed {
335				delete(labelSet, label.String())
336			}
337
338			labels := make([]string, 0, len(labelSet))
339			for key := range labelSet {
340				labels = append(labels, key)
341			}
342
343			if err := updateGitlabIssueLabels(ctx, client, ge.repositoryID, bugGitlabID, labels); err != nil {
344				err := errors.Wrap(err, "updating labels")
345				out <- core.NewExportError(err, b.Id())
346				return
347			}
348
349			out <- core.NewExportLabelChange(op.Id())
350			id = bugGitlabID
351		default:
352			panic("unhandled operation type case")
353		}
354
355		idString = strconv.Itoa(id)
356		// mark operation as exported
357		if err := markOperationAsExported(b, op.Id(), idString, url); err != nil {
358			err := errors.Wrap(err, "marking operation as exported")
359			out <- core.NewExportError(err, b.Id())
360			return
361		}
362
363		// commit at each operation export to avoid exporting same events multiple times
364		if err := b.CommitAsNeeded(); err != nil {
365			err := errors.Wrap(err, "bug commit")
366			out <- core.NewExportError(err, b.Id())
367			return
368		}
369
370		bugUpdated = true
371	}
372
373	if !bugUpdated {
374		out <- core.NewExportNothing(b.Id(), "nothing has been exported")
375	}
376}
377
378func markOperationAsExported(b *cache.BugCache, target entity.Id, gitlabID, gitlabURL string) error {
379	_, err := b.SetMetadata(
380		target,
381		map[string]string{
382			metaKeyGitlabId:  gitlabID,
383			metaKeyGitlabUrl: gitlabURL,
384		},
385	)
386
387	return err
388}
389
390// create a gitlab. issue and return it ID
391func createGitlabIssue(ctx context.Context, gc *gitlab.Client, repositoryID, title, body string) (int, int, string, error) {
392	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
393	defer cancel()
394	issue, _, err := gc.Issues.CreateIssue(
395		repositoryID,
396		&gitlab.CreateIssueOptions{
397			Title:       &title,
398			Description: &body,
399		},
400		gitlab.WithContext(ctx),
401	)
402	if err != nil {
403		return 0, 0, "", err
404	}
405
406	return issue.ID, issue.IID, issue.WebURL, nil
407}
408
409// add a comment to an issue and return it ID
410func addCommentGitlabIssue(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID int, body string) (int, error) {
411	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
412	defer cancel()
413	note, _, err := gc.Notes.CreateIssueNote(
414		repositoryID, issueID,
415		&gitlab.CreateIssueNoteOptions{
416			Body: &body,
417		},
418		gitlab.WithContext(ctx),
419	)
420	if err != nil {
421		return 0, err
422	}
423
424	return note.ID, nil
425}
426
427func editCommentGitlabIssue(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID, noteID int, body string) error {
428	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
429	defer cancel()
430	_, _, err := gc.Notes.UpdateIssueNote(
431		repositoryID, issueID, noteID,
432		&gitlab.UpdateIssueNoteOptions{
433			Body: &body,
434		},
435		gitlab.WithContext(ctx),
436	)
437
438	return err
439}
440
441func updateGitlabIssueStatus(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID int, status bug.Status) error {
442	var state string
443
444	switch status {
445	case bug.OpenStatus:
446		state = "reopen"
447	case bug.ClosedStatus:
448		state = "close"
449	default:
450		panic("unknown bug state")
451	}
452
453	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
454	defer cancel()
455	_, _, err := gc.Issues.UpdateIssue(
456		repositoryID, issueID,
457		&gitlab.UpdateIssueOptions{
458			StateEvent: &state,
459		},
460		gitlab.WithContext(ctx),
461	)
462
463	return err
464}
465
466func updateGitlabIssueBody(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID int, body string) error {
467	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
468	defer cancel()
469	_, _, err := gc.Issues.UpdateIssue(
470		repositoryID, issueID,
471		&gitlab.UpdateIssueOptions{
472			Description: &body,
473		},
474		gitlab.WithContext(ctx),
475	)
476
477	return err
478}
479
480func updateGitlabIssueTitle(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID int, title string) error {
481	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
482	defer cancel()
483	_, _, err := gc.Issues.UpdateIssue(
484		repositoryID, issueID,
485		&gitlab.UpdateIssueOptions{
486			Title: &title,
487		},
488		gitlab.WithContext(ctx),
489	)
490
491	return err
492}
493
494// update gitlab. issue labels
495func updateGitlabIssueLabels(ctx context.Context, gc *gitlab.Client, repositoryID string, issueID int, labels []string) error {
496	ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
497	defer cancel()
498	gitlabLabels := gitlab.Labels(labels)
499	_, _, err := gc.Issues.UpdateIssue(
500		repositoryID, issueID,
501		&gitlab.UpdateIssueOptions{
502			Labels: &gitlabLabels,
503		},
504		gitlab.WithContext(ctx),
505	)
506
507	return err
508}