1package core
2
3import (
4 "fmt"
5 "strings"
6
7 "github.com/MichaelMure/git-bug/entity"
8)
9
10type ImportEvent int
11
12const (
13 _ ImportEvent = iota
14
15 // Bug has been created
16 ImportEventBug
17 // Comment has been created
18 ImportEventComment
19 // Comment has been edited
20 ImportEventCommentEdition
21 // Bug's status has changed
22 ImportEventStatusChange
23 // Bug's title has changed
24 ImportEventTitleEdition
25 // Bug's labels changed
26 ImportEventLabelChange
27 // Nothing happened on a Bug
28 ImportEventNothing
29
30 // Identity has been created
31 ImportEventIdentity
32
33 // Error happened during import
34 ImportEventError
35 ImportEventWarning
36)
37
38// ImportResult is an event that is emitted during the import process, to
39// allow calling code to report on what is happening, collect metrics or
40// display meaningful errors if something went wrong.
41type ImportResult struct {
42 Err error
43 Event ImportEvent
44 ID entity.Id
45 Reason string
46}
47
48func (er ImportResult) String() string {
49 switch er.Event {
50 case ImportEventBug:
51 return fmt.Sprintf("new issue: %s", er.ID)
52 case ImportEventComment:
53 return fmt.Sprintf("new comment: %s", er.ID)
54 case ImportEventCommentEdition:
55 return fmt.Sprintf("updated comment: %s", er.ID)
56 case ImportEventStatusChange:
57 return fmt.Sprintf("changed status: %s", er.ID)
58 case ImportEventTitleEdition:
59 return fmt.Sprintf("changed title: %s", er.ID)
60 case ImportEventLabelChange:
61 return fmt.Sprintf("changed label: %s", er.ID)
62 case ImportEventIdentity:
63 return fmt.Sprintf("new identity: %s", er.ID)
64 case ImportEventNothing:
65 if er.ID != "" {
66 return fmt.Sprintf("no action taken for event %s: %s", er.ID, er.Reason)
67 }
68 return fmt.Sprintf("no action taken: %s", er.Reason)
69 case ImportEventError:
70 if er.ID != "" {
71 return fmt.Sprintf("import error at id %s: %s", er.ID, er.Err.Error())
72 }
73 return fmt.Sprintf("import error: %s", er.Err.Error())
74 case ImportEventWarning:
75 parts := make([]string, 0, 4)
76 parts = append(parts, "warning:")
77 if er.ID != "" {
78 parts = append(parts, fmt.Sprintf("at id %s", er.ID))
79 }
80 if er.Reason != "" {
81 parts = append(parts, fmt.Sprintf("reason: %s", er.Reason))
82 }
83 if er.Err != nil {
84 parts = append(parts, fmt.Sprintf("err: %s", er.Err))
85 }
86 return strings.Join(parts, " ")
87
88 default:
89 panic("unknown import result")
90 }
91}
92
93func NewImportError(err error, id entity.Id) ImportResult {
94 return ImportResult{
95 Err: err,
96 ID: id,
97 Event: ImportEventError,
98 }
99}
100
101func NewImportWarning(err error, id entity.Id) ImportResult {
102 return ImportResult{
103 Err: err,
104 ID: id,
105 Event: ImportEventWarning,
106 }
107}
108
109func NewImportNothing(id entity.Id, reason string) ImportResult {
110 return ImportResult{
111 ID: id,
112 Reason: reason,
113 Event: ImportEventNothing,
114 }
115}
116
117func NewImportBug(id entity.Id) ImportResult {
118 return ImportResult{
119 ID: id,
120 Event: ImportEventBug,
121 }
122}
123
124func NewImportComment(id entity.Id) ImportResult {
125 return ImportResult{
126 ID: id,
127 Event: ImportEventComment,
128 }
129}
130
131func NewImportCommentEdition(id entity.Id) ImportResult {
132 return ImportResult{
133 ID: id,
134 Event: ImportEventCommentEdition,
135 }
136}
137
138func NewImportStatusChange(id entity.Id) ImportResult {
139 return ImportResult{
140 ID: id,
141 Event: ImportEventStatusChange,
142 }
143}
144
145func NewImportLabelChange(id entity.Id) ImportResult {
146 return ImportResult{
147 ID: id,
148 Event: ImportEventLabelChange,
149 }
150}
151
152func NewImportTitleEdition(id entity.Id) ImportResult {
153 return ImportResult{
154 ID: id,
155 Event: ImportEventTitleEdition,
156 }
157}
158
159func NewImportIdentity(id entity.Id) ImportResult {
160 return ImportResult{
161 ID: id,
162 Event: ImportEventIdentity,
163 }
164}