mirror of
https://github.com/go-gitea/gitea.git
synced 2024-12-22 17:34:03 +03:00
A small refactor for agit implementation (#29614)
This PR made the code simpler, reduced unnecessary database queries and fixed some warnning for the errors.New . --------- Co-authored-by: KN4CK3R <admin@oldschoolhack.me>
This commit is contained in:
parent
8d32f3cb74
commit
da15d6127c
@ -7,6 +7,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
issues_model "code.gitea.io/gitea/models/issues"
|
issues_model "code.gitea.io/gitea/models/issues"
|
||||||
@ -21,26 +22,17 @@ import (
|
|||||||
|
|
||||||
// ProcReceive handle proc receive work
|
// ProcReceive handle proc receive work
|
||||||
func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, opts *private.HookOptions) ([]private.HookProcReceiveRefResult, error) {
|
func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, opts *private.HookOptions) ([]private.HookProcReceiveRefResult, error) {
|
||||||
// TODO: Add more options?
|
|
||||||
var (
|
|
||||||
topicBranch string
|
|
||||||
title string
|
|
||||||
description string
|
|
||||||
forcePush bool
|
|
||||||
)
|
|
||||||
|
|
||||||
results := make([]private.HookProcReceiveRefResult, 0, len(opts.OldCommitIDs))
|
results := make([]private.HookProcReceiveRefResult, 0, len(opts.OldCommitIDs))
|
||||||
|
topicBranch := opts.GitPushOptions["topic"]
|
||||||
ownerName := repo.OwnerName
|
forcePush, _ := strconv.ParseBool(opts.GitPushOptions["force-push"])
|
||||||
repoName := repo.Name
|
title := strings.TrimSpace(opts.GitPushOptions["title"])
|
||||||
|
description := strings.TrimSpace(opts.GitPushOptions["description"]) // TODO: Add more options?
|
||||||
topicBranch = opts.GitPushOptions["topic"]
|
|
||||||
_, forcePush = opts.GitPushOptions["force-push"]
|
|
||||||
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
|
objectFormat := git.ObjectFormatFromName(repo.ObjectFormatName)
|
||||||
|
userName := strings.ToLower(opts.UserName)
|
||||||
|
|
||||||
pusher, err := user_model.GetUserByID(ctx, opts.UserID)
|
pusher, err := user_model.GetUserByID(ctx, opts.UserID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Failed to get user. Error: %w", err)
|
return nil, fmt.Errorf("failed to get user. Error: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range opts.OldCommitIDs {
|
for i := range opts.OldCommitIDs {
|
||||||
@ -85,9 +77,6 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
var headBranch string
|
|
||||||
userName := strings.ToLower(opts.UserName)
|
|
||||||
|
|
||||||
if len(curentTopicBranch) == 0 {
|
if len(curentTopicBranch) == 0 {
|
||||||
curentTopicBranch = topicBranch
|
curentTopicBranch = topicBranch
|
||||||
}
|
}
|
||||||
@ -95,6 +84,7 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
|
|||||||
// because different user maybe want to use same topic,
|
// because different user maybe want to use same topic,
|
||||||
// So it's better to make sure the topic branch name
|
// So it's better to make sure the topic branch name
|
||||||
// has user name prefix
|
// has user name prefix
|
||||||
|
var headBranch string
|
||||||
if !strings.HasPrefix(curentTopicBranch, userName+"/") {
|
if !strings.HasPrefix(curentTopicBranch, userName+"/") {
|
||||||
headBranch = userName + "/" + curentTopicBranch
|
headBranch = userName + "/" + curentTopicBranch
|
||||||
} else {
|
} else {
|
||||||
@ -104,21 +94,26 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
|
|||||||
pr, err := issues_model.GetUnmergedPullRequest(ctx, repo.ID, repo.ID, headBranch, baseBranchName, issues_model.PullRequestFlowAGit)
|
pr, err := issues_model.GetUnmergedPullRequest(ctx, repo.ID, repo.ID, headBranch, baseBranchName, issues_model.PullRequestFlowAGit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !issues_model.IsErrPullRequestNotExist(err) {
|
if !issues_model.IsErrPullRequestNotExist(err) {
|
||||||
return nil, fmt.Errorf("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %w", ownerName, repoName, err)
|
return nil, fmt.Errorf("failed to get unmerged agit flow pull request in repository: %s Error: %w", repo.FullName(), err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var commit *git.Commit
|
||||||
|
if title == "" || description == "" {
|
||||||
|
commit, err = gitRepo.GetCommit(opts.NewCommitIDs[i])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get commit %s in repository: %s Error: %w", opts.NewCommitIDs[i], repo.FullName(), err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a new pull request
|
// create a new pull request
|
||||||
if len(title) == 0 {
|
if title == "" {
|
||||||
var has bool
|
|
||||||
title, has = opts.GitPushOptions["title"]
|
|
||||||
if !has || len(title) == 0 {
|
|
||||||
commit, err := gitRepo.GetCommit(opts.NewCommitIDs[i])
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Failed to get commit %s in repository: %s/%s Error: %w", opts.NewCommitIDs[i], ownerName, repoName, err)
|
|
||||||
}
|
|
||||||
title = strings.Split(commit.CommitMessage, "\n")[0]
|
title = strings.Split(commit.CommitMessage, "\n")[0]
|
||||||
}
|
}
|
||||||
description = opts.GitPushOptions["description"]
|
if description == "" {
|
||||||
|
_, description, _ = strings.Cut(commit.CommitMessage, "\n\n")
|
||||||
|
}
|
||||||
|
if description == "" {
|
||||||
|
description = title
|
||||||
}
|
}
|
||||||
|
|
||||||
prIssue := &issues_model.Issue{
|
prIssue := &issues_model.Issue{
|
||||||
@ -160,12 +155,12 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
|
|||||||
|
|
||||||
// update exist pull request
|
// update exist pull request
|
||||||
if err := pr.LoadBaseRepo(ctx); err != nil {
|
if err := pr.LoadBaseRepo(ctx); err != nil {
|
||||||
return nil, fmt.Errorf("Unable to load base repository for PR[%d] Error: %w", pr.ID, err)
|
return nil, fmt.Errorf("unable to load base repository for PR[%d] Error: %w", pr.ID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
oldCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
|
oldCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Unable to get ref commit id in base repository for PR[%d] Error: %w", pr.ID, err)
|
return nil, fmt.Errorf("unable to get ref commit id in base repository for PR[%d] Error: %w", pr.ID, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if oldCommitID == opts.NewCommitIDs[i] {
|
if oldCommitID == opts.NewCommitIDs[i] {
|
||||||
@ -179,9 +174,11 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !forcePush {
|
if !forcePush {
|
||||||
output, _, err := git.NewCommand(ctx, "rev-list", "--max-count=1").AddDynamicArguments(oldCommitID, "^"+opts.NewCommitIDs[i]).RunStdString(&git.RunOpts{Dir: repo.RepoPath(), Env: os.Environ()})
|
output, _, err := git.NewCommand(ctx, "rev-list", "--max-count=1").
|
||||||
|
AddDynamicArguments(oldCommitID, "^"+opts.NewCommitIDs[i]).
|
||||||
|
RunStdString(&git.RunOpts{Dir: repo.RepoPath(), Env: os.Environ()})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Fail to detect force push: %w", err)
|
return nil, fmt.Errorf("failed to detect force push: %w", err)
|
||||||
} else if len(output) > 0 {
|
} else if len(output) > 0 {
|
||||||
results = append(results, private.HookProcReceiveRefResult{
|
results = append(results, private.HookProcReceiveRefResult{
|
||||||
OriginalRef: opts.RefFullNames[i],
|
OriginalRef: opts.RefFullNames[i],
|
||||||
@ -195,17 +192,13 @@ func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.
|
|||||||
|
|
||||||
pr.HeadCommitID = opts.NewCommitIDs[i]
|
pr.HeadCommitID = opts.NewCommitIDs[i]
|
||||||
if err = pull_service.UpdateRef(ctx, pr); err != nil {
|
if err = pull_service.UpdateRef(ctx, pr); err != nil {
|
||||||
return nil, fmt.Errorf("Failed to update pull ref. Error: %w", err)
|
return nil, fmt.Errorf("failed to update pull ref. Error: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pull_service.AddToTaskQueue(ctx, pr)
|
pull_service.AddToTaskQueue(ctx, pr)
|
||||||
pusher, err := user_model.GetUserByID(ctx, opts.UserID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Failed to get user. Error: %w", err)
|
|
||||||
}
|
|
||||||
err = pr.LoadIssue(ctx)
|
err = pr.LoadIssue(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Failed to load pull issue. Error: %w", err)
|
return nil, fmt.Errorf("failed to load pull issue. Error: %w", err)
|
||||||
}
|
}
|
||||||
comment, err := pull_service.CreatePushPullComment(ctx, pusher, pr, oldCommitID, opts.NewCommitIDs[i])
|
comment, err := pull_service.CreatePushPullComment(ctx, pusher, pr, oldCommitID, opts.NewCommitIDs[i])
|
||||||
if err == nil && comment != nil {
|
if err == nil && comment != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user