mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-10 12:15:43 +01:00
Turn RepoRef and RepoAssignment back into func(*Context) (#15372)
Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
parent
d0eeba9ff9
commit
136a20926c
2 changed files with 312 additions and 325 deletions
|
@ -8,7 +8,6 @@ package context
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -394,13 +393,10 @@ func RepoIDAssignment() func(ctx *Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// RepoAssignment returns a middleware to handle repository assignment
|
// RepoAssignment returns a middleware to handle repository assignment
|
||||||
func RepoAssignment() func(http.Handler) http.Handler {
|
func RepoAssignment(ctx *Context) {
|
||||||
return func(next http.Handler) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
||||||
var (
|
var (
|
||||||
owner *models.User
|
owner *models.User
|
||||||
err error
|
err error
|
||||||
ctx = GetContext(req)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
userName := ctx.Params(":username")
|
userName := ctx.Params(":username")
|
||||||
|
@ -543,7 +539,6 @@ func RepoAssignment() func(http.Handler) http.Handler {
|
||||||
// Stop at this point when the repo is empty.
|
// Stop at this point when the repo is empty.
|
||||||
if ctx.Repo.Repository.IsEmpty {
|
if ctx.Repo.Repository.IsEmpty {
|
||||||
ctx.Data["BranchName"] = ctx.Repo.Repository.DefaultBranch
|
ctx.Data["BranchName"] = ctx.Repo.Repository.DefaultBranch
|
||||||
next.ServeHTTP(w, req)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -624,9 +619,6 @@ func RepoAssignment() func(http.Handler) http.Handler {
|
||||||
ctx.Data["GoDocDirectory"] = prefix + "{/dir}"
|
ctx.Data["GoDocDirectory"] = prefix + "{/dir}"
|
||||||
ctx.Data["GoDocFile"] = prefix + "{/dir}/{file}#L{line}"
|
ctx.Data["GoDocFile"] = prefix + "{/dir}/{file}#L{line}"
|
||||||
}
|
}
|
||||||
next.ServeHTTP(w, req)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RepoRefType type of repo reference
|
// RepoRefType type of repo reference
|
||||||
|
@ -651,7 +643,7 @@ const (
|
||||||
|
|
||||||
// RepoRef handles repository reference names when the ref name is not
|
// RepoRef handles repository reference names when the ref name is not
|
||||||
// explicitly given
|
// explicitly given
|
||||||
func RepoRef() func(http.Handler) http.Handler {
|
func RepoRef() func(*Context) {
|
||||||
// since no ref name is explicitly specified, ok to just use branch
|
// since no ref name is explicitly specified, ok to just use branch
|
||||||
return RepoRefByType(RepoRefBranch)
|
return RepoRefByType(RepoRefBranch)
|
||||||
}
|
}
|
||||||
|
@ -730,10 +722,8 @@ func getRefName(ctx *Context, pathType RepoRefType) string {
|
||||||
|
|
||||||
// RepoRefByType handles repository reference name for a specific type
|
// RepoRefByType handles repository reference name for a specific type
|
||||||
// of repository reference
|
// of repository reference
|
||||||
func RepoRefByType(refType RepoRefType) func(http.Handler) http.Handler {
|
func RepoRefByType(refType RepoRefType) func(*Context) {
|
||||||
return func(next http.Handler) http.Handler {
|
return func(ctx *Context) {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
||||||
ctx := GetContext(req)
|
|
||||||
// Empty repository does not have reference information.
|
// Empty repository does not have reference information.
|
||||||
if ctx.Repo.Repository.IsEmpty {
|
if ctx.Repo.Repository.IsEmpty {
|
||||||
return
|
return
|
||||||
|
@ -851,9 +841,6 @@ func RepoRefByType(refType RepoRefType) func(http.Handler) http.Handler {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
|
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
|
||||||
|
|
||||||
next.ServeHTTP(w, req)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -691,7 +691,7 @@ func RegisterRoutes(m *web.Route) {
|
||||||
}, reqSignIn)
|
}, reqSignIn)
|
||||||
|
|
||||||
// ***** Release Attachment Download without Signin
|
// ***** Release Attachment Download without Signin
|
||||||
m.Get("/{username}/{reponame}/releases/download/{vTag}/{fileName}", ignSignIn, context.RepoAssignment(), repo.MustBeNotEmpty, repo.RedirectDownload)
|
m.Get("/{username}/{reponame}/releases/download/{vTag}/{fileName}", ignSignIn, context.RepoAssignment, repo.MustBeNotEmpty, repo.RedirectDownload)
|
||||||
|
|
||||||
m.Group("/{username}/{reponame}", func() {
|
m.Group("/{username}/{reponame}", func() {
|
||||||
m.Group("/settings", func() {
|
m.Group("/settings", func() {
|
||||||
|
@ -771,9 +771,9 @@ func RegisterRoutes(m *web.Route) {
|
||||||
ctx.Data["PageIsSettings"] = true
|
ctx.Data["PageIsSettings"] = true
|
||||||
ctx.Data["LFSStartServer"] = setting.LFS.StartServer
|
ctx.Data["LFSStartServer"] = setting.LFS.StartServer
|
||||||
})
|
})
|
||||||
}, reqSignIn, context.RepoAssignment(), context.UnitTypes(), reqRepoAdmin, context.RepoRef())
|
}, reqSignIn, context.RepoAssignment, context.UnitTypes(), reqRepoAdmin, context.RepoRef())
|
||||||
|
|
||||||
m.Post("/{username}/{reponame}/action/{action}", reqSignIn, context.RepoAssignment(), context.UnitTypes(), repo.Action)
|
m.Post("/{username}/{reponame}/action/{action}", reqSignIn, context.RepoAssignment, context.UnitTypes(), repo.Action)
|
||||||
|
|
||||||
// Grouping for those endpoints not requiring authentication
|
// Grouping for those endpoints not requiring authentication
|
||||||
m.Group("/{username}/{reponame}", func() {
|
m.Group("/{username}/{reponame}", func() {
|
||||||
|
@ -783,7 +783,7 @@ func RegisterRoutes(m *web.Route) {
|
||||||
m.Combo("/compare/*", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists).
|
m.Combo("/compare/*", repo.MustBeNotEmpty, reqRepoCodeReader, repo.SetEditorconfigIfExists).
|
||||||
Get(ignSignIn, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff).
|
Get(ignSignIn, repo.SetDiffViewStyle, repo.SetWhitespaceBehavior, repo.CompareDiff).
|
||||||
Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, bindIgnErr(forms.CreateIssueForm{}), repo.SetWhitespaceBehavior, repo.CompareAndPullRequestPost)
|
Post(reqSignIn, context.RepoMustNotBeArchived(), reqRepoPullsReader, repo.MustAllowPulls, bindIgnErr(forms.CreateIssueForm{}), repo.SetWhitespaceBehavior, repo.CompareAndPullRequestPost)
|
||||||
}, context.RepoAssignment(), context.UnitTypes())
|
}, context.RepoAssignment, context.UnitTypes())
|
||||||
|
|
||||||
// Grouping for those endpoints that do require authentication
|
// Grouping for those endpoints that do require authentication
|
||||||
m.Group("/{username}/{reponame}", func() {
|
m.Group("/{username}/{reponame}", func() {
|
||||||
|
@ -890,7 +890,7 @@ func RegisterRoutes(m *web.Route) {
|
||||||
m.Post("/restore", repo.RestoreBranchPost)
|
m.Post("/restore", repo.RestoreBranchPost)
|
||||||
}, context.RepoMustNotBeArchived(), reqRepoCodeWriter, repo.MustBeNotEmpty)
|
}, context.RepoMustNotBeArchived(), reqRepoCodeWriter, repo.MustBeNotEmpty)
|
||||||
|
|
||||||
}, reqSignIn, context.RepoAssignment(), context.UnitTypes())
|
}, reqSignIn, context.RepoAssignment, context.UnitTypes())
|
||||||
|
|
||||||
// Releases
|
// Releases
|
||||||
m.Group("/{username}/{reponame}", func() {
|
m.Group("/{username}/{reponame}", func() {
|
||||||
|
@ -928,11 +928,11 @@ func RegisterRoutes(m *web.Route) {
|
||||||
}
|
}
|
||||||
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
|
ctx.Data["CommitsCount"] = ctx.Repo.CommitsCount
|
||||||
})
|
})
|
||||||
}, ignSignIn, context.RepoAssignment(), context.UnitTypes(), reqRepoReleaseReader)
|
}, ignSignIn, context.RepoAssignment, context.UnitTypes(), reqRepoReleaseReader)
|
||||||
|
|
||||||
m.Group("/{username}/{reponame}", func() {
|
m.Group("/{username}/{reponame}", func() {
|
||||||
m.Post("/topics", repo.TopicsPost)
|
m.Post("/topics", repo.TopicsPost)
|
||||||
}, context.RepoAssignment(), context.RepoMustNotBeArchived(), reqRepoAdmin)
|
}, context.RepoAssignment, context.RepoMustNotBeArchived(), reqRepoAdmin)
|
||||||
|
|
||||||
m.Group("/{username}/{reponame}", func() {
|
m.Group("/{username}/{reponame}", func() {
|
||||||
m.Group("", func() {
|
m.Group("", func() {
|
||||||
|
@ -1080,17 +1080,17 @@ func RegisterRoutes(m *web.Route) {
|
||||||
}, context.RepoRef(), reqRepoCodeReader)
|
}, context.RepoRef(), reqRepoCodeReader)
|
||||||
m.Get("/commit/{sha:([a-f0-9]{7,40})}.{ext:patch|diff}",
|
m.Get("/commit/{sha:([a-f0-9]{7,40})}.{ext:patch|diff}",
|
||||||
repo.MustBeNotEmpty, reqRepoCodeReader, repo.RawDiff)
|
repo.MustBeNotEmpty, reqRepoCodeReader, repo.RawDiff)
|
||||||
}, ignSignIn, context.RepoAssignment(), context.UnitTypes())
|
}, ignSignIn, context.RepoAssignment, context.UnitTypes())
|
||||||
m.Group("/{username}/{reponame}", func() {
|
m.Group("/{username}/{reponame}", func() {
|
||||||
m.Get("/stars", repo.Stars)
|
m.Get("/stars", repo.Stars)
|
||||||
m.Get("/watchers", repo.Watchers)
|
m.Get("/watchers", repo.Watchers)
|
||||||
m.Get("/search", reqRepoCodeReader, repo.Search)
|
m.Get("/search", reqRepoCodeReader, repo.Search)
|
||||||
}, ignSignIn, context.RepoAssignment(), context.RepoRef(), context.UnitTypes())
|
}, ignSignIn, context.RepoAssignment, context.RepoRef(), context.UnitTypes())
|
||||||
|
|
||||||
m.Group("/{username}", func() {
|
m.Group("/{username}", func() {
|
||||||
m.Group("/{reponame}", func() {
|
m.Group("/{reponame}", func() {
|
||||||
m.Get("", repo.SetEditorconfigIfExists, repo.Home)
|
m.Get("", repo.SetEditorconfigIfExists, repo.Home)
|
||||||
}, goGet, ignSignIn, context.RepoAssignment(), context.RepoRef(), context.UnitTypes())
|
}, goGet, ignSignIn, context.RepoAssignment, context.RepoRef(), context.UnitTypes())
|
||||||
|
|
||||||
m.Group("/{reponame}", func() {
|
m.Group("/{reponame}", func() {
|
||||||
m.Group("/info/lfs", func() {
|
m.Group("/info/lfs", func() {
|
||||||
|
|
Loading…
Reference in a new issue