1// Package core contains the target-agnostic code to define and run a bridge
2package core
3
4import (
5 "context"
6 "fmt"
7 "reflect"
8 "regexp"
9 "sort"
10 "strings"
11 "time"
12
13 "github.com/pkg/errors"
14
15 "github.com/MichaelMure/git-bug/cache"
16 "github.com/MichaelMure/git-bug/repository"
17)
18
19var ErrImportNotSupported = errors.New("import is not supported")
20var ErrExportNotSupported = errors.New("export is not supported")
21
22const (
23 ConfigKeyTarget = "target"
24
25 MetaKeyOrigin = "origin"
26
27 bridgeConfigKeyPrefix = "git-bug.bridge"
28)
29
30var bridgeImpl map[string]reflect.Type
31
32// BridgeParams holds parameters to simplify the bridge configuration without
33// having to make terminal prompts.
34type BridgeParams struct {
35 Owner string
36 Project string
37 URL string
38 BaseURL string
39 CredPrefix string
40 TokenRaw string
41}
42
43// Bridge is a wrapper around a BridgeImpl that will bind low-level
44// implementation with utility code to provide high-level functions.
45type Bridge struct {
46 Name string
47 repo *cache.RepoCache
48 impl BridgeImpl
49 importer Importer
50 exporter Exporter
51 conf Configuration
52 initDone bool
53}
54
55// Register will register a new BridgeImpl
56func Register(impl BridgeImpl) {
57 if bridgeImpl == nil {
58 bridgeImpl = make(map[string]reflect.Type)
59 }
60 bridgeImpl[impl.Target()] = reflect.TypeOf(impl)
61}
62
63// Targets return all known bridge implementation target
64func Targets() []string {
65 var result []string
66
67 for key := range bridgeImpl {
68 result = append(result, key)
69 }
70
71 sort.Strings(result)
72
73 return result
74}
75
76// TargetExist return true if the given target has a bridge implementation
77func TargetExist(target string) bool {
78 _, ok := bridgeImpl[target]
79 return ok
80}
81
82// Instantiate a new Bridge for a repo, from the given target and name
83func NewBridge(repo *cache.RepoCache, target string, name string) (*Bridge, error) {
84 implType, ok := bridgeImpl[target]
85 if !ok {
86 return nil, fmt.Errorf("unknown bridge target %v", target)
87 }
88
89 impl := reflect.New(implType).Elem().Interface().(BridgeImpl)
90
91 bridge := &Bridge{
92 Name: name,
93 repo: repo,
94 impl: impl,
95 }
96
97 return bridge, nil
98}
99
100// LoadBridge instantiate a new bridge from a repo configuration
101func LoadBridge(repo *cache.RepoCache, name string) (*Bridge, error) {
102 conf, err := loadConfig(repo, name)
103 if err != nil {
104 return nil, err
105 }
106
107 target := conf[ConfigKeyTarget]
108 bridge, err := NewBridge(repo, target, name)
109 if err != nil {
110 return nil, err
111 }
112
113 err = bridge.impl.ValidateConfig(conf)
114 if err != nil {
115 return nil, errors.Wrap(err, "invalid configuration")
116 }
117
118 // will avoid reloading configuration before an export or import call
119 bridge.conf = conf
120 return bridge, nil
121}
122
123// Attempt to retrieve a default bridge for the given repo. If zero or multiple
124// bridge exist, it fails.
125func DefaultBridge(repo *cache.RepoCache) (*Bridge, error) {
126 bridges, err := ConfiguredBridges(repo)
127 if err != nil {
128 return nil, err
129 }
130
131 if len(bridges) == 0 {
132 return nil, fmt.Errorf("no configured bridge")
133 }
134
135 if len(bridges) > 1 {
136 return nil, fmt.Errorf("multiple bridge are configured, you need to select one explicitely")
137 }
138
139 return LoadBridge(repo, bridges[0])
140}
141
142// ConfiguredBridges return the list of bridge that are configured for the given
143// repo
144func ConfiguredBridges(repo repository.RepoConfig) ([]string, error) {
145 configs, err := repo.LocalConfig().ReadAll(bridgeConfigKeyPrefix + ".")
146 if err != nil {
147 return nil, errors.Wrap(err, "can't read configured bridges")
148 }
149
150 re, err := regexp.Compile(bridgeConfigKeyPrefix + `.([^.]+)`)
151 if err != nil {
152 panic(err)
153 }
154
155 set := make(map[string]interface{})
156
157 for key := range configs {
158 res := re.FindStringSubmatch(key)
159
160 if res == nil {
161 continue
162 }
163
164 set[res[1]] = nil
165 }
166
167 result := make([]string, len(set))
168
169 i := 0
170 for key := range set {
171 result[i] = key
172 i++
173 }
174
175 return result, nil
176}
177
178// Check if a bridge exist
179func BridgeExist(repo repository.RepoConfig, name string) bool {
180 keyPrefix := fmt.Sprintf("git-bug.bridge.%s.", name)
181
182 conf, err := repo.LocalConfig().ReadAll(keyPrefix)
183
184 return err == nil && len(conf) > 0
185}
186
187// Remove a configured bridge
188func RemoveBridge(repo repository.RepoConfig, name string) error {
189 re, err := regexp.Compile(`^[a-zA-Z0-9]+`)
190 if err != nil {
191 panic(err)
192 }
193
194 if !re.MatchString(name) {
195 return fmt.Errorf("bad bridge fullname: %s", name)
196 }
197
198 keyPrefix := fmt.Sprintf("git-bug.bridge.%s", name)
199 return repo.LocalConfig().RemoveAll(keyPrefix)
200}
201
202// Configure run the target specific configuration process
203func (b *Bridge) Configure(params BridgeParams) error {
204 conf, err := b.impl.Configure(b.repo, params)
205 if err != nil {
206 return err
207 }
208
209 err = b.impl.ValidateConfig(conf)
210 if err != nil {
211 return fmt.Errorf("invalid configuration: %v", err)
212 }
213
214 b.conf = conf
215 return b.storeConfig(conf)
216}
217
218func (b *Bridge) storeConfig(conf Configuration) error {
219 for key, val := range conf {
220 storeKey := fmt.Sprintf("git-bug.bridge.%s.%s", b.Name, key)
221
222 err := b.repo.LocalConfig().StoreString(storeKey, val)
223 if err != nil {
224 return errors.Wrap(err, "error while storing bridge configuration")
225 }
226 }
227
228 return nil
229}
230
231func (b *Bridge) ensureConfig() error {
232 if b.conf == nil {
233 conf, err := loadConfig(b.repo, b.Name)
234 if err != nil {
235 return err
236 }
237 b.conf = conf
238 }
239
240 return nil
241}
242
243func loadConfig(repo repository.RepoConfig, name string) (Configuration, error) {
244 keyPrefix := fmt.Sprintf("git-bug.bridge.%s.", name)
245
246 pairs, err := repo.LocalConfig().ReadAll(keyPrefix)
247 if err != nil {
248 return nil, errors.Wrap(err, "error while reading bridge configuration")
249 }
250
251 result := make(Configuration, len(pairs))
252 for key, value := range pairs {
253 key := strings.TrimPrefix(key, keyPrefix)
254 result[key] = value
255 }
256
257 return result, nil
258}
259
260func (b *Bridge) getImporter() Importer {
261 if b.importer == nil {
262 b.importer = b.impl.NewImporter()
263 }
264
265 return b.importer
266}
267
268func (b *Bridge) getExporter() Exporter {
269 if b.exporter == nil {
270 b.exporter = b.impl.NewExporter()
271 }
272
273 return b.exporter
274}
275
276func (b *Bridge) ensureInit() error {
277 if b.initDone {
278 return nil
279 }
280
281 importer := b.getImporter()
282 if importer != nil {
283 err := importer.Init(b.repo, b.conf)
284 if err != nil {
285 return err
286 }
287 }
288
289 exporter := b.getExporter()
290 if exporter != nil {
291 err := exporter.Init(b.repo, b.conf)
292 if err != nil {
293 return err
294 }
295 }
296
297 b.initDone = true
298
299 return nil
300}
301
302func (b *Bridge) ImportAllSince(ctx context.Context, since time.Time) (<-chan ImportResult, error) {
303 // 5 seconds before the actual start just to be sure.
304 importStartTime := time.Now().Add(-5 * time.Second)
305
306 importer := b.getImporter()
307 if importer == nil {
308 return nil, ErrImportNotSupported
309 }
310
311 err := b.ensureConfig()
312 if err != nil {
313 return nil, err
314 }
315
316 err = b.ensureInit()
317 if err != nil {
318 return nil, err
319 }
320
321 events, err := importer.ImportAll(ctx, b.repo, since)
322 if err != nil {
323 return nil, err
324 }
325
326 out := make(chan ImportResult)
327 go func() {
328 defer close(out)
329 noError := true
330
331 // relay all events while checking that everything went well
332 for event := range events {
333 if event.Event == ImportEventError {
334 noError = false
335 }
336 out <- event
337 }
338
339 // store the last import time ONLY if no error happened
340 if noError {
341 key := fmt.Sprintf("git-bug.bridge.%s.lastImportTime", b.Name)
342 err = b.repo.LocalConfig().StoreTimestamp(key, importStartTime)
343 }
344 }()
345
346 return out, nil
347}
348
349func (b *Bridge) ImportAll(ctx context.Context) (<-chan ImportResult, error) {
350 // If possible, restart from the last import time
351 lastImport, err := b.repo.LocalConfig().ReadTimestamp(fmt.Sprintf("git-bug.bridge.%s.lastImportTime", b.Name))
352 if err == nil {
353 return b.ImportAllSince(ctx, lastImport)
354 }
355
356 return b.ImportAllSince(ctx, time.Time{})
357}
358
359func (b *Bridge) ExportAll(ctx context.Context, since time.Time) (<-chan ExportResult, error) {
360 exporter := b.getExporter()
361 if exporter == nil {
362 return nil, ErrExportNotSupported
363 }
364
365 err := b.ensureConfig()
366 if err != nil {
367 return nil, err
368 }
369
370 err = b.ensureInit()
371 if err != nil {
372 return nil, err
373 }
374
375 return exporter.ExportAll(ctx, b.repo, since)
376}