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