Skip to content

Commit

Permalink
Fix regression in changed chart detection (#85)
Browse files Browse the repository at this point in the history
* Fix regression in changed chart detection

 Please enter the commit message for your changes. Lines starting

Signed-off-by: Reinhard Nägele <[email protected]>

* Refactor to consider charts at root

Signed-off-by: Reinhard Nägele <[email protected]>

* Make sure charts are processed only once

Signed-off-by: Reinhard Nägele <[email protected]>

* Fix typo

Signed-off-by: Reinhard Nägele <[email protected]>
  • Loading branch information
unguiculus authored and scottrigby committed Jan 20, 2019
1 parent fa29453 commit b679b85
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
19 changes: 13 additions & 6 deletions pkg/chart/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ type DirectoryLister interface {

// ChartUtils is the interface that wraps chart-related methods
//
// IsChartdir checks if a directory is a chart directory
// LookupChartDir looks up the chart's root directory based on some chart file that has changed
//
// ReadChartYaml reads the `Chart.yaml` from the specified directory
type ChartUtils interface {
IsChartDir(dir string) bool
LookupChartDir(chartDirs []string, dir string) (string, error)
ReadChartYaml(dir string) (*util.ChartYaml, error)
}

Expand Down Expand Up @@ -433,9 +433,15 @@ func (t *Testing) ComputeChangedChartDirectories() ([]string, error) {
continue
}
dir := filepath.Dir(file)
// Only add if not already in list and double-check if it is a chart directory
if !util.StringSliceContains(changedChartDirs, dir) && t.chartUtils.IsChartDir(dir) {
changedChartDirs = append(changedChartDirs, dir)
// Make sure directory is really a chart directory
chartDir, err := t.chartUtils.LookupChartDir(cfg.ChartDirs, dir)
if err == nil {
// Only add it if not already in the list
if !util.StringSliceContains(changedChartDirs, chartDir) {
changedChartDirs = append(changedChartDirs, chartDir)
}
} else {
fmt.Printf("Directory '%s' is no chart directory. Skipping...", chartDir)
}
}

Expand All @@ -451,7 +457,8 @@ func (t *Testing) ReadAllChartDirectories() ([]string, error) {
for _, chartParentDir := range cfg.ChartDirs {
dirs, err := t.directoryLister.ListChildDirs(chartParentDir,
func(dir string) bool {
return t.chartUtils.IsChartDir(dir) && !util.StringSliceContains(cfg.ExcludedCharts, path.Base(dir))
_, err := t.chartUtils.LookupChartDir(chartDirs, dir)
return err == nil && !util.StringSliceContains(cfg.ExcludedCharts, path.Base(dir))
})
if err != nil {
return nil, errors.Wrap(err, "Error reading chart directories")
Expand Down
9 changes: 6 additions & 3 deletions pkg/chart/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (g fakeGit) ListChangedFilesInDirs(commit string, dirs ...string) ([]string
"incubator/excluded/values.yaml",
"stable/blah/Chart.yaml",
"stable/blah/README.md",
"stable/this-is-no-chart-dir/foo.md",
"foo/this-is-no-chart-dir/foo.md",
}, nil
}

Expand All @@ -76,8 +76,11 @@ func (l fakeDirLister) ListChildDirs(parentDir string, test func(dir string) boo

type fakeChartUtils struct{}

func (v fakeChartUtils) IsChartDir(dir string) bool {
return dir != "stable/this-is-no-chart-dir"
func (v fakeChartUtils) LookupChartDir(chartDirs []string, dir string) (string, error) {
if strings.HasPrefix(dir, "foo") {
return "", errors.New("no chart dir")
}
return dir, nil
}

func (v fakeChartUtils) ReadChartYaml(dir string) (*util.ChartYaml, error) {
Expand Down
19 changes: 17 additions & 2 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"math/rand"
"os"
"path"
"path/filepath"
"strings"
"time"
)
Expand Down Expand Up @@ -121,8 +122,22 @@ func (l DirectoryLister) ListChildDirs(parentDir string, test func(dir string) b

type ChartUtils struct{}

func (u ChartUtils) IsChartDir(dir string) bool {
return FileExists(path.Join(dir, "Chart.yaml"))
func (u ChartUtils) LookupChartDir(chartDirs []string, dir string) (string, error) {
for _, chartDir := range chartDirs {
currentDir := dir
for {
if FileExists(path.Join(currentDir, "Chart.yaml")) {
return currentDir, nil
}
currentDir = filepath.Dir(currentDir)
relativeDir, _ := filepath.Rel(chartDir, currentDir)
joined := filepath.Join(chartDir, relativeDir)
if joined == chartDir {
break
}
}
}
return "", errors.New("no chart directory")
}

func (u ChartUtils) ReadChartYaml(dir string) (*ChartYaml, error) {
Expand Down

0 comments on commit b679b85

Please sign in to comment.