Handling zero-step pipelines as multi-pipeline depedencies

This commit is contained in:
Laszlo Fogas 2019-07-22 14:13:46 +02:00
parent ed51df29b3
commit 6879bf62cb
2 changed files with 91 additions and 10 deletions

View file

@ -133,9 +133,44 @@ func (b *procBuilder) Build() ([]*buildItem, error) {
}
}
items = filterItemsWithMissingDependencies(items)
return items, nil
}
func filterItemsWithMissingDependencies(items []*buildItem) []*buildItem {
itemsToRemove := make([]*buildItem, 0)
for _, item := range items {
for _, dep := range item.DependsOn {
if !containsItemWithName(dep, items) {
itemsToRemove = append(itemsToRemove, item)
}
}
}
if len(itemsToRemove) > 0 {
filtered := make([]*buildItem, 0)
for _, item := range items {
if !containsItemWithName(item.Proc.Name, itemsToRemove) {
filtered = append(filtered, item)
}
}
return filtered
}
return items
}
func containsItemWithName(name string, items []*buildItem) bool {
for _, item := range items {
if name == item.Proc.Name {
return true
}
}
return false
}
func (b *procBuilder) envsubst_(y string, environ map[string]string) (string, error) {
return envsubst.Eval(y, func(name string) string {
env := environ[name]

View file

@ -70,13 +70,11 @@ func TestMultiPipeline(t *testing.T) {
pipeline:
xxx:
image: scratch
yyy: ${DRONE_COMMIT_MESSAGE}
`)},
&remote.FileMeta{Data: []byte(`
pipeline:
build:
image: scratch
yyy: ${DRONE_COMMIT_MESSAGE}
`)},
},
}
@ -100,6 +98,16 @@ func TestDependsOn(t *testing.T) {
Regs: []*model.Registry{},
Link: "",
Yamls: []*remote.FileMeta{
&remote.FileMeta{Name: "lint", Data: []byte(`
pipeline:
build:
image: scratch
`)},
&remote.FileMeta{Name: "test", Data: []byte(`
pipeline:
build:
image: scratch
`)},
&remote.FileMeta{Data: []byte(`
pipeline:
deploy:
@ -108,7 +116,6 @@ pipeline:
depends_on:
- lint
- test
- build
`)},
},
}
@ -117,7 +124,7 @@ depends_on:
if err != nil {
t.Fatal(err)
}
if len(buildItems[0].DependsOn) != 3 {
if len(buildItems[0].DependsOn) != 2 {
t.Fatal("Should have 3 dependencies")
}
if buildItems[0].DependsOn[1] != "test" {
@ -173,14 +180,12 @@ func TestBranchFilter(t *testing.T) {
pipeline:
xxx:
image: scratch
yyy: ${DRONE_COMMIT_MESSAGE}
branches: master
`)},
&remote.FileMeta{Data: []byte(`
pipeline:
build:
image: scratch
yyy: ${DRONE_COMMIT_MESSAGE}
`)},
},
}
@ -224,7 +229,6 @@ pipeline:
when:
branch: notdev
image: scratch
yyy: ${DRONE_COMMIT_MESSAGE}
`)},
},
}
@ -236,8 +240,51 @@ pipeline:
if len(buildItems) != 0 {
t.Fatal("Should not generate a build item if there are no steps")
}
if len(build.Procs) != 0 {
t.Fatal("Should not generate a build item if there are no steps")
}
func TestZeroStepsAsMultiPipelineDeps(t *testing.T) {
build := &model.Build{Branch: "dev"}
b := procBuilder{
Repo: &model.Repo{},
Curr: build,
Last: &model.Build{},
Netrc: &model.Netrc{},
Secs: []*model.Secret{},
Regs: []*model.Registry{},
Link: "",
Yamls: []*remote.FileMeta{
&remote.FileMeta{Name: "zerostep", Data: []byte(`
skip_clone: true
pipeline:
build:
when:
branch: notdev
image: scratch
`)},
&remote.FileMeta{Name: "justastep", Data: []byte(`
pipeline:
build:
image: scratch
`)},
&remote.FileMeta{Name: "shouldbefiltered", Data: []byte(`
pipeline:
build:
image: scratch
depends_on: [ zerostep ]
`)},
},
}
buildItems, err := b.Build()
if err != nil {
t.Fatal(err)
}
if len(buildItems) != 1 {
t.Fatal("Zerostep and the step that depends on it should not generate a build item")
}
if "justastep" != buildItems[0].Proc.Name {
t.Fatal("justastep should have been generated")
}
}
@ -257,7 +304,6 @@ func TestTree(t *testing.T) {
pipeline:
build:
image: scratch
yyy: ${DRONE_COMMIT_MESSAGE}
`)},
},
}