mirror of
https://gitea.com/gitea/tea.git
synced 2025-09-03 02:18:30 +02:00
[Refactor] move pull checkout & clean code into task module (#249)
Merge branch 'master' into refactor_checkout2task move pull clean code into task module fix lint format code unify PullCheckout() and gitConfigForPR() move pull checkout code into task module Co-authored-by: 6543 <6543@noreply.gitea.io> Reviewed-on: https://gitea.com/gitea/tea/pulls/249 Reviewed-by: Norwin <noerw@noreply.gitea.io> Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-Authored-By: 6543 <6543@obermui.de> Co-Committed-By: 6543 <6543@obermui.de>
This commit is contained in:
@ -5,16 +5,13 @@
|
||||
package pulls
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"code.gitea.io/tea/cmd/flags"
|
||||
"code.gitea.io/tea/modules/config"
|
||||
local_git "code.gitea.io/tea/modules/git"
|
||||
"code.gitea.io/tea/modules/task"
|
||||
"code.gitea.io/tea/modules/utils"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
@ -38,78 +35,5 @@ func runPullsCheckout(ctx *cli.Context) error {
|
||||
return err
|
||||
}
|
||||
|
||||
localRepo, err := local_git.RepoForWorkdir()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
localBranchName, remoteBranchName, newRemoteName, remoteURL, err :=
|
||||
gitConfigForPR(localRepo, login, owner, repo, idx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// verify related remote is in local repo, otherwise add it
|
||||
localRemote, err := localRepo.GetOrCreateRemote(remoteURL, newRemoteName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
localRemoteName := localRemote.Config().Name
|
||||
|
||||
// get auth & fetch remote
|
||||
fmt.Printf("Fetching PR %v (head %s:%s) from remote '%s'\n",
|
||||
idx, remoteURL, remoteBranchName, localRemoteName)
|
||||
url, err := local_git.ParseURL(remoteURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
auth, err := local_git.GetAuthForURL(url, login.User, login.SSHKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = localRemote.Fetch(&git.FetchOptions{Auth: auth})
|
||||
if err == git.NoErrAlreadyUpToDate {
|
||||
fmt.Println(err)
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// checkout local branch
|
||||
fmt.Printf("Creating branch '%s'\n", localBranchName)
|
||||
err = localRepo.TeaCreateBranch(localBranchName, remoteBranchName, localRemoteName)
|
||||
if err == git.ErrBranchExists {
|
||||
fmt.Println("There may be changes since you last checked out, run `git pull` to get them.")
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return localRepo.TeaCheckout(localBranchName)
|
||||
}
|
||||
|
||||
func gitConfigForPR(repo *local_git.TeaRepo, login *config.Login, owner, repoName string, idx int64) (localBranch, remoteBranch, remoteName, remoteURL string, err error) {
|
||||
// fetch PR source-repo & -branch from gitea
|
||||
pr, _, err := login.Client().GetPullRequest(owner, repoName, idx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// test if we can pull via SSH, and configure git remote accordingly
|
||||
remoteURL = pr.Head.Repository.CloneURL
|
||||
keys, _, err := login.Client().ListMyPublicKeys(gitea.ListPublicKeysOptions{})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if len(keys) != 0 {
|
||||
remoteURL = pr.Head.Repository.SSHURL
|
||||
}
|
||||
|
||||
// try to find a matching existing branch, otherwise return branch in pulls/ namespace
|
||||
localBranch = fmt.Sprintf("pulls/%v-%v", idx, pr.Head.Ref)
|
||||
if b, _ := repo.TeaFindBranchBySha(pr.Head.Sha, remoteURL); b != nil {
|
||||
localBranch = b.Name
|
||||
}
|
||||
|
||||
remoteBranch = pr.Head.Ref
|
||||
remoteName = fmt.Sprintf("pulls/%v", pr.Head.Repository.Owner.UserName)
|
||||
return
|
||||
return task.PullCheckout(login, owner, repo, idx)
|
||||
}
|
||||
|
Reference in New Issue
Block a user