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	EntityId    entity.Id         // optional for err, warnings
 51	OperationId entity.Id         // optional
 52	ComponentId entity.CombinedId // optional
 53	Reason      string
 54}
 55
 56func (er ImportResult) String() string {
 57	switch er.Event {
 58	case ImportEventBug:
 59		return fmt.Sprintf("[%s] new issue: %s", er.EntityId.Human(), er.EntityId)
 60	case ImportEventComment:
 61		return fmt.Sprintf("[%s] new comment: %s", er.EntityId.Human(), er.ComponentId)
 62	case ImportEventCommentEdition:
 63		return fmt.Sprintf("[%s] updated comment: %s", er.EntityId.Human(), er.ComponentId)
 64	case ImportEventStatusChange:
 65		return fmt.Sprintf("[%s] changed status with op: %s", er.EntityId.Human(), er.OperationId)
 66	case ImportEventTitleEdition:
 67		return fmt.Sprintf("[%s] changed title with op: %s", er.EntityId.Human(), er.OperationId)
 68	case ImportEventLabelChange:
 69		return fmt.Sprintf("[%s] changed label with op: %s", er.EntityId.Human(), er.OperationId)
 70	case ImportEventIdentity:
 71		return fmt.Sprintf("[%s] new identity: %s", er.EntityId.Human(), er.EntityId)
 72	case ImportEventNothing:
 73		if er.EntityId != "" {
 74			return fmt.Sprintf("no action taken on entity %s: %s", er.EntityId, er.Reason)
 75		}
 76		return fmt.Sprintf("no action taken: %s", er.Reason)
 77	case ImportEventError:
 78		if er.EntityId != "" {
 79			return fmt.Sprintf("import error on entity %s: %s", er.EntityId, er.Err.Error())
 80		}
 81		return fmt.Sprintf("import error: %s", er.Err.Error())
 82	case ImportEventWarning:
 83		parts := make([]string, 0, 4)
 84		parts = append(parts, "warning:")
 85		if er.EntityId != "" {
 86			parts = append(parts, fmt.Sprintf("on entity %s", er.EntityId))
 87		}
 88		if er.Reason != "" {
 89			parts = append(parts, fmt.Sprintf("reason: %s", er.Reason))
 90		}
 91		if er.Err != nil {
 92			parts = append(parts, fmt.Sprintf("err: %s", er.Err))
 93		}
 94		return strings.Join(parts, " ")
 95	case ImportEventRateLimiting:
 96		return fmt.Sprintf("rate limiting: %s", er.Reason)
 97
 98	default:
 99		panic("unknown import result")
100	}
101}
102
103func NewImportError(err error, entityId entity.Id) ImportResult {
104	return ImportResult{
105		Err:      err,
106		EntityId: entityId,
107		Event:    ImportEventError,
108	}
109}
110
111func NewImportWarning(err error, entityId entity.Id) ImportResult {
112	return ImportResult{
113		Err:      err,
114		EntityId: entityId,
115		Event:    ImportEventWarning,
116	}
117}
118
119func NewImportNothing(entityId entity.Id, reason string) ImportResult {
120	return ImportResult{
121		EntityId: entityId,
122		Reason:   reason,
123		Event:    ImportEventNothing,
124	}
125}
126
127func NewImportBug(entityId entity.Id) ImportResult {
128	return ImportResult{
129		EntityId: entityId,
130		Event:    ImportEventBug,
131	}
132}
133
134func NewImportComment(entityId entity.Id, commentId entity.CombinedId) ImportResult {
135	return ImportResult{
136		EntityId:    entityId,
137		ComponentId: commentId,
138		Event:       ImportEventComment,
139	}
140}
141
142func NewImportCommentEdition(entityId entity.Id, commentId entity.CombinedId) ImportResult {
143	return ImportResult{
144		EntityId:    entityId,
145		ComponentId: commentId,
146		Event:       ImportEventCommentEdition,
147	}
148}
149
150func NewImportStatusChange(entityId entity.Id, opId entity.Id) ImportResult {
151	return ImportResult{
152		EntityId:    entityId,
153		OperationId: opId,
154		Event:       ImportEventStatusChange,
155	}
156}
157
158func NewImportLabelChange(entityId entity.Id, opId entity.Id) ImportResult {
159	return ImportResult{
160		EntityId:    entityId,
161		OperationId: opId,
162		Event:       ImportEventLabelChange,
163	}
164}
165
166func NewImportTitleEdition(entityId entity.Id, opId entity.Id) ImportResult {
167	return ImportResult{
168		EntityId:    entityId,
169		OperationId: opId,
170		Event:       ImportEventTitleEdition,
171	}
172}
173
174func NewImportIdentity(entityId entity.Id) ImportResult {
175	return ImportResult{
176		EntityId: entityId,
177		Event:    ImportEventIdentity,
178	}
179}
180
181func NewImportRateLimiting(msg string) ImportResult {
182	return ImportResult{
183		Reason: msg,
184		Event:  ImportEventRateLimiting,
185	}
186}