2021-07-28 15:12:56 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2021-07-28 15:12:56 +05:30
|
|
|
|
|
|
|
package agit
|
|
|
|
|
|
|
|
import (
|
2022-06-17 23:47:12 +05:30
|
|
|
"context"
|
2021-07-28 15:12:56 +05:30
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-07-28 15:12:56 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/notification"
|
|
|
|
"code.gitea.io/gitea/modules/private"
|
|
|
|
pull_service "code.gitea.io/gitea/services/pull"
|
|
|
|
)
|
|
|
|
|
2022-06-01 08:36:31 +05:30
|
|
|
// ProcReceive handle proc receive work
|
2022-06-17 23:47:12 +05:30
|
|
|
func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, opts *private.HookOptions) ([]private.HookProcReceiveRefResult, error) {
|
2021-07-28 15:12:56 +05:30
|
|
|
// TODO: Add more options?
|
|
|
|
var (
|
|
|
|
topicBranch string
|
|
|
|
title string
|
|
|
|
description string
|
|
|
|
forcePush bool
|
|
|
|
)
|
|
|
|
|
2021-08-14 16:47:10 +05:30
|
|
|
results := make([]private.HookProcReceiveRefResult, 0, len(opts.OldCommitIDs))
|
2022-06-17 23:47:12 +05:30
|
|
|
|
|
|
|
ownerName := repo.OwnerName
|
|
|
|
repoName := repo.Name
|
2021-07-28 15:12:56 +05:30
|
|
|
|
|
|
|
topicBranch = opts.GitPushOptions["topic"]
|
|
|
|
_, forcePush = opts.GitPushOptions["force-push"]
|
|
|
|
|
|
|
|
for i := range opts.OldCommitIDs {
|
|
|
|
if opts.NewCommitIDs[i] == git.EmptySHA {
|
2021-08-14 16:47:10 +05:30
|
|
|
results = append(results, private.HookProcReceiveRefResult{
|
2021-07-28 15:12:56 +05:30
|
|
|
OriginalRef: opts.RefFullNames[i],
|
|
|
|
OldOID: opts.OldCommitIDs[i],
|
|
|
|
NewOID: opts.NewCommitIDs[i],
|
|
|
|
Err: "Can't delete not exist branch",
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(opts.RefFullNames[i], git.PullRequestPrefix) {
|
2021-08-14 16:47:10 +05:30
|
|
|
results = append(results, private.HookProcReceiveRefResult{
|
2021-07-28 15:12:56 +05:30
|
|
|
IsNotMatched: true,
|
|
|
|
OriginalRef: opts.RefFullNames[i],
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
baseBranchName := opts.RefFullNames[i][len(git.PullRequestPrefix):]
|
|
|
|
curentTopicBranch := ""
|
|
|
|
if !gitRepo.IsBranchExist(baseBranchName) {
|
|
|
|
// try match refs/for/<target-branch>/<topic-branch>
|
|
|
|
for p, v := range baseBranchName {
|
|
|
|
if v == '/' && gitRepo.IsBranchExist(baseBranchName[:p]) && p != len(baseBranchName)-1 {
|
|
|
|
curentTopicBranch = baseBranchName[p+1:]
|
|
|
|
baseBranchName = baseBranchName[:p]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(topicBranch) == 0 && len(curentTopicBranch) == 0 {
|
2021-08-14 16:47:10 +05:30
|
|
|
results = append(results, private.HookProcReceiveRefResult{
|
2021-07-28 15:12:56 +05:30
|
|
|
OriginalRef: opts.RefFullNames[i],
|
|
|
|
OldOID: opts.OldCommitIDs[i],
|
|
|
|
NewOID: opts.NewCommitIDs[i],
|
|
|
|
Err: "topic-branch is not set",
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-06-20 15:32:49 +05:30
|
|
|
var headBranch string
|
2021-07-28 15:12:56 +05:30
|
|
|
userName := strings.ToLower(opts.UserName)
|
|
|
|
|
|
|
|
if len(curentTopicBranch) == 0 {
|
|
|
|
curentTopicBranch = topicBranch
|
|
|
|
}
|
|
|
|
|
|
|
|
// because different user maybe want to use same topic,
|
|
|
|
// So it's better to make sure the topic branch name
|
|
|
|
// has user name prefix
|
|
|
|
if !strings.HasPrefix(curentTopicBranch, userName+"/") {
|
|
|
|
headBranch = userName + "/" + curentTopicBranch
|
|
|
|
} else {
|
|
|
|
headBranch = curentTopicBranch
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:42:33 +05:30
|
|
|
pr, err := issues_model.GetUnmergedPullRequest(ctx, repo.ID, repo.ID, headBranch, baseBranchName, issues_model.PullRequestFlowAGit)
|
2021-07-28 15:12:56 +05:30
|
|
|
if err != nil {
|
2022-06-13 15:07:59 +05:30
|
|
|
if !issues_model.IsErrPullRequestNotExist(err) {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %w", ownerName, repoName, err)
|
2021-07-28 15:12:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// create a new pull request
|
|
|
|
if len(title) == 0 {
|
2022-06-20 15:32:49 +05:30
|
|
|
var has bool
|
2021-07-28 15:12:56 +05:30
|
|
|
title, has = opts.GitPushOptions["title"]
|
|
|
|
if !has || len(title) == 0 {
|
|
|
|
commit, err := gitRepo.GetCommit(opts.NewCommitIDs[i])
|
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("Failed to get commit %s in repository: %s/%s Error: %w", opts.NewCommitIDs[i], ownerName, repoName, err)
|
2021-07-28 15:12:56 +05:30
|
|
|
}
|
|
|
|
title = strings.Split(commit.CommitMessage, "\n")[0]
|
|
|
|
}
|
|
|
|
description = opts.GitPushOptions["description"]
|
|
|
|
}
|
|
|
|
|
2022-12-03 08:18:26 +05:30
|
|
|
pusher, err := user_model.GetUserByID(ctx, opts.UserID)
|
2021-07-28 15:12:56 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("Failed to get user. Error: %w", err)
|
2021-07-28 15:12:56 +05:30
|
|
|
}
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
prIssue := &issues_model.Issue{
|
2021-07-28 15:12:56 +05:30
|
|
|
RepoID: repo.ID,
|
|
|
|
Title: title,
|
|
|
|
PosterID: pusher.ID,
|
|
|
|
Poster: pusher,
|
|
|
|
IsPull: true,
|
|
|
|
Content: description,
|
|
|
|
}
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
pr := &issues_model.PullRequest{
|
2021-07-28 15:12:56 +05:30
|
|
|
HeadRepoID: repo.ID,
|
|
|
|
BaseRepoID: repo.ID,
|
|
|
|
HeadBranch: headBranch,
|
|
|
|
HeadCommitID: opts.NewCommitIDs[i],
|
|
|
|
BaseBranch: baseBranchName,
|
|
|
|
HeadRepo: repo,
|
|
|
|
BaseRepo: repo,
|
|
|
|
MergeBase: "",
|
2022-06-13 15:07:59 +05:30
|
|
|
Type: issues_model.PullRequestGitea,
|
|
|
|
Flow: issues_model.PullRequestFlowAGit,
|
2021-07-28 15:12:56 +05:30
|
|
|
}
|
|
|
|
|
2022-01-20 04:56:57 +05:30
|
|
|
if err := pull_service.NewPullRequest(ctx, repo, prIssue, []int64{}, []string{}, pr, []int64{}); err != nil {
|
2022-06-17 23:47:12 +05:30
|
|
|
return nil, err
|
2021-07-28 15:12:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
log.Trace("Pull request created: %d/%d", repo.ID, prIssue.ID)
|
|
|
|
|
2021-08-14 16:47:10 +05:30
|
|
|
results = append(results, private.HookProcReceiveRefResult{
|
2021-07-28 15:12:56 +05:30
|
|
|
Ref: pr.GetGitRefName(),
|
|
|
|
OriginalRef: opts.RefFullNames[i],
|
|
|
|
OldOID: git.EmptySHA,
|
|
|
|
NewOID: opts.NewCommitIDs[i],
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// update exist pull request
|
2022-11-19 13:42:33 +05:30
|
|
|
if err := pr.LoadBaseRepo(ctx); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("Unable to load base repository for PR[%d] Error: %w", pr.ID, err)
|
2021-07-28 15:12:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
oldCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
|
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("Unable to get ref commit id in base repository for PR[%d] Error: %w", pr.ID, err)
|
2021-07-28 15:12:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if oldCommitID == opts.NewCommitIDs[i] {
|
2021-08-14 16:47:10 +05:30
|
|
|
results = append(results, private.HookProcReceiveRefResult{
|
2021-07-28 15:12:56 +05:30
|
|
|
OriginalRef: opts.RefFullNames[i],
|
|
|
|
OldOID: opts.OldCommitIDs[i],
|
|
|
|
NewOID: opts.NewCommitIDs[i],
|
|
|
|
Err: "new commit is same with old commit",
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !forcePush {
|
2022-10-23 20:14:45 +05:30
|
|
|
output, _, err := git.NewCommand(ctx, "rev-list", "--max-count=1").AddDynamicArguments(oldCommitID, "^"+opts.NewCommitIDs[i]).RunStdString(&git.RunOpts{Dir: repo.RepoPath(), Env: os.Environ()})
|
2021-07-28 15:12:56 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("Fail to detect force push: %w", err)
|
2021-07-28 15:12:56 +05:30
|
|
|
} else if len(output) > 0 {
|
2021-08-14 16:47:10 +05:30
|
|
|
results = append(results, private.HookProcReceiveRefResult{
|
2021-09-09 01:50:55 +05:30
|
|
|
OriginalRef: opts.RefFullNames[i],
|
2021-07-28 15:12:56 +05:30
|
|
|
OldOID: opts.OldCommitIDs[i],
|
|
|
|
NewOID: opts.NewCommitIDs[i],
|
|
|
|
Err: "request `force-push` push option",
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pr.HeadCommitID = opts.NewCommitIDs[i]
|
2022-01-20 04:56:57 +05:30
|
|
|
if err = pull_service.UpdateRef(ctx, pr); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("Failed to update pull ref. Error: %w", err)
|
2021-07-28 15:12:56 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
pull_service.AddToTaskQueue(pr)
|
2022-12-03 08:18:26 +05:30
|
|
|
pusher, err := user_model.GetUserByID(ctx, opts.UserID)
|
2021-07-28 15:12:56 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("Failed to get user. Error: %w", err)
|
2021-07-28 15:12:56 +05:30
|
|
|
}
|
2022-11-19 13:42:33 +05:30
|
|
|
err = pr.LoadIssue(ctx)
|
2021-07-28 15:12:56 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("Failed to load pull issue. Error: %w", err)
|
2021-07-28 15:12:56 +05:30
|
|
|
}
|
2022-12-10 08:16:31 +05:30
|
|
|
comment, err := pull_service.CreatePushPullComment(ctx, pusher, pr, oldCommitID, opts.NewCommitIDs[i])
|
2021-07-28 15:12:56 +05:30
|
|
|
if err == nil && comment != nil {
|
2022-11-19 13:42:33 +05:30
|
|
|
notification.NotifyPullRequestPushCommits(ctx, pusher, pr, comment)
|
2021-07-28 15:12:56 +05:30
|
|
|
}
|
2022-11-19 13:42:33 +05:30
|
|
|
notification.NotifyPullRequestSynchronized(ctx, pusher, pr)
|
2021-07-28 15:12:56 +05:30
|
|
|
isForcePush := comment != nil && comment.IsForcePush
|
|
|
|
|
2021-08-14 16:47:10 +05:30
|
|
|
results = append(results, private.HookProcReceiveRefResult{
|
2021-07-28 15:12:56 +05:30
|
|
|
OldOID: oldCommitID,
|
|
|
|
NewOID: opts.NewCommitIDs[i],
|
|
|
|
Ref: pr.GetGitRefName(),
|
|
|
|
OriginalRef: opts.RefFullNames[i],
|
|
|
|
IsForcePush: isForcePush,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-17 23:47:12 +05:30
|
|
|
return results, nil
|
2021-07-28 15:12:56 +05:30
|
|
|
}
|
|
|
|
|
2022-01-10 15:02:37 +05:30
|
|
|
// UserNameChanged handle user name change for agit flow pull
|
2021-11-24 15:19:20 +05:30
|
|
|
func UserNameChanged(user *user_model.User, newName string) error {
|
2022-06-13 15:07:59 +05:30
|
|
|
pulls, err := issues_model.GetAllUnmergedAgitPullRequestByPoster(user.ID)
|
2021-07-28 15:12:56 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newName = strings.ToLower(newName)
|
|
|
|
|
|
|
|
for _, pull := range pulls {
|
|
|
|
pull.HeadBranch = strings.TrimPrefix(pull.HeadBranch, user.LowerName+"/")
|
|
|
|
pull.HeadBranch = newName + "/" + pull.HeadBranch
|
|
|
|
if err = pull.UpdateCols("head_branch"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|