aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/github
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2020-01-24 00:30:13 +0100
committerMichael Muré <batolettre@gmail.com>2020-02-08 17:19:57 +0100
commit74e91144105790cc997c1d79a7f638e1e3a1f3f8 (patch)
tree33ef7b3cf547afc10b613e5d4de087ca0439232b /bridge/github
parent8da522d97af3dcaca8a8424e3541705c69779d6f (diff)
downloadgit-bug-74e91144105790cc997c1d79a7f638e1e3a1f3f8.tar.gz
more more wip
Diffstat (limited to 'bridge/github')
-rw-r--r--bridge/github/config.go37
-rw-r--r--bridge/github/export.go41
-rw-r--r--bridge/github/export_test.go2
-rw-r--r--bridge/github/import.go2
4 files changed, 40 insertions, 42 deletions
diff --git a/bridge/github/config.go b/bridge/github/config.go
index 40653afa..9ede72d4 100644
--- a/bridge/github/config.go
+++ b/bridge/github/config.go
@@ -22,7 +22,6 @@ import (
"github.com/MichaelMure/git-bug/bridge/core"
"github.com/MichaelMure/git-bug/bridge/core/auth"
"github.com/MichaelMure/git-bug/cache"
- "github.com/MichaelMure/git-bug/identity"
"github.com/MichaelMure/git-bug/input"
"github.com/MichaelMure/git-bug/repository"
"github.com/MichaelMure/git-bug/util/colors"
@@ -109,7 +108,7 @@ func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor
}
case params.TokenRaw != "":
cred = auth.NewToken(params.TokenRaw, target)
- cred.Metadata()[auth.MetaKeyLogin] = login
+ cred.SetMetadata(auth.MetaKeyLogin, login)
default:
cred, err = promptTokenOptions(repo, login, owner, project)
if err != nil {
@@ -140,34 +139,6 @@ func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor
return nil, err
}
- // TODO
- func(login string) error {
- // if no user exist with the given login
- _, err := repo.ResolveIdentityLogin(login)
- if err != nil && err != identity.ErrIdentityNotExist {
- return err
- }
-
- // tag the default user with the github login, if any
- user, err := repo.GetUserIdentity()
- if err == identity.ErrNoIdentitySet {
- return nil
- }
- if err != nil {
- return err
- }
-
- userLogin, ok := user.ImmutableMetadata()[metaKeyGithubLogin]
- if !ok {
- user.SetMetadata()
- }
-
- }(login)
-
- // Todo: if no user exist with the given login
- // - tag the default user with the github login
- // - add a command to manually tag a user ?
-
// don't forget to store the now known valid token
if !auth.IdExist(repo, cred.ID()) {
err = auth.Store(repo, cred)
@@ -176,7 +147,7 @@ func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor
}
}
- return conf, nil
+ return conf, core.FinishConfig(repo, metaKeyGithubLogin, login)
}
func (*Github) ValidateConfig(conf core.Configuration) error {
@@ -318,7 +289,7 @@ func promptTokenOptions(repo repository.RepoConfig, login, owner, project string
return nil, err
}
token := auth.NewToken(value, target)
- token.Metadata()[auth.MetaKeyLogin] = login
+ token.SetMetadata(auth.MetaKeyLogin, login)
return token, nil
case 2:
value, err := loginAndRequestToken(login, owner, project)
@@ -326,7 +297,7 @@ func promptTokenOptions(repo repository.RepoConfig, login, owner, project string
return nil, err
}
token := auth.NewToken(value, target)
- token.Metadata()[auth.MetaKeyLogin] = login
+ token.SetMetadata(auth.MetaKeyLogin, login)
return token, nil
default:
return creds[index-3], nil
diff --git a/bridge/github/export.go b/bridge/github/export.go
index 1cc66dee..663361f5 100644
--- a/bridge/github/export.go
+++ b/bridge/github/export.go
@@ -21,7 +21,6 @@ import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/identity"
- "github.com/MichaelMure/git-bug/repository"
)
var (
@@ -35,6 +34,13 @@ type githubExporter struct {
// cache identities clients
identityClient map[entity.Id]*githubv4.Client
+ // the client to use for non user-specific queries
+ // should be the client of the default user
+ defaultClient *githubv4.Client
+
+ // the token of the default user
+ defaultToken *auth.Token
+
// github repository ID
repositoryID string
@@ -53,12 +59,34 @@ func (ge *githubExporter) Init(repo *cache.RepoCache, conf core.Configuration) e
ge.cachedOperationIDs = make(map[entity.Id]string)
ge.cachedLabels = make(map[string]string)
+ user, err := repo.GetUserIdentity()
+ if err != nil {
+ return err
+ }
+
// preload all clients
- err := ge.cacheAllClient(repo)
+ err = ge.cacheAllClient(repo)
if err != nil {
return err
}
+ ge.defaultClient, err = ge.getClientForIdentity(user.Id())
+ if err != nil {
+ return err
+ }
+
+ login := user.ImmutableMetadata()[metaKeyGithubLogin]
+ creds, err := auth.List(repo, auth.WithMeta(metaKeyGithubLogin, login), auth.WithTarget(target), auth.WithKind(auth.KindToken))
+ if err != nil {
+ return err
+ }
+
+ if len(creds) == 0 {
+ return ErrMissingIdentityToken
+ }
+
+ ge.defaultToken = creds[0].(*auth.Token)
+
return nil
}
@@ -69,7 +97,7 @@ func (ge *githubExporter) cacheAllClient(repo *cache.RepoCache) error {
}
for _, cred := range creds {
- login, ok := cred.Metadata()[auth.MetaKeyLogin]
+ login, ok := cred.GetMetadata(auth.MetaKeyLogin)
if !ok {
_, _ = fmt.Fprintf(os.Stderr, "credential %s is not tagged with Github login\n", cred.ID().Human())
continue
@@ -80,9 +108,9 @@ func (ge *githubExporter) cacheAllClient(repo *cache.RepoCache) error {
continue
}
- if _, ok := ge.identityClient[cred.UserId()]; !ok {
+ if _, ok := ge.identityClient[user.Id()]; !ok {
client := buildClient(creds[0].(*auth.Token))
- ge.identityClient[cred.UserId()] = client
+ ge.identityClient[user.Id()] = client
}
}
@@ -462,11 +490,12 @@ func (ge *githubExporter) cacheGithubLabels(ctx context.Context, gc *githubv4.Cl
for hasNextPage {
// create a new timeout context at each iteration
ctx, cancel := context.WithTimeout(ctx, defaultTimeout)
- defer cancel()
if err := gc.Query(ctx, &q, variables); err != nil {
+ cancel()
return err
}
+ cancel()
for _, label := range q.Repository.Labels.Nodes {
ge.cachedLabels[label.Name] = label.ID
diff --git a/bridge/github/export_test.go b/bridge/github/export_test.go
index 5a0bc653..d2cfb1f9 100644
--- a/bridge/github/export_test.go
+++ b/bridge/github/export_test.go
@@ -176,7 +176,7 @@ func TestPushPull(t *testing.T) {
return deleteRepository(projectName, envUser, envToken)
})
- token := auth.NewToken(author.Id(), envToken, target)
+ token := auth.NewToken(envToken, target)
err = auth.Store(repo, token)
require.NoError(t, err)
diff --git a/bridge/github/import.go b/bridge/github/import.go
index aac4f119..f2c9a53d 100644
--- a/bridge/github/import.go
+++ b/bridge/github/import.go
@@ -543,7 +543,6 @@ func (gi *githubImporter) ensurePerson(repo *cache.RepoCache, actor *actor) (*ca
i, err = repo.NewIdentityRaw(
name,
email,
- string(actor.Login),
string(actor.AvatarUrl),
map[string]string{
metaKeyGithubLogin: string(actor.Login),
@@ -590,7 +589,6 @@ func (gi *githubImporter) getGhost(repo *cache.RepoCache) (*cache.IdentityCache,
return repo.NewIdentityRaw(
name,
"",
- string(q.User.Login),
string(q.User.AvatarUrl),
map[string]string{
metaKeyGithubLogin: string(q.User.Login),