move agit dependcy (#19998)

This commit is contained in:
Lunny Xiao 2022-06-18 02:17:12 +08:00 committed by GitHub
parent 719eb4a879
commit 27ba86d283
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 61 deletions

View file

@ -8,8 +8,10 @@ package private
import ( import (
"net/http" "net/http"
repo_model "code.gitea.io/gitea/models/repo"
gitea_context "code.gitea.io/gitea/modules/context" gitea_context "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/private" "code.gitea.io/gitea/modules/private"
"code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/services/agit" "code.gitea.io/gitea/services/agit"
@ -23,8 +25,17 @@ func HookProcReceive(ctx *gitea_context.PrivateContext) {
return return
} }
results := agit.ProcReceive(ctx, opts) results, err := agit.ProcReceive(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, opts)
if ctx.Written() { if err != nil {
if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) {
ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error())
} else {
log.Error(err.Error())
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
"Err": err.Error(),
})
}
return return
} }

View file

@ -5,15 +5,14 @@
package agit package agit
import ( import (
"context"
"fmt" "fmt"
"net/http"
"os" "os"
"strings" "strings"
issues_model "code.gitea.io/gitea/models/issues" issues_model "code.gitea.io/gitea/models/issues"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/notification"
@ -22,7 +21,7 @@ import (
) )
// ProcReceive handle proc receive work // ProcReceive handle proc receive work
func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []private.HookProcReceiveRefResult { func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, opts *private.HookOptions) ([]private.HookProcReceiveRefResult, error) {
// TODO: Add more options? // TODO: Add more options?
var ( var (
topicBranch string topicBranch string
@ -32,10 +31,9 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
) )
results := make([]private.HookProcReceiveRefResult, 0, len(opts.OldCommitIDs)) results := make([]private.HookProcReceiveRefResult, 0, len(opts.OldCommitIDs))
repo := ctx.Repo.Repository
gitRepo := ctx.Repo.GitRepo ownerName := repo.OwnerName
ownerName := ctx.Repo.Repository.OwnerName repoName := repo.Name
repoName := ctx.Repo.Repository.Name
topicBranch = opts.GitPushOptions["topic"] topicBranch = opts.GitPushOptions["topic"]
_, forcePush = opts.GitPushOptions["force-push"] _, forcePush = opts.GitPushOptions["force-push"]
@ -101,11 +99,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
pr, err := issues_model.GetUnmergedPullRequest(repo.ID, repo.ID, headBranch, baseBranchName, issues_model.PullRequestFlowAGit) pr, err := issues_model.GetUnmergedPullRequest(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) {
log.Error("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %v", ownerName, repoName, err) return nil, fmt.Errorf("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %v", ownerName, repoName, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
"Err": fmt.Sprintf("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %v", ownerName, repoName, err),
})
return nil
} }
// create a new pull request // create a new pull request
@ -115,11 +109,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
if !has || len(title) == 0 { if !has || len(title) == 0 {
commit, err := gitRepo.GetCommit(opts.NewCommitIDs[i]) commit, err := gitRepo.GetCommit(opts.NewCommitIDs[i])
if err != nil { if err != nil {
log.Error("Failed to get commit %s in repository: %s/%s Error: %v", opts.NewCommitIDs[i], ownerName, repoName, err) return nil, fmt.Errorf("Failed to get commit %s in repository: %s/%s Error: %v", opts.NewCommitIDs[i], ownerName, repoName, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
"Err": fmt.Sprintf("Failed to get commit %s in repository: %s/%s Error: %v", opts.NewCommitIDs[i], ownerName, repoName, err),
})
return nil
} }
title = strings.Split(commit.CommitMessage, "\n")[0] title = strings.Split(commit.CommitMessage, "\n")[0]
} }
@ -128,11 +118,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
pusher, err := user_model.GetUserByID(opts.UserID) pusher, err := user_model.GetUserByID(opts.UserID)
if err != nil { if err != nil {
log.Error("Failed to get user. Error: %v", err) return nil, fmt.Errorf("Failed to get user. Error: %v", err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
"Err": fmt.Sprintf("Failed to get user. Error: %v", err),
})
return nil
} }
prIssue := &issues_model.Issue{ prIssue := &issues_model.Issue{
@ -158,12 +144,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
} }
if err := pull_service.NewPullRequest(ctx, repo, prIssue, []int64{}, []string{}, pr, []int64{}); err != nil { if err := pull_service.NewPullRequest(ctx, repo, prIssue, []int64{}, []string{}, pr, []int64{}); err != nil {
if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) { return nil, err
ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error())
return nil
}
ctx.Error(http.StatusInternalServerError, "NewPullRequest", err.Error())
return nil
} }
log.Trace("Pull request created: %d/%d", repo.ID, prIssue.ID) log.Trace("Pull request created: %d/%d", repo.ID, prIssue.ID)
@ -179,20 +160,12 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
// update exist pull request // update exist pull request
if err := pr.LoadBaseRepoCtx(ctx); err != nil { if err := pr.LoadBaseRepoCtx(ctx); err != nil {
log.Error("Unable to load base repository for PR[%d] Error: %v", pr.ID, err) return nil, fmt.Errorf("Unable to load base repository for PR[%d] Error: %v", pr.ID, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
"Err": fmt.Sprintf("Unable to load base repository for PR[%d] Error: %v", pr.ID, err),
})
return nil
} }
oldCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName()) oldCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
if err != nil { if err != nil {
log.Error("Unable to get ref commit id in base repository for PR[%d] Error: %v", pr.ID, err) return nil, fmt.Errorf("Unable to get ref commit id in base repository for PR[%d] Error: %v", pr.ID, err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
"Err": fmt.Sprintf("Unable to get ref commit id in base repository for PR[%d] Error: %v", pr.ID, err),
})
return nil
} }
if oldCommitID == opts.NewCommitIDs[i] { if oldCommitID == opts.NewCommitIDs[i] {
@ -208,11 +181,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
if !forcePush { if !forcePush {
output, _, err := git.NewCommand(ctx, "rev-list", "--max-count=1", oldCommitID, "^"+opts.NewCommitIDs[i]).RunStdString(&git.RunOpts{Dir: repo.RepoPath(), Env: os.Environ()}) output, _, err := git.NewCommand(ctx, "rev-list", "--max-count=1", oldCommitID, "^"+opts.NewCommitIDs[i]).RunStdString(&git.RunOpts{Dir: repo.RepoPath(), Env: os.Environ()})
if err != nil { if err != nil {
log.Error("Unable to detect force push between: %s and %s in %-v Error: %v", oldCommitID, opts.NewCommitIDs[i], repo, err) return nil, fmt.Errorf("Fail to detect force push: %v", err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Fail to detect force push: %v", err),
})
return nil
} 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],
@ -226,29 +195,17 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
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 {
log.Error("Failed to update pull ref. Error: %v", err) return nil, fmt.Errorf("Failed to update pull ref. Error: %v", err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
"Err": fmt.Sprintf("Failed to update pull ref. Error: %v", err),
})
return nil
} }
pull_service.AddToTaskQueue(pr) pull_service.AddToTaskQueue(pr)
pusher, err := user_model.GetUserByID(opts.UserID) pusher, err := user_model.GetUserByID(opts.UserID)
if err != nil { if err != nil {
log.Error("Failed to get user. Error: %v", err) return nil, fmt.Errorf("Failed to get user. Error: %v", err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
"Err": fmt.Sprintf("Failed to get user. Error: %v", err),
})
return nil
} }
err = pr.LoadIssue() err = pr.LoadIssue()
if err != nil { if err != nil {
log.Error("Failed to load pull issue. Error: %v", err) return nil, fmt.Errorf("Failed to load pull issue. Error: %v", err)
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
"Err": fmt.Sprintf("Failed to load pull issue. Error: %v", err),
})
return nil
} }
comment, err := issues_model.CreatePushPullComment(ctx, pusher, pr, oldCommitID, opts.NewCommitIDs[i]) comment, err := issues_model.CreatePushPullComment(ctx, pusher, pr, oldCommitID, opts.NewCommitIDs[i])
if err == nil && comment != nil { if err == nil && comment != nil {
@ -266,7 +223,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
}) })
} }
return results return results, nil
} }
// UserNameChanged handle user name change for agit flow pull // UserNameChanged handle user name change for agit flow pull