import.go

  1package core
  2
  3import (
  4	"fmt"
  5	"strings"
  6
  7	"github.com/MichaelMure/git-bug/entity"
  8)
  9
 10type ImportEvent int
 11
 12const (
 13	_ ImportEvent = iota
 14
 15	// Bug has been created
 16	ImportEventBug
 17	// Comment has been created
 18	ImportEventComment
 19	// Comment has been edited
 20	ImportEventCommentEdition
 21	// Bug's status has changed
 22	ImportEventStatusChange
 23	// Bug's title has changed
 24	ImportEventTitleEdition
 25	// Bug's labels changed
 26	ImportEventLabelChange
 27	// Nothing happened on a Bug
 28	ImportEventNothing
 29
 30	// Identity has been created
 31	ImportEventIdentity
 32
 33	// Something wrong happened during import that is worth notifying to the user
 34	// but not severe enough to consider the import a failure.
 35	ImportEventWarning
 36
 37	// The import system (web API) has reached the rate limit
 38	ImportEventRateLimiting
 39
 40	// Error happened during import
 41	ImportEventError
 42)
 43
 44// ImportResult is an event that is emitted during the import process, to
 45// allow calling code to report on what is happening, collect metrics or
 46// display meaningful errors if something went wrong.
 47type ImportResult struct {
 48	Err    error
 49	Event  ImportEvent
 50	ID     entity.Id
 51	Reason string
 52}
 53
 54func (er ImportResult) String() string {
 55	switch er.Event {
 56	case ImportEventBug:
 57		return fmt.Sprintf("new issue: %s", er.ID)
 58	case ImportEventComment:
 59		return fmt.Sprintf("new comment: %s", er.ID)
 60	case ImportEventCommentEdition:
 61		return fmt.Sprintf("updated comment: %s", er.ID)
 62	case ImportEventStatusChange:
 63		return fmt.Sprintf("changed status: %s", er.ID)
 64	case ImportEventTitleEdition:
 65		return fmt.Sprintf("changed title: %s", er.ID)
 66	case ImportEventLabelChange:
 67		return fmt.Sprintf("changed label: %s", er.ID)
 68	case ImportEventIdentity:
 69		return fmt.Sprintf("new identity: %s", er.ID)
 70	case ImportEventNothing:
 71		if er.ID != "" {
 72			return fmt.Sprintf("no action taken for event %s: %s", er.ID, er.Reason)
 73		}
 74		return fmt.Sprintf("no action taken: %s", er.Reason)
 75	case ImportEventError:
 76		if er.ID != "" {
 77			return fmt.Sprintf("import error at id %s: %s", er.ID, er.Err.Error())
 78		}
 79		return fmt.Sprintf("import error: %s", er.Err.Error())
 80	case ImportEventWarning:
 81		parts := make([]string, 0, 4)
 82		parts = append(parts, "warning:")
 83		if er.ID != "" {
 84			parts = append(parts, fmt.Sprintf("at id %s", er.ID))
 85		}
 86		if er.Reason != "" {
 87			parts = append(parts, fmt.Sprintf("reason: %s", er.Reason))
 88		}
 89		if er.Err != nil {
 90			parts = append(parts, fmt.Sprintf("err: %s", er.Err))
 91		}
 92		return strings.Join(parts, " ")
 93	case ImportEventRateLimiting:
 94		return fmt.Sprintf("rate limiting: %s", er.Reason)
 95
 96	default:
 97		panic("unknown import result")
 98	}
 99}
100
101func NewImportError(err error, id entity.Id) ImportResult {
102	return ImportResult{
103		Err:   err,
104		ID:    id,
105		Event: ImportEventError,
106	}
107}
108
109func NewImportWarning(err error, id entity.Id) ImportResult {
110	return ImportResult{
111		Err:   err,
112		ID:    id,
113		Event: ImportEventWarning,
114	}
115}
116
117func NewImportNothing(id entity.Id, reason string) ImportResult {
118	return ImportResult{
119		ID:     id,
120		Reason: reason,
121		Event:  ImportEventNothing,
122	}
123}
124
125func NewImportBug(id entity.Id) ImportResult {
126	return ImportResult{
127		ID:    id,
128		Event: ImportEventBug,
129	}
130}
131
132func NewImportComment(id entity.Id) ImportResult {
133	return ImportResult{
134		ID:    id,
135		Event: ImportEventComment,
136	}
137}
138
139func NewImportCommentEdition(id entity.Id) ImportResult {
140	return ImportResult{
141		ID:    id,
142		Event: ImportEventCommentEdition,
143	}
144}
145
146func NewImportStatusChange(id entity.Id) ImportResult {
147	return ImportResult{
148		ID:    id,
149		Event: ImportEventStatusChange,
150	}
151}
152
153func NewImportLabelChange(id entity.Id) ImportResult {
154	return ImportResult{
155		ID:    id,
156		Event: ImportEventLabelChange,
157	}
158}
159
160func NewImportTitleEdition(id entity.Id) ImportResult {
161	return ImportResult{
162		ID:    id,
163		Event: ImportEventTitleEdition,
164	}
165}
166
167func NewImportIdentity(id entity.Id) ImportResult {
168	return ImportResult{
169		ID:    id,
170		Event: ImportEventIdentity,
171	}
172}
173
174func NewImportRateLimiting(msg string) ImportResult {
175	return ImportResult{
176		Reason: msg,
177		Event:  ImportEventRateLimiting,
178	}
179}