2020-11-10 14:51:48 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2023-09-08 09:40:02 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-11-10 14:51:48 +08:00
|
|
|
|
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-03-12 02:16:02 +08:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2020-11-10 14:51:48 +08:00
|
|
|
"code.gitea.io/tea/modules/config"
|
|
|
|
local_git "code.gitea.io/tea/modules/git"
|
2021-03-08 03:45:50 +08:00
|
|
|
"code.gitea.io/tea/modules/workaround"
|
2020-12-08 19:25:21 +08:00
|
|
|
|
2020-11-10 14:51:48 +08:00
|
|
|
"github.com/go-git/go-git/v5"
|
2021-03-12 02:16:02 +08:00
|
|
|
git_config "github.com/go-git/go-git/v5/config"
|
2021-03-02 21:50:11 +08:00
|
|
|
git_plumbing "github.com/go-git/go-git/v5/plumbing"
|
2020-11-10 14:51:48 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// PullCheckout checkout current workdir to the head branch of specified pull request
|
2021-03-12 02:16:02 +08:00
|
|
|
func PullCheckout(
|
|
|
|
login *config.Login,
|
|
|
|
repoOwner, repoName string,
|
|
|
|
forceCreateBranch bool,
|
|
|
|
index int64,
|
|
|
|
callback func(string) (string, error),
|
|
|
|
) error {
|
2020-11-10 14:51:48 +08:00
|
|
|
client := login.Client()
|
2021-03-12 02:16:02 +08:00
|
|
|
pr, _, err := client.GetPullRequest(repoOwner, repoName, index)
|
|
|
|
if err != nil {
|
2021-07-03 22:39:05 +08:00
|
|
|
return fmt.Errorf("couldn't fetch PR: %s", err)
|
2021-03-12 02:16:02 +08:00
|
|
|
}
|
|
|
|
if err := workaround.FixPullHeadSha(client, pr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-10 14:51:48 +08:00
|
|
|
|
2021-03-12 02:16:02 +08:00
|
|
|
// FIXME: should use ctx.LocalRepo..?
|
2020-11-10 14:51:48 +08:00
|
|
|
localRepo, err := local_git.RepoForWorkdir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-12 02:16:02 +08:00
|
|
|
// find or create a matching remote
|
|
|
|
remoteURL := remoteURLForPR(login, pr)
|
|
|
|
newRemoteName := fmt.Sprintf("pulls/%v", pr.Head.Repository.Owner.UserName)
|
|
|
|
// verify related remote is in local repo, otherwise add it
|
|
|
|
localRemote, err := localRepo.GetOrCreateRemote(remoteURL, newRemoteName)
|
2020-11-10 14:51:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-12 02:16:02 +08:00
|
|
|
localRemoteName := localRemote.Config().Name
|
|
|
|
|
|
|
|
localRemoteBranchName, err := doPRFetch(login, pr, localRepo, localRemote, callback)
|
|
|
|
if err != nil {
|
2021-03-08 03:45:50 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-12 02:16:02 +08:00
|
|
|
return doPRCheckout(localRepo, pr, localRemoteName, localRemoteBranchName, remoteURL, forceCreateBranch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isRemoteDeleted(pr *gitea.PullRequest) bool {
|
|
|
|
return pr.Head.Ref == fmt.Sprintf("refs/pull/%d/head", pr.Index)
|
|
|
|
}
|
2020-11-10 14:51:48 +08:00
|
|
|
|
2021-03-12 02:16:02 +08:00
|
|
|
func remoteURLForPR(login *config.Login, pr *gitea.PullRequest) string {
|
|
|
|
repo := pr.Head.Repository
|
|
|
|
if isRemoteDeleted(pr) {
|
|
|
|
repo = pr.Base.Repository
|
|
|
|
}
|
2020-12-11 21:42:41 +08:00
|
|
|
if len(login.SSHKey) != 0 {
|
|
|
|
// login.SSHKey is nonempty, if user specified a key manually or we automatically
|
|
|
|
// found a matching private key on this machine during login creation.
|
|
|
|
// this means, we are very likely to have a working ssh setup.
|
2021-03-12 02:16:02 +08:00
|
|
|
return repo.SSHURL
|
2020-11-10 14:51:48 +08:00
|
|
|
}
|
2021-03-12 02:16:02 +08:00
|
|
|
return repo.CloneURL
|
|
|
|
}
|
2020-11-10 14:51:48 +08:00
|
|
|
|
2021-03-12 02:16:02 +08:00
|
|
|
func doPRFetch(
|
|
|
|
login *config.Login,
|
|
|
|
pr *gitea.PullRequest,
|
|
|
|
localRepo *local_git.TeaRepo,
|
|
|
|
localRemote *git.Remote,
|
|
|
|
callback func(string) (string, error),
|
|
|
|
) (string, error) {
|
2020-11-10 14:51:48 +08:00
|
|
|
localRemoteName := localRemote.Config().Name
|
2021-03-12 02:16:02 +08:00
|
|
|
localBranchName := pr.Head.Ref
|
2020-12-11 21:42:41 +08:00
|
|
|
// get auth & fetch remote via its configured protocol
|
|
|
|
url, err := localRepo.TeaRemoteURL(localRemoteName)
|
2020-11-10 14:51:48 +08:00
|
|
|
if err != nil {
|
2021-03-12 02:16:02 +08:00
|
|
|
return "", err
|
2020-11-10 14:51:48 +08:00
|
|
|
}
|
2020-12-08 19:25:21 +08:00
|
|
|
auth, err := local_git.GetAuthForURL(url, login.Token, login.SSHKey, callback)
|
2020-11-10 14:51:48 +08:00
|
|
|
if err != nil {
|
2021-03-12 02:16:02 +08:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
fetchOpts := &git.FetchOptions{Auth: auth}
|
|
|
|
if isRemoteDeleted(pr) {
|
|
|
|
// When the head branch is already deleted, pr.Head.Ref points to
|
|
|
|
// `refs/pull/<idx>/head`, where the commits stay available.
|
|
|
|
// This ref must be fetched explicitly, and does not allow pushing, so we use it
|
|
|
|
// only in this case as fallback.
|
|
|
|
localBranchName = fmt.Sprintf("pulls/%d", pr.Index)
|
|
|
|
fetchOpts.RefSpecs = []git_config.RefSpec{git_config.RefSpec(fmt.Sprintf("%s:refs/remotes/%s/%s",
|
|
|
|
pr.Head.Ref,
|
|
|
|
localRemoteName,
|
|
|
|
localBranchName,
|
|
|
|
))}
|
2020-11-10 14:51:48 +08:00
|
|
|
}
|
2021-03-12 02:16:02 +08:00
|
|
|
fmt.Printf("Fetching PR %v (head %s:%s) from remote '%s'\n", pr.Index, url, pr.Head.Ref, localRemoteName)
|
|
|
|
|
|
|
|
err = localRemote.Fetch(fetchOpts)
|
2020-11-10 14:51:48 +08:00
|
|
|
if err == git.NoErrAlreadyUpToDate {
|
|
|
|
fmt.Println(err)
|
|
|
|
} else if err != nil {
|
2021-03-12 02:16:02 +08:00
|
|
|
return "", err
|
2020-11-10 14:51:48 +08:00
|
|
|
}
|
2021-03-12 02:16:02 +08:00
|
|
|
return localBranchName, nil
|
|
|
|
}
|
2020-11-10 14:51:48 +08:00
|
|
|
|
2021-03-12 02:16:02 +08:00
|
|
|
func doPRCheckout(
|
|
|
|
localRepo *local_git.TeaRepo,
|
|
|
|
pr *gitea.PullRequest,
|
|
|
|
localRemoteName,
|
|
|
|
localRemoteBranchName,
|
|
|
|
remoteURL string,
|
|
|
|
forceCreateBranch bool,
|
|
|
|
) error {
|
|
|
|
// determine the ref to checkout, depending on existence of a matching commit on a local branch
|
2021-03-02 21:50:11 +08:00
|
|
|
var info string
|
|
|
|
var checkoutRef git_plumbing.ReferenceName
|
|
|
|
|
|
|
|
if b, _ := localRepo.TeaFindBranchBySha(pr.Head.Sha, remoteURL); b != nil {
|
2021-03-12 02:16:02 +08:00
|
|
|
|
2021-03-02 21:50:11 +08:00
|
|
|
// if a matching branch exists, use that
|
|
|
|
checkoutRef = git_plumbing.NewBranchReferenceName(b.Name)
|
|
|
|
info = fmt.Sprintf("Found matching local branch %s, checking it out", checkoutRef.Short())
|
2021-03-12 02:16:02 +08:00
|
|
|
|
2021-03-02 21:50:11 +08:00
|
|
|
} else if forceCreateBranch {
|
2021-03-12 02:16:02 +08:00
|
|
|
|
2021-03-02 21:50:11 +08:00
|
|
|
// create a branch if wanted
|
2021-03-12 02:16:02 +08:00
|
|
|
localBranchName := fmt.Sprintf("pulls/%v", pr.Index)
|
|
|
|
if isRemoteDeleted(pr) {
|
|
|
|
localBranchName += "-" + pr.Head.Ref
|
|
|
|
}
|
2021-03-02 21:50:11 +08:00
|
|
|
checkoutRef = git_plumbing.NewBranchReferenceName(localBranchName)
|
2021-03-12 02:16:02 +08:00
|
|
|
if err := localRepo.TeaCreateBranch(localBranchName, localRemoteBranchName, localRemoteName); err == nil {
|
2021-03-02 21:50:11 +08:00
|
|
|
info = fmt.Sprintf("Created branch '%s'\n", localBranchName)
|
|
|
|
} else if err == git.ErrBranchExists {
|
|
|
|
info = "There may be changes since you last checked out, run `git pull` to get them."
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-12 02:16:02 +08:00
|
|
|
|
2021-03-02 21:50:11 +08:00
|
|
|
} else {
|
2021-03-12 02:16:02 +08:00
|
|
|
|
2021-03-02 21:50:11 +08:00
|
|
|
// use the remote tracking branch
|
2021-03-12 02:16:02 +08:00
|
|
|
checkoutRef = git_plumbing.NewRemoteReferenceName(localRemoteName, localRemoteBranchName)
|
2021-03-02 21:50:11 +08:00
|
|
|
info = fmt.Sprintf(
|
|
|
|
"Checking out remote tracking branch %s. To make changes, create a new branch:\n git checkout %s",
|
2021-03-12 02:16:02 +08:00
|
|
|
checkoutRef.String(), localRemoteBranchName)
|
|
|
|
|
2020-11-10 14:51:48 +08:00
|
|
|
}
|
|
|
|
|
2021-03-02 21:50:11 +08:00
|
|
|
fmt.Println(info)
|
|
|
|
return localRepo.TeaCheckout(checkoutRef)
|
2020-11-10 14:51:48 +08:00
|
|
|
}
|