Skip to content

Commit 207d583

Browse files
committed
Merge
1 parent 924fa81 commit 207d583

File tree

5 files changed

+40
-22
lines changed

5 files changed

+40
-22
lines changed

modules/context/private.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,29 @@ package context
77
import (
88
"context"
99
"net/http"
10+
11+
"code.gitea.io/gitea/models"
12+
"code.gitea.io/gitea/modules/git"
1013
)
1114

1215
// PrivateContext represents a context for private routes
13-
type PrivateContext = BaseContext
16+
type PrivateContext struct {
17+
*BaseContext
18+
Repository *models.Repository
19+
GitRepo *git.Repository
20+
}
1421

1522
var (
1623
privateContextKey interface{} = "default_private_context"
1724
)
1825

26+
// NewPrivateContext creates a new private context
27+
func NewPrivateContext(resp http.ResponseWriter, req *http.Request, data map[string]interface{}) *PrivateContext {
28+
return &PrivateContext{
29+
BaseContext: NewBaseContext(resp, req, map[string]interface{}{}),
30+
}
31+
}
32+
1933
// WithPrivateContext set up private context in request
2034
func WithPrivateContext(req *http.Request, ctx *PrivateContext) *http.Request {
2135
return req.WithContext(context.WithValue(req.Context(), privateContextKey, ctx))
@@ -30,7 +44,7 @@ func GetPrivateContext(req *http.Request) *PrivateContext {
3044
func PrivateContexter() func(http.Handler) http.Handler {
3145
return func(next http.Handler) http.Handler {
3246
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
33-
ctx := NewBaseContext(w, req, map[string]interface{}{})
47+
ctx := NewPrivateContext(w, req, map[string]interface{}{})
3448
ctx.Req = WithPrivateContext(req, ctx)
3549
next.ServeHTTP(ctx.Resp, ctx.Req)
3650
})

routers/private/hook_pre_receive.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID, refFullN
142142
return
143143
}
144144

145-
repo := ctx.Repo.Repository
146-
gitRepo := ctx.Repo.GitRepo
145+
repo := ctx.Repository
146+
gitRepo := ctx.GitRepo
147147
branchName := strings.TrimPrefix(refFullName, git.BranchPrefix)
148148

149149
if branchName == repo.DefaultBranch && newCommitID == git.EmptySHA {
@@ -363,9 +363,9 @@ func preReceiveTag(ctx *preReceiveContext, oldCommitID, newCommitID, refFullName
363363

364364
if !ctx.gotProtectedTags {
365365
var err error
366-
ctx.protectedTags, err = ctx.Repo.Repository.GetProtectedTags()
366+
ctx.protectedTags, err = ctx.Repository.GetProtectedTags()
367367
if err != nil {
368-
log.Error("Unable to get protected tags for %-v Error: %v", ctx.Repo.Repository, err)
368+
log.Error("Unable to get protected tags for %-v Error: %v", ctx.Repository, err)
369369
ctx.JSON(http.StatusInternalServerError, private.Response{
370370
Err: err.Error(),
371371
})
@@ -382,7 +382,7 @@ func preReceiveTag(ctx *preReceiveContext, oldCommitID, newCommitID, refFullName
382382
return
383383
}
384384
if !isAllowed {
385-
log.Warn("Forbidden: Tag %s in %-v is protected", tagName, ctx.Repo.Repository)
385+
log.Warn("Forbidden: Tag %s in %-v is protected", tagName, ctx.Repository)
386386
ctx.JSON(http.StatusForbidden, private.Response{
387387
Err: fmt.Sprintf("Tag %s is protected", tagName),
388388
})
@@ -395,7 +395,7 @@ func preReceivePullRequest(ctx *preReceiveContext, oldCommitID, newCommitID, ref
395395
return
396396
}
397397

398-
if ctx.Repo.Repository.IsEmpty {
398+
if ctx.Repository.IsEmpty {
399399
ctx.JSON(http.StatusForbidden, map[string]interface{}{
400400
"err": "Can't create pull request for an empty repository.",
401401
})
@@ -412,13 +412,13 @@ func preReceivePullRequest(ctx *preReceiveContext, oldCommitID, newCommitID, ref
412412
baseBranchName := refFullName[len(git.PullRequestPrefix):]
413413

414414
baseBranchExist := false
415-
if ctx.Repo.GitRepo.IsBranchExist(baseBranchName) {
415+
if ctx.GitRepo.IsBranchExist(baseBranchName) {
416416
baseBranchExist = true
417417
}
418418

419419
if !baseBranchExist {
420420
for p, v := range baseBranchName {
421-
if v == '/' && ctx.Repo.GitRepo.IsBranchExist(baseBranchName[:p]) && p != len(baseBranchName)-1 {
421+
if v == '/' && ctx.GitRepo.IsBranchExist(baseBranchName[:p]) && p != len(baseBranchName)-1 {
422422
baseBranchExist = true
423423
break
424424
}
@@ -460,11 +460,11 @@ func loadUserAndPermission(ctx *gitea_context.PrivateContext, id int64) (user *u
460460
return
461461
}
462462

463-
perm, err = models.GetUserRepoPermission(ctx.Repo.Repository, user)
463+
perm, err = models.GetUserRepoPermission(ctx.Repository, user)
464464
if err != nil {
465-
log.Error("Unable to get Repo permission of repo %s/%s of User %s", ctx.Repo.Repository.OwnerName, ctx.Repo.Repository.Name, user.Name, err)
465+
log.Error("Unable to get Repo permission of repo %s/%s of User %s", ctx.Repository.OwnerName, ctx.Repository.Name, user.Name, err)
466466
ctx.JSON(http.StatusInternalServerError, private.Response{
467-
Err: fmt.Sprintf("Unable to get Repo permission of repo %s/%s of User %s: %v", ctx.Repo.Repository.OwnerName, ctx.Repo.Repository.Name, user.Name, err),
467+
Err: fmt.Sprintf("Unable to get Repo permission of repo %s/%s of User %s: %v", ctx.Repository.OwnerName, ctx.Repository.Name, user.Name, err),
468468
})
469469
return
470470
}

routers/private/hook_proc_receive.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
gitea_context "code.gitea.io/gitea/modules/context"
1212
"code.gitea.io/gitea/modules/git"
13+
"code.gitea.io/gitea/modules/log"
1314
"code.gitea.io/gitea/modules/private"
1415
"code.gitea.io/gitea/modules/web"
1516
"code.gitea.io/gitea/services/agit"
@@ -23,7 +24,12 @@ func HookProcReceive(ctx *gitea_context.PrivateContext) {
2324
return
2425
}
2526

26-
results := agit.ProcRecive(ctx, opts)
27+
results, err := agit.ProcReceive(ctx.Repository, ctx.GitRepo, opts)
28+
if err != nil {
29+
log.Error("agit.ProcReceive failed: %v", err)
30+
ctx.Status(http.StatusInternalServerError)
31+
return
32+
}
2733
if ctx.Written() {
2834
return
2935
}

routers/private/internal_repo.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,14 @@ func RepoAssignment(ctx *gitea_context.PrivateContext) context.CancelFunc {
5252
return nil
5353
}
5454

55-
ctx.Repo = &gitea_context.Repository{
56-
Repository: repo,
57-
GitRepo: gitRepo,
58-
}
55+
ctx.Repository = repo
56+
ctx.GitRepo = gitRepo
5957

6058
// We opened it, we should close it
6159
cancel := func() {
6260
// If it's been set to nil then assume someone else has closed it.
63-
if ctx.Repo.GitRepo != nil {
64-
ctx.Repo.GitRepo.Close()
61+
if ctx.GitRepo != nil {
62+
ctx.GitRepo.Close()
6563
}
6664
}
6765

services/agit/agit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import (
1818
pull_service "code.gitea.io/gitea/services/pull"
1919
)
2020

21-
// ProcRecive handle proc receive work
22-
func ProcRecive(repo *models.Repository, gitRepo *git.Repository, opts *private.HookOptions) ([]private.HookProcReceiveRefResult, error) {
21+
// ProcReceive handle proc receive work
22+
func ProcReceive(repo *models.Repository, gitRepo *git.Repository, opts *private.HookOptions) ([]private.HookProcReceiveRefResult, error) {
2323
// TODO: Add more options?
2424
var (
2525
topicBranch string

0 commit comments

Comments
 (0)