loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Refactor web package and context package (#25298)

1. The "web" package shouldn't depends on "modules/context" package,
instead, let each "web context" register themselves to the "web"
package.
2. The old Init/Free doesn't make sense, so simplify it
* The ctx in "Init(ctx)" is never used, and shouldn't be used that way
* The "Free" is never called and shouldn't be called because the SSPI
instance is shared

---------

Co-authored-by: Giteabot <teabot@gitea.io>

authored by

wxiaoguang
Giteabot
and committed by
GitHub
4e2f1ee5 fc2115b4

+218 -292
+1 -1
cmd/web.go
··· 191 191 } 192 192 193 193 // Set up Chi routes 194 - c := routers.NormalRoutes(graceful.GetManager().HammerContext()) 194 + c := routers.NormalRoutes() 195 195 err := listen(c, true) 196 196 <-graceful.GetManager().Done() 197 197 log.Info("PID: %d Gitea Web Finished", os.Getpid())
+8
modules/context/api.go
··· 20 20 "code.gitea.io/gitea/modules/httpcache" 21 21 "code.gitea.io/gitea/modules/log" 22 22 "code.gitea.io/gitea/modules/setting" 23 + "code.gitea.io/gitea/modules/web" 24 + web_types "code.gitea.io/gitea/modules/web/types" 23 25 24 26 "gitea.com/go-chi/cache" 25 27 ) ··· 39 41 Repo *Repository 40 42 Org *APIOrganization 41 43 Package *Package 44 + } 45 + 46 + func init() { 47 + web.RegisterResponseStatusProvider[*APIContext](func(req *http.Request) web_types.ResponseStatusProvider { 48 + return req.Context().Value(apiContextKey).(*APIContext) 49 + }) 42 50 } 43 51 44 52 // Currently, we have the following common fields in error response:
+5 -1
modules/context/base.go
··· 96 96 97 97 // Written returns true if there are something sent to web browser 98 98 func (b *Base) Written() bool { 99 - return b.Resp.Status() > 0 99 + return b.Resp.WrittenStatus() != 0 100 + } 101 + 102 + func (b *Base) WrittenStatus() int { 103 + return b.Resp.WrittenStatus() 100 104 } 101 105 102 106 // Status writes status code
+8
modules/context/context.go
··· 21 21 "code.gitea.io/gitea/modules/setting" 22 22 "code.gitea.io/gitea/modules/templates" 23 23 "code.gitea.io/gitea/modules/translation" 24 + "code.gitea.io/gitea/modules/web" 24 25 "code.gitea.io/gitea/modules/web/middleware" 26 + web_types "code.gitea.io/gitea/modules/web/types" 25 27 26 28 "gitea.com/go-chi/cache" 27 29 "gitea.com/go-chi/session" ··· 56 58 Repo *Repository 57 59 Org *Organization 58 60 Package *Package 61 + } 62 + 63 + func init() { 64 + web.RegisterResponseStatusProvider[*Context](func(req *http.Request) web_types.ResponseStatusProvider { 65 + return req.Context().Value(WebContextKey).(*Context) 66 + }) 59 67 } 60 68 61 69 // TrHTMLEscapeArgs runs ".Locale.Tr()" but pre-escapes all arguments with html.EscapeString.
+8
modules/context/private.go
··· 11 11 12 12 "code.gitea.io/gitea/modules/graceful" 13 13 "code.gitea.io/gitea/modules/process" 14 + "code.gitea.io/gitea/modules/web" 15 + web_types "code.gitea.io/gitea/modules/web/types" 14 16 ) 15 17 16 18 // PrivateContext represents a context for private routes ··· 19 21 Override context.Context 20 22 21 23 Repo *Repository 24 + } 25 + 26 + func init() { 27 + web.RegisterResponseStatusProvider[*PrivateContext](func(req *http.Request) web_types.ResponseStatusProvider { 28 + return req.Context().Value(privateContextKey).(*PrivateContext) 29 + }) 22 30 } 23 31 24 32 // Deadline is part of the interface for context.Context and we pass this to the request context
+13 -4
modules/context/response.go
··· 5 5 6 6 import ( 7 7 "net/http" 8 + 9 + web_types "code.gitea.io/gitea/modules/web/types" 8 10 ) 9 11 10 12 // ResponseWriter represents a response writer for HTTP 11 13 type ResponseWriter interface { 12 14 http.ResponseWriter 13 15 http.Flusher 14 - Status() int 16 + web_types.ResponseStatusProvider 17 + 15 18 Before(func(ResponseWriter)) 16 - Size() int // used by access logger template 19 + 20 + Status() int // used by access logger template 21 + Size() int // used by access logger template 17 22 } 18 23 19 24 var _ ResponseWriter = &Response{} ··· 46 51 return size, nil 47 52 } 48 53 54 + func (r *Response) Status() int { 55 + return r.status 56 + } 57 + 49 58 func (r *Response) Size() int { 50 59 return r.written 51 60 } ··· 71 80 } 72 81 } 73 82 74 - // Status returned status code written 75 - func (r *Response) Status() int { 83 + // WrittenStatus returned status code written 84 + func (r *Response) WrittenStatus() int { 76 85 return r.status 77 86 } 78 87
+22 -20
modules/test/context_tests.go
··· 9 9 "net/http" 10 10 "net/http/httptest" 11 11 "net/url" 12 + "strings" 12 13 "testing" 13 14 14 15 access_model "code.gitea.io/gitea/models/perm/access" ··· 25 26 "github.com/stretchr/testify/assert" 26 27 ) 27 28 28 - // MockContext mock context for unit tests 29 - // TODO: move this function to other packages, because it depends on "models" package 30 - func MockContext(t *testing.T, path string) *context.Context { 31 - resp := httptest.NewRecorder() 29 + func mockRequest(t *testing.T, reqPath string) *http.Request { 30 + method, path, found := strings.Cut(reqPath, " ") 31 + if !found { 32 + method = "GET" 33 + path = reqPath 34 + } 32 35 requestURL, err := url.Parse(path) 33 36 assert.NoError(t, err) 34 - req := &http.Request{ 35 - URL: requestURL, 36 - Form: url.Values{}, 37 - } 37 + req := &http.Request{Method: method, URL: requestURL, Form: url.Values{}} 38 + req = req.WithContext(middleware.WithContextData(req.Context())) 39 + return req 40 + } 38 41 42 + // MockContext mock context for unit tests 43 + // TODO: move this function to other packages, because it depends on "models" package 44 + func MockContext(t *testing.T, reqPath string) (*context.Context, *httptest.ResponseRecorder) { 45 + resp := httptest.NewRecorder() 46 + req := mockRequest(t, reqPath) 39 47 base, baseCleanUp := context.NewBaseContext(resp, req) 40 - base.Data = middleware.ContextData{} 48 + base.Data = middleware.GetContextData(req.Context()) 41 49 base.Locale = &translation.MockLocale{} 42 50 ctx := &context.Context{ 43 51 Base: base, ··· 48 56 49 57 chiCtx := chi.NewRouteContext() 50 58 ctx.Base.AppendContextValue(chi.RouteCtxKey, chiCtx) 51 - return ctx 59 + return ctx, resp 52 60 } 53 61 54 62 // MockAPIContext mock context for unit tests 55 63 // TODO: move this function to other packages, because it depends on "models" package 56 - func MockAPIContext(t *testing.T, path string) *context.APIContext { 64 + func MockAPIContext(t *testing.T, reqPath string) (*context.APIContext, *httptest.ResponseRecorder) { 57 65 resp := httptest.NewRecorder() 58 - requestURL, err := url.Parse(path) 59 - assert.NoError(t, err) 60 - req := &http.Request{ 61 - URL: requestURL, 62 - Form: url.Values{}, 63 - } 64 - 66 + req := mockRequest(t, reqPath) 65 67 base, baseCleanUp := context.NewBaseContext(resp, req) 66 - base.Data = middleware.ContextData{} 68 + base.Data = middleware.GetContextData(req.Context()) 67 69 base.Locale = &translation.MockLocale{} 68 70 ctx := &context.APIContext{Base: base} 69 71 _ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later 70 72 71 73 chiCtx := chi.NewRouteContext() 72 74 ctx.Base.AppendContextValue(chi.RouteCtxKey, chiCtx) 73 - return ctx 75 + return ctx, resp 74 76 } 75 77 76 78 // LoadRepo load a repo into a test context.
+12 -22
modules/web/handler.go
··· 9 9 "net/http" 10 10 "reflect" 11 11 12 - "code.gitea.io/gitea/modules/context" 13 12 "code.gitea.io/gitea/modules/log" 14 13 "code.gitea.io/gitea/modules/web/routing" 14 + "code.gitea.io/gitea/modules/web/types" 15 15 ) 16 16 17 - // ResponseStatusProvider is an interface to check whether the response has been written by the handler 18 - type ResponseStatusProvider interface { 19 - Written() bool 20 - } 17 + var responseStatusProviders = map[reflect.Type]func(req *http.Request) types.ResponseStatusProvider{} 21 18 22 - // TODO: decouple this from the context package, let the context package register these providers 23 - var argTypeProvider = map[reflect.Type]func(req *http.Request) ResponseStatusProvider{ 24 - reflect.TypeOf(&context.APIContext{}): func(req *http.Request) ResponseStatusProvider { return context.GetAPIContext(req) }, 25 - reflect.TypeOf(&context.Context{}): func(req *http.Request) ResponseStatusProvider { return context.GetWebContext(req) }, 26 - reflect.TypeOf(&context.PrivateContext{}): func(req *http.Request) ResponseStatusProvider { return context.GetPrivateContext(req) }, 27 - } 28 - 29 - func RegisterHandleTypeProvider[T any](fn func(req *http.Request) ResponseStatusProvider) { 30 - argTypeProvider[reflect.TypeOf((*T)(nil)).Elem()] = fn 19 + func RegisterResponseStatusProvider[T any](fn func(req *http.Request) types.ResponseStatusProvider) { 20 + responseStatusProviders[reflect.TypeOf((*T)(nil)).Elem()] = fn 31 21 } 32 22 33 23 // responseWriter is a wrapper of http.ResponseWriter, to check whether the response has been written ··· 36 26 status int 37 27 } 38 28 39 - var _ ResponseStatusProvider = (*responseWriter)(nil) 29 + var _ types.ResponseStatusProvider = (*responseWriter)(nil) 40 30 41 - func (r *responseWriter) Written() bool { 42 - return r.status > 0 31 + func (r *responseWriter) WrittenStatus() int { 32 + return r.status 43 33 } 44 34 45 35 func (r *responseWriter) Header() http.Header { ··· 68 58 func preCheckHandler(fn reflect.Value, argsIn []reflect.Value) { 69 59 hasStatusProvider := false 70 60 for _, argIn := range argsIn { 71 - if _, hasStatusProvider = argIn.Interface().(ResponseStatusProvider); hasStatusProvider { 61 + if _, hasStatusProvider = argIn.Interface().(types.ResponseStatusProvider); hasStatusProvider { 72 62 break 73 63 } 74 64 } ··· 101 91 case httpReqType: 102 92 argsIn[i] = reflect.ValueOf(req) 103 93 default: 104 - if argFn, ok := argTypeProvider[argTyp]; ok { 94 + if argFn, ok := responseStatusProviders[argTyp]; ok { 105 95 if isPreCheck { 106 96 argsIn[i] = reflect.ValueOf(&responseWriter{}) 107 97 } else { ··· 129 119 130 120 func hasResponseBeenWritten(argsIn []reflect.Value) bool { 131 121 for _, argIn := range argsIn { 132 - if statusProvider, ok := argIn.Interface().(ResponseStatusProvider); ok { 133 - if statusProvider.Written() { 122 + if statusProvider, ok := argIn.Interface().(types.ResponseStatusProvider); ok { 123 + if statusProvider.WrittenStatus() != 0 { 134 124 return true 135 125 } 136 126 } ··· 161 151 return http.HandlerFunc(func(respOrig http.ResponseWriter, req *http.Request) { 162 152 // wrap the response writer to check whether the response has been written 163 153 resp := respOrig 164 - if _, ok := resp.(ResponseStatusProvider); !ok { 154 + if _, ok := resp.(types.ResponseStatusProvider); !ok { 165 155 resp = &responseWriter{respWriter: resp} 166 156 } 167 157
+1 -1
modules/web/middleware/data.go
··· 17 17 18 18 type ContextData map[string]any 19 19 20 - func (ds ContextData) GetData() map[string]any { 20 + func (ds ContextData) GetData() ContextData { 21 21 return ds 22 22 } 23 23
+12 -12
modules/web/route.go
··· 7 7 "net/http" 8 8 "strings" 9 9 10 - "code.gitea.io/gitea/modules/context" 11 10 "code.gitea.io/gitea/modules/web/middleware" 12 11 13 12 "gitea.com/go-chi/binding" 14 - chi "github.com/go-chi/chi/v5" 13 + "github.com/go-chi/chi/v5" 15 14 ) 16 15 17 - // Bind binding an obj to a handler 18 - func Bind[T any](_ T) any { 19 - return func(ctx *context.Context) { 16 + // Bind binding an obj to a handler's context data 17 + func Bind[T any](_ T) http.HandlerFunc { 18 + return func(resp http.ResponseWriter, req *http.Request) { 20 19 theObj := new(T) // create a new form obj for every request but not use obj directly 21 - binding.Bind(ctx.Req, theObj) 22 - SetForm(ctx, theObj) 23 - middleware.AssignForm(theObj, ctx.Data) 20 + data := middleware.GetContextData(req.Context()) 21 + binding.Bind(req, theObj) 22 + SetForm(data, theObj) 23 + middleware.AssignForm(theObj, data) 24 24 } 25 25 } 26 26 27 27 // SetForm set the form object 28 - func SetForm(data middleware.ContextDataStore, obj interface{}) { 29 - data.GetData()["__form"] = obj 28 + func SetForm(dataStore middleware.ContextDataStore, obj interface{}) { 29 + dataStore.GetData()["__form"] = obj 30 30 } 31 31 32 32 // GetForm returns the validate form information 33 - func GetForm(data middleware.ContextDataStore) interface{} { 34 - return data.GetData()["__form"] 33 + func GetForm(dataStore middleware.ContextDataStore) interface{} { 34 + return dataStore.GetData()["__form"] 35 35 } 36 36 37 37 // Route defines a route based on chi's router
+3 -3
modules/web/routing/logger.go
··· 8 8 "strings" 9 9 "time" 10 10 11 - "code.gitea.io/gitea/modules/context" 12 11 "code.gitea.io/gitea/modules/log" 12 + "code.gitea.io/gitea/modules/web/types" 13 13 ) 14 14 15 15 // NewLoggerHandler is a handler that will log routing to the router log taking account of ··· 86 86 } 87 87 88 88 var status int 89 - if v, ok := record.responseWriter.(context.ResponseWriter); ok { 90 - status = v.Status() 89 + if v, ok := record.responseWriter.(types.ResponseStatusProvider); ok { 90 + status = v.WrittenStatus() 91 91 } 92 92 logf := log.Info 93 93 if strings.HasPrefix(req.RequestURI, "/assets/") {
+10
modules/web/types/response.go
··· 1 + // Copyright 2023 The Gitea Authors. All rights reserved. 2 + // SPDX-License-Identifier: MIT 3 + 4 + package types 5 + 6 + // ResponseStatusProvider is an interface to get the written status in the response 7 + // Many packages need this interface, so put it in the separate package to avoid import cycle 8 + type ResponseStatusProvider interface { 9 + WrittenStatus() int 10 + }
+1 -2
routers/api/actions/actions.go
··· 4 4 package actions 5 5 6 6 import ( 7 - "context" 8 7 "net/http" 9 8 10 9 "code.gitea.io/gitea/modules/web" ··· 12 11 "code.gitea.io/gitea/routers/api/actions/runner" 13 12 ) 14 13 15 - func Routes(_ context.Context, prefix string) *web.Route { 14 + func Routes(prefix string) *web.Route { 16 15 m := web.NewRoute() 17 16 18 17 path, handler := ping.NewPingServiceHandler()
+2 -1
routers/api/actions/artifacts.go
··· 82 82 "code.gitea.io/gitea/modules/storage" 83 83 "code.gitea.io/gitea/modules/util" 84 84 "code.gitea.io/gitea/modules/web" 85 + web_types "code.gitea.io/gitea/modules/web/types" 85 86 ) 86 87 87 88 const ( ··· 102 103 } 103 104 104 105 func init() { 105 - web.RegisterHandleTypeProvider[*ArtifactContext](func(req *http.Request) web.ResponseStatusProvider { 106 + web.RegisterResponseStatusProvider[*ArtifactContext](func(req *http.Request) web_types.ResponseStatusProvider { 106 107 return req.Context().Value(artifactContextKey).(*ArtifactContext) 107 108 }) 108 109 }
+2 -3
routers/api/packages/api.go
··· 4 4 package packages 5 5 6 6 import ( 7 - gocontext "context" 8 7 "net/http" 9 8 "regexp" 10 9 "strings" ··· 96 95 97 96 // CommonRoutes provide endpoints for most package managers (except containers - see below) 98 97 // These are mounted on `/api/packages` (not `/api/v1/packages`) 99 - func CommonRoutes(ctx gocontext.Context) *web.Route { 98 + func CommonRoutes() *web.Route { 100 99 r := web.NewRoute() 101 100 102 101 r.Use(context.PackageContexter()) ··· 590 589 // ContainerRoutes provides endpoints that implement the OCI API to serve containers 591 590 // These have to be mounted on `/v2/...` to comply with the OCI spec: 592 591 // https://github.com/opencontainers/distribution-spec/blob/main/spec.md 593 - func ContainerRoutes(ctx gocontext.Context) *web.Route { 592 + func ContainerRoutes() *web.Route { 594 593 r := web.NewRoute() 595 594 596 595 r.Use(context.PackageContexter())
+2 -8
routers/api/v1/api.go
··· 64 64 package v1 65 65 66 66 import ( 67 - gocontext "context" 68 67 "fmt" 69 68 "net/http" 70 69 "strings" ··· 705 704 } 706 705 707 706 // Routes registers all v1 APIs routes to web application. 708 - func Routes(ctx gocontext.Context) *web.Route { 707 + func Routes() *web.Route { 709 708 m := web.NewRoute() 710 709 711 710 m.Use(securityHeaders()) ··· 722 721 } 723 722 m.Use(context.APIContexter()) 724 723 725 - group := buildAuthGroup() 726 - if err := group.Init(ctx); err != nil { 727 - log.Error("Could not initialize '%s' auth method, error: %s", group.Name(), err) 728 - } 729 - 730 724 // Get user from session if logged in. 731 - m.Use(auth.APIAuth(group)) 725 + m.Use(auth.APIAuth(buildAuthGroup())) 732 726 733 727 m.Use(auth.VerifyAuthWithOptionsAPI(&auth.VerifyOptions{ 734 728 SignInRequired: setting.Service.RequireSignInView,
+7 -51
routers/api/v1/misc/markup_test.go
··· 7 7 go_context "context" 8 8 "io" 9 9 "net/http" 10 - "net/http/httptest" 11 - "net/url" 12 10 "strings" 13 11 "testing" 14 12 15 - "code.gitea.io/gitea/modules/context" 16 13 "code.gitea.io/gitea/modules/markup" 17 14 "code.gitea.io/gitea/modules/setting" 18 15 api "code.gitea.io/gitea/modules/structs" 19 - "code.gitea.io/gitea/modules/util" 16 + "code.gitea.io/gitea/modules/test" 20 17 "code.gitea.io/gitea/modules/web" 21 - "code.gitea.io/gitea/modules/web/middleware" 22 18 23 19 "github.com/stretchr/testify/assert" 24 20 ) ··· 29 25 AppSubURL = AppURL + Repo + "/" 30 26 ) 31 27 32 - func createAPIContext(req *http.Request) (*context.APIContext, *httptest.ResponseRecorder) { 33 - resp := httptest.NewRecorder() 34 - base, baseCleanUp := context.NewBaseContext(resp, req) 35 - base.Data = middleware.ContextData{} 36 - c := &context.APIContext{Base: base} 37 - _ = baseCleanUp // during test, it doesn't need to do clean up. TODO: this can be improved later 38 - 39 - return c, resp 40 - } 41 - 42 28 func testRenderMarkup(t *testing.T, mode, filePath, text, responseBody string, responseCode int) { 43 29 setting.AppURL = AppURL 44 - 45 30 options := api.MarkupOption{ 46 31 Mode: mode, 47 - Text: "", 32 + Text: text, 48 33 Context: Repo, 49 34 Wiki: true, 50 35 FilePath: filePath, 51 36 } 52 - requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markup")) 53 - req := &http.Request{ 54 - Method: "POST", 55 - URL: requrl, 56 - } 57 - ctx, resp := createAPIContext(req) 58 - 59 - options.Text = text 37 + ctx, resp := test.MockAPIContext(t, "POST /api/v1/markup") 60 38 web.SetForm(ctx, &options) 61 39 Markup(ctx) 62 40 assert.Equal(t, responseBody, resp.Body.String()) ··· 66 44 67 45 func testRenderMarkdown(t *testing.T, mode, text, responseBody string, responseCode int) { 68 46 setting.AppURL = AppURL 69 - 70 47 options := api.MarkdownOption{ 71 48 Mode: mode, 72 - Text: "", 49 + Text: text, 73 50 Context: Repo, 74 51 Wiki: true, 75 52 } 76 - requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown")) 77 - req := &http.Request{ 78 - Method: "POST", 79 - URL: requrl, 80 - } 81 - ctx, resp := createAPIContext(req) 82 - 83 - options.Text = text 53 + ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown") 84 54 web.SetForm(ctx, &options) 85 55 Markdown(ctx) 86 56 assert.Equal(t, responseBody, resp.Body.String()) ··· 187 157 188 158 func TestAPI_RenderSimple(t *testing.T) { 189 159 setting.AppURL = AppURL 190 - 191 160 options := api.MarkdownOption{ 192 161 Mode: "markdown", 193 162 Text: "", 194 163 Context: Repo, 195 164 } 196 - requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown")) 197 - req := &http.Request{ 198 - Method: "POST", 199 - URL: requrl, 200 - } 201 - ctx, resp := createAPIContext(req) 202 - 165 + ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown") 203 166 for i := 0; i < len(simpleCases); i += 2 { 204 167 options.Text = simpleCases[i] 205 168 web.SetForm(ctx, &options) ··· 211 174 212 175 func TestAPI_RenderRaw(t *testing.T) { 213 176 setting.AppURL = AppURL 214 - 215 - requrl, _ := url.Parse(util.URLJoin(AppURL, "api", "v1", "markdown")) 216 - req := &http.Request{ 217 - Method: "POST", 218 - URL: requrl, 219 - } 220 - ctx, resp := createAPIContext(req) 221 - 177 + ctx, resp := test.MockAPIContext(t, "POST /api/v1/markdown") 222 178 for i := 0; i < len(simpleCases); i += 2 { 223 179 ctx.Req.Body = io.NopCloser(strings.NewReader(simpleCases[i])) 224 180 MarkdownRaw(ctx)
+1 -1
routers/api/v1/repo/hook_test.go
··· 17 17 func TestTestHook(t *testing.T) { 18 18 unittest.PrepareTestEnv(t) 19 19 20 - ctx := test.MockAPIContext(t, "user2/repo1/wiki/_pages") 20 + ctx, _ := test.MockAPIContext(t, "user2/repo1/wiki/_pages") 21 21 ctx.SetParams(":id", "1") 22 22 test.LoadRepo(t, ctx, 1) 23 23 test.LoadRepoCommit(t, ctx)
+2 -2
routers/api/v1/repo/repo_test.go
··· 19 19 func TestRepoEdit(t *testing.T) { 20 20 unittest.PrepareTestEnv(t) 21 21 22 - ctx := test.MockAPIContext(t, "user2/repo1") 22 + ctx, _ := test.MockAPIContext(t, "user2/repo1") 23 23 test.LoadRepo(t, ctx, 1) 24 24 test.LoadUser(t, ctx, 2) 25 25 ctx.Repo.Owner = ctx.Doer ··· 65 65 func TestRepoEditNameChange(t *testing.T) { 66 66 unittest.PrepareTestEnv(t) 67 67 68 - ctx := test.MockAPIContext(t, "user2/repo1") 68 + ctx, _ := test.MockAPIContext(t, "user2/repo1") 69 69 test.LoadRepo(t, ctx, 1) 70 70 test.LoadUser(t, ctx, 2) 71 71 ctx.Repo.Owner = ctx.Doer
+6 -6
routers/init.go
··· 174 174 } 175 175 176 176 // NormalRoutes represents non install routes 177 - func NormalRoutes(ctx context.Context) *web.Route { 177 + func NormalRoutes() *web.Route { 178 178 _ = templates.HTMLRenderer() 179 179 r := web.NewRoute() 180 180 r.Use(common.ProtocolMiddlewares()...) 181 181 182 - r.Mount("/", web_routers.Routes(ctx)) 183 - r.Mount("/api/v1", apiv1.Routes(ctx)) 182 + r.Mount("/", web_routers.Routes()) 183 + r.Mount("/api/v1", apiv1.Routes()) 184 184 r.Mount("/api/internal", private.Routes()) 185 185 186 186 r.Post("/-/fetch-redirect", common.FetchRedirectDelegate) 187 187 188 188 if setting.Packages.Enabled { 189 189 // This implements package support for most package managers 190 - r.Mount("/api/packages", packages_router.CommonRoutes(ctx)) 190 + r.Mount("/api/packages", packages_router.CommonRoutes()) 191 191 // This implements the OCI API (Note this is not preceded by /api but is instead /v2) 192 - r.Mount("/v2", packages_router.ContainerRoutes(ctx)) 192 + r.Mount("/v2", packages_router.ContainerRoutes()) 193 193 } 194 194 195 195 if setting.Actions.Enabled { 196 196 prefix := "/api/actions" 197 - r.Mount(prefix, actions_router.Routes(ctx, prefix)) 197 + r.Mount(prefix, actions_router.Routes(prefix)) 198 198 199 199 // TODO: Pipeline api used for runner internal communication with gitea server. but only artifact is used for now. 200 200 // In Github, it uses ACTIONS_RUNTIME_URL=https://pipelines.actions.githubusercontent.com/fLgcSHkPGySXeIFrg8W8OBSfeg3b5Fls1A1CwX566g8PayEGlg/
+5 -5
routers/web/admin/users_test.go
··· 19 19 20 20 func TestNewUserPost_MustChangePassword(t *testing.T) { 21 21 unittest.PrepareTestEnv(t) 22 - ctx := test.MockContext(t, "admin/users/new") 22 + ctx, _ := test.MockContext(t, "admin/users/new") 23 23 24 24 u := unittest.AssertExistsAndLoadBean(t, &user_model.User{ 25 25 IsAdmin: true, ··· 56 56 57 57 func TestNewUserPost_MustChangePasswordFalse(t *testing.T) { 58 58 unittest.PrepareTestEnv(t) 59 - ctx := test.MockContext(t, "admin/users/new") 59 + ctx, _ := test.MockContext(t, "admin/users/new") 60 60 61 61 u := unittest.AssertExistsAndLoadBean(t, &user_model.User{ 62 62 IsAdmin: true, ··· 93 93 94 94 func TestNewUserPost_InvalidEmail(t *testing.T) { 95 95 unittest.PrepareTestEnv(t) 96 - ctx := test.MockContext(t, "admin/users/new") 96 + ctx, _ := test.MockContext(t, "admin/users/new") 97 97 98 98 u := unittest.AssertExistsAndLoadBean(t, &user_model.User{ 99 99 IsAdmin: true, ··· 123 123 124 124 func TestNewUserPost_VisibilityDefaultPublic(t *testing.T) { 125 125 unittest.PrepareTestEnv(t) 126 - ctx := test.MockContext(t, "admin/users/new") 126 + ctx, _ := test.MockContext(t, "admin/users/new") 127 127 128 128 u := unittest.AssertExistsAndLoadBean(t, &user_model.User{ 129 129 IsAdmin: true, ··· 161 161 162 162 func TestNewUserPost_VisibilityPrivate(t *testing.T) { 163 163 unittest.PrepareTestEnv(t) 164 - ctx := test.MockContext(t, "admin/users/new") 164 + ctx, _ := test.MockContext(t, "admin/users/new") 165 165 166 166 u := unittest.AssertExistsAndLoadBean(t, &user_model.User{ 167 167 IsAdmin: true,
+1 -1
routers/web/org/projects_test.go
··· 15 15 16 16 func TestCheckProjectBoardChangePermissions(t *testing.T) { 17 17 unittest.PrepareTestEnv(t) 18 - ctx := test.MockContext(t, "user2/-/projects/4/4") 18 + ctx, _ := test.MockContext(t, "user2/-/projects/4/4") 19 19 test.LoadUser(t, ctx, 2) 20 20 ctx.ContextUser = ctx.Doer // user2 21 21 ctx.SetParams(":id", "4")
+2 -2
routers/web/repo/editor_test.go
··· 41 41 42 42 func TestGetUniquePatchBranchName(t *testing.T) { 43 43 unittest.PrepareTestEnv(t) 44 - ctx := test.MockContext(t, "user2/repo1") 44 + ctx, _ := test.MockContext(t, "user2/repo1") 45 45 ctx.SetParams(":id", "1") 46 46 test.LoadRepo(t, ctx, 1) 47 47 test.LoadRepoCommit(t, ctx) ··· 56 56 57 57 func TestGetClosestParentWithFiles(t *testing.T) { 58 58 unittest.PrepareTestEnv(t) 59 - ctx := test.MockContext(t, "user2/repo1") 59 + ctx, _ := test.MockContext(t, "user2/repo1") 60 60 ctx.SetParams(":id", "1") 61 61 test.LoadRepo(t, ctx, 1) 62 62 test.LoadRepoCommit(t, ctx)
+7 -7
routers/web/repo/issue_label_test.go
··· 32 32 func TestInitializeLabels(t *testing.T) { 33 33 unittest.PrepareTestEnv(t) 34 34 assert.NoError(t, repository.LoadRepoConfig()) 35 - ctx := test.MockContext(t, "user2/repo1/labels/initialize") 35 + ctx, _ := test.MockContext(t, "user2/repo1/labels/initialize") 36 36 test.LoadUser(t, ctx, 2) 37 37 test.LoadRepo(t, ctx, 2) 38 38 web.SetForm(ctx, &forms.InitializeLabelsForm{TemplateName: "Default"}) ··· 57 57 {1, "leastissues", []int64{2, 1}}, 58 58 {2, "", []int64{}}, 59 59 } { 60 - ctx := test.MockContext(t, "user/repo/issues") 60 + ctx, _ := test.MockContext(t, "user/repo/issues") 61 61 test.LoadUser(t, ctx, 2) 62 62 test.LoadRepo(t, ctx, testCase.RepoID) 63 63 ctx.Req.Form.Set("sort", testCase.Sort) ··· 75 75 76 76 func TestNewLabel(t *testing.T) { 77 77 unittest.PrepareTestEnv(t) 78 - ctx := test.MockContext(t, "user2/repo1/labels/edit") 78 + ctx, _ := test.MockContext(t, "user2/repo1/labels/edit") 79 79 test.LoadUser(t, ctx, 2) 80 80 test.LoadRepo(t, ctx, 1) 81 81 web.SetForm(ctx, &forms.CreateLabelForm{ ··· 93 93 94 94 func TestUpdateLabel(t *testing.T) { 95 95 unittest.PrepareTestEnv(t) 96 - ctx := test.MockContext(t, "user2/repo1/labels/edit") 96 + ctx, _ := test.MockContext(t, "user2/repo1/labels/edit") 97 97 test.LoadUser(t, ctx, 2) 98 98 test.LoadRepo(t, ctx, 1) 99 99 web.SetForm(ctx, &forms.CreateLabelForm{ ··· 113 113 114 114 func TestDeleteLabel(t *testing.T) { 115 115 unittest.PrepareTestEnv(t) 116 - ctx := test.MockContext(t, "user2/repo1/labels/delete") 116 + ctx, _ := test.MockContext(t, "user2/repo1/labels/delete") 117 117 test.LoadUser(t, ctx, 2) 118 118 test.LoadRepo(t, ctx, 1) 119 119 ctx.Req.Form.Set("id", "2") ··· 126 126 127 127 func TestUpdateIssueLabel_Clear(t *testing.T) { 128 128 unittest.PrepareTestEnv(t) 129 - ctx := test.MockContext(t, "user2/repo1/issues/labels") 129 + ctx, _ := test.MockContext(t, "user2/repo1/issues/labels") 130 130 test.LoadUser(t, ctx, 2) 131 131 test.LoadRepo(t, ctx, 1) 132 132 ctx.Req.Form.Set("issue_ids", "1,3") ··· 151 151 {"toggle", []int64{1, 2}, 2, true}, 152 152 } { 153 153 unittest.PrepareTestEnv(t) 154 - ctx := test.MockContext(t, "user2/repo1/issues/labels") 154 + ctx, _ := test.MockContext(t, "user2/repo1/issues/labels") 155 155 test.LoadUser(t, ctx, 2) 156 156 test.LoadRepo(t, ctx, 1) 157 157 ctx.Req.Form.Set("issue_ids", int64SliceToCommaSeparated(testCase.IssueIDs))
+1 -1
routers/web/repo/projects_test.go
··· 14 14 15 15 func TestCheckProjectBoardChangePermissions(t *testing.T) { 16 16 unittest.PrepareTestEnv(t) 17 - ctx := test.MockContext(t, "user2/repo1/projects/1/2") 17 + ctx, _ := test.MockContext(t, "user2/repo1/projects/1/2") 18 18 test.LoadUser(t, ctx, 2) 19 19 test.LoadRepo(t, ctx, 1) 20 20 ctx.SetParams(":id", "1")
+2 -2
routers/web/repo/release_test.go
··· 47 47 } { 48 48 unittest.PrepareTestEnv(t) 49 49 50 - ctx := test.MockContext(t, "user2/repo1/releases/new") 50 + ctx, _ := test.MockContext(t, "user2/repo1/releases/new") 51 51 test.LoadUser(t, ctx, 2) 52 52 test.LoadRepo(t, ctx, 1) 53 53 test.LoadGitRepo(t, ctx) ··· 67 67 68 68 func TestNewReleasesList(t *testing.T) { 69 69 unittest.PrepareTestEnv(t) 70 - ctx := test.MockContext(t, "user2/repo-release/releases") 70 + ctx, _ := test.MockContext(t, "user2/repo-release/releases") 71 71 test.LoadUser(t, ctx, 2) 72 72 test.LoadRepo(t, ctx, 57) 73 73 test.LoadGitRepo(t, ctx)
+11 -11
routers/web/repo/settings_test.go
··· 42 42 } 43 43 unittest.PrepareTestEnv(t) 44 44 45 - ctx := test.MockContext(t, "user2/repo1/settings/keys") 45 + ctx, _ := test.MockContext(t, "user2/repo1/settings/keys") 46 46 47 47 test.LoadUser(t, ctx, 2) 48 48 test.LoadRepo(t, ctx, 2) ··· 71 71 72 72 unittest.PrepareTestEnv(t) 73 73 74 - ctx := test.MockContext(t, "user2/repo1/settings/keys") 74 + ctx, _ := test.MockContext(t, "user2/repo1/settings/keys") 75 75 76 76 test.LoadUser(t, ctx, 2) 77 77 test.LoadRepo(t, ctx, 2) ··· 94 94 95 95 func TestCollaborationPost(t *testing.T) { 96 96 unittest.PrepareTestEnv(t) 97 - ctx := test.MockContext(t, "user2/repo1/issues/labels") 97 + ctx, _ := test.MockContext(t, "user2/repo1/issues/labels") 98 98 test.LoadUser(t, ctx, 2) 99 99 test.LoadUser(t, ctx, 4) 100 100 test.LoadRepo(t, ctx, 1) ··· 129 129 130 130 func TestCollaborationPost_InactiveUser(t *testing.T) { 131 131 unittest.PrepareTestEnv(t) 132 - ctx := test.MockContext(t, "user2/repo1/issues/labels") 132 + ctx, _ := test.MockContext(t, "user2/repo1/issues/labels") 133 133 test.LoadUser(t, ctx, 2) 134 134 test.LoadUser(t, ctx, 9) 135 135 test.LoadRepo(t, ctx, 1) ··· 152 152 153 153 func TestCollaborationPost_AddCollaboratorTwice(t *testing.T) { 154 154 unittest.PrepareTestEnv(t) 155 - ctx := test.MockContext(t, "user2/repo1/issues/labels") 155 + ctx, _ := test.MockContext(t, "user2/repo1/issues/labels") 156 156 test.LoadUser(t, ctx, 2) 157 157 test.LoadUser(t, ctx, 4) 158 158 test.LoadRepo(t, ctx, 1) ··· 193 193 194 194 func TestCollaborationPost_NonExistentUser(t *testing.T) { 195 195 unittest.PrepareTestEnv(t) 196 - ctx := test.MockContext(t, "user2/repo1/issues/labels") 196 + ctx, _ := test.MockContext(t, "user2/repo1/issues/labels") 197 197 test.LoadUser(t, ctx, 2) 198 198 test.LoadRepo(t, ctx, 1) 199 199 ··· 215 215 216 216 func TestAddTeamPost(t *testing.T) { 217 217 unittest.PrepareTestEnv(t) 218 - ctx := test.MockContext(t, "org26/repo43") 218 + ctx, _ := test.MockContext(t, "org26/repo43") 219 219 220 220 ctx.Req.Form.Set("team", "team11") 221 221 ··· 255 255 256 256 func TestAddTeamPost_NotAllowed(t *testing.T) { 257 257 unittest.PrepareTestEnv(t) 258 - ctx := test.MockContext(t, "org26/repo43") 258 + ctx, _ := test.MockContext(t, "org26/repo43") 259 259 260 260 ctx.Req.Form.Set("team", "team11") 261 261 ··· 295 295 296 296 func TestAddTeamPost_AddTeamTwice(t *testing.T) { 297 297 unittest.PrepareTestEnv(t) 298 - ctx := test.MockContext(t, "org26/repo43") 298 + ctx, _ := test.MockContext(t, "org26/repo43") 299 299 300 300 ctx.Req.Form.Set("team", "team11") 301 301 ··· 336 336 337 337 func TestAddTeamPost_NonExistentTeam(t *testing.T) { 338 338 unittest.PrepareTestEnv(t) 339 - ctx := test.MockContext(t, "org26/repo43") 339 + ctx, _ := test.MockContext(t, "org26/repo43") 340 340 341 341 ctx.Req.Form.Set("team", "team-non-existent") 342 342 ··· 369 369 370 370 func TestDeleteTeam(t *testing.T) { 371 371 unittest.PrepareTestEnv(t) 372 - ctx := test.MockContext(t, "org3/team1/repo3") 372 + ctx, _ := test.MockContext(t, "org3/team1/repo3") 373 373 374 374 ctx.Req.Form.Set("id", "2") 375 375
+9 -9
routers/web/repo/wiki_test.go
··· 78 78 func TestWiki(t *testing.T) { 79 79 unittest.PrepareTestEnv(t) 80 80 81 - ctx := test.MockContext(t, "user2/repo1/wiki/?action=_pages") 81 + ctx, _ := test.MockContext(t, "user2/repo1/wiki/?action=_pages") 82 82 ctx.SetParams("*", "Home") 83 83 test.LoadRepo(t, ctx, 1) 84 84 Wiki(ctx) ··· 90 90 func TestWikiPages(t *testing.T) { 91 91 unittest.PrepareTestEnv(t) 92 92 93 - ctx := test.MockContext(t, "user2/repo1/wiki/?action=_pages") 93 + ctx, _ := test.MockContext(t, "user2/repo1/wiki/?action=_pages") 94 94 test.LoadRepo(t, ctx, 1) 95 95 WikiPages(ctx) 96 96 assert.EqualValues(t, http.StatusOK, ctx.Resp.Status()) ··· 100 100 func TestNewWiki(t *testing.T) { 101 101 unittest.PrepareTestEnv(t) 102 102 103 - ctx := test.MockContext(t, "user2/repo1/wiki/?action=_new") 103 + ctx, _ := test.MockContext(t, "user2/repo1/wiki/?action=_new") 104 104 test.LoadUser(t, ctx, 2) 105 105 test.LoadRepo(t, ctx, 1) 106 106 NewWiki(ctx) ··· 115 115 } { 116 116 unittest.PrepareTestEnv(t) 117 117 118 - ctx := test.MockContext(t, "user2/repo1/wiki/?action=_new") 118 + ctx, _ := test.MockContext(t, "user2/repo1/wiki/?action=_new") 119 119 test.LoadUser(t, ctx, 2) 120 120 test.LoadRepo(t, ctx, 1) 121 121 web.SetForm(ctx, &forms.NewWikiForm{ ··· 133 133 func TestNewWikiPost_ReservedName(t *testing.T) { 134 134 unittest.PrepareTestEnv(t) 135 135 136 - ctx := test.MockContext(t, "user2/repo1/wiki/?action=_new") 136 + ctx, _ := test.MockContext(t, "user2/repo1/wiki/?action=_new") 137 137 test.LoadUser(t, ctx, 2) 138 138 test.LoadRepo(t, ctx, 1) 139 139 web.SetForm(ctx, &forms.NewWikiForm{ ··· 150 150 func TestEditWiki(t *testing.T) { 151 151 unittest.PrepareTestEnv(t) 152 152 153 - ctx := test.MockContext(t, "user2/repo1/wiki/Home?action=_edit") 153 + ctx, _ := test.MockContext(t, "user2/repo1/wiki/Home?action=_edit") 154 154 ctx.SetParams("*", "Home") 155 155 test.LoadUser(t, ctx, 2) 156 156 test.LoadRepo(t, ctx, 1) ··· 166 166 "New/<page>", 167 167 } { 168 168 unittest.PrepareTestEnv(t) 169 - ctx := test.MockContext(t, "user2/repo1/wiki/Home?action=_new") 169 + ctx, _ := test.MockContext(t, "user2/repo1/wiki/Home?action=_new") 170 170 ctx.SetParams("*", "Home") 171 171 test.LoadUser(t, ctx, 2) 172 172 test.LoadRepo(t, ctx, 1) ··· 188 188 func TestDeleteWikiPagePost(t *testing.T) { 189 189 unittest.PrepareTestEnv(t) 190 190 191 - ctx := test.MockContext(t, "user2/repo1/wiki/Home?action=_delete") 191 + ctx, _ := test.MockContext(t, "user2/repo1/wiki/Home?action=_delete") 192 192 test.LoadUser(t, ctx, 2) 193 193 test.LoadRepo(t, ctx, 1) 194 194 DeleteWikiPagePost(ctx) ··· 207 207 } { 208 208 unittest.PrepareTestEnv(t) 209 209 210 - ctx := test.MockContext(t, "user2/repo1/wiki/raw/"+url.PathEscape(filepath)) 210 + ctx, _ := test.MockContext(t, "user2/repo1/wiki/raw/"+url.PathEscape(filepath)) 211 211 ctx.SetParams("*", filepath) 212 212 test.LoadUser(t, ctx, 2) 213 213 test.LoadRepo(t, ctx, 1)
+5 -5
routers/web/user/home_test.go
··· 20 20 setting.UI.IssuePagingNum = 1 21 21 assert.NoError(t, unittest.LoadFixtures()) 22 22 23 - ctx := test.MockContext(t, "issues") 23 + ctx, _ := test.MockContext(t, "issues") 24 24 test.LoadUser(t, ctx, 30) 25 25 ctx.Req.Form.Set("state", "open") 26 26 ··· 53 53 setting.UI.IssuePagingNum = 1 54 54 assert.NoError(t, unittest.LoadFixtures()) 55 55 56 - ctx := test.MockContext(t, "issues") 56 + ctx, _ := test.MockContext(t, "issues") 57 57 test.LoadUser(t, ctx, 2) 58 58 ctx.Req.Form.Set("state", "closed") 59 59 Issues(ctx) ··· 69 69 setting.UI.IssuePagingNum = 20 70 70 assert.NoError(t, unittest.LoadFixtures()) 71 71 72 - ctx := test.MockContext(t, "pulls") 72 + ctx, _ := test.MockContext(t, "pulls") 73 73 test.LoadUser(t, ctx, 2) 74 74 ctx.Req.Form.Set("state", "open") 75 75 Pulls(ctx) ··· 82 82 setting.UI.IssuePagingNum = 1 83 83 assert.NoError(t, unittest.LoadFixtures()) 84 84 85 - ctx := test.MockContext(t, "milestones") 85 + ctx, _ := test.MockContext(t, "milestones") 86 86 test.LoadUser(t, ctx, 2) 87 87 ctx.SetParams("sort", "issues") 88 88 ctx.Req.Form.Set("state", "closed") ··· 101 101 setting.UI.IssuePagingNum = 1 102 102 assert.NoError(t, unittest.LoadFixtures()) 103 103 104 - ctx := test.MockContext(t, "milestones") 104 + ctx, _ := test.MockContext(t, "milestones") 105 105 test.LoadUser(t, ctx, 2) 106 106 ctx.SetParams("sort", "issues") 107 107 ctx.SetParams("repo", "1")
+1 -1
routers/web/user/setting/account_test.go
··· 83 83 t.Run(req.OldPassword+"__"+req.NewPassword, func(t *testing.T) { 84 84 unittest.PrepareTestEnv(t) 85 85 setting.PasswordComplexity = req.PasswordComplexity 86 - ctx := test.MockContext(t, "user/settings/security") 86 + ctx, _ := test.MockContext(t, "user/settings/security") 87 87 test.LoadUser(t, ctx, 2) 88 88 test.LoadRepo(t, ctx, 1) 89 89
+2 -7
routers/web/web.go
··· 104 104 } 105 105 106 106 // Routes returns all web routes 107 - func Routes(ctx gocontext.Context) *web.Route { 107 + func Routes() *web.Route { 108 108 routes := web.NewRoute() 109 109 110 110 routes.Head("/", misc.DummyOK) // for health check - doesn't need to be passed through gzip handler ··· 146 146 147 147 mid = append(mid, common.Sessioner(), context.Contexter()) 148 148 149 - group := buildAuthGroup() 150 - if err := group.Init(ctx); err != nil { 151 - log.Error("Could not initialize '%s' auth method, error: %s", group.Name(), err) 152 - } 153 - 154 149 // Get user from session if logged in. 155 - mid = append(mid, auth_service.Auth(group)) 150 + mid = append(mid, auth_service.Auth(buildAuthGroup())) 156 151 157 152 // GetHead allows a HEAD request redirect to GET if HEAD method is not defined for that route 158 153 mid = append(mid, middleware.GetHead)
+1 -33
services/auth/group.go
··· 4 4 package auth 5 5 6 6 import ( 7 - "context" 8 7 "net/http" 9 8 "reflect" 10 9 "strings" ··· 14 13 15 14 // Ensure the struct implements the interface. 16 15 var ( 17 - _ Method = &Group{} 18 - _ Initializable = &Group{} 19 - _ Freeable = &Group{} 16 + _ Method = &Group{} 20 17 ) 21 18 22 19 // Group implements the Auth interface with serval Auth. ··· 47 44 } 48 45 } 49 46 return strings.Join(names, ",") 50 - } 51 - 52 - // Init does nothing as the Basic implementation does not need to allocate any resources 53 - func (b *Group) Init(ctx context.Context) error { 54 - for _, method := range b.methods { 55 - initializable, ok := method.(Initializable) 56 - if !ok { 57 - continue 58 - } 59 - 60 - if err := initializable.Init(ctx); err != nil { 61 - return err 62 - } 63 - } 64 - return nil 65 - } 66 - 67 - // Free does nothing as the Basic implementation does not have to release any resources 68 - func (b *Group) Free() error { 69 - for _, method := range b.methods { 70 - freeable, ok := method.(Freeable) 71 - if !ok { 72 - continue 73 - } 74 - if err := freeable.Free(); err != nil { 75 - return err 76 - } 77 - } 78 - return nil 79 47 } 80 48 81 49 // Verify extracts and validates
-15
services/auth/interface.go
··· 29 29 Verify(http *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) 30 30 } 31 31 32 - // Initializable represents a structure that requires initialization 33 - // It usually should only be called once before anything else is called 34 - type Initializable interface { 35 - // Init should be called exactly once before using any of the other methods, 36 - // in order to allow the plugin to allocate necessary resources 37 - Init(ctx context.Context) error 38 - } 39 - 40 32 // Named represents a named thing 41 33 type Named interface { 42 34 Name() string 43 - } 44 - 45 - // Freeable represents a structure that is required to be freed 46 - type Freeable interface { 47 - // Free should be called exactly once before application closes, in order to 48 - // give chance to the plugin to free any allocated resources 49 - Free() error 50 35 } 51 36 52 37 // PasswordAuthenticator represents a source of authentication
+14 -22
services/auth/sspi_windows.go
··· 4 4 package auth 5 5 6 6 import ( 7 - "context" 8 7 "errors" 9 8 "net/http" 10 9 "strings" 10 + "sync" 11 11 12 12 "code.gitea.io/gitea/models/auth" 13 13 "code.gitea.io/gitea/models/avatars" ··· 32 32 // sspiAuth is a global instance of the websspi authentication package, 33 33 // which is used to avoid acquiring the server credential handle on 34 34 // every request 35 - sspiAuth *websspi.Authenticator 35 + sspiAuth *websspi.Authenticator 36 + sspiAuthOnce sync.Once 36 37 37 38 // Ensure the struct implements the interface. 38 - _ Method = &SSPI{} 39 - _ Named = &SSPI{} 40 - _ Initializable = &SSPI{} 41 - _ Freeable = &SSPI{} 39 + _ Method = &SSPI{} 40 + _ Named = &SSPI{} 42 41 ) 43 42 44 43 // SSPI implements the SingleSignOn interface and authenticates requests ··· 47 46 // Returns nil if authentication fails. 48 47 type SSPI struct{} 49 48 50 - // Init creates a new global websspi.Authenticator object 51 - func (s *SSPI) Init(ctx context.Context) error { 52 - config := websspi.NewConfig() 53 - var err error 54 - sspiAuth, err = websspi.New(config) 55 - if err != nil { 56 - return err 57 - } 58 - return nil 59 - } 60 - 61 49 // Name represents the name of auth method 62 50 func (s *SSPI) Name() string { 63 51 return "sspi" 64 - } 65 - 66 - // Free releases resources used by the global websspi.Authenticator object 67 - func (s *SSPI) Free() error { 68 - return sspiAuth.Free() 69 52 } 70 53 71 54 // Verify uses SSPI (Windows implementation of SPNEGO) to authenticate the request. ··· 73 56 // If negotiation should continue or authentication fails, immediately returns a 401 HTTP 74 57 // response code, as required by the SPNEGO protocol. 75 58 func (s *SSPI) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) { 59 + var errInit error 60 + sspiAuthOnce.Do(func() { 61 + config := websspi.NewConfig() 62 + sspiAuth, errInit = websspi.New(config) 63 + }) 64 + if errInit != nil { 65 + return nil, errInit 66 + } 67 + 76 68 if !s.shouldAuthenticate(req) { 77 69 return nil, nil 78 70 }
+1 -1
services/repository/archiver/archiver_test.go
··· 24 24 func TestArchive_Basic(t *testing.T) { 25 25 assert.NoError(t, unittest.PrepareTestDatabase()) 26 26 27 - ctx := test.MockContext(t, "user27/repo49") 27 + ctx, _ := test.MockContext(t, "user27/repo49") 28 28 firstCommit, secondCommit := "51f84af23134", "aacbdfe9e1c4" 29 29 30 30 test.LoadRepo(t, ctx, 49)
+7 -7
services/repository/files/content_test.go
··· 54 54 55 55 func TestGetContents(t *testing.T) { 56 56 unittest.PrepareTestEnv(t) 57 - ctx := test.MockContext(t, "user2/repo1") 57 + ctx, _ := test.MockContext(t, "user2/repo1") 58 58 ctx.SetParams(":id", "1") 59 59 test.LoadRepo(t, ctx, 1) 60 60 test.LoadRepoCommit(t, ctx) ··· 82 82 83 83 func TestGetContentsOrListForDir(t *testing.T) { 84 84 unittest.PrepareTestEnv(t) 85 - ctx := test.MockContext(t, "user2/repo1") 85 + ctx, _ := test.MockContext(t, "user2/repo1") 86 86 ctx.SetParams(":id", "1") 87 87 test.LoadRepo(t, ctx, 1) 88 88 test.LoadRepoCommit(t, ctx) ··· 117 117 118 118 func TestGetContentsOrListForFile(t *testing.T) { 119 119 unittest.PrepareTestEnv(t) 120 - ctx := test.MockContext(t, "user2/repo1") 120 + ctx, _ := test.MockContext(t, "user2/repo1") 121 121 ctx.SetParams(":id", "1") 122 122 test.LoadRepo(t, ctx, 1) 123 123 test.LoadRepoCommit(t, ctx) ··· 145 145 146 146 func TestGetContentsErrors(t *testing.T) { 147 147 unittest.PrepareTestEnv(t) 148 - ctx := test.MockContext(t, "user2/repo1") 148 + ctx, _ := test.MockContext(t, "user2/repo1") 149 149 ctx.SetParams(":id", "1") 150 150 test.LoadRepo(t, ctx, 1) 151 151 test.LoadRepoCommit(t, ctx) ··· 176 176 177 177 func TestGetContentsOrListErrors(t *testing.T) { 178 178 unittest.PrepareTestEnv(t) 179 - ctx := test.MockContext(t, "user2/repo1") 179 + ctx, _ := test.MockContext(t, "user2/repo1") 180 180 ctx.SetParams(":id", "1") 181 181 test.LoadRepo(t, ctx, 1) 182 182 test.LoadRepoCommit(t, ctx) ··· 207 207 208 208 func TestGetContentsOrListOfEmptyRepos(t *testing.T) { 209 209 unittest.PrepareTestEnv(t) 210 - ctx := test.MockContext(t, "user30/empty") 210 + ctx, _ := test.MockContext(t, "user30/empty") 211 211 ctx.SetParams(":id", "52") 212 212 test.LoadRepo(t, ctx, 52) 213 213 test.LoadUser(t, ctx, 30) ··· 225 225 226 226 func TestGetBlobBySHA(t *testing.T) { 227 227 unittest.PrepareTestEnv(t) 228 - ctx := test.MockContext(t, "user2/repo1") 228 + ctx, _ := test.MockContext(t, "user2/repo1") 229 229 test.LoadRepo(t, ctx, 1) 230 230 test.LoadRepoCommit(t, ctx) 231 231 test.LoadUser(t, ctx, 2)
+2 -2
services/repository/files/diff_test.go
··· 17 17 18 18 func TestGetDiffPreview(t *testing.T) { 19 19 unittest.PrepareTestEnv(t) 20 - ctx := test.MockContext(t, "user2/repo1") 20 + ctx, _ := test.MockContext(t, "user2/repo1") 21 21 ctx.SetParams(":id", "1") 22 22 test.LoadRepo(t, ctx, 1) 23 23 test.LoadRepoCommit(t, ctx) ··· 139 139 140 140 func TestGetDiffPreviewErrors(t *testing.T) { 141 141 unittest.PrepareTestEnv(t) 142 - ctx := test.MockContext(t, "user2/repo1") 142 + ctx, _ := test.MockContext(t, "user2/repo1") 143 143 ctx.SetParams(":id", "1") 144 144 test.LoadRepo(t, ctx, 1) 145 145 test.LoadRepoCommit(t, ctx)
+1 -1
services/repository/files/file_test.go
··· 98 98 99 99 func TestGetFileResponseFromCommit(t *testing.T) { 100 100 unittest.PrepareTestEnv(t) 101 - ctx := test.MockContext(t, "user2/repo1") 101 + ctx, _ := test.MockContext(t, "user2/repo1") 102 102 ctx.SetParams(":id", "1") 103 103 test.LoadRepo(t, ctx, 1) 104 104 test.LoadRepoCommit(t, ctx)
+1 -1
services/repository/files/tree_test.go
··· 15 15 16 16 func TestGetTreeBySHA(t *testing.T) { 17 17 unittest.PrepareTestEnv(t) 18 - ctx := test.MockContext(t, "user2/repo1") 18 + ctx, _ := test.MockContext(t, "user2/repo1") 19 19 test.LoadRepo(t, ctx, 1) 20 20 test.LoadRepoCommit(t, ctx) 21 21 test.LoadUser(t, ctx, 2)
+1 -1
tests/e2e/e2e_test.go
··· 38 38 defer cancel() 39 39 40 40 tests.InitTest(false) 41 - c = routers.NormalRoutes(context.TODO()) 41 + c = routers.NormalRoutes() 42 42 43 43 os.Unsetenv("GIT_AUTHOR_NAME") 44 44 os.Unsetenv("GIT_AUTHOR_EMAIL")
+6 -6
tests/integration/api_activitypub_person_test.go
··· 22 22 23 23 func TestActivityPubPerson(t *testing.T) { 24 24 setting.Federation.Enabled = true 25 - c = routers.NormalRoutes(context.TODO()) 25 + c = routers.NormalRoutes() 26 26 defer func() { 27 27 setting.Federation.Enabled = false 28 - c = routers.NormalRoutes(context.TODO()) 28 + c = routers.NormalRoutes() 29 29 }() 30 30 31 31 onGiteaRun(t, func(*testing.T, *url.URL) { ··· 60 60 61 61 func TestActivityPubMissingPerson(t *testing.T) { 62 62 setting.Federation.Enabled = true 63 - c = routers.NormalRoutes(context.TODO()) 63 + c = routers.NormalRoutes() 64 64 defer func() { 65 65 setting.Federation.Enabled = false 66 - c = routers.NormalRoutes(context.TODO()) 66 + c = routers.NormalRoutes() 67 67 }() 68 68 69 69 onGiteaRun(t, func(*testing.T, *url.URL) { ··· 75 75 76 76 func TestActivityPubPersonInbox(t *testing.T) { 77 77 setting.Federation.Enabled = true 78 - c = routers.NormalRoutes(context.TODO()) 78 + c = routers.NormalRoutes() 79 79 defer func() { 80 80 setting.Federation.Enabled = false 81 - c = routers.NormalRoutes(context.TODO()) 81 + c = routers.NormalRoutes() 82 82 }() 83 83 84 84 srv := httptest.NewServer(c)
+2 -3
tests/integration/api_nodeinfo_test.go
··· 4 4 package integration 5 5 6 6 import ( 7 - "context" 8 7 "net/http" 9 8 "net/url" 10 9 "testing" ··· 18 17 19 18 func TestNodeinfo(t *testing.T) { 20 19 setting.Federation.Enabled = true 21 - c = routers.NormalRoutes(context.TODO()) 20 + c = routers.NormalRoutes() 22 21 defer func() { 23 22 setting.Federation.Enabled = false 24 - c = routers.NormalRoutes(context.TODO()) 23 + c = routers.NormalRoutes() 25 24 }() 26 25 27 26 onGiteaRun(t, func(*testing.T, *url.URL) {
+2 -3
tests/integration/create_no_session_test.go
··· 4 4 package integration 5 5 6 6 import ( 7 - "context" 8 7 "net/http" 9 8 "net/http/httptest" 10 9 "os" ··· 57 56 oldSessionConfig := setting.SessionConfig.ProviderConfig 58 57 defer func() { 59 58 setting.SessionConfig.ProviderConfig = oldSessionConfig 60 - c = routers.NormalRoutes(context.TODO()) 59 + c = routers.NormalRoutes() 61 60 }() 62 61 63 62 var config session.Options ··· 76 75 77 76 setting.SessionConfig.ProviderConfig = string(newConfigBytes) 78 77 79 - c = routers.NormalRoutes(context.TODO()) 78 + c = routers.NormalRoutes() 80 79 81 80 t.Run("NoSessionOnViewIssue", func(t *testing.T) { 82 81 defer tests.PrintCurrentTest(t)()
+1 -1
tests/integration/integration_test.go
··· 87 87 defer cancel() 88 88 89 89 tests.InitTest(true) 90 - c = routers.NormalRoutes(context.TODO()) 90 + c = routers.NormalRoutes() 91 91 92 92 // integration test settings... 93 93 if setting.CfgProvider != nil {
+7 -7
tests/integration/repofiles_change_test.go
··· 244 244 func TestChangeRepoFilesForCreate(t *testing.T) { 245 245 // setup 246 246 onGiteaRun(t, func(t *testing.T, u *url.URL) { 247 - ctx := test.MockContext(t, "user2/repo1") 247 + ctx, _ := test.MockContext(t, "user2/repo1") 248 248 ctx.SetParams(":id", "1") 249 249 test.LoadRepo(t, ctx, 1) 250 250 test.LoadRepoCommit(t, ctx) ··· 281 281 func TestChangeRepoFilesForUpdate(t *testing.T) { 282 282 // setup 283 283 onGiteaRun(t, func(t *testing.T, u *url.URL) { 284 - ctx := test.MockContext(t, "user2/repo1") 284 + ctx, _ := test.MockContext(t, "user2/repo1") 285 285 ctx.SetParams(":id", "1") 286 286 test.LoadRepo(t, ctx, 1) 287 287 test.LoadRepoCommit(t, ctx) ··· 315 315 func TestChangeRepoFilesForUpdateWithFileMove(t *testing.T) { 316 316 // setup 317 317 onGiteaRun(t, func(t *testing.T, u *url.URL) { 318 - ctx := test.MockContext(t, "user2/repo1") 318 + ctx, _ := test.MockContext(t, "user2/repo1") 319 319 ctx.SetParams(":id", "1") 320 320 test.LoadRepo(t, ctx, 1) 321 321 test.LoadRepoCommit(t, ctx) ··· 366 366 func TestChangeRepoFilesWithoutBranchNames(t *testing.T) { 367 367 // setup 368 368 onGiteaRun(t, func(t *testing.T, u *url.URL) { 369 - ctx := test.MockContext(t, "user2/repo1") 369 + ctx, _ := test.MockContext(t, "user2/repo1") 370 370 ctx.SetParams(":id", "1") 371 371 test.LoadRepo(t, ctx, 1) 372 372 test.LoadRepoCommit(t, ctx) ··· 402 402 func testDeleteRepoFiles(t *testing.T, u *url.URL) { 403 403 // setup 404 404 unittest.PrepareTestEnv(t) 405 - ctx := test.MockContext(t, "user2/repo1") 405 + ctx, _ := test.MockContext(t, "user2/repo1") 406 406 ctx.SetParams(":id", "1") 407 407 test.LoadRepo(t, ctx, 1) 408 408 test.LoadRepoCommit(t, ctx) ··· 441 441 func testDeleteRepoFilesWithoutBranchNames(t *testing.T, u *url.URL) { 442 442 // setup 443 443 unittest.PrepareTestEnv(t) 444 - ctx := test.MockContext(t, "user2/repo1") 444 + ctx, _ := test.MockContext(t, "user2/repo1") 445 445 ctx.SetParams(":id", "1") 446 446 test.LoadRepo(t, ctx, 1) 447 447 test.LoadRepoCommit(t, ctx) ··· 471 471 func TestChangeRepoFilesErrors(t *testing.T) { 472 472 // setup 473 473 onGiteaRun(t, func(t *testing.T, u *url.URL) { 474 - ctx := test.MockContext(t, "user2/repo1") 474 + ctx, _ := test.MockContext(t, "user2/repo1") 475 475 ctx.SetParams(":id", "1") 476 476 test.LoadRepo(t, ctx, 1) 477 477 test.LoadRepoCommit(t, ctx)