Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Pipeline Trigger Logic with path and skip_path Constraints #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 25 additions & 35 deletions pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"fmt"
"os"
"reflect"
"strings"

"github.com/bmatcuk/doublestar/v2"
Expand Down Expand Up @@ -109,40 +108,50 @@ func stepsToTrigger(files []string, watch []WatchConfig) ([]Step, error) {
defaultStep = &w.Step
continue
}
for _, p := range w.Paths {
for _, f := range files {
match, err := matchPath(p, f)

skip := false
for _, sp := range w.SkipPaths {
skipMatch, errSkip := matchPath(sp, f)
match := false
skip := false
for _, f := range files {
for _, p := range w.Paths {

if errSkip != nil {
return nil, errSkip
}
m, err := matchPath(p, f)
if err != nil {
return nil, err
}

if skipMatch {
skip = true
}
if m {
log.Printf("matched: %s\n", f)
match = true
break
}
}

for _, sp := range w.SkipPaths {

sm, err := matchPath(sp, f)
if err != nil {
return nil, err
}

if match && !skip {
steps = append(steps, w.Step)
if sm {
log.Printf("skipped: %s\n", f)
skip = true
break
}
}
}

if match && !skip {
log.Printf("adding step: %s\n", w.Step.Trigger)
steps = append(steps, w.Step)
}
}

if len(steps) == 0 && defaultStep != nil {
steps = append(steps, *defaultStep)
}

return dedupSteps(steps), nil
return steps, nil
}

// matchPath checks if the file f matches the path p.
Expand All @@ -165,25 +174,6 @@ func matchPath(p string, f string) (bool, error) {
return false, nil
}

func dedupSteps(steps []Step) []Step {
unique := []Step{}
for _, p := range steps {
duplicate := false
for _, t := range unique {
if reflect.DeepEqual(p, t) {
duplicate = true
break
}
}

if !duplicate {
unique = append(unique, p)
}
}

return unique
}

func generatePipeline(steps []Step, plugin Plugin) (*os.File, bool, error) {
tmp, err := os.CreateTemp(os.TempDir(), "bmrd-")
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func TestPipelinesStepsToTrigger(t *testing.T) {
{Trigger: "service-1"},
},
},
"step is included even when one of the files is skipped": {
"step is not included if one of the files is skipped": {
ChangedFiles: []string{
"docs/text.secret.txt",
"docs/text.txt",
Expand All @@ -356,7 +356,6 @@ func TestPipelinesStepsToTrigger(t *testing.T) {
}},
Expected: []Step{
{Trigger: "service-1"},
{Trigger: "service-2"},
},
},
"fails if not path is included": {
Expand Down