aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/core/import.go
blob: a072d24a4106d9350948fba15fd8d2ea62969ed8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package core

import (
	"fmt"
	"strings"

	"github.com/git-bug/git-bug/entity"
)

type ImportEvent int

const (
	_ ImportEvent = iota

	// Bug has been created
	ImportEventBug
	// Comment has been created
	ImportEventComment
	// Comment has been edited
	ImportEventCommentEdition
	// Bug's status has changed
	ImportEventStatusChange
	// Bug's title has changed
	ImportEventTitleEdition
	// Bug's labels changed
	ImportEventLabelChange
	// Nothing happened on a Bug
	ImportEventNothing

	// Identity has been created
	ImportEventIdentity

	// Something wrong happened during import that is worth notifying to the user
	// but not severe enough to consider the import a failure.
	ImportEventWarning

	// The import system (web API) has reached the rate limit
	ImportEventRateLimiting

	// Error happened during import
	ImportEventError
)

// ImportResult is an event that is emitted during the import process, to
// allow calling code to report on what is happening, collect metrics or
// display meaningful errors if something went wrong.
type ImportResult struct {
	Err         error
	Event       ImportEvent
	EntityId    entity.Id         // optional for err, warnings
	OperationId entity.Id         // optional
	ComponentId entity.CombinedId // optional
	Reason      string
}

func (er ImportResult) String() string {
	switch er.Event {
	case ImportEventBug:
		return fmt.Sprintf("[%s] new issue: %s", er.EntityId.Human(), er.EntityId)
	case ImportEventComment:
		return fmt.Sprintf("[%s] new comment: %s", er.EntityId.Human(), er.ComponentId)
	case ImportEventCommentEdition:
		return fmt.Sprintf("[%s] updated comment: %s", er.EntityId.Human(), er.ComponentId)
	case ImportEventStatusChange:
		return fmt.Sprintf("[%s] changed status with op: %s", er.EntityId.Human(), er.OperationId)
	case ImportEventTitleEdition:
		return fmt.Sprintf("[%s] changed title with op: %s", er.EntityId.Human(), er.OperationId)
	case ImportEventLabelChange:
		return fmt.Sprintf("[%s] changed label with op: %s", er.EntityId.Human(), er.OperationId)
	case ImportEventIdentity:
		return fmt.Sprintf("[%s] new identity: %s", er.EntityId.Human(), er.EntityId)
	case ImportEventNothing:
		if er.EntityId != "" {
			return fmt.Sprintf("no action taken on entity %s: %s", er.EntityId, er.Reason)
		}
		return fmt.Sprintf("no action taken: %s", er.Reason)
	case ImportEventError:
		if er.EntityId != "" {
			return fmt.Sprintf("import error on entity %s: %s", er.EntityId, er.Err.Error())
		}
		return fmt.Sprintf("import error: %s", er.Err.Error())
	case ImportEventWarning:
		parts := make([]string, 0, 4)
		parts = append(parts, "warning:")
		if er.EntityId != "" {
			parts = append(parts, fmt.Sprintf("on entity %s", er.EntityId))
		}
		if er.Reason != "" {
			parts = append(parts, fmt.Sprintf("reason: %s", er.Reason))
		}
		if er.Err != nil {
			parts = append(parts, fmt.Sprintf("err: %s", er.Err))
		}
		return strings.Join(parts, " ")
	case ImportEventRateLimiting:
		return fmt.Sprintf("rate limiting: %s", er.Reason)

	default:
		panic("unknown import result")
	}
}

func NewImportError(err error, entityId entity.Id) ImportResult {
	return ImportResult{
		Err:      err,
		EntityId: entityId,
		Event:    ImportEventError,
	}
}

func NewImportWarning(err error, entityId entity.Id) ImportResult {
	return ImportResult{
		Err:      err,
		EntityId: entityId,
		Event:    ImportEventWarning,
	}
}

func NewImportNothing(entityId entity.Id, reason string) ImportResult {
	return ImportResult{
		EntityId: entityId,
		Reason:   reason,
		Event:    ImportEventNothing,
	}
}

func NewImportBug(entityId entity.Id) ImportResult {
	return ImportResult{
		EntityId: entityId,
		Event:    ImportEventBug,
	}
}

func NewImportComment(entityId entity.Id, commentId entity.CombinedId) ImportResult {
	return ImportResult{
		EntityId:    entityId,
		ComponentId: commentId,
		Event:       ImportEventComment,
	}
}

func NewImportCommentEdition(entityId entity.Id, commentId entity.CombinedId) ImportResult {
	return ImportResult{
		EntityId:    entityId,
		ComponentId: commentId,
		Event:       ImportEventCommentEdition,
	}
}

func NewImportStatusChange(entityId entity.Id, opId entity.Id) ImportResult {
	return ImportResult{
		EntityId:    entityId,
		OperationId: opId,
		Event:       ImportEventStatusChange,
	}
}

func NewImportLabelChange(entityId entity.Id, opId entity.Id) ImportResult {
	return ImportResult{
		EntityId:    entityId,
		OperationId: opId,
		Event:       ImportEventLabelChange,
	}
}

func NewImportTitleEdition(entityId entity.Id, opId entity.Id) ImportResult {
	return ImportResult{
		EntityId:    entityId,
		OperationId: opId,
		Event:       ImportEventTitleEdition,
	}
}

func NewImportIdentity(entityId entity.Id) ImportResult {
	return ImportResult{
		EntityId: entityId,
		Event:    ImportEventIdentity,
	}
}

func NewImportRateLimiting(msg string) ImportResult {
	return ImportResult{
		Reason: msg,
		Event:  ImportEventRateLimiting,
	}
}