aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/core
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2019-11-24 23:54:23 +0100
committerGitHub <noreply@github.com>2019-11-24 23:54:23 +0100
commit44f648a931b43d707d37cb469fead5e21a615e04 (patch)
tree356bbe10b81a1ed06ded54a5570027fda51fead6 /bridge/core
parent6345895a3d9a055927f1d4b78f254a55c1d271b6 (diff)
parent06abb5a5312dfaa1edac58cced94691e477c2ed7 (diff)
downloadgit-bug-44f648a931b43d707d37cb469fead5e21a615e04.tar.gz
Merge pull request #260 from MichaelMure/bridge
Support bridge configuration with global tokens
Diffstat (limited to 'bridge/core')
-rw-r--r--bridge/core/bridge.go15
-rw-r--r--bridge/core/token.go118
2 files changed, 129 insertions, 4 deletions
diff --git a/bridge/core/bridge.go b/bridge/core/bridge.go
index a3133b9c..3a36dfaa 100644
--- a/bridge/core/bridge.go
+++ b/bridge/core/bridge.go
@@ -13,6 +13,7 @@ import (
"github.com/pkg/errors"
"github.com/MichaelMure/git-bug/cache"
+ "github.com/MichaelMure/git-bug/entity"
"github.com/MichaelMure/git-bug/repository"
)
@@ -20,8 +21,10 @@ var ErrImportNotSupported = errors.New("import is not supported")
var ErrExportNotSupported = errors.New("export is not supported")
const (
- ConfigKeyTarget = "target"
- MetaKeyOrigin = "origin"
+ ConfigKeyTarget = "target"
+ ConfigKeyToken = "token"
+ ConfigKeyTokenId = "token-id"
+ MetaKeyOrigin = "origin"
bridgeConfigKeyPrefix = "git-bug.bridge"
)
@@ -35,6 +38,7 @@ type BridgeParams struct {
Project string
URL string
Token string
+ TokenId string
TokenStdin bool
}
@@ -276,6 +280,13 @@ func (b *Bridge) ensureInit() error {
return nil
}
+ token, err := LoadToken(b.repo, entity.Id(b.conf[ConfigKeyTokenId]))
+ if err != nil {
+ return err
+ }
+
+ b.conf[ConfigKeyToken] = token.Value
+
importer := b.getImporter()
if importer != nil {
err := importer.Init(b.conf)
diff --git a/bridge/core/token.go b/bridge/core/token.go
index 449ebbb5..28c64f5c 100644
--- a/bridge/core/token.go
+++ b/bridge/core/token.go
@@ -122,8 +122,7 @@ func LoadTokenPrefix(repo repository.RepoCommon, prefix string) (*Token, error)
return LoadToken(repo, matching[0])
}
-// ListTokens return a map representing the stored tokens in the repo config and global config
-// along with their type (global: true, local:false)
+// ListTokens list all existing token ids
func ListTokens(repo repository.RepoCommon) ([]entity.Id, error) {
configs, err := repo.GlobalConfig().ReadAll(tokenConfigKeyPrefix + ".")
if err != nil {
@@ -157,6 +156,99 @@ func ListTokens(repo repository.RepoCommon) ([]entity.Id, error) {
return result, nil
}
+// ListTokensWithTarget list all token ids associated with the target
+func ListTokensWithTarget(repo repository.RepoCommon, target string) ([]entity.Id, error) {
+ var ids []entity.Id
+ tokensIds, err := ListTokens(repo)
+ if err != nil {
+ return nil, err
+ }
+
+ for _, tokenId := range tokensIds {
+ token, err := LoadToken(repo, tokenId)
+ if err != nil {
+ return nil, err
+ }
+
+ if token.Target == target {
+ ids = append(ids, tokenId)
+ }
+ }
+ return ids, nil
+}
+
+// LoadTokens load all existing tokens
+func LoadTokens(repo repository.RepoCommon) ([]*Token, error) {
+ tokensIds, err := ListTokens(repo)
+ if err != nil {
+ return nil, err
+ }
+
+ var tokens []*Token
+ for _, id := range tokensIds {
+ token, err := LoadToken(repo, id)
+ if err != nil {
+ return nil, err
+ }
+ tokens = append(tokens, token)
+ }
+ return tokens, nil
+}
+
+// LoadTokensWithTarget load all existing tokens for a given target
+func LoadTokensWithTarget(repo repository.RepoCommon, target string) ([]*Token, error) {
+ tokensIds, err := ListTokens(repo)
+ if err != nil {
+ return nil, err
+ }
+
+ var tokens []*Token
+ for _, id := range tokensIds {
+ token, err := LoadToken(repo, id)
+ if err != nil {
+ return nil, err
+ }
+ if token.Target == target {
+ tokens = append(tokens, token)
+ }
+ }
+ return tokens, nil
+}
+
+// TokenIdExist return wether token id exist or not
+func TokenIdExist(repo repository.RepoCommon, id entity.Id) bool {
+ _, err := LoadToken(repo, id)
+ return err == nil
+}
+
+// TokenExist return wether there is a token with a certain value or not
+func TokenExist(repo repository.RepoCommon, value string) bool {
+ tokens, err := LoadTokens(repo)
+ if err != nil {
+ return false
+ }
+ for _, token := range tokens {
+ if token.Value == value {
+ return true
+ }
+ }
+ return false
+}
+
+// TokenExistWithTarget same as TokenExist but restrict search for a given target
+func TokenExistWithTarget(repo repository.RepoCommon, value string, target string) bool {
+ tokens, err := LoadTokensWithTarget(repo, target)
+ if err != nil {
+ return false
+ }
+ for _, token := range tokens {
+ if token.Value == value {
+ return true
+ }
+ }
+ return false
+}
+
// StoreToken stores a token in the repo config
func StoreToken(repo repository.RepoCommon, token *Token) error {
storeValueKey := fmt.Sprintf("git-bug.token.%s.%s", token.ID().String(), tokenValueKey)
@@ -180,3 +272,25 @@ func RemoveToken(repo repository.RepoCommon, id entity.Id) error {
keyPrefix := fmt.Sprintf("git-bug.token.%s", id)
return repo.GlobalConfig().RemoveAll(keyPrefix)
}
+
+// LoadOrCreateToken will try to load a token matching the same value or create it
+func LoadOrCreateToken(repo repository.RepoCommon, target, tokenValue string) (*Token, error) {
+ tokens, err := LoadTokensWithTarget(repo, target)
+ if err != nil {
+ return nil, err
+ }
+
+ for _, token := range tokens {
+ if token.Value == tokenValue {
+ return token, nil
+ }
+ }
+
+ token := NewToken(tokenValue, target)
+ err = StoreToken(repo, token)
+ if err != nil {
+ return nil, err
+ }
+
+ return token, nil
+}