2019-06-04 15:04:18 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2019-06-24 09:04:30 +02:00
|
|
|
"strings"
|
2019-06-04 15:04:18 +02:00
|
|
|
"time"
|
|
|
|
|
2021-05-25 14:08:27 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/model"
|
|
|
|
"github.com/woodpecker-ci/woodpecker/remote"
|
2019-06-04 15:04:18 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type configFetcher struct {
|
|
|
|
remote_ remote.Remote
|
|
|
|
user *model.User
|
|
|
|
repo *model.Repo
|
|
|
|
build *model.Build
|
|
|
|
}
|
|
|
|
|
2021-08-20 16:32:52 +02:00
|
|
|
func NewConfigFetcher(remote remote.Remote, user *model.User, repo *model.Repo, build *model.Build) *configFetcher {
|
|
|
|
return &configFetcher{
|
|
|
|
remote_: remote,
|
|
|
|
user: user,
|
|
|
|
repo: repo,
|
|
|
|
build: build,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 15:04:18 +02:00
|
|
|
func (cf *configFetcher) Fetch() ([]*remote.FileMeta, error) {
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Second * time.Duration(i)):
|
2019-06-24 09:04:30 +02:00
|
|
|
// either a file
|
2019-06-25 13:45:43 +02:00
|
|
|
file, fileerr := cf.remote_.File(cf.user, cf.repo, cf.build, cf.repo.Config)
|
2019-06-05 10:08:49 +02:00
|
|
|
if fileerr == nil {
|
2021-08-20 16:32:52 +02:00
|
|
|
return []*remote.FileMeta{{
|
2019-06-04 15:04:18 +02:00
|
|
|
Name: cf.repo.Config,
|
|
|
|
Data: file,
|
|
|
|
}}, nil
|
|
|
|
}
|
|
|
|
|
2019-06-24 09:04:30 +02:00
|
|
|
// or a folder
|
|
|
|
dir, direrr := cf.remote_.Dir(cf.user, cf.repo, cf.build, strings.TrimSuffix(cf.repo.Config, "/"))
|
2019-06-25 13:45:43 +02:00
|
|
|
|
|
|
|
if direrr == nil {
|
|
|
|
return dir, nil
|
|
|
|
} else if !cf.repo.Fallback {
|
2019-06-05 10:08:49 +02:00
|
|
|
return nil, direrr
|
2019-06-04 15:04:18 +02:00
|
|
|
}
|
2019-06-13 18:09:25 +02:00
|
|
|
|
2019-06-25 13:45:43 +02:00
|
|
|
// or fallback
|
|
|
|
file, fileerr = cf.remote_.File(cf.user, cf.repo, cf.build, ".drone.yml")
|
|
|
|
if fileerr != nil {
|
|
|
|
return nil, fileerr
|
|
|
|
}
|
|
|
|
|
2021-08-20 16:32:52 +02:00
|
|
|
return []*remote.FileMeta{{
|
2019-06-25 13:45:43 +02:00
|
|
|
Name: cf.repo.Config,
|
|
|
|
Data: file,
|
|
|
|
}}, nil
|
2019-06-04 15:04:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return []*remote.FileMeta{}, nil
|
|
|
|
}
|