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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 22:54:21 +02:00
|
|
|
func (cf *configFetcher) Fetch() (files []*remote.FileMeta, err error) {
|
|
|
|
var file []byte
|
|
|
|
|
2019-06-04 15:04:18 +02:00
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
select {
|
|
|
|
case <-time.After(time.Second * time.Duration(i)):
|
2021-09-17 21:40:43 +02:00
|
|
|
if len(cf.repo.Config) > 0 {
|
|
|
|
// either a file
|
|
|
|
if !strings.HasSuffix(cf.repo.Config, "/") {
|
|
|
|
file, err = cf.remote_.File(cf.user, cf.repo, cf.build, cf.repo.Config)
|
|
|
|
if err == nil {
|
|
|
|
return []*remote.FileMeta{{
|
|
|
|
Name: cf.repo.Config,
|
|
|
|
Data: file,
|
|
|
|
}}, nil
|
|
|
|
}
|
|
|
|
}
|
2021-08-30 22:54:21 +02:00
|
|
|
|
2021-09-17 21:40:43 +02:00
|
|
|
// or a folder
|
|
|
|
if strings.HasSuffix(cf.repo.Config, "/") {
|
|
|
|
files, err = cf.remote_.Dir(cf.user, cf.repo, cf.build, strings.TrimSuffix(cf.repo.Config, "/"))
|
|
|
|
if err == nil {
|
|
|
|
return filterPipelineFiles(files), nil
|
|
|
|
}
|
2021-08-30 22:54:21 +02:00
|
|
|
}
|
2021-09-17 21:40:43 +02:00
|
|
|
} else {
|
|
|
|
// no user defined config so try .woodpecker/*.yml -> .woodpecker.yml -> .drone.yml
|
2019-06-04 15:04:18 +02:00
|
|
|
|
2021-09-17 21:40:43 +02:00
|
|
|
// test .woodpecker/ folder
|
|
|
|
// if folder is not supported we will get a "Not implemented" error and continue
|
|
|
|
files, err = cf.remote_.Dir(cf.user, cf.repo, cf.build, ".woodpecker")
|
2021-08-30 22:54:21 +02:00
|
|
|
if err == nil {
|
|
|
|
return filterPipelineFiles(files), nil
|
|
|
|
}
|
2019-06-13 18:09:25 +02:00
|
|
|
|
2021-09-17 21:40:43 +02:00
|
|
|
file, err = cf.remote_.File(cf.user, cf.repo, cf.build, ".woodpecker.yml")
|
|
|
|
if err == nil {
|
|
|
|
return []*remote.FileMeta{{
|
|
|
|
Name: ".woodpecker.yml",
|
|
|
|
Data: file,
|
|
|
|
}}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-30 22:54:21 +02:00
|
|
|
file, err = cf.remote_.File(cf.user, cf.repo, cf.build, ".drone.yml")
|
|
|
|
if err == nil {
|
|
|
|
return []*remote.FileMeta{{
|
|
|
|
Name: ".drone.yml",
|
|
|
|
Data: file,
|
|
|
|
}}, nil
|
|
|
|
}
|
2019-06-25 13:45:43 +02:00
|
|
|
}
|
|
|
|
|
2021-08-30 22:54:21 +02:00
|
|
|
return nil, err
|
2019-06-04 15:04:18 +02:00
|
|
|
}
|
|
|
|
}
|
2021-08-30 22:54:21 +02:00
|
|
|
|
2019-06-04 15:04:18 +02:00
|
|
|
return []*remote.FileMeta{}, nil
|
|
|
|
}
|
2021-08-30 22:54:21 +02:00
|
|
|
|
|
|
|
func filterPipelineFiles(files []*remote.FileMeta) []*remote.FileMeta {
|
|
|
|
var res []*remote.FileMeta
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
if strings.HasSuffix(file.Name, ".yml") || strings.HasSuffix(file.Name, ".yaml") {
|
|
|
|
res = append(res, file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|