Skip to content

Commit 27ba86d

Browse files
authored
move agit dependcy (#19998)
1 parent 719eb4a commit 27ba86d

File tree

2 files changed

+29
-61
lines changed

2 files changed

+29
-61
lines changed

routers/private/hook_proc_receive.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ package private
88
import (
99
"net/http"
1010

11+
repo_model "code.gitea.io/gitea/models/repo"
1112
gitea_context "code.gitea.io/gitea/modules/context"
1213
"code.gitea.io/gitea/modules/git"
14+
"code.gitea.io/gitea/modules/log"
1315
"code.gitea.io/gitea/modules/private"
1416
"code.gitea.io/gitea/modules/web"
1517
"code.gitea.io/gitea/services/agit"
@@ -23,8 +25,17 @@ func HookProcReceive(ctx *gitea_context.PrivateContext) {
2325
return
2426
}
2527

26-
results := agit.ProcReceive(ctx, opts)
27-
if ctx.Written() {
28+
results, err := agit.ProcReceive(ctx, ctx.Repo.Repository, ctx.Repo.GitRepo, opts)
29+
if err != nil {
30+
if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) {
31+
ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error())
32+
} else {
33+
log.Error(err.Error())
34+
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
35+
"Err": err.Error(),
36+
})
37+
}
38+
2839
return
2940
}
3041

services/agit/agit.go

Lines changed: 16 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
package agit
66

77
import (
8+
"context"
89
"fmt"
9-
"net/http"
1010
"os"
1111
"strings"
1212

1313
issues_model "code.gitea.io/gitea/models/issues"
1414
repo_model "code.gitea.io/gitea/models/repo"
1515
user_model "code.gitea.io/gitea/models/user"
16-
"code.gitea.io/gitea/modules/context"
1716
"code.gitea.io/gitea/modules/git"
1817
"code.gitea.io/gitea/modules/log"
1918
"code.gitea.io/gitea/modules/notification"
@@ -22,7 +21,7 @@ import (
2221
)
2322

2423
// ProcReceive handle proc receive work
25-
func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []private.HookProcReceiveRefResult {
24+
func ProcReceive(ctx context.Context, repo *repo_model.Repository, gitRepo *git.Repository, opts *private.HookOptions) ([]private.HookProcReceiveRefResult, error) {
2625
// TODO: Add more options?
2726
var (
2827
topicBranch string
@@ -32,10 +31,9 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
3231
)
3332

3433
results := make([]private.HookProcReceiveRefResult, 0, len(opts.OldCommitIDs))
35-
repo := ctx.Repo.Repository
36-
gitRepo := ctx.Repo.GitRepo
37-
ownerName := ctx.Repo.Repository.OwnerName
38-
repoName := ctx.Repo.Repository.Name
34+
35+
ownerName := repo.OwnerName
36+
repoName := repo.Name
3937

4038
topicBranch = opts.GitPushOptions["topic"]
4139
_, forcePush = opts.GitPushOptions["force-push"]
@@ -101,11 +99,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
10199
pr, err := issues_model.GetUnmergedPullRequest(repo.ID, repo.ID, headBranch, baseBranchName, issues_model.PullRequestFlowAGit)
102100
if err != nil {
103101
if !issues_model.IsErrPullRequestNotExist(err) {
104-
log.Error("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %v", ownerName, repoName, err)
105-
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
106-
"Err": fmt.Sprintf("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %v", ownerName, repoName, err),
107-
})
108-
return nil
102+
return nil, fmt.Errorf("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %v", ownerName, repoName, err)
109103
}
110104

111105
// create a new pull request
@@ -115,11 +109,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
115109
if !has || len(title) == 0 {
116110
commit, err := gitRepo.GetCommit(opts.NewCommitIDs[i])
117111
if err != nil {
118-
log.Error("Failed to get commit %s in repository: %s/%s Error: %v", opts.NewCommitIDs[i], ownerName, repoName, err)
119-
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
120-
"Err": fmt.Sprintf("Failed to get commit %s in repository: %s/%s Error: %v", opts.NewCommitIDs[i], ownerName, repoName, err),
121-
})
122-
return nil
112+
return nil, fmt.Errorf("Failed to get commit %s in repository: %s/%s Error: %v", opts.NewCommitIDs[i], ownerName, repoName, err)
123113
}
124114
title = strings.Split(commit.CommitMessage, "\n")[0]
125115
}
@@ -128,11 +118,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
128118

