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