aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/gitlab/export.go
blob: 2f70e01aaa82ecd69eb7cf10566bfed717e53813 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package gitlab

import (
	"fmt"
	"strconv"
	"time"

	"github.com/pkg/errors"
	"github.com/xanzy/go-gitlab"

	"github.com/MichaelMure/git-bug/bridge/core"
	"github.com/MichaelMure/git-bug/bug"
	"github.com/MichaelMure/git-bug/cache"
	"github.com/MichaelMure/git-bug/util/git"
)

var (
	ErrMissingIdentityToken = errors.New("missing identity token")
)

const (
	keyGitlabId  = "gitlab-id"
	keyGitlabUrl = "gitlab-url"
	keyOrigin    = "origin"
)

// gitlabExporter implement the Exporter interface
type gitlabExporter struct {
	conf core.Configuration

	// cache identities clients
	identityClient map[string]*gitlab.Client

	// map identities with their tokens
	identityToken map[string]string

	// gitlab repository ID
	repositoryID string

	// cache identifiers used to speed up exporting operations
	// cleared for each bug
	cachedOperationIDs map[string]string

	// cache labels used to speed up exporting labels events
	cachedLabels map[string]string
}

// Init .
func (ge *gitlabExporter) Init(conf core.Configuration) error {
	ge.conf = conf
	//TODO: initialize with multiple tokens
	ge.identityToken = make(map[string]string)
	ge.identityClient = make(map[string]*gitlab.Client)
	ge.cachedOperationIDs = make(map[string]string)
	ge.cachedLabels = make(map[string]string)
	return nil
}

// getIdentityClient return a gitlab v4 API client configured with the access token of the given identity.
// if no client were found it will initialize it from the known tokens map and cache it for next use
func (ge *gitlabExporter) getIdentityClient(id string) (*gitlab.Client, error) {
	client, ok := ge.identityClient[id]
	if ok {
		return client, nil
	}

	// get token
	token, ok := ge.identityToken[id]
	if !ok {
		return nil, ErrMissingIdentityToken
	}

	// create client
	client = buildClient(token)
	// cache client
	ge.identityClient[id] = client

	return client, nil
}

// ExportAll export all event made by the current user to Gitlab
func (ge *gitlabExporter) ExportAll(repo *cache.RepoCache, since time.Time) (<-chan core.ExportResult, error) {
	out := make(chan core.ExportResult)

	user, err := repo.GetUserIdentity()
	if err != nil {
		return nil, err
	}

	ge.identityToken[user.Id()] = ge.conf[keyToken]

	// get repository node id
	ge.repositoryID = ge.conf[keyProjectID]

	if err != nil {
		return nil, err
	}

	go func() {
		defer close(out)

		var allIdentitiesIds []string
		for id := range ge.identityToken {
			allIdentitiesIds = append(allIdentitiesIds, id)
		}

		allBugsIds := repo.AllBugsIds()

		for _, id := range allBugsIds {
			b, err := repo.ResolveBug(id)
			if err != nil {
				out <- core.NewExportError(err, id)
				return
			}

			snapshot := b.Snapshot()

			// ignore issues created before since date
			// TODO: compare the Lamport time instead of using the unix time
			if snapshot.CreatedAt.Before(since) {
				out <- core.NewExportNothing(b.Id(), "bug created before the since date")
				continue
			}

			if snapshot.HasAnyActor(allIdentitiesIds...) {
				// try to export the bug and it associated events
				ge.exportBug(b, since, out)
			} else {
				out <- core.NewExportNothing(id, "not an actor")
			}
		}
	}()

	return out, nil
}

