import.go

  1package jira
  2
  3import (
  4	"context"
  5	"encoding/json"
  6	"fmt"
  7	"net/http"
  8	"sort"
  9	"strings"
 10	"time"
 11
 12	"github.com/MichaelMure/git-bug/bridge/core"
 13	"github.com/MichaelMure/git-bug/bridge/core/auth"
 14	"github.com/MichaelMure/git-bug/cache"
 15	"github.com/MichaelMure/git-bug/entities/bug"
 16	"github.com/MichaelMure/git-bug/entities/common"
 17	"github.com/MichaelMure/git-bug/entity"
 18	"github.com/MichaelMure/git-bug/entity/dag"
 19	"github.com/MichaelMure/git-bug/util/text"
 20)
 21
 22const (
 23	defaultPageSize = 10
 24)
 25
 26// jiraImporter implement the Importer interface
 27type jiraImporter struct {
 28	conf core.Configuration
 29
 30	client *Client
 31
 32	// send only channel
 33	out chan<- core.ImportResult
 34}
 35
 36// Init .
 37func (ji *jiraImporter) Init(ctx context.Context, repo *cache.RepoCache, conf core.Configuration) error {
 38	ji.conf = conf
 39
 40	var cred auth.Credential
 41
 42	// Prioritize LoginPassword credentials to avoid a prompt
 43	creds, err := auth.List(repo,
 44		auth.WithTarget(target),
 45		auth.WithKind(auth.KindLoginPassword),
 46		auth.WithMeta(auth.MetaKeyBaseURL, conf[confKeyBaseUrl]),
 47		auth.WithMeta(auth.MetaKeyLogin, conf[confKeyDefaultLogin]),
 48	)
 49	if err != nil {
 50		return err
 51	}
 52	if len(creds) > 0 {
 53		cred = creds[0]
 54		goto end
 55	}
 56
 57	creds, err = auth.List(repo,
 58		auth.WithTarget(target),
 59		auth.WithKind(auth.KindLogin),
 60		auth.WithMeta(auth.MetaKeyBaseURL, conf[confKeyBaseUrl]),
 61		auth.WithMeta(auth.MetaKeyLogin, conf[confKeyDefaultLogin]),
 62	)
 63	if err != nil {
 64		return err
 65	}
 66	if len(creds) > 0 {
 67		cred = creds[0]
 68	}
 69
 70end:
 71	if cred == nil {
 72		return fmt.Errorf("no credential for this bridge")
 73	}
 74
 75	// TODO(josh)[da52062]: Validate token and if it is expired then prompt for
 76	// credentials and generate a new one
 77	ji.client, err = buildClient(ctx, conf[confKeyBaseUrl], conf[confKeyCredentialType], cred)
 78	return err
 79}
 80
 81// ImportAll iterate over all the configured repository issues and ensure the
 82// creation of the missing issues / timeline items / edits / label events ...
 83func (ji *jiraImporter) ImportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ImportResult, error) {
 84	sinceStr := since.Format("2006-01-02 15:04")
 85	project := ji.conf[confKeyProject]
 86
 87	out := make(chan core.ImportResult)
 88	ji.out = out
 89
 90	go func() {
 91		defer close(ji.out)
 92
 93		message, err := ji.client.Search(
 94			fmt.Sprintf("project=%s AND updatedDate>\"%s\"", project, sinceStr), 0, 0)
 95		if err != nil {
 96			out <- core.NewImportError(err, "")
 97			return
 98		}
 99
100		fmt.Printf("So far so good. Have %d issues to import\n", message.Total)
101
102		jql := fmt.Sprintf("project=%s AND updatedDate>\"%s\"", project, sinceStr)
103		var searchIter *SearchIterator
104		for searchIter =
105			ji.client.IterSearch(jql, defaultPageSize); searchIter.HasNext(); {
106			issue := searchIter.Next()
107			b, err := ji.ensureIssue(repo, *issue)
108			if err != nil {
109				err := fmt.Errorf("issue creation: %v", err)
110				out <- core.NewImportError(err, "")
111				return
112			}
113
114			var commentIter *CommentIterator
115			for commentIter =
116				ji.client.IterComments(issue.ID, defaultPageSize); commentIter.HasNext(); {
117				comment := commentIter.Next()
118				err := ji.ensureComment(repo, b, *comment)
119				if err != nil {
120					out <- core.NewImportError(err, "")
121				}
122			}
123			if commentIter.HasError() {
124				out <- core.NewImportError(commentIter.Err, "")
125			}
126
127			snapshot := b.Snapshot()
128			opIdx := 0
129
130			var changelogIter *ChangeLogIterator
131			for changelogIter =
132				ji.client.IterChangeLog(issue.ID, defaultPageSize); changelogIter.HasNext(); {
133				changelogEntry := changelogIter.Next()
134
135				// Advance the operation iterator up to the first operation which has
136				// an export date not before the changelog entry date. If the changelog
137				// entry was created in response to an exported operation, then this
138				// will be that operation.
139				var exportTime time.Time
140				for ; opIdx < len(snapshot.Operations); opIdx++ {
141					exportTimeStr, hasTime := snapshot.Operations[opIdx].GetMetadata(
142						metaKeyJiraExportTime)
143					if !hasTime {
144						continue
145					}
146					exportTime, err = http.ParseTime(exportTimeStr)
147					if err != nil {
148						continue
149					}
150					if !exportTime.Before(changelogEntry.Created.Time) {
151						break
152					}
153				}
154				if opIdx < len(snapshot.Operations) {
155					err = ji.ensureChange(repo, b, *changelogEntry, snapshot.Operations[opIdx])
156				} else {
157					err = ji.ensureChange(repo, b, *changelogEntry, nil)
158				}
159				if err != nil {
160					out <- core.NewImportError(err, "")
161				}
162
163			}
164			if changelogIter.HasError() {
165				out <- core.NewImportError(changelogIter.Err, "")
166			}
167
168			if !b.NeedCommit() {
169				out <- core.NewImportNothing(b.Id(), "no imported operation")
170			} else if err := b.Commit(); err != nil {
171				err = fmt.Errorf("bug commit: %v", err)
172				out <- core.NewImportError(err, "")
173				return
174			}
175		}
176		if searchIter.HasError() {
177			out <- core.NewImportError(searchIter.Err, "")
178		}
179	}()
180
181	return out, nil
182}
183
184// Create a bug.Person from a JIRA user
185func (ji *jiraImporter) ensurePerson(repo *cache.RepoCache, user User) (*cache.IdentityCache, error) {
186	// Look first in the cache
187	i, err := repo.ResolveIdentityImmutableMetadata(
188		metaKeyJiraUser, string(user.Key))
189	if err == nil {
190		return i, nil
191	}
192	if _, ok := err.(entity.ErrMultipleMatch); ok {
193		return nil, err
194	}
195
196	i, err = repo.NewIdentityRaw(
197		user.DisplayName,
198		user.EmailAddress,
199		user.Key,
200		"",
201		nil,
202		map[string]string{
203			metaKeyJiraUser: user.Key,
204		},
205	)
206
207	if err != nil {
208		return nil, err
209	}
210
211	ji.out <- core.NewImportIdentity(i.Id())
212	return i, nil
213}
214
215// Create a bug.Bug based from a JIRA issue
216func (ji *jiraImporter) ensureIssue(repo *cache.RepoCache, issue Issue) (*cache.BugCache, error) {
217	author, err := ji.ensurePerson(repo, issue.Fields.Creator)
218	if err != nil {
219		return nil, err
220	}
221
222	b, err := repo.ResolveBugMatcher(func(excerpt *cache.BugExcerpt) bool {
223		if _, ok := excerpt.CreateMetadata[metaKeyJiraBaseUrl]; ok &&
224			excerpt.CreateMetadata[metaKeyJiraBaseUrl] != ji.conf[confKeyBaseUrl] {
225			return false
226		}
227
228		return excerpt.CreateMetadata[core.MetaKeyOrigin] == target &&
229			excerpt.CreateMetadata[metaKeyJiraId] == issue.ID &&
230			excerpt.CreateMetadata[metaKeyJiraProject] == ji.conf[confKeyProject]
231	})
232	if err != nil && err != bug.ErrBugNotExist {
233		return nil, err
234	}
235
236	if err == bug.ErrBugNotExist {
237		b, _, err = repo.NewBugRaw(
238			author,
239			issue.Fields.Created.Unix(),
240			text.CleanupOneLine(issue.Fields.Summary),
241			text.Cleanup(issue.Fields.Description),
242			nil,
243			map[string]string{
244				core.MetaKeyOrigin: target,
245				metaKeyJiraId:      issue.ID,
246				metaKeyJiraKey:     issue.Key,
247				metaKeyJiraProject: ji.conf[confKeyProject],
248				metaKeyJiraBaseUrl: ji.conf[confKeyBaseUrl],
249			})
250		if err != nil {
251			return nil, err
252		}
253
254		ji.out <- core.NewImportBug(b.Id())
255	}
256
257	return b, nil
258}
259
260// Return a unique string derived from a unique jira id and a timestamp
261func getTimeDerivedID(jiraID string, timestamp Time) string {
262	return fmt.Sprintf("%s-%d", jiraID, timestamp.Unix())
263}
264
265// Create a bug.Comment from a JIRA comment
266func (ji *jiraImporter) ensureComment(repo *cache.RepoCache, b *cache.BugCache, item Comment) error {
267	// ensure person
268	author, err := ji.ensurePerson(repo, item.Author)
269	if err != nil {
270		return err
271	}
272
273	targetOpID, err := b.ResolveOperationWithMetadata(metaKeyJiraId, item.ID)
274	if err != nil && err != cache.ErrNoMatchingOp {
275		return err
276	}
277
278	// If the comment is a new comment then create it
279	if targetOpID == "" && err == cache.ErrNoMatchingOp {
280		var cleanText string
281		if item.Updated != item.Created {
282			// We don't know the original text... we only have the updated text.
283			cleanText = ""
284		} else {
285			cleanText = text.Cleanup(item.Body)
286		}
287
288		// add comment operation
289		op, err := b.AddCommentRaw(
290			author,
291			item.Created.Unix(),
292			cleanText,
293			nil,
294			map[string]string{
295				metaKeyJiraId: item.ID,
296			},
297		)
298		if err != nil {
299			return err
300		}
301
302		ji.out <- core.NewImportComment(op.Id())
303		targetOpID = op.Id()
304	}
305
306	// If there are no updates to this comment, then we are done
307	if item.Updated == item.Created {
308		return nil
309	}
310
311	// If there has been an update to this comment, we try to find it in the
312	// database. We need a unique id so we'll concat the issue id with the update
313	// timestamp. Note that this must be consistent with the exporter during
314	// export of an EditCommentOperation
315	derivedID := getTimeDerivedID(item.ID, item.Updated)
316	_, err = b.ResolveOperationWithMetadata(metaKeyJiraId, derivedID)
317	if err == nil {
318		// Already imported this edition
319		return nil
320	}
321
322	if err != cache.ErrNoMatchingOp {
323		return err
324	}
325
326	// ensure editor identity
327	editor, err := ji.ensurePerson(repo, item.UpdateAuthor)
328	if err != nil {
329		return err
330	}
331
332	// comment edition
333	op, err := b.EditCommentRaw(
334		editor,
335		item.Updated.Unix(),
336		entity.CombineIds(b.Id(), targetOpID),
337		text.Cleanup(item.Body),
338		map[string]string{
339			metaKeyJiraId: derivedID,
340		},
341	)
342
343	if err != nil {
344		return err
345	}
346
347	ji.out <- core.NewImportCommentEdition(op.Id())
348
349	return nil
350}
351
352// Return a unique string derived from a unique jira id and an index into the
353// data referred to by that jira id.
354func getIndexDerivedID(jiraID string, idx int) string {
355	return fmt.Sprintf("%s-%d", jiraID, idx)
356}
357
358func labelSetsMatch(jiraSet []string, gitbugSet []bug.Label) bool {
359	if len(jiraSet) != len(gitbugSet) {
360		return false
361	}
362
363	sort.Strings(jiraSet)
364	gitbugStrSet := make([]string, len(gitbugSet))
365	for idx, label := range gitbugSet {
366		gitbugStrSet[idx] = label.String()
367	}
368	sort.Strings(gitbugStrSet)
369
370	for idx, value := range jiraSet {
371		if value != gitbugStrSet[idx] {
372			return false
373		}
374	}
375
376	return true
377}
378
379// Create a bug.Operation (or a series of operations) from a JIRA changelog
380// entry
381func (ji *jiraImporter) ensureChange(repo *cache.RepoCache, b *cache.BugCache, entry ChangeLogEntry, potentialOp dag.Operation) error {
382
383	// If we have an operation which is already mapped to the entire changelog
384	// entry then that means this changelog entry was induced by an export
385	// operation and we've already done the match, so we skip this one
386	_, err := b.ResolveOperationWithMetadata(metaKeyJiraDerivedId, entry.ID)
387	if err == nil {
388		return nil
389	} else if err != cache.ErrNoMatchingOp {
390		return err
391	}
392
393	// In general, multiple fields may be changed in changelog entry  on
394	// JIRA. For example, when an issue is closed both its "status" and its
395	// "resolution" are updated within a single changelog entry.
396	// I don't thing git-bug has a single operation to modify an arbitrary
397	// number of fields in one go, so we break up the single JIRA changelog
398	// entry into individual field updates.
399	author, err := ji.ensurePerson(repo, entry.Author)
400	if err != nil {
401		return err
402	}
403
404	if len(entry.Items) < 1 {
405		return fmt.Errorf("Received changelog entry with no item! (%s)", entry.ID)
406	}
407
408	statusMap, err := getStatusMapReverse(ji.conf)
409	if err != nil {
410		return err
411	}
412
413	// NOTE(josh): first do an initial scan and see if any of the changed items
414	// matches the current potential operation. If it does, then we know that this
415	// entire changelog entry was created in response to that git-bug operation.
416	// So we associate the operation with the entire changelog, and not a specific
417	// entry.
418	for _, item := range entry.Items {
419		switch item.Field {
420		case "labels":
421			fromLabels := removeEmpty(strings.Split(item.FromString, " "))
422			toLabels := removeEmpty(strings.Split(item.ToString, " "))
423			removedLabels, addedLabels, _ := setSymmetricDifference(fromLabels, toLabels)
424
425			opr, isRightType := potentialOp.(*bug.LabelChangeOperation)
426			if isRightType && labelSetsMatch(addedLabels, opr.Added) && labelSetsMatch(removedLabels, opr.Removed) {
427				_, err := b.SetMetadata(opr.Id(), map[string]string{
428					metaKeyJiraDerivedId: entry.ID,
429				})
430				if err != nil {
431					return err
432				}
433				return nil
434			}
435
436		case "status":
437			opr, isRightType := potentialOp.(*bug.SetStatusOperation)
438			if isRightType && statusMap[opr.Status.String()] == item.To {
439				_, err := b.SetMetadata(opr.Id(), map[string]string{
440					metaKeyJiraDerivedId: entry.ID,
441				})
442				if err != nil {
443					return err
444				}
445				return nil
446			}
447
448		case "summary":
449			// NOTE(josh): JIRA calls it "summary", which sounds more like the body
450			// text, but it's the title
451			opr, isRightType := potentialOp.(*bug.SetTitleOperation)
452			if isRightType && opr.Title == item.To {
453				_, err := b.SetMetadata(opr.Id(), map[string]string{
454					metaKeyJiraDerivedId: entry.ID,
455				})
456				if err != nil {
457					return err
458				}
459				return nil
460			}
461
462		case "description":
463			// NOTE(josh): JIRA calls it "description", which sounds more like the
464			// title but it's actually the body
465			opr, isRightType := potentialOp.(*bug.EditCommentOperation)
466			if isRightType &&
467				opr.Target == b.Snapshot().Operations[0].Id() &&
468				opr.Message == item.ToString {
469				_, err := b.SetMetadata(opr.Id(), map[string]string{
470					metaKeyJiraDerivedId: entry.ID,
471				})
472				if err != nil {
473					return err
474				}
475				return nil
476			}
477		}
478	}
479
480	// Since we didn't match the changelog entry to a known export operation,
481	// then this is a changelog entry that we should import. We import each
482	// changelog entry item as a separate git-bug operation.
483	for idx, item := range entry.Items {
484		derivedID := getIndexDerivedID(entry.ID, idx)
485		_, err := b.ResolveOperationWithMetadata(metaKeyJiraDerivedId, derivedID)
486		if err == nil {
487			continue
488		}
489		if err != cache.ErrNoMatchingOp {
490			return err
491		}
492
493		switch item.Field {
494		case "labels":
495			fromLabels := removeEmpty(strings.Split(item.FromString, " "))
496			toLabels := removeEmpty(strings.Split(item.ToString, " "))
497			removedLabels, addedLabels, _ := setSymmetricDifference(fromLabels, toLabels)
498
499			op, err := b.ForceChangeLabelsRaw(
500				author,
501				entry.Created.Unix(),
502				text.CleanupOneLineArray(addedLabels),
503				text.CleanupOneLineArray(removedLabels),
504				map[string]string{
505					metaKeyJiraId:        entry.ID,
506					metaKeyJiraDerivedId: derivedID,
507				},
508			)
509			if err != nil {
510				return err
511			}
512
513			ji.out <- core.NewImportLabelChange(op.Id())
514
515		case "status":
516			statusStr, hasMap := statusMap[item.To]
517			if hasMap {
518				switch statusStr {
519				case common.OpenStatus.String():
520					op, err := b.OpenRaw(
521						author,
522						entry.Created.Unix(),
523						map[string]string{
524							metaKeyJiraId:        entry.ID,
525							metaKeyJiraDerivedId: derivedID,
526						},
527					)
528					if err != nil {
529						return err
530					}
531					ji.out <- core.NewImportStatusChange(op.Id())
532
533				case common.ClosedStatus.String():
534					op, err := b.CloseRaw(
535						author,
536						entry.Created.Unix(),
537						map[string]string{
538							metaKeyJiraId:        entry.ID,
539							metaKeyJiraDerivedId: derivedID,
540						},
541					)
542					if err != nil {
543						return err
544					}
545					ji.out <- core.NewImportStatusChange(op.Id())
546				}
547			} else {
548				ji.out <- core.NewImportError(
549					fmt.Errorf(
550						"No git-bug status mapped for jira status %s (%s)",
551						item.ToString, item.To), "")
552			}
553
554		case "summary":
555			// NOTE(josh): JIRA calls it "summary", which sounds more like the body
556			// text, but it's the title
557			op, err := b.SetTitleRaw(
558				author,
559				entry.Created.Unix(),
560				text.CleanupOneLine(item.ToString),
561				map[string]string{
562					metaKeyJiraId:        entry.ID,
563					metaKeyJiraDerivedId: derivedID,
564				},
565			)
566			if err != nil {
567				return err
568			}
569
570			ji.out <- core.NewImportTitleEdition(op.Id())
571
572		case "description":
573			// NOTE(josh): JIRA calls it "description", which sounds more like the
574			// title but it's actually the body
575			op, err := b.EditCreateCommentRaw(
576				author,
577				entry.Created.Unix(),
578				text.Cleanup(item.ToString),
579				map[string]string{
580					metaKeyJiraId:        entry.ID,
581					metaKeyJiraDerivedId: derivedID,
582				},
583			)
584			if err != nil {
585				return err
586			}
587
588			ji.out <- core.NewImportCommentEdition(op.Id())
589
590		default:
591			ji.out <- core.NewImportWarning(
592				fmt.Errorf(
593					"Unhandled changelog event %s", item.Field), "")
594		}
595
596		// Other Examples:
597		// "assignee" (jira)
598		// "Attachment" (jira)
599		// "Epic Link" (custom)
600		// "Rank" (custom)
601		// "resolution" (jira)
602		// "Sprint" (custom)
603	}
604	return nil
605}
606
607func getStatusMap(conf core.Configuration) (map[string]string, error) {
608	mapStr, hasConf := conf[confKeyIDMap]
609	if !hasConf {
610		return map[string]string{
611			common.OpenStatus.String():   "1",
612			common.ClosedStatus.String(): "6",
613		}, nil
614	}
615
616	statusMap := make(map[string]string)
617	err := json.Unmarshal([]byte(mapStr), &statusMap)
618	return statusMap, err
619}
620
621func getStatusMapReverse(conf core.Configuration) (map[string]string, error) {
622	fwdMap, err := getStatusMap(conf)
623	if err != nil {
624		return fwdMap, err
625	}
626
627	outMap := map[string]string{}
628	for key, val := range fwdMap {
629		outMap[val] = key
630	}
631
632	mapStr, hasConf := conf[confKeyIDRevMap]
633	if !hasConf {
634		return outMap, nil
635	}
636
637	revMap := make(map[string]string)
638	err = json.Unmarshal([]byte(mapStr), &revMap)
639	for key, val := range revMap {
640		outMap[key] = val
641	}
642
643	return outMap, err
644}
645
646func removeEmpty(values []string) []string {
647	output := make([]string, 0, len(values))
648	for _, value := range values {
649		value = strings.TrimSpace(value)
650		if value != "" {
651			output = append(output, value)
652		}
653	}
654	return output
655}