Skip to content

Commit e59b1b9

Browse files
committed
services should not depends on context
1 parent 8dfd55f commit e59b1b9

File tree

2 files changed

+28
-77
lines changed

2 files changed

+28
-77
lines changed

modules/context/base.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -109,41 +109,38 @@ func (ctx *BaseContext) Value(key interface{}) interface{} {
109109
return ctx.Req.Context().Value(key)
110110
}
111111

112-
// FIXME: We should differ Query and Form, currently we just use form as query
113-
// Currently to be compatible with macaron, we keep it.
114-
115-
// Query returns request form as string with default
116-
func (ctx *BaseContext) Query(key string, defaults ...string) string {
112+
// Form returns request form as string with default
113+
func (ctx *BaseContext) Form(key string, defaults ...string) string {
117114
return (*Forms)(ctx.Req).MustString(key, defaults...)
118115
}
119116

120-
// QueryTrim returns request form as string with default and trimmed spaces
121-
func (ctx *BaseContext) QueryTrim(key string, defaults ...string) string {
117+
// FormTrim returns request form as string with default and trimmed spaces
118+
func (ctx *BaseContext) FormTrim(key string, defaults ...string) string {
122119
return (*Forms)(ctx.Req).MustTrimmed(key, defaults...)
123120
}
124121

125-
// QueryStrings returns request form as strings with default
126-
func (ctx *BaseContext) QueryStrings(key string, defaults ...[]string) []string {
122+
// FormStrings returns request form as strings with default
123+
func (ctx *BaseContext) FormStrings(key string, defaults ...[]string) []string {
127124
return (*Forms)(ctx.Req).MustStrings(key, defaults...)
128125
}
129126

130-
// QueryInt returns request form as int with default
131-
func (ctx *BaseContext) QueryInt(key string, defaults ...int) int {
127+
// FormInt returns request form as int with default
128+
func (ctx *BaseContext) FormInt(key string, defaults ...int) int {
132129
return (*Forms)(ctx.Req).MustInt(key, defaults...)
133130
}
134131

135-
// QueryInt64 returns request form as int64 with default
136-
func (ctx *BaseContext) QueryInt64(key string, defaults ...int64) int64 {
132+
// FormInt64 returns request form as int64 with default
133+
func (ctx *BaseContext) FormInt64(key string, defaults ...int64) int64 {
137134
return (*Forms)(ctx.Req).MustInt64(key, defaults...)
138135
}
139136

140-
// QueryBool returns request form as bool with default
141-
func (ctx *BaseContext) QueryBool(key string, defaults ...bool) bool {
137+
// FormBool returns request form as bool with default
138+
func (ctx *BaseContext) FormBool(key string, defaults ...bool) bool {
142139
return (*Forms)(ctx.Req).MustBool(key, defaults...)
143140
}
144141

145-
// QueryOptionalBool returns request form as OptionalBool with default
146-
func (ctx *BaseContext) QueryOptionalBool(key string, defaults ...util.OptionalBool) util.OptionalBool {
142+
// FormOptionalBool returns request form as OptionalBool with default
143+
func (ctx *BaseContext) FormOptionalBool(key string, defaults ...util.OptionalBool) util.OptionalBool {
147144
return (*Forms)(ctx.Req).MustOptionalBool(key, defaults...)
148145
}
149146

services/agit/agit.go

Lines changed: 14 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ package agit
66

77
import (
88
"fmt"
9-
"net/http"
109
"os"
1110
"strings"
1211

1312
"code.gitea.io/gitea/models"
1413
user_model "code.gitea.io/gitea/models/user"
15-
"code.gitea.io/gitea/modules/context"
1614
"code.gitea.io/gitea/modules/git"
1715
"code.gitea.io/gitea/modules/log"
1816
"code.gitea.io/gitea/modules/notification"
@@ -21,7 +19,7 @@ import (
2119
)
2220

2321
// ProcRecive handle proc receive work
24-
func ProcRecive(ctx *context.PrivateContext, opts *private.HookOptions) []private.HookProcReceiveRefResult {
22+
func ProcRecive(repo *models.Repository, gitRepo *git.Repository, opts *private.HookOptions) ([]private.HookProcReceiveRefResult, error) {
2523
// TODO: Add more options?
2624
var (
2725
topicBranch string
@@ -31,10 +29,8 @@ func ProcRecive(ctx *context.PrivateContext, opts *private.HookOptions) []privat
3129
)
3230

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

3935
topicBranch = opts.GitPushOptions["topic"]
4036
_, forcePush = opts.GitPushOptions["force-push"]
@@ -100,11 +96,7 @@ func ProcRecive(ctx *context.PrivateContext, opts *private.HookOptions) []privat
10096
pr, err := models.GetUnmergedPullRequest(repo.ID, repo.ID, headBranch, baseBranchName, models.PullRequestFlowAGit)
10197
if err != nil {
10298
if !models.IsErrPullRequestNotExist(err) {
103-
log.Error("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %v", ownerName, repoName, err)
104-
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
105-
"Err": fmt.Sprintf("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %v", ownerName, repoName, err),
106-
})
107-
return nil
99+
return nil, fmt.Errorf("Failed to get unmerged agit flow pull request in repository: %s/%s Error: %v", ownerName, repoName, err)
108100
}
109101

110102
// create a new pull request
@@ -114,24 +106,15 @@ func ProcRecive(ctx *context.PrivateContext, opts *private.HookOptions) []privat
114106
if !has || len(title) == 0 {
115107
commit, err := gitRepo.GetCommit(opts.NewCommitIDs[i])
116108
if err != nil {
117-
log.Error("Failed to get commit %s in repository: %s/%s Error: %v", opts.NewCommitIDs[i], ownerName, repoName, err)
118-
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
119-
"Err": fmt.Sprintf("Failed to get commit %s in repository: %s/%s Error: %v", opts.NewCommitIDs[i], ownerName, repoName, err),
120-
})
121-
return nil
109+
return nil, fmt.Errorf("Failed to get commit %s in repository: %s/%s Error: %v", opts.NewCommitIDs[i], ownerName, repoName, err)
122110
}
123111
title = strings.Split(commit.CommitMessage, "\n")[0]
124112
}
125113
description = opts.GitPushOptions["description"]
126114
}
127-
128115
pusher, err := user_model.GetUserByID(opts.UserID)
129116
if err != nil {
130-
log.Error("Failed to get user. Error: %v", err)
131-
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
132-
"Err": fmt.Sprintf("Failed to get user. Error: %v", err),
133-
})
134-
return nil
117+
return nil, fmt.Errorf("Failed to get user. Error: %v", err)
135118
}
136119

137120
prIssue := &models.Issue{
@@ -157,12 +140,7 @@ func ProcRecive(ctx *context.PrivateContext, opts *private.HookOptions) []privat
157140
}
158141

159142
if err := pull_service.NewPullRequest(repo, prIssue, []int64{}, []string{}, pr, []int64{}); err != nil {
160-
if models.IsErrUserDoesNotHaveAccessToRepo(err) {
161-
ctx.Error(http.StatusBadRequest, "UserDoesNotHaveAccessToRepo", err.Error())
162-
return nil
163-
}
164-
ctx.Error(http.StatusInternalServerError, "NewPullRequest", err.Error())
165-
return nil
143+
return nil, err
166144
}
167145

168146
log.Trace("Pull request created: %d/%d", repo.ID, prIssue.ID)
@@ -178,20 +156,12 @@ func ProcRecive(ctx *context.PrivateContext, opts *private.HookOptions) []privat
178156

179157
// update exist pull request
180158
if err := pr.LoadBaseRepo(); err != nil {
181-
log.Error("Unable to load base repository for PR[%d] Error: %v", pr.ID, err)
182-
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
183-
"Err": fmt.Sprintf("Unable to load base repository for PR[%d] Error: %v", pr.ID, err),
184-
})
185-
return nil
159+
return nil, fmt.Errorf("Unable to load base repository for PR[%d] Error: %v", pr.ID, err)
186160
}
187161

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

197167
if oldCommitID == opts.NewCommitIDs[i] {
@@ -207,11 +177,7 @@ func ProcRecive(ctx *context.PrivateContext, opts *private.HookOptions) []privat
207177
if !forcePush {
208178
output, err := git.NewCommand("rev-list", "--max-count=1", oldCommitID, "^"+opts.NewCommitIDs[i]).RunInDirWithEnv(repo.RepoPath(), os.Environ())
209179
if err != nil {
210-
log.Error("Unable to detect force push between: %s and %s in %-v Error: %v", oldCommitID, opts.NewCommitIDs[i], repo, err)
211-
ctx.JSON(http.StatusInternalServerError, private.Response{
212-
Err: fmt.Sprintf("Fail to detect force push: %v", err),
213-
})
214-
return nil
180+
return nil, fmt.Errorf("Unable to detect force push between: %s and %s in %-v Error: %v", oldCommitID, opts.NewCommitIDs[i], repo, err)
215181
} else if len(output) > 0 {
216182
results = append(results, private.HookProcReceiveRefResult{
217183
OriginalRef: opts.RefFullNames[i],
@@ -225,29 +191,17 @@ func ProcRecive(ctx *context.PrivateContext, opts *private.HookOptions) []privat
225191

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

235197
pull_service.AddToTaskQueue(pr)
236198
pusher, err := user_model.GetUserByID(opts.UserID)
237199
if err != nil {
238-
log.Error("Failed to get user. Error: %v", err)
239-
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
240-
"Err": fmt.Sprintf("Failed to get user. Error: %v", err),
241-
})
242-
return nil
200+
return nil, fmt.Errorf("Failed to get user. Error: %v", err)
243201
}
244202
err = pr.LoadIssue()
245203
if err != nil {
246-
log.Error("Failed to load pull issue. Error: %v", err)
247-
ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
248-
"Err": fmt.Sprintf("Failed to load pull issue. Error: %v", err),
249-
})
250-
return nil
204+
return nil, fmt.Errorf("Failed to load pull issue. Error: %v", err)
251205
}
252206
comment, err := models.CreatePushPullComment(pusher, pr, oldCommitID, opts.NewCommitIDs[i])
253207
if err == nil && comment != nil {
@@ -265,7 +219,7 @@ func ProcRecive(ctx *context.PrivateContext, opts *private.HookOptions) []privat
265219
})
266220
}
267221

268-
return results
222+
return results, nil
269223
}
270224

271225
// UserNameChanged hanle user name change for agit flow pull

0 commit comments

Comments
 (0)