import.go

  1package core
  2
  3import (
  4	"fmt"
  5
  6	"github.com/MichaelMure/git-bug/entity"
  7)
  8
  9type ImportEvent int
 10
 11const (
 12	_ ImportEvent = iota
 13
 14	// Bug has been created
 15	ImportEventBug
 16	// Comment has been created
 17	ImportEventComment
 18	// Comment has been edited
 19	ImportEventCommentEdition
 20	// Bug's status has changed
 21	ImportEventStatusChange
 22	// Bug's title has changed
 23	ImportEventTitleEdition
 24	// Bug's labels changed
 25	ImportEventLabelChange
 26	// Nothing happened on a Bug
 27	ImportEventNothing
 28
 29	// Identity has been created
 30	ImportEventIdentity
 31
 32	// Error happened during import
 33	ImportEventError
 34)
 35
 36// ImportResult is an event that is emitted during the import process, to
 37// allow calling code to report on what is happening, collect metrics or
 38// display meaningful errors if something went wrong.
 39type ImportResult struct {
 40	Err    error
 41	Event  ImportEvent
 42	ID     entity.Id
 43	Reason string
 44}
 45
 46func (er ImportResult) String() string {
 47	switch er.Event {
 48	case ImportEventBug:
 49		return fmt.Sprintf("new issue: %s", er.ID)
 50	case ImportEventComment:
 51		return fmt.Sprintf("new comment: %s", er.ID)
 52	case ImportEventCommentEdition:
 53		return fmt.Sprintf("updated comment: %s", er.ID)
 54	case ImportEventStatusChange:
 55		return fmt.Sprintf("changed status: %s", er.ID)
 56	case ImportEventTitleEdition:
 57		return fmt.Sprintf("changed title: %s", er.ID)
 58	case ImportEventLabelChange:
 59		return fmt.Sprintf("changed label: %s", er.ID)
 60	case ImportEventIdentity:
 61		return fmt.Sprintf("new identity: %s", er.ID)
 62	case ImportEventNothing:
 63		if er.ID != "" {
 64			return fmt.Sprintf("no action taken for event %s: %s", er.ID, er.Reason)
 65		}
 66		return fmt.Sprintf("no action taken: %s", er.Reason)
 67	case ImportEventError:
 68		if er.ID != "" {
 69			return fmt.Sprintf("import error at id %s: %s", er.ID, er.Err.Error())
 70		}
 71		return fmt.Sprintf("import error: %s", er.Err.Error())
 72	default:
 73		panic("unknown import result")
 74	}
 75}
 76
 77func NewImportError(err error, id entity.Id) ImportResult {
 78	return ImportResult{
 79		Err:   err,
 80		ID:    id,
 81		Event: ImportEventError,
 82	}
 83}
 84
 85func NewImportNothing(id entity.Id, reason string) ImportResult {
 86	return ImportResult{
 87		ID:     id,
 88		Reason: reason,
 89		Event:  ImportEventNothing,
 90	}
 91}
 92
 93func NewImportBug(id entity.Id) ImportResult {
 94	return ImportResult{
 95		ID:    id,
 96		Event: ImportEventBug,
 97	}
 98}
 99
100func NewImportComment(id entity.Id) ImportResult {
101	return ImportResult{
102		ID:    id,
103		Event: ImportEventComment,
104	}
105}
106
107func NewImportCommentEdition(id entity.Id) ImportResult {
108	return ImportResult{
109		ID:    id,
110		Event: ImportEventCommentEdition,
111	}
112}
113
114func NewImportStatusChange(id entity.Id) ImportResult {
115	return ImportResult{
116		ID:    id,
117		Event: ImportEventStatusChange,
118	}
119}
120
121func NewImportLabelChange(id entity.Id) ImportResult {
122	return ImportResult{
123		ID:    id,
124		Event: ImportEventLabelChange,
125	}
126}
127
128func NewImportTitleEdition(id entity.Id) ImportResult {
129	return ImportResult{
130		ID:    id,
131		Event: ImportEventTitleEdition,
132	}
133}
134
135func NewImportIdentity(id entity.Id) ImportResult {
136	return ImportResult{
137		ID:    id,
138		Event: ImportEventIdentity,
139	}
140}