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