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	// The export system (web API) has reached a rate limit
 35	ExportEventRateLimiting
 36
 37	// Error happened during export
 38	ExportEventError
 39)
 40
 41// ExportResult is an event that is emitted during the export process, to
 42// allow calling code to report on what is happening, collect metrics or
 43// display meaningful errors if something went wrong.
 44type ExportResult struct {
 45	Err    error
 46	Event  ExportEvent
 47	ID     entity.Id
 48	Reason string
 49}
 50
 51func (er ExportResult) String() string {
 52	switch er.Event {
 53	case ExportEventBug:
 54		return fmt.Sprintf("new issue: %s", er.ID)
 55	case ExportEventComment:
 56		return fmt.Sprintf("new comment: %s", er.ID)
 57	case ExportEventCommentEdition:
 58		return fmt.Sprintf("updated comment: %s", er.ID)
 59	case ExportEventStatusChange:
 60		return fmt.Sprintf("changed status: %s", er.ID)
 61	case ExportEventTitleEdition:
 62		return fmt.Sprintf("changed title: %s", er.ID)
 63	case ExportEventLabelChange:
 64		return fmt.Sprintf("changed label: %s", er.ID)
 65	case ExportEventNothing:
 66		if er.ID != "" {
 67			return fmt.Sprintf("no actions taken for event %s: %s", er.ID, er.Reason)
 68		}
 69		return fmt.Sprintf("no actions taken: %s", er.Reason)
 70	case ExportEventError:
 71		if er.ID != "" {
 72			return fmt.Sprintf("export error at %s: %s", er.ID, er.Err.Error())
 73		}
 74		return fmt.Sprintf("export error: %s", er.Err.Error())
 75	case ExportEventWarning:
 76		if er.ID != "" {
 77			return fmt.Sprintf("warning at %s: %s", er.ID, er.Err.Error())
 78		}
 79		return fmt.Sprintf("warning: %s", er.Err.Error())
 80	case ExportEventRateLimiting:
 81		return fmt.Sprintf("rate limiting: %s", er.Reason)
 82
 83	default:
 84		panic("unknown export result")
 85	}
 86}
 87
 88func NewExportError(err error, id entity.Id) ExportResult {
 89	return ExportResult{
 90		ID:    id,
 91		Err:   err,
 92		Event: ExportEventError,
 93	}
 94}
 95
 96func NewExportWarning(err error, id entity.Id) ExportResult {
 97	return ExportResult{
 98		ID:    id,
 99		Err:   err,
100		Event: ExportEventWarning,
101	}
102}
103
104func NewExportNothing(id entity.Id, reason string) ExportResult {
105	return ExportResult{
106		ID:     id,
107		Reason: reason,
108		Event:  ExportEventNothing,
109	}
110}
111
112func NewExportBug(id entity.Id) ExportResult {
113	return ExportResult{
114		ID:    id,
115		Event: ExportEventBug,
116	}
117}
118
119func NewExportComment(id entity.Id) ExportResult {
120	return ExportResult{
121		ID:    id,
122		Event: ExportEventComment,
123	}
124}
125
126func NewExportCommentEdition(id entity.Id) ExportResult {
127	return ExportResult{
128		ID:    id,
129		Event: ExportEventCommentEdition,
130	}
131}
132
133func NewExportStatusChange(id entity.Id) ExportResult {
134	return ExportResult{
135		ID:    id,
136		Event: ExportEventStatusChange,
137	}
138}
139
140func NewExportLabelChange(id entity.Id) ExportResult {
141	return ExportResult{
142		ID:    id,
143		Event: ExportEventLabelChange,
144	}
145}
146
147func NewExportTitleEdition(id entity.Id) ExportResult {
148	return ExportResult{
149		ID:    id,
150		Event: ExportEventTitleEdition,
151	}
152}
153
154func NewExportRateLimiting(msg string) ExportResult {
155	return ExportResult{
156		Reason: msg,
157		Event:  ExportEventRateLimiting,
158	}
159}