mirror of
https://gitea.com/gitea/tea.git
synced 2026-10-04 09:14:32 +00:00
bcd62a1fb2
Fixes #866 ## Problem `tea` still prompts for an SSH key passphrase for logins created with `--ssh-agent-key` or `--ssh-agent-principal`. During `tea login add`, tea auto-discovers a matching private key in `~/.ssh` even when the login is configured to use the running ssh-agent. That on-disk key is stored in `ssh_key`, so later `Login.Client()` asks for its passphrase and tells the SDK to load the file from disk instead of signing through the agent. ## Changes - Stop auto-discovering a private key when the login uses the ssh-agent. - Add `Login.SSHKeyPath()` and use it for HTTPSign and git auth, so agent logins pass an empty key path and the SDK talks to `ssh-agent`. - Prefer SSH remotes for agent logins, matching key-file logins. - Add unit tests for the new key-path and auto-discovery behavior. ## Testing - `go test ./modules/config ./modules/task ./modules/git` - `go build ./...` --------- Co-authored-by: bircni <bircni@icloud.com> Reviewed-on: https://gitea.com/gitea/tea/pulls/1102 Reviewed-by: bircni <bircni@icloud.com> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
106 lines
3.0 KiB
Go
106 lines
3.0 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package task
|
|
|
|
import (
|
|
stdctx "context"
|
|
"fmt"
|
|
|
|
gitea "gitea.dev/sdk"
|
|
|
|
"gitea.dev/tea/modules/config"
|
|
local_git "gitea.dev/tea/modules/git"
|
|
)
|
|
|
|
// PullClean deletes local & remote feature-branches for a closed pull
|
|
func PullClean(ctx stdctx.Context, login *config.Login, repoOwner, repoName string, index int64, ignoreSHA bool, callback func(string) (string, error)) error {
|
|
client := login.Client()
|
|
|
|
repo, _, err := client.Repositories.GetRepo(ctx, repoOwner, repoName)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defaultBranch := repo.DefaultBranch
|
|
if len(defaultBranch) == 0 {
|
|
defaultBranch = "master"
|
|
}
|
|
|
|
// fetch PR source-repo & -branch from gitea
|
|
pr, _, err := client.PullRequests.GetPullRequest(ctx, repoOwner, repoName, index)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if pr.State == gitea.StateOpen {
|
|
return fmt.Errorf("PR is still open, won't delete branches")
|
|
}
|
|
|
|
// if remote head branch is already deleted, pr.Head.Ref points to "pulls/<idx>/head"
|
|
remoteBranch := pr.Head.Ref
|
|
remoteDeleted := isRemoteDeleted(pr)
|
|
if remoteDeleted {
|
|
remoteBranch = pr.Head.Name // this still holds the original branch name
|
|
fmt.Printf("Remote branch '%s' already deleted.\n", remoteBranch)
|
|
}
|
|
|
|
r, err := local_git.RepoForWorkdir()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// find a branch with matching sha or name, that has a remote matching the repo url
|
|
var branch *local_git.Branch
|
|
if ignoreSHA {
|
|
branch, err = r.TeaFindBranchByName(remoteBranch, pr.Head.Repository.CloneURL)
|
|
} else {
|
|
branch, err = r.TeaFindBranchBySha(pr.Head.Sha, pr.Head.Repository.CloneURL)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if branch == nil {
|
|
if ignoreSHA {
|
|
return fmt.Errorf("remote branch %s not found in local repo", remoteBranch)
|
|
}
|
|
return fmt.Errorf(`remote branch %s not found in local repo.
|
|
Either you don't track this PR, or the local branch has diverged from the remote.
|
|
If you still want to continue & are sure you don't loose any important commits,
|
|
call me again with the --ignore-sha flag`, remoteBranch)
|
|
}
|
|
|
|
// prepare deletion of local branch:
|
|
headRef, err := r.Head()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if headRef.Name().Short() == branch.Name {
|
|
fmt.Printf("Checking out '%s' to delete local branch '%s'\n", defaultBranch, branch.Name)
|
|
ref := local_git.NewBranchReferenceName(defaultBranch)
|
|
if err = r.TeaCheckout(ref); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// remove local & remote branch
|
|
fmt.Printf("Deleting local branch %s\n", branch.Name)
|
|
err = r.TeaDeleteLocalBranch(branch)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !remoteDeleted && pr.Head.Repository.Permissions.Push {
|
|
fmt.Printf("Deleting remote branch %s\n", remoteBranch)
|
|
url, urlErr := r.TeaRemoteURL(branch.Remote)
|
|
if urlErr != nil {
|
|
return urlErr
|
|
}
|
|
auth, authErr := local_git.GetAuthForURL(url, login.GetAccessToken(), login.SSHKeyPath(), callback)
|
|
if authErr != nil {
|
|
return authErr
|
|
}
|
|
return r.TeaDeleteRemoteBranch(branch.Remote, remoteBranch, auth)
|
|
}
|
|
return nil
|
|
}
|