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