Skip to content

Commit

Permalink
feat(finder): using GithubEvent.TotalCommits for git diff
Browse files Browse the repository at this point in the history
  • Loading branch information
anarcher committed Mar 29, 2020
1 parent 0813def commit ed2c7ae
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
13 changes: 4 additions & 9 deletions pkg/finder/gitchanged.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type GitChanged struct {
}

const (
gitDiffTreeFmt = "diff-tree --no-commit-id --name-only -r %s %s"
gitDiffTreeFmt = "diff-tree --no-commit-id --name-only -r HEAD HEAD~%d"
kustomizeFileName = "kustomization.yaml"
)

Expand Down Expand Up @@ -50,9 +50,8 @@ func (f *GitChanged) PathFind() ([]string, error) {
}

func (f *GitChanged) gitDiffTree() ([]string, error) {
cmt := f.cfg.GitHub.Commit
cmtBefore := f.cfg.Inputs.CommitBefore
args := fmt.Sprintf(gitDiffTreeFmt, cmt, cmtBefore)
commits := f.cfg.GithubEvent.TotalCommits()
args := fmt.Sprintf(gitDiffTreeFmt, commits)

out, err := f.cmd.Run("git", strings.Split(args, " ")...)
if err != nil {
Expand Down Expand Up @@ -102,16 +101,12 @@ func (f *GitChanged) matchPaths(paths []string) ([]string, error) {

for _, p := range paths {
for _, pat := range patterns {
matched, err := filepath.Match(pat, p)
if err != nil {
return nil, err
}
matched := strings.HasPrefix(p, pat)
if matched {
ps = append(ps, p)
break
}
}
}

return ps, nil
}
36 changes: 36 additions & 0 deletions pkg/finder/gitchanged_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package finder

import (
"reflect"
"testing"

"github.com/anarcher/kustomize-check-action/pkg/command"
"github.com/anarcher/kustomize-check-action/pkg/config"
)

func TestMatchPaths(t *testing.T) {
cmd := command.NewOSExec()

cfg, err := config.LoadConfigFromEnv()
if err != nil {
t.Fatal(err)
}

f, err := NewGitChanged("", cfg, cmd)
if err != nil {
t.Fatal(err)
}

paths := []string{"test/base/t2"}
cfg.Inputs.Paths = []string{"test/"}

ps, err := f.matchPaths(paths)
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(paths, ps) {
t.Logf("paths=%v,ps=%v", paths, ps)
t.Error("paths != ps")
}
}

0 comments on commit ed2c7ae

Please sign in to comment.