Skip to content

Commit

Permalink
Remove some interfaces (woodpecker-ci#3220)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerty287 authored and fernandrone committed Feb 1, 2024
1 parent 32625bc commit 9132419
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 26 deletions.
2 changes: 1 addition & 1 deletion cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 9 additions & 15 deletions server/forge/configFetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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, "/") {
Expand Down
3 changes: 1 addition & 2 deletions server/router/middleware/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 4 additions & 8 deletions server/store/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down

0 comments on commit 9132419

Please sign in to comment.