fix sanitzie-path

This commit is contained in:
Anton Bracke 2021-09-19 13:28:27 +02:00
parent 9c208c1ab0
commit aa4fa9aab3
No known key found for this signature in database
GPG key ID: B1222603899C6B25
3 changed files with 53 additions and 24 deletions

View file

@ -338,7 +338,7 @@ func findOrPersistPipelineConfig(repo *model.Repo, build *model.Build, remoteYam
RepoID: build.RepoID, RepoID: build.RepoID,
Data: string(remoteYamlConfig.Data), Data: string(remoteYamlConfig.Data),
Hash: sha, Hash: sha,
Name: sanitizePath(remoteYamlConfig.Name, repo.Config), Name: sanitizePath(remoteYamlConfig.Name),
} }
err = Config.Storage.Config.ConfigCreate(conf) err = Config.Storage.Config.ConfigCreate(conf)
if err != nil { if err != nil {

View file

@ -18,6 +18,7 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"net/url" "net/url"
"path/filepath"
"sort" "sort"
"strings" "strings"
@ -78,7 +79,7 @@ func (b *procBuilder) Build() ([]*buildItem, error) {
PGID: pidSequence, PGID: pidSequence,
State: model.StatusPending, State: model.StatusPending,
Environ: axis, Environ: axis,
Name: sanitizePath(y.Name, b.Repo.Config), Name: sanitizePath(y.Name),
} }
metadata := metadataFromStruct(b.Repo, b.Curr, b.Last, proc, b.Link) metadata := metadataFromStruct(b.Repo, b.Curr, b.Last, proc, b.Link)
@ -358,9 +359,9 @@ func metadataFromStruct(repo *model.Repo, build, last *model.Build, proc *model.
} }
} }
func sanitizePath(path string, configFolder string) string { func sanitizePath(path string) string {
path = filepath.Base(path)
path = strings.TrimSuffix(path, ".yml") path = strings.TrimSuffix(path, ".yml")
path = strings.TrimPrefix(path, configFolder)
path = strings.TrimPrefix(path, ".") path = strings.TrimPrefix(path, ".")
return path return path
} }

View file

