Remove some interfaces (#3220)

This commit is contained in:
qwerty287 2024-01-19 16:20:35 +01:00 committed by GitHub
parent d1d2e9723d
commit b82790d54c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 26 deletions

View file

@ -182,7 +182,7 @@ func run(c *cli.Context) error {
webUIServe, webUIServe,
middleware.Logger(time.RFC3339, true), middleware.Logger(time.RFC3339, true),
middleware.Version, middleware.Version,
middleware.Store(c, _store), middleware.Store(_store),
) )
switch { switch {

View file

@ -29,39 +29,33 @@ import (
"go.woodpecker-ci.org/woodpecker/v2/shared/constant" "go.woodpecker-ci.org/woodpecker/v2/shared/constant"
) )
type ConfigFetcher interface { type ConfigFetcher struct {
Fetch(ctx context.Context) (files []*types.FileMeta, err error)
}
type configFetcher struct {
forge Forge forge Forge
user *model.User user *model.User
repo *model.Repo repo *model.Repo
pipeline *model.Pipeline pipeline *model.Pipeline
configExtension config.Extension configExtension config.Extension
configPath string
timeout time.Duration timeout time.Duration
} }
func NewConfigFetcher(forge Forge, timeout time.Duration, configExtension config.Extension, user *model.User, repo *model.Repo, pipeline *model.Pipeline) ConfigFetcher { func NewConfigFetcher(forge Forge, timeout time.Duration, configExtension config.Extension, user *model.User, repo *model.Repo, pipeline *model.Pipeline) *ConfigFetcher {
return &configFetcher{ return &ConfigFetcher{
forge: forge, forge: forge,
user: user, user: user,
repo: repo, repo: repo,
pipeline: pipeline, pipeline: pipeline,
configExtension: configExtension, configExtension: configExtension,
configPath: repo.Config,
timeout: timeout, timeout: timeout,
} }
} }
// Fetch pipeline config from source forge // 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) log.Trace().Msgf("start fetching config for '%s'", cf.repo.FullName)
// try to fetch 3 times // try to fetch 3 times
for i := 0; i < 3; i++ { 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 { if err != nil {
log.Trace().Err(err).Msgf("%d. try failed", i+1) 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 // fetch config by timeout
func (cf *configFetcher) fetch(c context.Context, timeout time.Duration, config string) ([]*types.FileMeta, error) { func (cf *ConfigFetcher) fetch(c context.Context, config string) ([]*types.FileMeta, error) {
ctx, cancel := context.WithTimeout(c, timeout) ctx, cancel := context.WithTimeout(c, cf.timeout)
defer cancel() defer cancel()
if len(config) > 0 { if len(config) > 0 {
@ -141,7 +135,7 @@ func filterPipelineFiles(files []*types.FileMeta) []*types.FileMeta {
return res 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) file, err := cf.forge.File(c, cf.user, cf.repo, cf.pipeline, config)
if err == nil && len(file) != 0 { if err == nil && len(file) != 0 {
@ -156,7 +150,7 @@ func (cf *configFetcher) checkPipelineFile(c context.Context, config string) ([]
return nil, err 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 var forgeErr []error
for _, fileOrFolder := range configs { for _, fileOrFolder := range configs {
if strings.HasSuffix(fileOrFolder, "/") { if strings.HasSuffix(fileOrFolder, "/") {

View file

@ -16,14 +16,13 @@ package middleware
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/urfave/cli/v2"
"go.woodpecker-ci.org/woodpecker/v2/server/store" "go.woodpecker-ci.org/woodpecker/v2/server/store"
) )
// Store is a middleware function that initializes the Datastore and attaches to // Store is a middleware function that initializes the Datastore and attaches to
// the context of every http.Request. // 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) { return func(c *gin.Context) {
store.ToContext(c, v) store.ToContext(c, v)
c.Next() c.Next()

View file

@ -16,15 +16,12 @@ package store
import ( import (
"context" "context"
"github.com/gin-gonic/gin"
) )
const key = "store" 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. // FromContext returns the Store associated with this context.
func FromContext(c context.Context) Store { func FromContext(c context.Context) Store {
store, _ := c.Value(key).(Store) store, _ := c.Value(key).(Store)
@ -37,9 +34,8 @@ func TryFromContext(c context.Context) (Store, bool) {
return store, ok return store, ok
} }
// ToContext adds the Store to this context if it supports // ToContext adds the Store to this context.
// the Setter interface. func ToContext(c *gin.Context, store Store) {
func ToContext(c Setter, store Store) {
c.Set(key, store) c.Set(key, store)
} }