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 Event: ExportEventError,
68 }
69}
70
71func NewExportNothing(id entity.Id, reason string) ExportResult {
72 return ExportResult{
73 ID: id,
74 Reason: reason,
75 Event: ExportEventNothing,
76 }
77}
78
79func NewExportBug(id entity.Id) ExportResult {
80 return ExportResult{
81 ID: id,
82 Event: ExportEventBug,
83 }
84}
85
86func NewExportComment(id entity.Id) ExportResult {
87 return ExportResult{
88 ID: id,
89 Event: ExportEventComment,
90 }
91}
92
93func NewExportCommentEdition(id entity.Id) ExportResult {
94 return ExportResult{
95 ID: id,
96 Event: ExportEventCommentEdition,
97 }
98}
99
100func NewExportStatusChange(id entity.Id) ExportResult {
101 return ExportResult{
102 ID: id,
103 Event: ExportEventStatusChange,
104 }
105}
106
107func NewExportLabelChange(id entity.Id) ExportResult {
108 return ExportResult{
109 ID: id,
110 Event: ExportEventLabelChange,
111 }
112}
113
114func NewExportTitleEdition(id entity.Id) ExportResult {
115 return ExportResult{
116 ID: id,
117 Event: ExportEventTitleEdition,
118 }
119}