2020-11-10 07:51:48 +01:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2023-09-08 03:40:02 +02:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-11-10 07:51:48 +01:00
|
|
|
|
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/tea/modules/config"
|
|
|
|
local_git "code.gitea.io/tea/modules/git"
|
2021-03-07 20:45:50 +01:00
|
|
|
"code.gitea.io/tea/modules/workaround"
|
2020-11-10 07:51:48 +01:00
|
|
|
|
2020-12-08 02:21:05 +01:00
|
|
|
"code.gitea.io/sdk/gitea"
|
2020-11-10 07:51:48 +01:00
|
|
|
git_config "github.com/go-git/go-git/v5/config"
|
2021-03-02 14:50:11 +01:00
|
|
|
git_plumbing "github.com/go-git/go-git/v5/plumbing"
|
2020-11-10 07:51:48 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// PullClean deletes local & remote feature-branches for a closed pull
|
2020-12-08 12:25:21 +01:00
|
|
|
func PullClean(login *config.Login, repoOwner, repoName string, index int64, ignoreSHA bool, callback func(string) (string, error)) error {
|
2020-11-10 07:51:48 +01:00
|
|
|
client := login.Client()
|
|
|
|
|
|
|
|
repo, _, err := client.GetRepo(repoOwner, repoName)
|
2020-12-17 15:00:16 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-11-10 07:51:48 +01:00
|
|
|
defaultBranch := repo.DefaultBranch
|
|
|
|
if len(defaultBranch) == 0 {
|
|
|
|
defaultBranch = "master"
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch PR source-repo & -branch from gitea
|
|
|
|
pr, _, err := client.GetPullRequest(repoOwner, repoName, index)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-07 20:45:50 +01:00
|
|
|
if err := workaround.FixPullHeadSha(client, pr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-10 07:51:48 +01:00
|
|
|
if pr.State == gitea.StateOpen {
|
|
|
|
return fmt.Errorf("PR is still open, won't delete branches")
|
|
|
|
}
|
|
|
|
|
2020-12-17 15:00:16 +01:00
|
|
|
// if remote head branch is already deleted, pr.Head.Ref points to "pulls/<idx>/head"
|
|
|
|
remoteBranch := pr.Head.Ref
|
|
|
|
remoteDeleted := remoteBranch == fmt.Sprintf("refs/pull/%d/head", pr.Index)
|
|
|
|
if remoteDeleted {
|
|
|
|
remoteBranch = pr.Head.Name // this still holds the original branch name
|
|
|
|
fmt.Printf("Remote branch '%s' already deleted.\n", remoteBranch)
|
|
|
|
}
|
2020-11-10 07:51:48 +01:00
|
|
|
|
|
|
|
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 *git_config.Branch
|
|
|
|
if ignoreSHA {
|
2020-12-17 15:00:16 +01:00
|
|
|
branch, err = r.TeaFindBranchByName(remoteBranch, pr.Head.Repository.CloneURL)
|
2020-11-10 07:51:48 +01:00
|
|
|
} else {
|
|
|
|
branch, err = r.TeaFindBranchBySha(pr.Head.Sha, pr.Head.Repository.CloneURL)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if branch == nil {
|
|
|
|
if ignoreSHA {
|
2020-12-17 15:00:16 +01:00
|
|
|
return fmt.Errorf("Remote branch %s not found in local repo", remoteBranch)
|
2020-11-10 07:51:48 +01:00
|
|
|
}
|
|
|
|
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,
|
2020-12-17 15:00:16 +01:00
|
|
|
call me again with the --ignore-sha flag`, remoteBranch)
|
2020-11-10 07:51:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2021-03-02 14:50:11 +01:00
|
|
|
ref := git_plumbing.NewBranchReferenceName(defaultBranch)
|
|
|
|
if err = r.TeaCheckout(ref); err != nil {
|
2020-11-10 07:51:48 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove local & remote branch
|
2020-12-17 15:00:16 +01:00
|
|
|
fmt.Printf("Deleting local branch %s\n", branch.Name)
|
|
|
|
err = r.TeaDeleteLocalBranch(branch)
|
2020-11-10 07:51:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-17 15:00:16 +01:00
|
|
|
|
|
|
|
if !remoteDeleted && pr.Head.Repository.Permissions.Push {
|
|
|
|
fmt.Printf("Deleting remote branch %s\n", remoteBranch)
|
|
|
|
url, err := r.TeaRemoteURL(branch.Remote)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
auth, err := local_git.GetAuthForURL(url, login.Token, login.SSHKey, callback)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = r.TeaDeleteRemoteBranch(branch.Remote, remoteBranch, auth)
|
2020-11-10 07:51:48 +01:00
|
|
|
}
|
2020-12-17 15:00:16 +01:00
|
|
|
return err
|
2020-11-10 07:51:48 +01:00
|
|
|
}
|