export.go

  1package core
  2
  3import (
  4	"fmt"
  5
  6	"github.com/MichaelMure/git-bug/entity"
  7)
  8
  9type ExportEvent int
 10
 11const (
 12	_ ExportEvent = iota
 13
 14	// Bug has been exported on the remote tracker
 15	ExportEventBug
 16	// Comment has been exported on the remote tracker
 17	ExportEventComment
 18	// Comment has been edited on the remote tracker
 19	ExportEventCommentEdition
 20	// Bug's status has been changed on on the remote tracker
 21	ExportEventStatusChange
 22	// Bug's title has been changed on the remote tracker
 23	ExportEventTitleEdition
 24	// Bug's labels have been changed on the remote tracker
 25	ExportEventLabelChange
 26
 27	// Nothing changed on the bug
 28	ExportEventNothing
 29
 30	// Something wrong happened during export that is worth notifying to the user
 31	// but not severe enough to consider the export a failure.
 32	ExportEventWarning
 33
 34	// Error happened during export
 35	ExportEventError
 36)
 37
 38// ExportResult is an event that is emitted during the export process, to
 39// allow calling code to report on what is happening, collect metrics or
 40// display meaningful errors if something went wrong.
 41type ExportResult struct {
 42	Err    error
 43	Event  ExportEvent
 44	ID     entity.Id
 45	Reason string
 46}
 47
 48func (er ExportResult) String() string {
 49	switch er.Event {
 50	case ExportEventBug:
 51		return fmt.Sprintf("new issue: %s", er.ID)
 52	case ExportEventComment:
 53		return fmt.Sprintf("new comment: %s", er.ID)
 54	case ExportEventCommentEdition:
 55		return fmt.Sprintf("updated comment: %s", er.ID)
 56	case ExportEventStatusChange:
 57		return fmt.Sprintf("changed status: %s", er.ID)
 58	case ExportEventTitleEdition:
 59		return fmt.Sprintf("changed title: %s", er.ID)
 60	case ExportEventLabelChange:
 61		return fmt.Sprintf("changed label: %s", er.ID)
 62	case ExportEventNothing:
 63		if er.ID != "" {
 64			return fmt.Sprintf("no actions taken for event %s: %s", er.ID, er.Reason)
 65		}
 66		return fmt.Sprintf("no actions taken: %s", er.Reason)
 67	case ExportEventError:
 68		if er.ID != "" {
 69			return fmt.Sprintf("export error at %s: %s", er.ID, er.Err.Error())
 70		}
 71		return fmt.Sprintf("export error: %s", er.Err.Error())
 72	case ExportEventWarning:
 73		if er.ID != "" {
 74			return fmt.Sprintf("warning at %s: %s", er.ID, er.Err.Error())
 75		}
 76		return fmt.Sprintf("warning: %s", er.Err.Error())
 77
 78	default:
 79		panic("unknown export result")
 80	}
 81}
 82
 83func NewExportError(err error, id entity.Id) ExportResult {
 84	return ExportResult{
 85		ID:    id,
 86		Err:   err,
 87		Event: ExportEventError,
 88	}
 89}
 90
 91func NewExportWarning(err error, id entity.Id) ExportResult {
 92	return ExportResult{
 93		ID:    id,
 94		Err:   err,
 95		Event: ExportEventWarning,
 96	}
 97}
 98
 99func NewExportNothing(id entity.Id, reason string) ExportResult {
100	return ExportResult{
101		ID:     id,
102		Reason: reason,
103		Event:  ExportEventNothing,
104	}
105}
106
107func NewExportBug(id entity.Id) ExportResult {
108	return ExportResult{
109		ID:    id,
110		Event: ExportEventBug,
111	}
112}
113
114func NewExportComment(id entity.Id) ExportResult {
115	return ExportResult{
116		ID:    id,
117		Event: ExportEventComment,
118	}
119}
120
121func NewExportCommentEdition(id entity.Id) ExportResult {
122	return ExportResult{
123		ID:    id,
124		Event: ExportEventCommentEdition,
125	}
126}
127
128func NewExportStatusChange(id entity.Id) ExportResult {
129	return ExportResult{
130		ID:    id,
131		Event: ExportEventStatusChange,
132	}
133}
134
135func NewExportLabelChange(id entity.Id) ExportResult {
136	return ExportResult{
137		ID:    id,
138		Event: ExportEventLabelChange,
139	}
140}
141
142func NewExportTitleEdition(id entity.Id) ExportResult {
143	return ExportResult{
144		ID:    id,
145		Event: ExportEventTitleEdition,
146	}
147}