1package core
2
3import (
4 "fmt"
5
6 "github.com/git-bug/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 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 EntityId entity.Id // optional for err, warning
48 Reason string
49}
50
51func (er ExportResult) String() string {
52 switch er.Event {
53 case ExportEventBug:
54 return fmt.Sprintf("[%s] new issue: %s", er.EntityId.Human(), er.EntityId)
55 case ExportEventComment:
56 return fmt.Sprintf("[%s] new comment", er.EntityId.Human())
57 case ExportEventCommentEdition:
58 return fmt.Sprintf("[%s] updated comment", er.EntityId.Human())
59 case ExportEventStatusChange:
60 return fmt.Sprintf("[%s] changed status", er.EntityId.Human())
61 case ExportEventTitleEdition:
62 return fmt.Sprintf("[%s] changed title", er.EntityId.Human())
63 case ExportEventLabelChange:
64 return fmt.Sprintf("[%s] changed label", er.EntityId.Human())
65 case ExportEventNothing:
66 if er.EntityId != "" {
67 return fmt.Sprintf("no actions taken on entity %s: %s", er.EntityId, er.Reason)
68 }
69 return fmt.Sprintf("no actions taken: %s", er.Reason)
70 case ExportEventError:
71 if er.EntityId != "" {
72 return fmt.Sprintf("export error on entity %s: %s", er.EntityId, er.Err.Error())
73 }
74 return fmt.Sprintf("export error: %s", er.Err.Error())
75 case ExportEventWarning:
76 if er.EntityId != "" {
77 return fmt.Sprintf("warning on entity %s: %s", er.EntityId, 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, entityId entity.Id) ExportResult {
89 return ExportResult{
90 EntityId: entityId,
91 Err: err,
92 Event: ExportEventError,
93 }
94}
95
96func NewExportWarning(err error, entityId entity.Id) ExportResult {
97 return ExportResult{
98 EntityId: entityId,
99 Err: err,
100 Event: ExportEventWarning,
101 }
102}
103
104func NewExportNothing(entityId entity.Id, reason string) ExportResult {
105 return ExportResult{
106 EntityId: entityId,
107 Reason: reason,
108 Event: ExportEventNothing,
109 }
110}
111
112func NewExportBug(entityId entity.Id) ExportResult {
113 return ExportResult{
114 EntityId: entityId,
115 Event: ExportEventBug,
116 }
117}
118
119func NewExportComment(entityId entity.Id) ExportResult {
120 return ExportResult{
121 EntityId: entityId,
122 Event: ExportEventComment,
123 }
124}
125
126func NewExportCommentEdition(entityId entity.Id) ExportResult {
127 return ExportResult{
128 EntityId: entityId,
129 Event: ExportEventCommentEdition,
130 }
131}
132
133func NewExportStatusChange(entityId entity.Id) ExportResult {
134 return ExportResult{
135 EntityId: entityId,
136 Event: ExportEventStatusChange,
137 }
138}
139
140func NewExportLabelChange(entityId entity.Id) ExportResult {
141 return ExportResult{
142 EntityId: entityId,
143 Event: ExportEventLabelChange,
144 }
145}
146
147func NewExportTitleEdition(entityId entity.Id) ExportResult {
148 return ExportResult{
149 EntityId: entityId,
150 Event: ExportEventTitleEdition,
151 }
152}
153
154func NewExportRateLimiting(msg string) ExportResult {
155 return ExportResult{
156 Reason: msg,
157 Event: ExportEventRateLimiting,
158 }
159}