[bridge/github] cache user token

Amine Hilaly created

fix import typo

init tests

verify issue

Change summary

bridge/github/export.go      | 17 ++++++---
bridge/github/export_test.go | 67 +++++++++++++++++++++++++++++++++++++
bridge/github/import_test.go |  2 
3 files changed, 78 insertions(+), 8 deletions(-)

Detailed changes

bridge/github/export.go 🔗

@@ -21,7 +21,6 @@ import (
 
 // githubImporter implement the Importer interface
 type githubExporter struct {
-	gc   *githubv4.Client
 	conf core.Configuration
 
 	// a map containing
@@ -45,7 +44,6 @@ type githubExporter struct {
 func (ge *githubExporter) Init(conf core.Configuration) error {
 	//TODO: initialize with multiple tokens
 	ge.conf = conf
-	ge.gc = buildClient(conf["token"])
 	ge.tokens = make(map[string]string)
 	ge.clients = make(map[string]*githubv4.Client)
 	ge.cachedIDs = make(map[string]string)
@@ -74,6 +72,8 @@ func (ge *githubExporter) ExportAll(repo *cache.RepoCache, since time.Time) erro
 		return err
 	}
 
+	ge.tokens[user.Id()] = ge.conf[keyToken]
+
 	// get repository node id
 	ge.repositoryID, err = getRepositoryNodeID(
 		ge.conf[keyOwner],
@@ -86,6 +86,7 @@ func (ge *githubExporter) ExportAll(repo *cache.RepoCache, since time.Time) erro
 	}
 
 	allBugsIds := repo.AllBugsIds()
+bugLoop:
 	for _, id := range allBugsIds {
 		b, err := repo.ResolveBug(id)
 		if err != nil {
@@ -101,10 +102,14 @@ func (ge *githubExporter) ExportAll(repo *cache.RepoCache, since time.Time) erro
 
 		// if identity participated in a bug
 		for _, p := range snapshot.Participants {
-			if p.Id() == user.Id() {
-				// try to export the bug and it associated events
-				if err := ge.exportBug(b, user.Identity, since); err != nil {
-					return err
+			for userId := range ge.tokens {
+				if p.Id() == userId {
+					// try to export the bug and it associated events
+					if err := ge.exportBug(b, user.Identity, since); err != nil {
+						return err
+					}
+
+					continue bugLoop
 				}
 			}
 		}

bridge/github/export_test.go 🔗

@@ -1,7 +1,72 @@
 package github
 
-import "testing"
+import (
+	"fmt"
+	"os"
+	"testing"
+	"time"
+
+	"github.com/MichaelMure/git-bug/bridge/core"
+	"github.com/MichaelMure/git-bug/cache"
+	"github.com/MichaelMure/git-bug/repository"
+	"github.com/MichaelMure/git-bug/util/interrupt"
+	"github.com/stretchr/testify/require"
+)
 
 func TestExporter(t *testing.T) {
 	//TODO test strategy
+	tests := []struct {
+		name string
+	}{
+		{
+			name: "bug creation",
+		},
+	}
+
+	repo := repository.CreateTestRepo(false)
+	defer repository.CleanupTestRepos(t, repo)
+
+	backend, err := cache.NewRepoCache(repo)
+	require.NoError(t, err)
+
+	defer backend.Close()
+	interrupt.RegisterCleaner(backend.Close)
+
+	token := os.Getenv("GITHUB_TOKEN_PRIVATE")
+	if token == "" {
+		t.Skip("Env var GITHUB_TOKEN_PRIVATE missing")
+	}
+
+	exporter := &githubExporter{}
+	err = exporter.Init(core.Configuration{
+		keyOwner:   "MichaelMure",
+		keyProject: "git-bug-exporter-tests",
+		keyToken:   token,
+	})
+	require.NoError(t, err)
+
+	start := time.Now()
+
+	err = exporter.ExportAll(backend, time.Time{})
+	require.NoError(t, err)
+
+	fmt.Printf("test repository exported in %f seconds\n", time.Since(start).Seconds())
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+
+		})
+	}
+}
+
+func genRepoName() {}
+
+func createRepository() {}
+
+func deleteRepository() {}
+
+// verifyIssue is full
+// comments
+func verifyIssue() {
+
 }

bridge/github/import_test.go 🔗

@@ -190,7 +190,7 @@ func Test_Importer(t *testing.T) {
 					assert.Equal(t, op.(*bug.EditCommentOperation).Author.Name(), ops[i].(*bug.EditCommentOperation).Author.Name())
 
 				default:
-					panic("Unknown operation type")
+					panic("unknown operation type")
 				}
 			}
 		})