From c98441b13c78570d5b3a64482651a9d7bc84b9a3 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 8 Dec 2020 19:25:21 +0800 Subject: [PATCH] Remove Interact Dependency Of Task Module (#280) remove interact dependency in task module accept nil callback format code Reviewed-on: https://gitea.com/gitea/tea/pulls/280 Reviewed-by: Norwin Reviewed-by: Andrew Thornton Co-Authored-By: 6543 <6543@obermui.de> Co-Committed-By: 6543 <6543@obermui.de> --- cmd/pulls/checkout.go | 3 ++- cmd/pulls/clean.go | 3 ++- modules/git/auth.go | 12 ++++++------ modules/task/pull_checkout.go | 6 +++--- modules/task/pull_clean.go | 5 ++--- 5 files changed, 15 insertions(+), 14 deletions(-) diff --git a/cmd/pulls/checkout.go b/cmd/pulls/checkout.go index b072f00..03b1a29 100644 --- a/cmd/pulls/checkout.go +++ b/cmd/pulls/checkout.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/config" + "code.gitea.io/tea/modules/interact" "code.gitea.io/tea/modules/task" "code.gitea.io/tea/modules/utils" @@ -35,5 +36,5 @@ func runPullsCheckout(ctx *cli.Context) error { return err } - return task.PullCheckout(login, owner, repo, idx) + return task.PullCheckout(login, owner, repo, idx, interact.PromptPassword) } diff --git a/cmd/pulls/clean.go b/cmd/pulls/clean.go index a750061..dee4470 100644 --- a/cmd/pulls/clean.go +++ b/cmd/pulls/clean.go @@ -9,6 +9,7 @@ import ( "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/config" + "code.gitea.io/tea/modules/interact" "code.gitea.io/tea/modules/task" "code.gitea.io/tea/modules/utils" @@ -41,5 +42,5 @@ func runPullsClean(ctx *cli.Context) error { return err } - return task.PullClean(login, owner, repo, idx, ctx.Bool("ignore-sha")) + return task.PullClean(login, owner, repo, idx, ctx.Bool("ignore-sha"), interact.PromptPassword) } diff --git a/modules/git/auth.go b/modules/git/auth.go index 7d9dd63..7b78bad 100644 --- a/modules/git/auth.go +++ b/modules/git/auth.go @@ -17,7 +17,7 @@ import ( "golang.org/x/crypto/ssh" ) -type pwCallback = func(ctx string) (string, error) +type pwCallback = func(string) (string, error) // GetAuthForURL returns the appropriate AuthMethod to be used in Push() / Pull() // operations depending on the protocol, and prompts the user for credentials if @@ -32,10 +32,10 @@ func GetAuthForURL(remoteURL *url.URL, authToken, keyFile string, passwordCallba // try to select right key via ssh-agent. if it fails, try to read a key manually user := remoteURL.User.Username() auth, err = gogit_ssh.DefaultAuthBuilder(user) - if err != nil { - signer, err := readSSHPrivKey(keyFile, passwordCallback) - if err != nil { - return nil, err + if err != nil && passwordCallback != nil { + signer, err2 := readSSHPrivKey(keyFile, passwordCallback) + if err2 != nil { + return nil, err2 } auth = &gogit_ssh.PublicKeys{User: user, Signer: signer} } @@ -44,7 +44,7 @@ func GetAuthForURL(remoteURL *url.URL, authToken, keyFile string, passwordCallba return nil, fmt.Errorf("don't know how to handle url scheme %v", remoteURL.Scheme) } - return auth, nil + return } func readSSHPrivKey(keyFile string, passwordCallback pwCallback) (sig ssh.Signer, err error) { diff --git a/modules/task/pull_checkout.go b/modules/task/pull_checkout.go index ba7e1f9..14e48e0 100644 --- a/modules/task/pull_checkout.go +++ b/modules/task/pull_checkout.go @@ -10,12 +10,12 @@ import ( "code.gitea.io/sdk/gitea" "code.gitea.io/tea/modules/config" local_git "code.gitea.io/tea/modules/git" - "code.gitea.io/tea/modules/interact" + "github.com/go-git/go-git/v5" ) // PullCheckout checkout current workdir to the head branch of specified pull request -func PullCheckout(login *config.Login, repoOwner, repoName string, index int64) error { +func PullCheckout(login *config.Login, repoOwner, repoName string, index int64, callback func(string) (string, error)) error { client := login.Client() localRepo, err := local_git.RepoForWorkdir() @@ -60,7 +60,7 @@ func PullCheckout(login *config.Login, repoOwner, repoName string, index int64) if err != nil { return err } - auth, err := local_git.GetAuthForURL(url, login.Token, login.SSHKey, interact.PromptPassword) + auth, err := local_git.GetAuthForURL(url, login.Token, login.SSHKey, callback) if err != nil { return err } diff --git a/modules/task/pull_clean.go b/modules/task/pull_clean.go index f944efb..fc7e20a 100644 --- a/modules/task/pull_clean.go +++ b/modules/task/pull_clean.go @@ -9,14 +9,13 @@ import ( "code.gitea.io/tea/modules/config" local_git "code.gitea.io/tea/modules/git" - "code.gitea.io/tea/modules/interact" "code.gitea.io/sdk/gitea" git_config "github.com/go-git/go-git/v5/config" ) // PullClean deletes local & remote feature-branches for a closed pull -func PullClean(login *config.Login, repoOwner, repoName string, index int64, ignoreSHA bool) error { +func PullClean(login *config.Login, repoOwner, repoName string, index int64, ignoreSHA bool, callback func(string) (string, error)) error { client := login.Client() repo, _, err := client.GetRepo(repoOwner, repoName) @@ -79,7 +78,7 @@ call me again with the --ignore-sha flag`, pr.Head.Ref) if err != nil { return err } - auth, err := local_git.GetAuthForURL(url, login.Token, login.SSHKey, interact.PromptPassword) + auth, err := local_git.GetAuthForURL(url, login.Token, login.SSHKey, callback) if err != nil { return err }