aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/jira/import.go
blob: 3fcac921703456258849ff412155018de729b7f4 (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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package jira

import (
	"context"
	"fmt"
	"net/http"
	"sort"
	"strings"
	"time"

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

const (
	keyOrigin          = "origin"
	keyJiraID          = "jira-id"
	keyJiraOperationID = "jira-derived-id"
	keyJiraKey         = "jira-key"
	keyJiraUser        = "jira-user"
	keyJiraProject     = "jira-project"
	keyJiraExportTime  = "jira-export-time"
	defaultPageSize    = 10
)

// jiraImporter implement the Importer interface
type jiraImporter struct {
	conf core.Configuration

	// send only channel
	out chan<- core.ImportResult
}

// Init .
func (gi *jiraImporter) Init(conf core.Configuration) error {
	gi.conf = conf
	return nil
}

// ImportAll iterate over all the configured repository issues and ensure the
// creation of the missing issues / timeline items / edits / label events ...
func (self *jiraImporter) ImportAll(
	ctx context.Context, repo *cache.RepoCache, since time.Time) (
	<-chan core.ImportResult, error) {

	sinceStr := since.Format("2006-01-02 15:04")
	serverURL := self.conf[keyServer]
	project := self.conf[keyProject]
	// TODO(josh)[da52062]: Validate token and if it is expired then prompt for
	// credentials and generate a new one
	out := make(chan core.ImportResult)
	self.out = out

	go func() {
		defer close(self.out)

		client := NewClient(serverURL, &ctx)
		err := client.Login(self.conf)
		if err != nil {
			out <- core.NewImportError(err, "")
			return
		}

		message, err := client.Search(
			fmt.Sprintf("project=%s AND updatedDate>\"%s\"", project, sinceStr), 0, 0)
		if err != nil {
			out <- core.NewImportError(err, "")
			return
		}

		fmt.Printf("So far so good. Have %d issues to import\n", message.Total)

		jql := fmt.Sprintf("project=%s AND updatedDate>\"%s\"", project, sinceStr)
		var searchIter *SearchIterator
		for searchIter =
			client.IterSearch(jql, defaultPageSize); searchIter.HasNext(); {
			issue := searchIter.Next()
			bug, err := self.ensureIssue(repo, *issue)
			if err != nil {
				err := fmt.Errorf("issue creation: %v", err)
				out <- core.NewImportError(err, "")
				return
			}

			var commentIter *CommentIterator
			for commentIter =
				client.IterComments(issue.ID, defaultPageSize); commentIter.HasNext(); {
				comment := commentIter.Next()
				err := self.ensureComment(repo, bug, *comment)
				if err != nil {
					out <- core.NewImportError(err, "")
				}
			}
			if commentIter.HasError() {
				out <- core.NewImportError(commentIter.Err, "")
			}

			snapshot := bug.Snapshot()
			opIdx := 0

			var changelogIter *ChangeLogIterator
			for changelogIter =
				client.IterChangeLog(issue.ID, defaultPageSize); changelogIter.HasNext(); {
				changelogEntry := changelogIter.Next()

				// Advance the operation iterator up to the first operation which has
				// an export date not before the changelog entry date. If the changelog
				// entry was created in response to an exported operation, then this
				// will be that operation.
				var exportTime time.Time
				for ; opIdx < len(snapshot.Operations); opIdx++ {
					exportTimeStr, hasTime := snapshot.Operations[opIdx].GetMetadata(
						keyJiraExportTime)
					if !hasTime {
						continue
					}
					exportTime, err = http.ParseTime(exportTimeStr)
					if err != nil {
						continue
					}
					if !exportTime.Before(changelogEntry.Created.Time) {
						break
					}
				}
				if opIdx < len(snapshot.Operations) {
					err = self.ensureChange(
						repo, bug, *changelogEntry, snapshot.Operations[opIdx])
				} else {
					err = self.ensureChange(repo, bug, *changelogEntry, nil)
				}
				if err != nil {
					out <- core.NewImportError(err, "")
				}

			}
			if changelogIter.HasError() {
				out <- core.NewImportError(changelogIter.Err, "")
			}

			if err := bug.CommitAsNeeded(); err != nil {
				err = fmt.Errorf("bug commit: %v", err)
				out <- core.NewImportError(err, "")
				return
			}
		}
		if searchIter.HasError() {
			out <- core.NewImportError(searchIter.Err, "")
		}
	}()

	return out, nil
}

// Create a bug.Person from a JIRA user
func (self *jiraImporter) ensurePerson(
	repo *cache.RepoCache, user User) (*cache.IdentityCache, error) {

	// Look first in the cache
	i, err := repo.ResolveIdentityImmutableMetadata(
		keyJiraUser, string(user.Key))
	if err == nil {
		return i, nil
	}
	if _, ok := err.(entity.ErrMultipleMatch); ok {
		return nil, err
	}

	i, err = repo.NewIdentityRaw(
		user.DisplayName,
		user.EmailAddress,
		user.Key,
		"",
		map[string]string{
			keyJiraUser: string(user.Key),
		},
	)

	if err != nil {
		return nil, err
	}

	self.out <- core.NewImportIdentity(i.Id())
	return i, nil
}

// Create a bug.Bug based from a JIRA issue
func (self *jiraImporter) ensureIssue(
	repo *cache.RepoCache, issue Issue) (*cache.BugCache, error) {
	author, err := self.ensurePerson(repo, issue.Fields.Creator)
	if err != nil {
		return nil, err
	}

	// TODO(josh)[f8808eb]: Consider looking up the git-bug entry directly from
	// the jira field which contains it, if we have a custom field configured
	// to store git-bug IDs.
	b, err := repo.ResolveBugCreateMetadata(keyJiraID, issue.ID)
	if err != nil && err != bug.ErrBugNotExist {
		return nil, err
	}

	if err == bug.ErrBugNotExist {
		cleanText, err := text.Cleanup(string(issue.Fields.Description))
		if err != nil {
			return nil, err
		}

		title := fmt.Sprintf("[%s]: %s", issue.Key, issue.Fields.Summary)
		b, _, err = repo.NewBugRaw(
			author,
			issue.Fields.Created.Unix(),
			title,
			cleanText,
			nil,
			map[string]string{
				keyOrigin:      target,
				keyJiraID:      issue.ID,
				keyJiraKey:     issue.Key,
				keyJiraProject: self.conf[keyProject],
			})
		if err != nil {
			return nil, err
		}

		self.out <- core.NewImportBug(b.Id())
	} else {
		self.out <- core.NewImportNothing("", "bug already imported")
	}

	return b, nil
}

// Return a unique string derived from a unique jira id and a timestamp
func getTimeDerivedID(jiraID string, timestamp MyTime) string {
	return fmt.Sprintf("%s-%d", jiraID, timestamp.Unix())
}

// Create a bug.Comment from a JIRA comment
func (self *jiraImporter) ensureComment(
	repo *cache.RepoCache, b *cache.BugCache, item Comment) error {
	// ensure person
	author, err := self.ensurePerson(repo, item.Author)
	if err != nil {
		return err
	}

	targetOpID, err := b.ResolveOperationWithMetadata(
		keyJiraID, item.ID)
	if err == nil {
		self.out <- core.NewImportNothing("", "comment already imported")
	} else if err != cache.ErrNoMatchingOp {
		return err
	}

	// If the comment is a new comment then create it
	if targetOpID == "" && err == cache.ErrNoMatchingOp {
		var cleanText string
		if item.Updated != item.Created {
			// We don't know the original text... we only have the updated text.
			cleanText = ""
		} else {
			cleanText, err = text.Cleanup(string(item.Body))
			if err != nil {
				return err
			}
		}

		// add comment operation
		op, err := b.AddCommentRaw(
			author,
			item.Created.Unix(),
			cleanText,
			nil,
			map[string]string{
				keyJiraID:      item.ID,
				keyJiraProject: self.conf[keyProject],
			},
		)
		if err != nil {
			return err
		}

		self.out <- core.NewImportComment(op.Id())
	}

	// If there are no updates to this comment, then we are done
	if item.Updated == item.Created {
		return nil
	}

	// If there has been an update to this comment, we try to find it in the
	// database. We need a unique id so we'll concat the issue id with the update
	// timestamp. Note that this must be consistent with the exporter during
	// export of an EditCommentOperation
	derivedID := getTimeDerivedID(item.ID, item.Updated)
	_, err = b.ResolveOperationWithMetadata(
		keyJiraID, derivedID)
	if err == nil {
		self.out <- core.NewImportNothing("", "update already imported")
	} else if err != cache.ErrNoMatchingOp {
		return err
	}

	// ensure editor identity
	editor, err := self.ensurePerson(repo, item.UpdateAuthor)
	if err != nil {
		return err
	}

	// comment edition
	cleanText, err := text.Cleanup(string(item.Body))
	if err != nil {
		return err
	}
	op, err := b.EditCommentRaw(
		editor,
		item.Updated.Unix(),
		target,
		cleanText,
		map[string]string{
			keyJiraID:      derivedID,
			keyJiraProject: self.conf[keyProject],
		},
	)

	if err != nil {
		return err
	}

	self.out <- core.NewImportCommentEdition(op.Id())

	return nil
}

// Return a unique string derived from a unique jira id and an index into the
// data referred to by that jira id.
func getIndexDerivedID(jiraID string, idx int) string {
	return fmt.Sprintf("%s-%d", jiraID, idx)
}

func labelSetsMatch(jiraSet []string, gitbugSet []bug.Label) bool {
	if len(jiraSet) != len(gitbugSet) {
		return false
	}

	sort.Strings(jiraSet)
	gitbugStrSet := make([]string, len(gitbugSet))
	for idx, label := range gitbugSet {
		gitbugStrSet[idx] = label.String()
	}
	sort.Strings(gitbugStrSet)

	for idx, value := range jiraSet {
		if value != gitbugStrSet[idx] {
			return false
		}
	}

	return true
}

// Create a bug.Operation (or a series of operations) from a JIRA changelog
// entry
func (self *jiraImporter) ensureChange(
	repo *cache.RepoCache, b *cache.BugCache, entry ChangeLogEntry,
	potentialOp bug.Operation) error {

	// If we have an operation which is already mapped to the entire changelog
	// entry then that means this changelog entry was induced by an export
	// operation and we've already done the match, so we skip this one
	_, err := b.ResolveOperationWithMetadata(keyJiraOperationID, entry.ID)
	if err == nil {
		self.out <- core.NewImportNothing(
			"", "changelog entry already matched to export")
		return nil
	} else if err != cache.ErrNoMatchingOp {
		return err
	}

	// In general, multiple fields may be changed in changelog entry  on
	// JIRA. For example, when an issue is closed both its "status" and its
	// "resolution" are updated within a single changelog entry.
	// I don't thing git-bug has a single operation to modify an arbitrary
	// number of fields in one go, so we break up the single JIRA changelog
	// entry into individual field updates.
	author, err := self.ensurePerson(repo, entry.Author)
	if err != nil {
		return err
	}

	if len(entry.Items) < 1 {
		return fmt.Errorf("Received changelog entry with no item! (%s)", entry.ID)
	}

	statusMap := getStatusMap(self.conf)

	// NOTE(josh): first do an initial scan and see if any of the changed items
	// matches the current potential operation. If it does, then we know that this
	// entire changelog entry was created in response to that git-bug operation.
	// So we associate the operation with the entire changelog, and not a specific
	// entry.
	for _, item := range entry.Items {
		switch item.Field {
		case "labels":
			// TODO(josh)[d7fd71c]: move set-symmetric-difference code to a helper
			// function. Probably in jira.go or something.
			fromLabels := strings.Split(item.FromString, " ")
			toLabels := strings.Split(item.ToString, " ")
			removedLabels, addedLabels, _ := setSymmetricDifference(
				fromLabels, toLabels)

			opr, isRightType := potentialOp.(*bug.LabelChangeOperation)
			if isRightType &&
				labelSetsMatch(addedLabels, opr.Added) &&
				labelSetsMatch(removedLabels, opr.Removed) {
				b.SetMetadata(opr.Id(), map[string]string{
					keyJiraOperationID: entry.ID,
				})
				self.out <- core.NewImportNothing("", "matched export")
				return nil
			}

		case "status":
			opr, isRightType := potentialOp.(*bug.SetStatusOperation)
			if isRightType && statusMap[opr.Status.String()] == item.ToString {
				_, err := b.SetMetadata(opr.Id(), map[string]string{
					keyJiraOperationID: entry.ID,
				})
				if err != nil {
					panic("Can't set metadata")
				}
				self.out <- core.NewImportNothing("", "matched export")
				return nil
			}

		case "summary":
			// NOTE(josh): JIRA calls it "summary", which sounds more like the body
			// text, but it's the title
			opr, isRightType := potentialOp.(*bug.SetTitleOperation)
			if isRightType && opr.Title == item.ToString {
				_, err := b.SetMetadata(opr.Id(), map[string]string{
					keyJiraOperationID: entry.ID,
				})
				if err != nil {
					panic("Can't set metadata")
				}
				self.out <- core.NewImportNothing("", "matched export")
				return nil
			}

		case "description":
			// NOTE(josh): JIRA calls it "description", which sounds more like the
			// title but it's actually the body
			opr, isRightType := potentialOp.(*bug.EditCommentOperation)
			if isRightType &&
				opr.Target == b.Snapshot().Operations[0].Id() &&
				opr.Message == item.ToString {
				_, err := b.SetMetadata(opr.Id(), map[string]string{
					keyJiraOperationID: entry.ID,
				})
				if err != nil {
					panic("Can't set metadata")
				}
				self.out <- core.NewImportNothing("", "matched export")
				return nil
			}
		}
	}

	// Since we didn't match the changelog entry to a known export operation,
	// then this is a changelog entry that we should import. We import each
	// changelog entry item as a separate git-bug operation.
	for idx, item := range entry.Items {
		derivedID := getIndexDerivedID(entry.ID, idx)
		_, err := b.ResolveOperationWithMetadata(keyJiraOperationID, derivedID)
		if err == nil {
			self.out <- core.NewImportNothing("", "update already imported")
			continue
		} else if err != cache.ErrNoMatchingOp {
			return err
		}

		switch item.Field {
		case "labels":
			// TODO(josh)[d7fd71c]: move set-symmetric-difference code to a helper
			// function. Probably in jira.go or something.
			fromLabels := strings.Split(item.FromString, " ")
			toLabels := strings.Split(item.ToString, " ")
			removedLabels, addedLabels, _ := setSymmetricDifference(
				fromLabels, toLabels)

			op, err := b.ForceChangeLabelsRaw(
				author,
				entry.Created.Unix(),
				addedLabels,
				removedLabels,
				map[string]string{
					keyJiraID:          entry.ID,
					keyJiraOperationID: derivedID,
					keyJiraProject:     self.conf[keyProject],
				},
			)
			if err != nil {
				return err
			}

			self.out <- core.NewImportLabelChange(op.Id())

		case "status":
			if statusMap[bug.OpenStatus.String()] == item.ToString {
				op, err := b.OpenRaw(
					author,
					entry.Created.Unix(),
					map[string]string{
						keyJiraID: entry.ID,

						keyJiraProject:     self.conf[keyProject],
						keyJiraOperationID: derivedID,
					},
				)
				if err != nil {
					return err
				}
				self.out <- core.NewImportStatusChange(op.Id())
			} else if statusMap[bug.ClosedStatus.String()] == item.ToString {
				op, err := b.CloseRaw(
					author,
					entry.Created.Unix(),
					map[string]string{
						keyJiraID: entry.ID,

						keyJiraProject:     self.conf[keyProject],
						keyJiraOperationID: derivedID,
					},
				)
				if err != nil {
					return err
				}
				self.out <- core.NewImportStatusChange(op.Id())
			} else {
				self.out <- core.NewImportNothing(
					"", fmt.Sprintf(
						"No git-bug status mapped for jira status %s", item.ToString))
			}

		case "summary":
			// NOTE(josh): JIRA calls it "summary", which sounds more like the body
			// text, but it's the title
			op, err := b.SetTitleRaw(
				author,
				entry.Created.Unix(),
				string(item.ToString),
				map[string]string{
					keyJiraID:          entry.ID,
					keyJiraOperationID: derivedID,
					keyJiraProject:     self.conf[keyProject],
				},
			)
			if err != nil {
				return err
			}

			self.out <- core.NewImportTitleEdition(op.Id())

		case "description":
			// NOTE(josh): JIRA calls it "description", which sounds more like the
			// title but it's actually the body
			op, err := b.EditBodyRaw(
				author,
				entry.Created.Unix(),
				string(item.ToString),
				map[string]string{
					keyJiraID:          entry.ID,
					keyJiraOperationID: derivedID,
					keyJiraProject:     self.conf[keyProject],
				},
			)
			if err != nil {
				return err
			}

			self.out <- core.NewImportCommentEdition(op.Id())
		}

		// Other Examples:
		// "assignee" (jira)
		// "Attachment" (jira)
		// "Epic Link" (custom)
		// "Rank" (custom)
		// "resolution" (jira)
		// "Sprint" (custom)
	}
	return nil
}

func getStatusMap(conf core.Configuration) map[string]string {
	var hasConf bool
	statusMap := make(map[string]string)
	statusMap[bug.OpenStatus.String()], hasConf = conf[keyMapOpenID]
	if !hasConf {
		// Default to "1" which is the built-in jira "Open" status
		statusMap[bug.OpenStatus.String()] = "1"
	}
	statusMap[bug.ClosedStatus.String()], hasConf = conf[keyMapCloseID]
	if !hasConf {
		// Default to "6" which is the built-in jira "Closed" status
		statusMap[bug.OpenStatus.String()] = "6"
	}
	return statusMap
}