aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/core
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2018-10-06 11:55:16 +0200
committerMichael Muré <batolettre@gmail.com>2018-10-06 11:55:16 +0200
commit64133ee5ba4b03e2f7f2f2161b1d551a97bd0d80 (patch)
tree9459d5c83f38441985c003a1a306783c47ddfaf8 /bridge/core
parent558e149b2524864beff3912f44fdb887edeb7ac5 (diff)
downloadgit-bug-64133ee5ba4b03e2f7f2f2161b1d551a97bd0d80.tar.gz
github: deal with the deleted user case where github return a null actor
Diffstat (limited to 'bridge/core')
-rw-r--r--bridge/core/bridge.go119
-rw-r--r--bridge/core/interfaces.go18
2 files changed, 102 insertions, 35 deletions
diff --git a/bridge/core/bridge.go b/bridge/core/bridge.go
index 69e9e522..112c56ea 100644
--- a/bridge/core/bridge.go
+++ b/bridge/core/bridge.go
@@ -20,10 +20,13 @@ var bridgeImpl map[string]reflect.Type
// Bridge is a wrapper around a BridgeImpl that will bind low-level
// implementation with utility code to provide high-level functions.
type Bridge struct {
- Name string
- repo *cache.RepoCache
- impl BridgeImpl
- conf Configuration
+ Name string
+ repo *cache.RepoCache
+ impl BridgeImpl
+ importer Importer
+ exporter Exporter
+ conf Configuration
+ initDone bool
}
// Register will register a new BridgeImpl
@@ -65,7 +68,7 @@ func NewBridge(repo *cache.RepoCache, target string, name string) (*Bridge, erro
// Instantiate a new bridge for a repo, from the combined target and name contained
// in the full name
-func NewBridgeFullName(repo *cache.RepoCache, fullName string) (*Bridge, error) {
+func NewBridgeFromFullName(repo *cache.RepoCache, fullName string) (*Bridge, error) {
target, name, err := splitFullName(fullName)
if err != nil {
return nil, err
@@ -184,19 +187,19 @@ func (b *Bridge) storeConfig(conf Configuration) error {
return nil
}
-func (b Bridge) getConfig() (Configuration, error) {
- var err error
+func (b *Bridge) ensureConfig() error {
if b.conf == nil {
- b.conf, err = b.loadConfig()
+ conf, err := b.loadConfig()
if err != nil {
- return nil, err
+ return err
}
+ b.conf = conf
}
- return b.conf, nil
+ return nil
}
-func (b Bridge) loadConfig() (Configuration, error) {
+func (b *Bridge) loadConfig() (Configuration, error) {
keyPrefix := fmt.Sprintf("git-bug.bridge.%s.%s.", b.impl.Target(), b.Name)
pairs, err := b.repo.ReadConfigs(keyPrefix)
@@ -218,58 +221,120 @@ func (b Bridge) loadConfig() (Configuration, error) {
return result, nil
}
-func (b Bridge) ImportAll() error {
- importer := b.impl.Importer()
+func (b *Bridge) getImporter() Importer {
+ if b.importer == nil {
+ b.importer = b.impl.NewImporter()
+ }
+
+ return b.importer
+}
+
+func (b *Bridge) getExporter() Exporter {
+ if b.exporter == nil {
+ b.exporter = b.impl.NewExporter()
+ }
+
+ return b.exporter
+}
+
+func (b *Bridge) ensureInit() error {
+ if b.initDone {
+ return nil
+ }
+
+ importer := b.getImporter()
+ if importer != nil {
+ err := importer.Init(b.conf)
+ if err != nil {
+ return err
+ }
+ }
+
+ exporter := b.getExporter()
+ if exporter != nil {
+ err := exporter.Init(b.conf)
+ if err != nil {
+ return err
+ }
+ }
+
+ b.initDone = true
+
+ return nil
+}
+
+func (b *Bridge) ImportAll() error {
+ importer := b.getImporter()
if importer == nil {
return ErrImportNorSupported
}
- conf, err := b.getConfig()
+ err := b.ensureConfig()
+ if err != nil {
+ return err
+ }
+
+ err = b.ensureInit()
if err != nil {
return err
}
- return b.impl.Importer().ImportAll(b.repo, conf)
+ return importer.ImportAll(b.repo)
}
-func (b Bridge) Import(id string) error {
- importer := b.impl.Importer()
+func (b *Bridge) Import(id string) error {
+ importer := b.getImporter()
if importer == nil {
return ErrImportNorSupported
}
- conf, err := b.getConfig()
+ err := b.ensureConfig()
if err != nil {
return err
}
- return b.impl.Importer().Import(b.repo, conf, id)
+ err = b.ensureInit()
+ if err != nil {
+ return err
+ }
+
+ return importer.Import(b.repo, id)
}
-func (b Bridge) ExportAll() error {
- exporter := b.impl.Exporter()
+func (b *Bridge) ExportAll() error {
+ exporter := b.getExporter()
if exporter == nil {
return ErrExportNorSupported
}
- conf, err := b.getConfig()
+ err := b.ensureConfig()
+ if err != nil {
+ return err
+ }
+
+ err = b.ensureInit()
if err != nil {
return err
}
- return b.impl.Exporter().ExportAll(b.repo, conf)
+ return exporter.ExportAll(b.repo)
}
-func (b Bridge) Export(id string) error {
- exporter := b.impl.Exporter()
+func (b *Bridge) Export(id string) error {
+ exporter := b.getExporter()
if exporter == nil {
return ErrExportNorSupported
}
- conf, err := b.getConfig()
+ err := b.ensureConfig()
+ if err != nil {
+ return err
+ }
+
+ err = b.ensureInit()
if err != nil {
return err
}
- return b.impl.Exporter().Export(b.repo, conf, id)
+ return exporter.Export(b.repo, id)
}
diff --git a/bridge/core/interfaces.go b/bridge/core/interfaces.go
index 79b75606..4836dab3 100644
--- a/bridge/core/interfaces.go
+++ b/bridge/core/interfaces.go
@@ -18,19 +18,21 @@ type BridgeImpl interface {
// ValidateConfig check the configuration for error
ValidateConfig(conf Configuration) error
- // Importer return an Importer implementation if the import is supported
- Importer() Importer
+ // NewImporter return an Importer implementation if the import is supported
+ NewImporter() Importer
- // Exporter return an Exporter implementation if the export is supported
- Exporter() Exporter
+ // NewExporter return an Exporter implementation if the export is supported
+ NewExporter() Exporter
}
type Importer interface {
- ImportAll(repo *cache.RepoCache, conf Configuration) error
- Import(repo *cache.RepoCache, conf Configuration, id string) error
+ Init(conf Configuration) error
+ ImportAll(repo *cache.RepoCache) error
+ Import(repo *cache.RepoCache, id string) error
}
type Exporter interface {
- ExportAll(repo *cache.RepoCache, conf Configuration) error
- Export(repo *cache.RepoCache, conf Configuration, id string) error
+ Init(conf Configuration) error
+ ExportAll(repo *cache.RepoCache) error
+ Export(repo *cache.RepoCache, id string) error
}