From 9132419139cef744dc01e13ce62a61a3cd2cb4bc Mon Sep 17 00:00:00 2001 From: qwerty287 <80460567+qwerty287@users.noreply.github.com> Date: Fri, 19 Jan 2024 16:20:35 +0100 Subject: [PATCH] Remove some interfaces (#3220) --- cmd/server/server.go | 2 +- server/forge/configFetcher.go | 24 +++++++++--------------- server/router/middleware/store.go | 3 +-- server/store/context.go | 12 ++++-------- 4 files changed, 15 insertions(+), 26 deletions(-) diff --git a/cmd/server/server.go b/cmd/server/server.go index 956176af02a..eb5b9f03509 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -182,7 +182,7 @@ func run(c *cli.Context) error { webUIServe, middleware.Logger(time.RFC3339, true), middleware.Version, - middleware.Store(c, _store), + middleware.Store(_store), ) switch { diff --git a/server/forge/configFetcher.go b/server/forge/configFetcher.go index a1176231026..ef989b34c50 100644 --- a/server/forge/configFetcher.go +++ b/server/forge/configFetcher.go @@ -29,39 +29,33 @@ import ( "go.woodpecker-ci.org/woodpecker/v2/shared/constant" ) -type ConfigFetcher interface { - Fetch(ctx context.Context) (files []*types.FileMeta, err error) -} - -type configFetcher struct { +type ConfigFetcher struct { forge Forge user *model.User repo *model.Repo pipeline *model.Pipeline configExtension config.Extension - configPath string timeout time.Duration } -func NewConfigFetcher(forge Forge, timeout time.Duration, configExtension config.Extension, user *model.User, repo *model.Repo, pipeline *model.Pipeline) ConfigFetcher { - return &configFetcher{ +func NewConfigFetcher(forge Forge, timeout time.Duration, configExtension config.Extension, user *model.User, repo *model.Repo, pipeline *model.Pipeline) *ConfigFetcher { + return &ConfigFetcher{ forge: forge, user: user, repo: repo, pipeline: pipeline, configExtension: configExtension, - configPath: repo.Config, timeout: timeout, } } // Fetch pipeline config from source forge -func (cf *configFetcher) Fetch(ctx context.Context) (files []*types.FileMeta, err error) { +func (cf *ConfigFetcher) Fetch(ctx context.Context) (files []*types.FileMeta, err error) { log.Trace().Msgf("start fetching config for '%s'", cf.repo.FullName) // try to fetch 3 times for i := 0; i < 3; i++ { - files, err = cf.fetch(ctx, cf.timeout, strings.TrimSpace(cf.configPath)) + files, err = cf.fetch(ctx, strings.TrimSpace(cf.repo.Config)) if err != nil { log.Trace().Err(err).Msgf("%d. try failed", i+1) } @@ -96,8 +90,8 @@ func (cf *configFetcher) Fetch(ctx context.Context) (files []*types.FileMeta, er } // fetch config by timeout -func (cf *configFetcher) fetch(c context.Context, timeout time.Duration, config string) ([]*types.FileMeta, error) { - ctx, cancel := context.WithTimeout(c, timeout) +func (cf *ConfigFetcher) fetch(c context.Context, config string) ([]*types.FileMeta, error) { + ctx, cancel := context.WithTimeout(c, cf.timeout) defer cancel() if len(config) > 0 { @@ -141,7 +135,7 @@ func filterPipelineFiles(files []*types.FileMeta) []*types.FileMeta { return res } -func (cf *configFetcher) checkPipelineFile(c context.Context, config string) ([]*types.FileMeta, error) { +func (cf *ConfigFetcher) checkPipelineFile(c context.Context, config string) ([]*types.FileMeta, error) { file, err := cf.forge.File(c, cf.user, cf.repo, cf.pipeline, config) if err == nil && len(file) != 0 { @@ -156,7 +150,7 @@ func (cf *configFetcher) checkPipelineFile(c context.Context, config string) ([] return nil, err } -func (cf *configFetcher) getFirstAvailableConfig(c context.Context, configs []string) ([]*types.FileMeta, error) { +func (cf *ConfigFetcher) getFirstAvailableConfig(c context.Context, configs []string) ([]*types.FileMeta, error) { var forgeErr []error for _, fileOrFolder := range configs { if strings.HasSuffix(fileOrFolder, "/") { diff --git a/server/router/middleware/store.go b/server/router/middleware/store.go index e57a604e580..6ab61167e3f 100644 --- a/server/router/middleware/store.go +++ b/server/router/middleware/store.go @@ -16,14 +16,13 @@ package middleware import ( "github.com/gin-gonic/gin" - "github.com/urfave/cli/v2" "go.woodpecker-ci.org/woodpecker/v2/server/store" ) // Store is a middleware function that initializes the Datastore and attaches to // the context of every http.Request. -func Store(_ *cli.Context, v store.Store) gin.HandlerFunc { +func Store(v store.Store) gin.HandlerFunc { return func(c *gin.Context) { store.ToContext(c, v) c.Next() diff --git a/server/store/context.go b/server/store/context.go index 7c3fd23fe7f..6615302f347 100644 --- a/server/store/context.go +++ b/server/store/context.go @@ -16,15 +16,12 @@ package store import ( "context" + + "github.com/gin-gonic/gin" ) const key = "store" -// Setter defines a context that enables setting values. -type Setter interface { - Set(string, any) -} - // FromContext returns the Store associated with this context. func FromContext(c context.Context) Store { store, _ := c.Value(key).(Store) @@ -37,9 +34,8 @@ func TryFromContext(c context.Context) (Store, bool) { return store, ok } -// ToContext adds the Store to this context if it supports -// the Setter interface. -func ToContext(c Setter, store Store) { +// ToContext adds the Store to this context. +func ToContext(c *gin.Context, store Store) { c.Set(key, store) }