@ -37,13 +37,13 @@ bbb`,
Regs: []*model.Registry{}, Regs: []*model.Registry{},
Link: "", Link: "",
Yamls: []*remote.FileMeta{ Yamls: []*remote.FileMeta{
&remote.FileMeta{Data: []byte(` {Data: []byte(`
pipeline: pipeline:
xxx: xxx:
image: scratch image: scratch
yyy: ${DRONE_COMMIT_MESSAGE} yyy: ${DRONE_COMMIT_MESSAGE}
`)}, `)},
&remote.FileMeta{Data: []byte(` {Data: []byte(`
pipeline: pipeline:
build: build:
image: scratch image: scratch
@ -70,12 +70,12 @@ func TestMultiPipeline(t *testing.T) {
Regs: []*model.Registry{}, Regs: []*model.Registry{},
Link: "", Link: "",
Yamls: []*remote.FileMeta{ Yamls: []*remote.FileMeta{
&remote.FileMeta{Data: []byte(` {Data: []byte(`
pipeline: pipeline:
xxx: xxx:
image: scratch image: scratch
`)}, `)},
&remote.FileMeta{Data: []byte(` {Data: []byte(`
pipeline: pipeline:
build: build:
image: scratch image: scratch
@ -104,17 +104,17 @@ func TestDependsOn(t *testing.T) {
Regs: []*model.Registry{}, Regs: []*model.Registry{},
Link: "", Link: "",
Yamls: []*remote.FileMeta{ Yamls: []*remote.FileMeta{
&remote.FileMeta{Name: "lint", Data: []byte(` {Name: "lint", Data: []byte(`
pipeline: pipeline:
build: build:
image: scratch image: scratch
`)}, `)},
&remote.FileMeta{Name: "test", Data: []byte(` {Name: "test", Data: []byte(`
pipeline: pipeline:
build: build:
image: scratch image: scratch
`)}, `)},
&remote.FileMeta{Data: []byte(` {Data: []byte(`
pipeline: pipeline:
deploy: deploy:
image: scratch image: scratch
@ -150,7 +150,7 @@ func TestRunsOn(t *testing.T) {
Regs: []*model.Registry{}, Regs: []*model.Registry{},
Link: "", Link: "",
Yamls: []*remote.FileMeta{ Yamls: []*remote.FileMeta{
&remote.FileMeta{Data: []byte(` {Data: []byte(`
pipeline: pipeline:
deploy: deploy:
image: scratch image: scratch
@ -186,13 +186,13 @@ func TestBranchFilter(t *testing.T) {
Regs: []*model.Registry{}, Regs: []*model.Registry{},
Link: "", Link: "",
Yamls: []*remote.FileMeta{ Yamls: []*remote.FileMeta{
&remote.FileMeta{Data: []byte(` {Data: []byte(`
pipeline: pipeline:
xxx: xxx:
image: scratch image: scratch
branches: master branches: master
`)}, `)},
&remote.FileMeta{Data: []byte(` {Data: []byte(`
pipeline: pipeline:
build: build:
image: scratch image: scratch
@ -234,7 +234,7 @@ func TestZeroSteps(t *testing.T) {
Regs: []*model.Registry{}, Regs: []*model.Registry{},
Link: "", Link: "",
Yamls: []*remote.FileMeta{ Yamls: []*remote.FileMeta{
&remote.FileMeta{Data: []byte(` {Data: []byte(`
skip_clone: true skip_clone: true
pipeline: pipeline:
build: build:
@ -268,7 +268,7 @@ func TestZeroStepsAsMultiPipelineDeps(t *testing.T) {
Regs: []*model.Registry{}, Regs: []*model.Registry{},
Link: "", Link: "",
Yamls: []*remote.FileMeta{ Yamls: []*remote.FileMeta{
&remote.FileMeta{Name: "zerostep", Data: []byte(` {Name: "zerostep", Data: []byte(`
skip_clone: true skip_clone: true
pipeline: pipeline:
build: build:
@ -276,12 +276,12 @@ pipeline:
branch: notdev branch: notdev
image: scratch image: scratch
`)}, `)},
&remote.FileMeta{Name: "justastep", Data: []byte(` {Name: "justastep", Data: []byte(`
pipeline: pipeline:
build: build:
image: scratch image: scratch
`)}, `)},
&remote.FileMeta{Name: "shouldbefiltered", Data: []byte(` {Name: "shouldbefiltered", Data: []byte(`
pipeline: pipeline:
build: build:
image: scratch image: scratch
@ -316,7 +316,7 @@ func TestZeroStepsAsMultiPipelineTransitiveDeps(t *testing.T) {
Regs: []*model.Registry{}, Regs: []*model.Registry{},
Link: "", Link: "",
Yamls: []*remote.FileMeta{ Yamls: []*remote.FileMeta{
&remote.FileMeta{Name: "zerostep", Data: []byte(` {Name: "zerostep", Data: []byte(`
skip_clone: true skip_clone: true
pipeline: pipeline:
build: build:
@ -324,18 +324,18 @@ pipeline:
branch: notdev branch: notdev
image: scratch image: scratch
`)}, `)},
&remote.FileMeta{Name: "justastep", Data: []byte(` {Name: "justastep", Data: []byte(`
pipeline: pipeline:
build: build:
image: scratch image: scratch
`)}, `)},
&remote.FileMeta{Name: "shouldbefiltered", Data: []byte(` {Name: "shouldbefiltered", Data: []byte(`
pipeline: pipeline:
build: build:
image: scratch image: scratch
depends_on: [ zerostep ] depends_on: [ zerostep ]
`)}, `)},
&remote.FileMeta{Name: "shouldbefilteredtoo", Data: []byte(` {Name: "shouldbefilteredtoo", Data: []byte(`
pipeline: pipeline:
build: build:
image: scratch image: scratch
@ -369,8 +369,7 @@ func TestTree(t *testing.T) {
Secs: []*model.Secret{}, Secs: []*model.Secret{},
Regs: []*model.Registry{}, Regs: []*model.Registry{},
Link: "", Link: "",
Yamls: []*remote.FileMeta{ Yamls: []*remote.FileMeta{{Data: []byte(`
&remote.FileMeta{Data: []byte(`
pipeline: pipeline:
build: build:
image: scratch image: scratch
@ -393,3 +392,32 @@ pipeline:
t.Fatal("Build step should be a children of the stage") t.Fatal("Build step should be a children of the stage")
} }
} }
func TestSanitizePath(t *testing.T) {
t.Parallel()
testTable := []struct {
path string
sanitizedPath string
}{
{
path: ".woodpecker/test.yml",
sanitizedPath: "test",
},
{
path: ".woodpecker.yml",
sanitizedPath: "woodpecker",
},
{
path: "folder/sub-folder/test.yml",
sanitizedPath: "test",
},
}
for _, test := range testTable {
if test.sanitizedPath != sanitizePath(test.path) {
t.Fatal("Path hasn't been sanitized correctly")
}
}
}