// exportBug publish bugs and related events
func (ge *gitlabExporter) exportBug(b *cache.BugCache, since time.Time, out chan<- core.ExportResult) {
	snapshot := b.Snapshot()

	var bugGitlabID string
	var bugGitlabURL string
	var bugCreationHash string

	// Special case:
	// if a user try to export a bug that is not already exported to Gitlab (or imported
	// from Gitlab) and we do not have the token of the bug author, there is nothing we can do.

	// first operation is always createOp
	createOp := snapshot.Operations[0].(*bug.CreateOperation)
	author := snapshot.Author

	// skip bug if origin is not allowed
	origin, ok := snapshot.GetCreateMetadata(keyOrigin)
	if ok && origin != target {
		out <- core.NewExportNothing(b.Id(), fmt.Sprintf("issue tagged with origin: %s", origin))
		return
	}

	// get gitlab bug ID
	gitlabID, ok := snapshot.GetCreateMetadata(keyGitlabId)
	if ok {
		gitlabURL, ok := snapshot.GetCreateMetadata(keyGitlabUrl)
		if !ok {
			// if we find gitlab ID, gitlab URL must be found too
			err := fmt.Errorf("expected to find gitlab issue URL")
			out <- core.NewExportError(err, b.Id())
		}

		//FIXME:
		// ignore issue comming from other repositories

		out <- core.NewExportNothing(b.Id(), "bug already exported")
		// will be used to mark operation related to a bug as exported
		bugGitlabID = gitlabID
		bugGitlabURL = gitlabURL

	} else {
		// check that we have a token for operation author
		client, err := ge.getIdentityClient(author.Id())
		if err != nil {
			// if bug is still not exported and we do not have the author stop the execution
			out <- core.NewExportNothing(b.Id(), fmt.Sprintf("missing author token"))
			return
		}

		// create bug
		id, url, err := createGitlabIssue(client, ge.repositoryID, createOp.Title, createOp.Message)
		if err != nil {
			err := errors.Wrap(err, "exporting gitlab issue")
			out <- core.NewExportError(err, b.Id())
			return
		}

		out <- core.NewExportBug(b.Id())

		hash, err := createOp.Hash()
		if err != nil {
			err := errors.Wrap(err, "comment hash")
			out <- core.NewExportError(err, b.Id())
			return
		}

		// mark bug creation operation as exported
		if err := markOperationAsExported(b, hash, id, url); err != nil {
			err := errors.Wrap(err, "marking operation as exported")
			out <- core.NewExportError(err, b.Id())
			return
		}

		// commit operation to avoid creating multiple issues with multiple pushes
		if err := b.CommitAsNeeded(); err != nil {
			err := errors.Wrap(err, "bug commit")
			out <- core.NewExportError(err, b.Id())
			return
		}

		// cache bug gitlab ID and URL
		bugGitlabID = id
		bugGitlabURL = url
	}

	// get createOp hash
	hash, err := createOp.Hash()
	if err != nil {
		out <- core.NewExportError(err, b.Id())
		return
	}

	bugCreationHash = hash.String()

	// cache operation gitlab id
	ge.cachedOperationIDs[bugCreationHash] = bugGitlabID

	for _, op := range snapshot.Operations[1:] {
		// ignore SetMetadata operations
		if _, ok := op.(*bug.SetMetadataOperation); ok {
			continue
		}

		// get operation hash
		hash, err := op.Hash()
		if err != nil {
			err := errors.Wrap(err, "operation hash")
			out <- core.NewExportError(err, b.Id())
			return
		}

		// ignore operations already existing in gitlab (due to import or export)
		// cache the ID of already exported or imported issues and events from Gitlab
		if id, ok := op.GetMetadata(keyGitlabId); ok {
			ge.cachedOperationIDs[hash.String()] = id
			out <- core.NewExportNothing(hash.String(), "already exported operation")
			continue
		}

		opAuthor := op.GetAuthor()
		client, err := ge.getIdentityClient(opAuthor.Id())
		if err != nil {
			out <- core.NewExportNothing(hash.String(), "missing operation author token")
			continue
		}

		var id, url string
		switch op.(type) {
		case *bug.AddCommentOperation:
			opr := op.(*bug.AddCommentOperation)

			// send operation to gitlab
			id, url, err = addCommentGitlabIssue(client, bugGitlabID, opr.Message)
			if err != nil {
				err := errors.Wrap(err, "adding comment")
				out <- core.NewExportError(err, b.Id())
				return
			}

			out <- core.NewExportComment(hash.String())

			// cache comment id
			ge.cachedOperationIDs[hash.String()] = id

		case *bug.EditCommentOperation:

			opr := op.(*bug.EditCommentOperation)
			targetHash := opr.Target.String()

			// Since gitlab doesn't consider the issue body as a comment
			if targetHash == bugCreationHash {

				// case bug creation operation: we need to edit the Gitlab issue
				if err := updateGitlabIssueBody(client, bugGitlabID, opr.Message); err != nil {
					err := errors.Wrap(err, "editing issue")
					out <- core.NewExportError(err, b.Id())
					return
				}

				out <- core.NewExportCommentEdition(hash.String())

				id = bugGitlabID
				url = bugGitlabURL

			} else {

				// case comment edition operation: we need to edit the Gitlab comment
				commentID, ok := ge.cachedOperationIDs[targetHash]
				if !ok {
					panic("unexpected error: comment id not found")
				}

				eid, eurl, err := editCommentGitlabIssue(client, commentID, opr.Message)
				if err != nil {
					err := errors.Wrap(err, "editing comment")
					out <- core.NewExportError(err, b.Id())
					return
				}

				out <- core.NewExportCommentEdition(hash.String())

				// use comment id/url instead of issue id/url
				id = eid
				url = eurl
			}

		case *bug.SetStatusOperation:
			opr := op.(*bug.SetStatusOperation)
			if err := updateGitlabIssueStatus(client, bugGitlabID, opr.Status); err != nil {
				err := errors.Wrap(err, "editing status")
				out <- core.NewExportError(err, b.Id())
				return
			}

			out <- core.NewExportStatusChange(hash.String())

			id = bugGitlabID
			url = bugGitlabURL

		case *bug.SetTitleOperation:
			opr := op.(*bug.SetTitleOperation)
			if err := updateGitlabIssueTitle(client, bugGitlabID, opr.Title); err != nil {
				err := errors.Wrap(err, "editing title")
				out <- core.NewExportError(err, b.Id())
				return
			}

			out <- core.NewExportTitleEdition(hash.String())

			id = bugGitlabID
			url = bugGitlabURL

		case *bug.LabelChangeOperation:
			opr := op.(*bug.LabelChangeOperation)
			if err := ge.updateGitlabIssueLabels(client, bugGitlabID, opr.Added, opr.Removed); err != nil {
				err := errors.Wrap(err, "updating labels")
				out <- core.NewExportError(err, b.Id())
				return
			}

			out <- core.NewExportLabelChange(hash.String())

			id = bugGitlabID
			url = bugGitlabURL

		default:
			panic("unhandled operation type case")
		}

		// mark operation as exported
		if err := markOperationAsExported(b, hash, id, url); err != nil {
			err := errors.Wrap(err, "marking operation as exported")
			out <- core.NewExportError(err, b.Id())
			return
		}

		// commit at each operation export to avoid exporting same events multiple times
		if err := b.CommitAsNeeded(); err != nil {
			err := errors.Wrap(err, "bug commit")
			out <- core.NewExportError(err, b.Id())
			return
		}
	}
}

