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