bridge.go

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