From afe69d0c93bf9a4aaa0b6b39e8fce48c2d4ec346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Tue, 26 Nov 2019 19:46:50 +0100 Subject: [PATCH 1/3] github: tiny cleanups of the configurator --- bridge/github/config.go | 36 ++++++++++++++++++++---------- doc/man/git-bug-bridge-configure.1 | 4 ++++ doc/md/git-bug_bridge_configure.md | 17 +++++++------- misc/bash_completion/git-bug | 4 ++++ misc/powershell_completion/git-bug | 2 ++ misc/zsh_completion/git-bug | 1 + 6 files changed, 44 insertions(+), 20 deletions(-) diff --git a/bridge/github/config.go b/bridge/github/config.go index f0403062ba0a982591e03b3766d3f320faa18b31..f6fbe7ec1ba08983c42e9de07f8acfcb21a0ff86 100644 --- a/bridge/github/config.go +++ b/bridge/github/config.go @@ -17,12 +17,14 @@ import ( "syscall" "time" + text "github.com/MichaelMure/go-term-text" "github.com/pkg/errors" "golang.org/x/crypto/ssh/terminal" "github.com/MichaelMure/git-bug/bridge/core" "github.com/MichaelMure/git-bug/entity" "github.com/MichaelMure/git-bug/repository" + "github.com/MichaelMure/git-bug/util/colors" "github.com/MichaelMure/git-bug/util/interrupt" ) @@ -43,31 +45,29 @@ var ( func (g *Github) Configure(repo repository.RepoCommon, params core.BridgeParams) (core.Configuration, error) { conf := make(core.Configuration) var err error - var token string - var tokenId entity.Id - var tokenObj *core.Token - var owner string - var project string if (params.Token != "" || params.TokenId != "" || params.TokenStdin) && (params.URL == "" && (params.Project == "" || params.Owner == "")) { return nil, fmt.Errorf("you must provide a project URL or Owner/Name to configure this bridge with a token") } + var owner string + var project string // getting owner and project name - if params.Owner != "" && params.Project != "" { + switch { + case params.Owner != "" && params.Project != "": // first try to use params if both or project and owner are provided owner = params.Owner project = params.Project - } else if params.URL != "" { + case params.URL != "": // try to parse params URL and extract owner and project owner, project, err = splitURL(params.URL) if err != nil { return nil, err } - } else { + default: // remote suggestions remotes, err := repo.GetRemotes() if err != nil { @@ -90,6 +90,10 @@ func (g *Github) Configure(repo repository.RepoCommon, params core.BridgeParams) return nil, fmt.Errorf("invalid parameter owner: %v", owner) } + var token string + var tokenId entity.Id + var tokenObj *core.Token + // try to get token from params if provided, else use terminal prompt // to either enter a token or login and generate a new one, or choose // an existing token @@ -118,7 +122,7 @@ func (g *Github) Configure(repo repository.RepoCommon, params core.BridgeParams) return nil, err } } else if tokenId != "" { - tokenObj, err = core.LoadToken(repo, entity.Id(tokenId)) + tokenObj, err = core.LoadToken(repo, tokenId) if err != nil { return nil, err } @@ -253,13 +257,21 @@ func promptTokenOptions(repo repository.RepoCommon, owner, project string) (*cor fmt.Println("[2]: interactive token creation") if len(tokens) > 0 { - fmt.Println("known tokens for Github:") + fmt.Println() + fmt.Println("Existing tokens for Github:") for i, token := range tokens { if token.Target == target { - fmt.Printf("[%d]: %s\n", i+3, token.ID()) + fmt.Printf("[%d]: %s => %s (%s)\n", + i+3, + colors.Cyan(token.ID().Human()), + text.TruncateMax(token.Value, 10), + token.CreateTime.Format(time.RFC822), + ) } } } + + fmt.Println() fmt.Print("Select option: ") line, err := bufio.NewReader(os.Stdin).ReadString('\n') @@ -330,7 +342,7 @@ func promptToken() (string, error) { } func loginAndRequestToken(owner, project string) (string, error) { - fmt.Println("git-bug will now generate an access token in your Github profile. Your credential are not stored and are only used to generate the token. The token is stored in the repository git config.") + fmt.Println("git-bug will now generate an access token in your Github profile. Your credential are not stored and are only used to generate the token. The token is stored in the global git config.") fmt.Println() fmt.Println("The access scope depend on the type of repository.") fmt.Println("Public:") diff --git a/doc/man/git-bug-bridge-configure.1 b/doc/man/git-bug-bridge-configure.1 index e93a9e6728b3595800adc04c460bafd72c1da290..6ccd44cb2172e81e667ed626c82d81554af8f596 100644 --- a/doc/man/git-bug-bridge-configure.1 +++ b/doc/man/git-bug-bridge-configure.1 @@ -47,6 +47,10 @@ Token configuration can be directly passed with the \-\-token flag or in the ter \fB\-T\fP, \fB\-\-token\fP="" The authentication token for the API +.PP +\fB\-i\fP, \fB\-\-token\-id\fP="" + The authentication token identifier for the API + .PP \fB\-\-token\-stdin\fP[=false] Will read the token from stdin and ignore \-\-token diff --git a/doc/md/git-bug_bridge_configure.md b/doc/md/git-bug_bridge_configure.md index 410183449e08c2f6be2562f4e5b6fc7b8f8f48bb..66b72a9401e1a44c49c5aab336ba3d40c2aca4d4 100644 --- a/doc/md/git-bug_bridge_configure.md +++ b/doc/md/git-bug_bridge_configure.md @@ -70,14 +70,15 @@ git bug bridge configure \ ### Options ``` - -n, --name string A distinctive name to identify the bridge - -t, --target string The target of the bridge. Valid values are [github,gitlab,launchpad-preview] - -u, --url string The URL of the target repository - -o, --owner string The owner of the target repository - -T, --token string The authentication token for the API - --token-stdin Will read the token from stdin and ignore --token - -p, --project string The name of the target repository - -h, --help help for configure + -n, --name string A distinctive name to identify the bridge + -t, --target string The target of the bridge. Valid values are [github,gitlab,launchpad-preview] + -u, --url string The URL of the target repository + -o, --owner string The owner of the target repository + -T, --token string The authentication token for the API + -i, --token-id string The authentication token identifier for the API + --token-stdin Will read the token from stdin and ignore --token + -p, --project string The name of the target repository + -h, --help help for configure ``` ### SEE ALSO diff --git a/misc/bash_completion/git-bug b/misc/bash_completion/git-bug index 9dc3ac87a107fce96235763ef27d0fd9d6e4a209..8b8502012b56337d621f4c44fa5cfb847d45a9a6 100644 --- a/misc/bash_completion/git-bug +++ b/misc/bash_completion/git-bug @@ -408,6 +408,10 @@ _git-bug_bridge_configure() two_word_flags+=("--token") two_word_flags+=("-T") local_nonpersistent_flags+=("--token=") + flags+=("--token-id=") + two_word_flags+=("--token-id") + two_word_flags+=("-i") + local_nonpersistent_flags+=("--token-id=") flags+=("--token-stdin") local_nonpersistent_flags+=("--token-stdin") flags+=("--project=") diff --git a/misc/powershell_completion/git-bug b/misc/powershell_completion/git-bug index 5f0439329b024624ca0f3ed4889974bc9d737af2..bdf08f428f2b98fc21e0f1e7227c5e41f2eaff21 100644 --- a/misc/powershell_completion/git-bug +++ b/misc/powershell_completion/git-bug @@ -83,6 +83,8 @@ Register-ArgumentCompleter -Native -CommandName 'git-bug' -ScriptBlock { [CompletionResult]::new('--owner', 'owner', [CompletionResultType]::ParameterName, 'The owner of the target repository') [CompletionResult]::new('-T', 'T', [CompletionResultType]::ParameterName, 'The authentication token for the API') [CompletionResult]::new('--token', 'token', [CompletionResultType]::ParameterName, 'The authentication token for the API') + [CompletionResult]::new('-i', 'i', [CompletionResultType]::ParameterName, 'The authentication token identifier for the API') + [CompletionResult]::new('--token-id', 'token-id', [CompletionResultType]::ParameterName, 'The authentication token identifier for the API') [CompletionResult]::new('--token-stdin', 'token-stdin', [CompletionResultType]::ParameterName, 'Will read the token from stdin and ignore --token') [CompletionResult]::new('-p', 'p', [CompletionResultType]::ParameterName, 'The name of the target repository') [CompletionResult]::new('--project', 'project', [CompletionResultType]::ParameterName, 'The name of the target repository') diff --git a/misc/zsh_completion/git-bug b/misc/zsh_completion/git-bug index 230061dd6e859a2e7328143adf6254db95ca1e0f..7a4580c01fb2ada7fbf8019b591a128a0ce37a33 100644 --- a/misc/zsh_completion/git-bug +++ b/misc/zsh_completion/git-bug @@ -195,6 +195,7 @@ function _git-bug_bridge_configure { '(-u --url)'{-u,--url}'[The URL of the target repository]:' \ '(-o --owner)'{-o,--owner}'[The owner of the target repository]:' \ '(-T --token)'{-T,--token}'[The authentication token for the API]:' \ + '(-i --token-id)'{-i,--token-id}'[The authentication token identifier for the API]:' \ '--token-stdin[Will read the token from stdin and ignore --token]' \ '(-p --project)'{-p,--project}'[The name of the target repository]:' } From 4f856d6f78f8ffe1795a71195244e5a332ac3215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Tue, 26 Nov 2019 20:45:32 +0100 Subject: [PATCH 2/3] gitlab: configurator cleanup --- bridge/github/config.go | 2 +- bridge/gitlab/config.go | 35 ++++++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/bridge/github/config.go b/bridge/github/config.go index f6fbe7ec1ba08983c42e9de07f8acfcb21a0ff86..9d059b0029f6ce23056de50db961d524e06755bc 100644 --- a/bridge/github/config.go +++ b/bridge/github/config.go @@ -253,7 +253,7 @@ func promptTokenOptions(repo repository.RepoCommon, owner, project string) (*cor } fmt.Println() - fmt.Println("[1]: user provided token") + fmt.Println("[1]: enter my token") fmt.Println("[2]: interactive token creation") if len(tokens) > 0 { diff --git a/bridge/gitlab/config.go b/bridge/gitlab/config.go index 5c6ccb0e93651afb13e3d32b7ca368d1d3c80f45..6b85e8cbcae329b859a468cb2698c9f6cb9c8d34 100644 --- a/bridge/gitlab/config.go +++ b/bridge/gitlab/config.go @@ -8,13 +8,16 @@ import ( "regexp" "strconv" "strings" + "time" + text "github.com/MichaelMure/go-term-text" "github.com/pkg/errors" "github.com/xanzy/go-gitlab" "github.com/MichaelMure/git-bug/bridge/core" "github.com/MichaelMure/git-bug/entity" "github.com/MichaelMure/git-bug/repository" + "github.com/MichaelMure/git-bug/util/colors" ) var ( @@ -135,17 +138,31 @@ func promptTokenOptions(repo repository.RepoCommon) (*core.Token, error) { return nil, err } + if len(tokens) == 0 { + token, err := promptToken() + if err != nil { + return nil, err + } + return core.LoadOrCreateToken(repo, target, token) + } + fmt.Println() - fmt.Println("[1]: user provided token") - - if len(tokens) > 0 { - fmt.Println("known tokens for Gitlab:") - for i, token := range tokens { - if token.Target == target { - fmt.Printf("[%d]: %s\n", i+2, token.ID()) - } + fmt.Println("[1]: enter my token") + + fmt.Println() + fmt.Println("Existing tokens for Gitlab:") + for i, token := range tokens { + if token.Target == target { + fmt.Printf("[%d]: %s => %s (%s)\n", + i+2, + colors.Cyan(token.ID().Human()), + text.TruncateMax(token.Value, 10), + token.CreateTime.Format(time.RFC822), + ) } } + + fmt.Println() fmt.Print("Select option: ") line, err := bufio.NewReader(os.Stdin).ReadString('\n') @@ -251,7 +268,7 @@ func promptURL(remotes map[string]string) (string, error) { } url := strings.TrimSpace(line) - if line == "" { + if url == "" { fmt.Println("URL is empty") continue } From 1a1e313f3028243680c29520d96f722092a2bd38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Tue, 26 Nov 2019 20:46:00 +0100 Subject: [PATCH 3/3] bridge: move export event handling to the CLI --- bridge/core/bridge.go | 28 +++++----------------------- commands/bridge_push.go | 16 +++++++++++++++- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/bridge/core/bridge.go b/bridge/core/bridge.go index 3a36dfaa8a0dcc10ca7d05d13e0af6bec76c0e6a..1cad10e9307619cd2e0d883f3a2dbc0dfa0eb101 100644 --- a/bridge/core/bridge.go +++ b/bridge/core/bridge.go @@ -365,39 +365,21 @@ func (b *Bridge) ImportAll(ctx context.Context) (<-chan ImportResult, error) { return b.ImportAllSince(ctx, time.Time{}) } -func (b *Bridge) ExportAll(ctx context.Context, since time.Time) error { +func (b *Bridge) ExportAll(ctx context.Context, since time.Time) (<-chan ExportResult, error) { exporter := b.getExporter() if exporter == nil { - return ErrExportNotSupported + return nil, ErrExportNotSupported } err := b.ensureConfig() if err != nil { - return err + return nil, err } err = b.ensureInit() if err != nil { - return err - } - - events, err := exporter.ExportAll(ctx, b.repo, since) - if err != nil { - return err - } - - exportedIssues := 0 - for result := range events { - if result.Event != ExportEventNothing { - fmt.Println(result.String()) - } - - switch result.Event { - case ExportEventBug: - exportedIssues++ - } + return nil, err } - fmt.Printf("exported %d issues with %s bridge\n", exportedIssues, b.Name) - return nil + return exporter.ExportAll(ctx, b.repo, since) } diff --git a/commands/bridge_push.go b/commands/bridge_push.go index 26f42dc42feaf43e6d69fed5d6a145111e234a11..95ad5f5e422668192433a8d82232dab21acee34f 100644 --- a/commands/bridge_push.go +++ b/commands/bridge_push.go @@ -63,11 +63,25 @@ func runBridgePush(cmd *cobra.Command, args []string) error { return nil }) - err = b.ExportAll(ctx, time.Time{}) + events, err := b.ExportAll(ctx, time.Time{}) if err != nil { return err } + exportedIssues := 0 + for result := range events { + if result.Event != core.ExportEventNothing { + fmt.Println(result.String()) + } + + switch result.Event { + case core.ExportEventBug: + exportedIssues++ + } + } + + fmt.Printf("exported %d issues with %s bridge\n", exportedIssues, b.Name) + // send done signal close(done) return nil