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