129119
pusher, err := user_model.GetUserByID(opts.UserID)
130120
if err != nil {
131-
log.Error("Failed to get user. Error: %v", err)
132-
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
133-
"Err": fmt.Sprintf("Failed to get user. Error: %v", err),
134-
})
135-
return nil
121+
return nil, fmt.Errorf("Failed to get user. Error: %v", err)
136122
}
137123

138124
prIssue := &issues_model.Issue{
@@ -158,12 +144,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
158144
}
159145

160146
if err := pull_service.NewPullRequest(ctx, repo, prIssue, []int64{}, []string{}, pr, []int64{}); err != nil {
161-
if repo_model.IsErrUserDoesNotHaveAccessToRepo(err) {
162-
ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error())
163-
return nil
164-
}
165-
ctx.Error(http.StatusInternalServerError, "NewPullRequest", err.Error())
166-
return nil
147+
return nil, err
167148
}
168149

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

180161
// update exist pull request
181162
if err := pr.LoadBaseRepoCtx(ctx); err != nil {
182-
log.Error("Unable to load base repository for PR[%d] Error: %v", pr.ID, err)
183-
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
184-
"Err": fmt.Sprintf("Unable to load base repository for PR[%d] Error: %v", pr.ID, err),
185-
})
186-
return nil
163+
return nil, fmt.Errorf("Unable to load base repository for PR[%d] Error: %v", pr.ID, err)
187164
}
188165

189166
oldCommitID, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
190167
if err != nil {
191-
log.Error("Unable to get ref commit id in base repository for PR[%d] Error: %v", pr.ID, err)
192-
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
193-
"Err": fmt.Sprintf("Unable to get ref commit id in base repository for PR[%d] Error: %v", pr.ID, err),
194-
})
195-
return nil
168+
return nil, fmt.Errorf("Unable to get ref commit id in base repository for PR[%d] Error: %v", pr.ID, err)
196169
}
197170

198171
if oldCommitID == opts.NewCommitIDs[i] {
@@ -208,11 +181,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
208181
if !forcePush {
209182
output, _, err := git.NewCommand(ctx, "rev-list", "--max-count=1", oldCommitID, "^"+opts.NewCommitIDs[i]).RunStdString(&git.RunOpts{Dir: repo.RepoPath(), Env: os.Environ()})
210183
if err != nil {
211-
log.Error("Unable to detect force push between: %s and %s in %-v Error: %v", oldCommitID, opts.NewCommitIDs[i], repo, err)
212-
ctx.JSON(http.StatusInternalServerError, private.Response{
213-
Err: fmt.Sprintf("Fail to detect force push: %v", err),
214-
})
215-
return nil
184+
return nil, fmt.Errorf("Fail to detect force push: %v", err)
216185
} else if len(output) > 0 {
217186
results = append(results, private.HookProcReceiveRefResult{
218187
OriginalRef: opts.RefFullNames[i],
@@ -226,29 +195,17 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
226195

227196
pr.HeadCommitID = opts.NewCommitIDs[i]
228197
if err = pull_service.UpdateRef(ctx, pr); err != nil {
229-
log.Error("Failed to update pull ref. Error: %v", err)
230-
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
231-
"Err": fmt.Sprintf("Failed to update pull ref. Error: %v", err),
232-
})
233-
return nil
198+
return nil, fmt.Errorf("Failed to update pull ref. Error: %v", err)
234199
}
235200

236201
pull_service.AddToTaskQueue(pr)
237202
pusher, err := user_model.GetUserByID(opts.UserID)
238203
if err != nil {
239-
log.Error("Failed to get user. Error: %v", err)
240-
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
241-
"Err": fmt.Sprintf("Failed to get user. Error: %v", err),
242-
})
243-
return nil
204+
return nil, fmt.Errorf("Failed to get user. Error: %v", err)
244205
}
245206
err = pr.LoadIssue()
246207
if err != nil {
247-
log.Error("Failed to load pull issue. Error: %v", err)
248-
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
249-
"Err": fmt.Sprintf("Failed to load pull issue. Error: %v", err),
250-
})
251-
return nil
208+
return nil, fmt.Errorf("Failed to load pull issue. Error: %v", err)
252209
}
253210
comment, err := issues_model.CreatePushPullComment(ctx, pusher, pr, oldCommitID, opts.NewCommitIDs[i])
254211
if err == nil && comment != nil {
@@ -266,7 +223,7 @@ func ProcReceive(ctx *context.PrivateContext, opts *private.HookOptions) []priva
266223
})
267224
}
268225

269-
return results
226+
return results, nil
270227
}
271228

272229
// UserNameChanged handle user name change for agit flow pull

0 commit comments

Comments
 (0)