aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/github/import.go
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2019-12-10 00:42:23 +0100
committerGitHub <noreply@github.com>2019-12-10 00:42:23 +0100
commitf1ed857cbd3a253d77b31c0c896fdc4ade40844f (patch)
treed1efe28a1fa666039bf8180bbed0202f0437910f /bridge/github/import.go
parent69af7a1e0c2647c354fd9c5b55a254ba677200e1 (diff)
parent58c0e5aac97eabc02fa890123f3845ae6fe632a8 (diff)
downloadgit-bug-f1ed857cbd3a253d77b31c0c896fdc4ade40844f.tar.gz
Merge pull request #271 from MichaelMure/bridge-credentials
bridge: huge refactor to accept multiple kind of credentials
Diffstat (limited to 'bridge/github/import.go')
-rw-r--r--bridge/github/import.go38
1 files changed, 31 insertions, 7 deletions
diff --git a/bridge/github/import.go b/bridge/github/import.go
index 67ab9351..092e3e71 100644
--- a/bridge/github/import.go
+++ b/bridge/github/import.go
@@ -8,6 +8,7 @@ import (
"github.com/shurcooL/githubv4"
"github.com/MichaelMure/git-bug/bridge/core"
+ "github.com/MichaelMure/git-bug/bridge/core/auth"
"github.com/MichaelMure/git-bug/bug"
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/entity"
@@ -24,6 +25,9 @@ const (
type githubImporter struct {
conf core.Configuration
+ // default user client
+ client *githubv4.Client
+
// iterator
iterator *iterator
@@ -31,15 +35,37 @@ type githubImporter struct {
out chan<- core.ImportResult
}
-func (gi *githubImporter) Init(conf core.Configuration) error {
+func (gi *githubImporter) Init(repo *cache.RepoCache, conf core.Configuration) error {
gi.conf = conf
+
+ opts := []auth.Option{
+ auth.WithTarget(target),
+ auth.WithKind(auth.KindToken),
+ }
+
+ user, err := repo.GetUserIdentity()
+ if err == nil {
+ opts = append(opts, auth.WithUserId(user.Id()))
+ }
+
+ creds, err := auth.List(repo, opts...)
+ if err != nil {
+ return err
+ }
+
+ if len(creds) == 0 {
+ return ErrMissingIdentityToken
+ }
+
+ gi.client = buildClient(creds[0].(*auth.Token))
+
return nil
}
// ImportAll iterate over all the configured repository issues and ensure the creation of the
// missing issues / timeline items / edits / label events ...
func (gi *githubImporter) ImportAll(ctx context.Context, repo *cache.RepoCache, since time.Time) (<-chan core.ImportResult, error) {
- gi.iterator = NewIterator(ctx, 10, gi.conf[keyOwner], gi.conf[keyProject], gi.conf[core.ConfigKeyToken], since)
+ gi.iterator = NewIterator(ctx, gi.client, 10, gi.conf[keyOwner], gi.conf[keyProject], since)
out := make(chan core.ImportResult)
gi.out = out
@@ -494,7 +520,7 @@ func (gi *githubImporter) ensurePerson(repo *cache.RepoCache, actor *actor) (*ca
if err == nil {
return i, nil
}
- if _, ok := err.(entity.ErrMultipleMatch); ok {
+ if entity.IsErrMultipleMatch(err) {
return nil, err
}
@@ -543,7 +569,7 @@ func (gi *githubImporter) getGhost(repo *cache.RepoCache) (*cache.IdentityCache,
if err == nil {
return i, nil
}
- if _, ok := err.(entity.ErrMultipleMatch); ok {
+ if entity.IsErrMultipleMatch(err) {
return nil, err
}
@@ -553,12 +579,10 @@ func (gi *githubImporter) getGhost(repo *cache.RepoCache) (*cache.IdentityCache,
"login": githubv4.String("ghost"),
}
- gc := buildClient(gi.conf[core.ConfigKeyToken])
-
ctx, cancel := context.WithTimeout(gi.iterator.ctx, defaultTimeout)
defer cancel()
- err = gc.Query(ctx, &q, variables)
+ err = gi.client.Query(ctx, &q, variables)
if err != nil {
return nil, err
}