aboutsummaryrefslogtreecommitdiffstats
path: root/bridge
diff options
context:
space:
mode:
authorAmine <hilalyamine@gmail.com>2019-11-03 17:34:51 +0100
committerGitHub <noreply@github.com>2019-11-03 17:34:51 +0100
commit163ea9c93306c387f84ff0b85c2d8fca4c01e449 (patch)
tree05ab55090bf196b940a4f6b0e4aaec9975b997bc /bridge
parent16bd116971bb7abc7308e95c474b433512672432 (diff)
parent57e23c8ada0a9d921a6b68187a76eb5c8b8a407d (diff)
downloadgit-bug-163ea9c93306c387f84ff0b85c2d8fca4c01e449.tar.gz
Merge pull request #236 from MichaelMure/import-after
commands: support bridge imports after a given date
Diffstat (limited to 'bridge')
-rw-r--r--bridge/core/bridge.go70
1 files changed, 63 insertions, 7 deletions
diff --git a/bridge/core/bridge.go b/bridge/core/bridge.go
index 47a89389..ddf041f1 100644
--- a/bridge/core/bridge.go
+++ b/bridge/core/bridge.go
@@ -291,7 +291,10 @@ func (b *Bridge) ensureInit() error {
return nil
}
-func (b *Bridge) ImportAll(ctx context.Context, since time.Time) (<-chan ImportResult, error) {
+func (b *Bridge) ImportAllSince(ctx context.Context, since time.Time) (<-chan ImportResult, error) {
+ // 5 seconds before the actual start just to be sure.
+ importStartTime := time.Now().Add(-5 * time.Second)
+
importer := b.getImporter()
if importer == nil {
return nil, ErrImportNotSupported
@@ -307,24 +310,77 @@ func (b *Bridge) ImportAll(ctx context.Context, since time.Time) (<-chan ImportR
return nil, err
}
- return importer.ImportAll(ctx, b.repo, since)
+ events, err := importer.ImportAll(ctx, b.repo, since)
+ if err != nil {
+ return nil, err
+ }
+
+ out := make(chan ImportResult)
+ go func() {
+ defer close(out)
+ noError := true
+
+ // relay all events while checking that everything went well
+ for event := range events {
+ if event.Err != nil {
+ noError = false
+ }
+ out <- event
+ }
+
+ // store the last import time ONLY if no error happened
+ if noError {
+ key := fmt.Sprintf("git-bug.bridge.%s.lastImportTime", b.Name)
+ err = b.repo.LocalConfig().StoreTimestamp(key, importStartTime)
+ }
+ }()
+
+ return out, nil
+}
+
+func (b *Bridge) ImportAll(ctx context.Context) (<-chan ImportResult, error) {
+ // If possible, restart from the last import time
+ lastImport, err := b.repo.LocalConfig().ReadTimestamp(fmt.Sprintf("git-bug.bridge.%s.lastImportTime", b.Name))
+ if err == nil {
+ return b.ImportAllSince(ctx, lastImport)
+ }
+
+ return b.ImportAllSince(ctx, time.Time{})
}
-func (b *Bridge) ExportAll(ctx context.Context, since time.Time) (<-chan ExportResult, error) {
+func (b *Bridge) ExportAll(ctx context.Context, since time.Time) error {
exporter := b.getExporter()
if exporter == nil {
- return nil, ErrExportNotSupported
+ return ErrExportNotSupported
}
err := b.ensureConfig()
if err != nil {
- return nil, err
+ return err
}
err = b.ensureInit()
if err != nil {
- return nil, err
+ return err
+ }
+
+ events, err := exporter.ExportAll(ctx, b.repo, since)
+ if err != nil {
+ return err
}
- return exporter.ExportAll(ctx, b.repo, since)
+ exportedIssues := 0
+ for result := range events {
+ if result.Event != ExportEventNothing {
+ fmt.Println(result.String())
+ }
+
+ switch result.Event {
+ case ExportEventBug:
+ exportedIssues++
+ }
+ }
+
+ fmt.Printf("exported %d issues with %s bridge\n", exportedIssues, b.Name)
+ return nil
}