Skip to content

Move agit dependency #19998

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions routers/private/hook_proc_receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ package private
import (
"net/http"

repo_model "code.gitea.io/gitea/models/repo"
gitea_context "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/private"
"code.gitea.io/gitea/modules/web"
"code.gitea.io/gitea/services/agit"
Expand All @@ -23,8 +25,17 @@ func HookProcReceive(ctx *gitea_context.PrivateContext) {
return
}

results := agit.ProcReceive(ctx, opts)
if ctx.Written() {
results, err := agit.ProcReceive(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, opts)
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
}

Expand Down
75 changes: 16 additions & 59 deletions services/agit/agit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
package agit

import (
"context"
"fmt"
"net/http"
"os"
"strings"

issues_model "code.gitea.io/gitea/models/issues"
repo_model "code.gitea.io/gitea/models/repo"
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/log"
"code.gitea.io/gitea/modules/notification"
Expand All @@ -22,7 +21,7 @@ import (
)

// 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?
var (
topicBranch string
Expand All @@ -32,10 +31,9 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
)

results := make([]private.HookProcReceiveRefResult, 0, len(opts.OldCommitIDs))
repo := ctx.Repo.Repository
gitRepo := ctx.Repo.GitRepo
ownerName := ctx.Repo.Repository.OwnerName
repoName := ctx.Repo.Repository.Name

ownerName := repo.OwnerName
repoName := repo.Name

topicBranch = opts.GitPushOptions["topic"]
_, forcePush = opts.GitPushOptions["force-push"]
Expand Down Expand Up @@ -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)
if err != nil {
if !issues_model.IsErrPullRequestNotExist(err) {
log.Error("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
return nil, fmt.Errorf("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %v", ownerName, repoName, err)
}

// create a new pull request
Expand All @@ -115,11 +109,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
if !has || len(title) == 0 {
commit, err := gitRepo.GetCommit(opts.NewCommitIDs[i])
if err != nil {
log.Error("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
return nil, fmt.Errorf("Failed to get commit %s in repository: %s/%s Error: %v", opts.NewCommitIDs[i], ownerName, repoName, err)
}
title = strings.Split(commit.CommitMessage, "\n")[0]
}
Expand All @@ -128,11 +118,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva

pusher, err := user_model.GetUserByID(opts.UserID)
if err != nil {
log.Error("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
return nil, fmt.Errorf("Failed to get user. Error: %v", err)
}

prIssue := &issues_model.Issue{
Expand All @@ -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 repo_model.IsErrUserDoesNotHaveAccessToRepo(err) {
ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error())
return nil
}
ctx.Error(http.StatusInternalServerError, "NewPullRequest", err.Error())
return nil
return nil, err
}

log.Trace("Pull request created: %d/%d", repo.ID, prIssue.ID)
Expand All @@ -179,20 +160,12 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva

// update exist pull request
if err := pr.LoadBaseRepoCtx(ctx); err != nil {
log.Error("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
return nil, fmt.Errorf("Unable to load base repository for PR[%d] Error: %v", pr.ID, err)
}

oldCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
if err != nil {
log.Error("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
return nil, fmt.Errorf("Unable to get ref commit id in base repository for PR[%d] Error: %v", pr.ID, err)
}

if oldCommitID == opts.NewCommitIDs[i] {
Expand All @@ -208,11 +181,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
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()})
if err != nil {
log.Error("Unable to detect force push between: %s and %s in %-v Error: %v", oldCommitID, opts.NewCommitIDs[i], repo, err)
ctx.JSON(http.StatusInternalServerError, private.Response{
Err: fmt.Sprintf("Fail to detect force push: %v", err),
})
return nil
return nil, fmt.Errorf("Fail to detect force push: %v", err)
} else if len(output) > 0 {
results = append(results, private.HookProcReceiveRefResult{
OriginalRef: opts.RefFullNames[i],
Expand All @@ -226,29 +195,17 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva

pr.HeadCommitID = opts.NewCommitIDs[i]
if err = pull_service.UpdateRef(ctx, pr); err != nil {
log.Error("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
return nil, fmt.Errorf("Failed to update pull ref. Error: %v", err)
}

pull_service.AddToTaskQueue(pr)
pusher, err := user_model.GetUserByID(opts.UserID)
if err != nil {
log.Error("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
return nil, fmt.Errorf("Failed to get user. Error: %v", err)
}
err = pr.LoadIssue()
if err != nil {
log.Error("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
return nil, fmt.Errorf("Failed to load pull issue. Error: %v", err)
}
comment, err := issues_model.CreatePushPullComment(ctx, pusher, pr, oldCommitID, opts.NewCommitIDs[i])
if err == nil && comment != nil {
Expand All @@ -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
Expand Down