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	// Something wrong happened during import that is worth notifying to the user
 36	// but not severe enough to consider the import a failure.
 37	ImportEventWarning
 38)
 39
 40// ImportResult is an event that is emitted during the import process, to
 41// allow calling code to report on what is happening, collect metrics or
 42// display meaningful errors if something went wrong.
 43type ImportResult struct {
 44	Err    error
 45	Event  ImportEvent
 46	ID     entity.Id
 47	Reason string
 48}
 49
 50func (er ImportResult) String() string {
 51	switch er.Event {
 52	case ImportEventBug:
 53		return fmt.Sprintf("new issue: %s", er.ID)
 54	case ImportEventComment:
 55		return fmt.Sprintf("new comment: %s", er.ID)
 56	case ImportEventCommentEdition:
 57		return fmt.Sprintf("updated comment: %s", er.ID)
 58	case ImportEventStatusChange:
 59		return fmt.Sprintf("changed status: %s", er.ID)
 60	case ImportEventTitleEdition:
 61		return fmt.Sprintf("changed title: %s", er.ID)
 62	case ImportEventLabelChange:
 63		return fmt.Sprintf("changed label: %s", er.ID)
 64	case ImportEventIdentity:
 65		return fmt.Sprintf("new identity: %s", er.ID)
 66	case ImportEventNothing:
 67		if er.ID != "" {
 68			return fmt.Sprintf("no action taken for event %s: %s", er.ID, er.Reason)
 69		}
 70		return fmt.Sprintf("no action taken: %s", er.Reason)
 71	case ImportEventError:
 72		if er.ID != "" {
 73			return fmt.Sprintf("import error at id %s: %s", er.ID, er.Err.Error())
 74		}
 75		return fmt.Sprintf("import error: %s", er.Err.Error())
 76	case ImportEventWarning:
 77		if er.ID != "" {
 78			return fmt.Sprintf("warning at id %s: %s", er.ID, er.Err.Error())
 79		}
 80		return fmt.Sprintf("warning: %s", er.Err.Error())
 81
 82	default:
 83		panic("unknown import result")
 84	}
 85}
 86
 87func NewImportError(err error, id entity.Id) ImportResult {
 88	return ImportResult{
 89		Err:   err,
 90		ID:    id,
 91		Event: ImportEventError,
 92	}
 93}
 94
 95func NewImportWarning(err error, id entity.Id) ImportResult {
 96	return ImportResult{
 97		Err:   err,
 98		ID:    id,
 99		Event: ImportEventWarning,
100	}
101}
102
103func NewImportNothing(id entity.Id, reason string) ImportResult {
104	return ImportResult{
105		ID:     id,
106		Reason: reason,
107		Event:  ImportEventNothing,
108	}
109}
110
111func NewImportBug(id entity.Id) ImportResult {
112	return ImportResult{
113		ID:    id,
114		Event: ImportEventBug,
115	}
116}
117
118func NewImportComment(id entity.Id) ImportResult {
119	return ImportResult{
120		ID:    id,
121		Event: ImportEventComment,
122	}
123}
124
125func NewImportCommentEdition(id entity.Id) ImportResult {
126	return ImportResult{
127		ID:    id,
128		Event: ImportEventCommentEdition,
129	}
130}
131
132func NewImportStatusChange(id entity.Id) ImportResult {
133	return ImportResult{
134		ID:    id,
135		Event: ImportEventStatusChange,
136	}
137}
138
139func NewImportLabelChange(id entity.Id) ImportResult {
140	return ImportResult{
141		ID:    id,
142		Event: ImportEventLabelChange,
143	}
144}
145
146func NewImportTitleEdition(id entity.Id) ImportResult {
147	return ImportResult{
148		ID:    id,
149		Event: ImportEventTitleEdition,
150	}
151}
152
153func NewImportIdentity(id entity.Id) ImportResult {
154	return ImportResult{
155		ID:    id,
156		Event: ImportEventIdentity,
157	}
158}