aboutsummaryrefslogtreecommitdiffstats
path: root/bridge/github
diff options
context:
space:
mode:
authorMichael Muré <batolettre@gmail.com>2020-02-15 03:06:10 +0100
committerMichael Muré <batolettre@gmail.com>2020-02-15 03:07:45 +0100
commit432a816dde18a12c1479ed8151a11c29929476e0 (patch)
tree89c965c63f07c1091ff34ec3d163171a73eba29f /bridge/github
parente9aff2a2a103b43852ecf7b57ae9ab297890eeed (diff)
parent362c0c7e2e1ce9a8e2918376a340c76f46569d64 (diff)
downloadgit-bug-432a816dde18a12c1479ed8151a11c29929476e0.tar.gz
Merge remote-tracking branch 'origin/master' into cheshirekow-jira
Diffstat (limited to 'bridge/github')
-rw-r--r--bridge/github/config.go198
-rw-r--r--bridge/github/export.go16
-rw-r--r--bridge/github/export_test.go8
-rw-r--r--bridge/github/github.go10
-rw-r--r--bridge/github/import.go2
-rw-r--r--bridge/github/import_test.go4
6 files changed, 79 insertions, 159 deletions
diff --git a/bridge/github/config.go b/bridge/github/config.go
index afb8086c..9167ac26 100644
--- a/bridge/github/config.go
+++ b/bridge/github/config.go
@@ -1,7 +1,6 @@
package github
import (
- "bufio"
"bytes"
"context"
"encoding/json"
@@ -10,14 +9,11 @@ import (
"io/ioutil"
"math/rand"
"net/http"
- "os"
"regexp"
"sort"
- "strconv"
"strings"
"time"
- text "github.com/MichaelMure/go-term-text"
"github.com/pkg/errors"
"github.com/MichaelMure/git-bug/bridge/core"
@@ -25,19 +21,24 @@ import (
"github.com/MichaelMure/git-bug/cache"
"github.com/MichaelMure/git-bug/input"
"github.com/MichaelMure/git-bug/repository"
- "github.com/MichaelMure/git-bug/util/colors"
)
var (
ErrBadProjectURL = errors.New("bad project url")
)
-func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (core.Configuration, error) {
- if params.BaseURL != "" {
- fmt.Println("warning: --base-url is ineffective for a Github bridge")
+func (g *Github) ValidParams() map[string]interface{} {
+ return map[string]interface{}{
+ "URL": nil,
+ "Login": nil,
+ "CredPrefix": nil,
+ "TokenRaw": nil,
+ "Owner": nil,
+ "Project": nil,
}
+}
- conf := make(core.Configuration)
+func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (core.Configuration, error) {
var err error
var owner string
var project string
@@ -121,9 +122,10 @@ func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor
return nil, fmt.Errorf("project doesn't exist or authentication token has an incorrect scope")
}
+ conf := make(core.Configuration)
conf[core.ConfigKeyTarget] = target
- conf[keyOwner] = owner
- conf[keyProject] = project
+ conf[confKeyOwner] = owner
+ conf[confKeyProject] = project
err = g.ValidateConfig(conf)
if err != nil {
@@ -141,25 +143,25 @@ func (g *Github) Configure(repo *cache.RepoCache, params core.BridgeParams) (cor
return conf, core.FinishConfig(repo, metaKeyGithubLogin, login)
}
-func (*Github) ValidateConfig(conf core.Configuration) error {
+func (Github) ValidateConfig(conf core.Configuration) error {
if v, ok := conf[core.ConfigKeyTarget]; !ok {
return fmt.Errorf("missing %s key", core.ConfigKeyTarget)
} else if v != target {
return fmt.Errorf("unexpected target name: %v", v)
}
- if _, ok := conf[keyOwner]; !ok {
- return fmt.Errorf("missing %s key", keyOwner)
+ if _, ok := conf[confKeyOwner]; !ok {
+ return fmt.Errorf("missing %s key", confKeyOwner)
}
- if _, ok := conf[keyProject]; !ok {
- return fmt.Errorf("missing %s key", keyProject)
+ if _, ok := conf[confKeyProject]; !ok {
+ return fmt.Errorf("missing %s key", confKeyProject)
}
return nil
}
-func usernameValidator(name string, value string) (string, error) {
+func usernameValidator(_ string, value string) (string, error) {
ok, err := validateUsername(value)
if err != nil {
return "", err
@@ -241,67 +243,31 @@ func randomFingerprint() string {
}
func promptTokenOptions(repo repository.RepoConfig, login, owner, project string) (auth.Credential, error) {
- for {
- creds, err := auth.List(repo,
- auth.WithTarget(target),
- auth.WithKind(auth.KindToken),
- auth.WithMeta(auth.MetaKeyLogin, login),
- )
- if err != nil {
- return nil, err
- }
-
- fmt.Println()
- fmt.Println("[1]: enter my token")
- fmt.Println("[2]: interactive token creation")
-
- if len(creds) > 0 {
- sort.Sort(auth.ById(creds))
-
- fmt.Println()
- fmt.Println("Existing tokens for Github:")
- for i, cred := range creds {
- token := cred.(*auth.Token)
- fmt.Printf("[%d]: %s => %s (login: %s, %s)\n",
- i+3,
- colors.Cyan(token.ID().Human()),
- colors.Red(text.TruncateMax(token.Value, 10)),
- token.Metadata()[auth.MetaKeyLogin],
- token.CreateTime().Format(time.RFC822),
- )
- }
- }
-
- fmt.Println()
- fmt.Print("Select option: ")
+ creds, err := auth.List(repo,
+ auth.WithTarget(target),
+ auth.WithKind(auth.KindToken),
+ auth.WithMeta(auth.MetaKeyLogin, login),
+ )
+ if err != nil {
+ return nil, err
+ }
- line, err := bufio.NewReader(os.Stdin).ReadString('\n')
- fmt.Println()
+ cred, err := input.PromptCredentialWithInteractive(target, "token", creds)
+ switch err {
+ case nil:
+ return cred, nil
+ case input.ErrDirectPrompt:
+ return promptToken()
+ case input.ErrInteractiveCreation:
+ value, err := loginAndRequestToken(login, owner, project)
if err != nil {
return nil, err
}
-
- line = strings.TrimSpace(line)
- index, err := strconv.Atoi(line)
- if err != nil || index < 1 || index > len(creds)+2 {
- fmt.Println("invalid input")
- continue
- }
-
- switch index {
- case 1:
- return promptToken()
- case 2:
- value, err := loginAndRequestToken(login, owner, project)
- if err != nil {
- return nil, err
- }
- token := auth.NewToken(target, value)
- token.SetMetadata(auth.MetaKeyLogin, login)
- return token, nil
- default:
- return creds[index-3], nil
- }
+ token := auth.NewToken(target, value)
+ token.SetMetadata(auth.MetaKeyLogin, login)
+ return token, nil
+ default:
+ return nil, err
}
}
@@ -413,73 +379,25 @@ func loginAndRequestToken(login, owner, project string) (string, error) {
}
func promptURL(repo repository.RepoCommon) (string, string, error) {
- // remote suggestions
- remotes, err := repo.GetRemotes()
+ validRemotes, err := getValidGithubRemoteURLs(repo)
if err != nil {
return "", "", err
}
- validRemotes := getValidGithubRemoteURLs(remotes)
- if len(validRemotes) > 0 {
- for {
- fmt.Println("\nDetected projects:")
-
- // print valid remote github urls
- for i, remote := range validRemotes {
- fmt.Printf("[%d]: %v\n", i+1, remote)
- }
-
- fmt.Printf("\n[0]: Another project\n\n")
- fmt.Printf("Select option: ")
-
- line, err := bufio.NewReader(os.Stdin).ReadString('\n')
- if err != nil {
- return "", "", err
- }
-
- line = strings.TrimSpace(line)
-
- index, err := strconv.Atoi(line)
- if err != nil || index < 0 || index > len(validRemotes) {
- fmt.Println("invalid input")
- continue
- }
-
- // if user want to enter another project url break this loop
- if index == 0 {
- break
- }
-
- // get owner and project with index
- owner, project, _ := splitURL(validRemotes[index-1])
- return owner, project, nil
- }
- }
-
- // manually enter github url
- for {
- fmt.Print("Github project URL: ")
-
- line, err := bufio.NewReader(os.Stdin).ReadString('\n')
- if err != nil {
- return "", "", err
- }
-
- line = strings.TrimSpace(line)
- if line == "" {
- fmt.Println("URL is empty")
- continue
- }
-
- // get owner and project from url
- owner, project, err := splitURL(line)
+ validator := func(name, value string) (string, error) {
+ _, _, err := splitURL(value)
if err != nil {
- fmt.Println(err)
- continue
+ return err.Error(), nil
}
+ return "", nil
+ }
- return owner, project, nil
+ url, err := input.PromptURLWithRemote("Github project URL", "URL", validRemotes, input.Required, validator)
+ if err != nil {
+ return "", "", err
}
+
+ return splitURL(url)
}
// splitURL extract the owner and project from a github repository URL. It will remove the
@@ -488,10 +406,7 @@ func promptURL(repo repository.RepoCommon) (string, string, error) {
func splitURL(url string) (owner string, project string, err error) {
cleanURL := strings.TrimSuffix(url, ".git")
- re, err := regexp.Compile(`github\.com[/:]([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_.]+)`)
- if err != nil {
- panic("regexp compile:" + err.Error())
- }
+ re := regexp.MustCompile(`github\.com[/:]([a-zA-Z0-9\-_]+)/([a-zA-Z0-9\-_.]+)`)
res := re.FindStringSubmatch(cleanURL)
if res == nil {
@@ -503,7 +418,12 @@ func splitURL(url string) (owner string, project string, err error) {
return
}
-func getValidGithubRemoteURLs(remotes map[string]string) []string {
+func getValidGithubRemoteURLs(repo repository.RepoCommon) ([]string, error) {
+ remotes, err := repo.GetRemotes()
+ if err != nil {
+ return nil, err
+ }
+
urls := make([]string, 0, len(remotes))
for _, url := range remotes {
// split url can work again with shortURL
@@ -516,7 +436,7 @@ func getValidGithubRemoteURLs(remotes map[string]string) []string {
sort.Strings(urls)
- return urls
+ return urls, nil
}
func validateUsername(username string) (bool, error) {
diff --git a/bridge/github/export.go b/bridge/github/export.go
index c363e188..6cee4188 100644
--- a/bridge/github/export.go
+++ b/bridge/github/export.go
@@ -139,8 +139,8 @@ func (ge *githubExporter) ExportAll(ctx context.Context, repo *cache.RepoCache,
ge.repositoryID, err = getRepositoryNodeID(
ctx,
ge.defaultToken,
- ge.conf[keyOwner],
- ge.conf[keyProject],
+ ge.conf[confKeyOwner],
+ ge.conf[confKeyProject],
)
if err != nil {
return nil, err
@@ -187,7 +187,7 @@ func (ge *githubExporter) ExportAll(ctx context.Context, repo *cache.RepoCache,
if snapshot.HasAnyActor(allIdentitiesIds...) {
// try to export the bug and it associated events
- ge.exportBug(ctx, b, since, out)
+ ge.exportBug(ctx, b, out)
}
}
}
@@ -197,7 +197,7 @@ func (ge *githubExporter) ExportAll(ctx context.Context, repo *cache.RepoCache,
}
// exportBug publish bugs and related events
-func (ge *githubExporter) exportBug(ctx context.Context, b *cache.BugCache, since time.Time, out chan<- core.ExportResult) {
+func (ge *githubExporter) exportBug(ctx context.Context, b *cache.BugCache, out chan<- core.ExportResult) {
snapshot := b.Snapshot()
var bugUpdated bool
@@ -238,7 +238,7 @@ func (ge *githubExporter) exportBug(ctx context.Context, b *cache.BugCache, sinc
}
// ignore issue coming from other repositories
- if owner != ge.conf[keyOwner] && project != ge.conf[keyProject] {
+ if owner != ge.conf[confKeyOwner] && project != ge.conf[confKeyProject] {
out <- core.NewExportNothing(b.Id(), fmt.Sprintf("skipping issue from url:%s", githubURL))
return
}
@@ -481,8 +481,8 @@ func markOperationAsExported(b *cache.BugCache, target entity.Id, githubID, gith
func (ge *githubExporter) cacheGithubLabels(ctx context.Context, gc *githubv4.Client) error {
variables := map[string]interface{}{
- "owner": githubv4.String(ge.conf[keyOwner]),
- "name": githubv4.String(ge.conf[keyProject]),
+ "owner": githubv4.String(ge.conf[confKeyOwner]),
+ "name": githubv4.String(ge.conf[confKeyProject]),
"first": githubv4.Int(10),
"after": (*githubv4.String)(nil),
}
@@ -526,7 +526,7 @@ func (ge *githubExporter) getLabelID(gc *githubv4.Client, label string) (string,
// NOTE: since createLabel mutation is still in preview mode we use github api v3 to create labels
// see https://developer.github.com/v4/mutation/createlabel/ and https://developer.github.com/v4/previews/#labels-preview
func (ge *githubExporter) createGithubLabel(ctx context.Context, label, color string) (string, error) {
- url := fmt.Sprintf("%s/repos/%s/%s/labels", githubV3Url, ge.conf[keyOwner], ge.conf[keyProject])
+ url := fmt.Sprintf("%s/repos/%s/%s/labels", githubV3Url, ge.conf[confKeyOwner], ge.conf[confKeyProject])
client := &http.Client{}
params := struct {
diff --git a/bridge/github/export_test.go b/bridge/github/export_test.go
index 56e29835..a25d56d7 100644
--- a/bridge/github/export_test.go
+++ b/bridge/github/export_test.go
@@ -188,8 +188,8 @@ func TestPushPull(t *testing.T) {
// initialize exporter
exporter := &githubExporter{}
err = exporter.Init(backend, core.Configuration{
- keyOwner: envUser,
- keyProject: projectName,
+ confKeyOwner: envUser,
+ confKeyProject: projectName,
})
require.NoError(t, err)
@@ -216,8 +216,8 @@ func TestPushPull(t *testing.T) {
importer := &githubImporter{}
err = importer.Init(backend, core.Configuration{
- keyOwner: envUser,
- keyProject: projectName,
+ confKeyOwner: envUser,
+ confKeyProject: projectName,
})
require.NoError(t, err)
diff --git a/bridge/github/github.go b/bridge/github/github.go
index 19dc8a08..a02d9460 100644
--- a/bridge/github/github.go
+++ b/bridge/github/github.go
@@ -19,8 +19,8 @@ const (
metaKeyGithubUrl = "github-url"
metaKeyGithubLogin = "github-login"
- keyOwner = "owner"
- keyProject = "project"
+ confKeyOwner = "owner"
+ confKeyProject = "project"
githubV3Url = "https://api.github.com"
defaultTimeout = 60 * time.Second
@@ -30,7 +30,7 @@ var _ core.BridgeImpl = &Github{}
type Github struct{}
-func (*Github) Target() string {
+func (Github) Target() string {
return target
}
@@ -38,11 +38,11 @@ func (g *Github) LoginMetaKey() string {
return metaKeyGithubLogin
}
-func (*Github) NewImporter() core.Importer {
+func (Github) NewImporter() core.Importer {
return &githubImporter{}
}
-func (*Github) NewExporter() core.Exporter {
+func (Github) NewExporter() core.Exporter {
return &githubExporter{}
}
diff --git a/bridge/github/import.go b/bridge/github/import.go
index ea0ccba3..3267c013 100644
--- a/bridge/github/import.go
+++ b/bridge/github/import.go
@@ -49,7 +49,7 @@ func (gi *githubImporter) Init(repo *cache.RepoCache, conf core.Configuration) e
// 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, gi.client, 10, gi.conf[keyOwner], gi.conf[keyProject], since)
+ gi.iterator = NewIterator(ctx, gi.client, 10, gi.conf[confKeyOwner], gi.conf[confKeyProject], since)
out := make(chan core.ImportResult)
gi.out = out
diff --git a/bridge/github/import_test.go b/bridge/github/import_test.go
index 7eb901d3..4f75f368 100644
--- a/bridge/github/import_test.go
+++ b/bridge/github/import_test.go
@@ -151,8 +151,8 @@ func Test_Importer(t *testing.T) {
importer := &githubImporter{}
err = importer.Init(backend, core.Configuration{
- keyOwner: "MichaelMure",
- keyProject: "git-bug-test-github-bridge",
+ confKeyOwner: "MichaelMure",
+ confKeyProject: "git-bug-test-github-bridge",
})
require.NoError(t, err)