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