func markOperationAsExported(b *cache.BugCache, target git.Hash, gitlabID, gitlabURL string) error {
	_, err := b.SetMetadata(
		target,
		map[string]string{
			keyGitlabId:  gitlabID,
			keyGitlabUrl: gitlabURL,
		},
	)

	return err
}

func (ge *gitlabExporter) getGitlabLabelID(label string) (string, error) {
	id, ok := ge.cachedLabels[label]
	if !ok {
		return "", fmt.Errorf("non cached label")
	}

	return id, nil
}

// get label from gitlab
func (ge *gitlabExporter) loadLabelsFromGitlab(client *gitlab.Client) error {

	labels, _, err := client.Labels.ListLabels(
		ge.repositoryID,
		&gitlab.ListLabelsOptions{
			Page: 0,
		},
	)

	if err != nil {
		return err
	}

	for _, label := range labels {
		ge.cachedLabels[label.Name] = strconv.Itoa(label.ID)
	}

	for page := 2; len(labels) != 0; page++ {

		labels, _, err = client.Labels.ListLabels(
			ge.repositoryID,
			&gitlab.ListLabelsOptions{
				Page: page,
			},
		)

		if err != nil {
			return err
		}

		for _, label := range labels {
			ge.cachedLabels[label.Name] = strconv.Itoa(label.ID)
		}
	}

	return nil
}

func (ge *gitlabExporter) createGitlabLabel(gc *gitlab.Client, label bug.Label) (string, error) {
	client := buildClient(ge.conf[keyToken])

	// RGBA to hex color
	rgba := label.RGBA()
	hexColor := fmt.Sprintf("%.2x%.2x%.2x", rgba.R, rgba.G, rgba.B)
	name := label.String()

	_, _, err := client.Labels.CreateLabel(ge.repositoryID, &gitlab.CreateLabelOptions{
		Name:  &name,
		Color: &hexColor,
	})

	if err != nil {
		return "", err
	}

	return "", nil
}

func (ge *gitlabExporter) getLabelsIDs(gc *gitlab.Client, repositoryID string, labels []bug.Label) ([]string, error) {
	return []string{}, nil
}

// create a gitlab. issue and return it ID
func createGitlabIssue(gc *gitlab.Client, repositoryID, title, body string) (string, string, error) {
	return "", "", nil

}

// add a comment to an issue and return it ID
func addCommentGitlabIssue(gc *gitlab.Client, subjectID string, body string) (string, string, error) {
	return "", "", nil
}

func editCommentGitlabIssue(gc *gitlab.Client, commentID, body string) (string, string, error) {
	return "", "", nil
}

func updateGitlabIssueStatus(gc *gitlab.Client, id string, status bug.Status) error {
	return nil
}

func updateGitlabIssueBody(gc *gitlab.Client, id string, body string) error {
	return nil
}

func updateGitlabIssueTitle(gc *gitlab.Client, id, title string) error {
	return nil
}

// update gitlab. issue labels
func (ge *gitlabExporter) updateGitlabIssueLabels(gc *gitlab.Client, labelableID string, added, removed []bug.Label) error {
	return nil
}