bridge.go

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