Skip to content

Commit

Permalink
fix: Gitlab Generator PathExists for files (#8997)
Browse files Browse the repository at this point in the history
This is a fix for argoproj/applicationset#386

Signed-off-by: David Schneider <david.schneider@chargepoint.com>

Co-authored-by: David Schneider <david.schneider@chargepoint.com>
  • Loading branch information
dsch and davidschneider-cpi committed Apr 5, 2022
1 parent e53a3f8 commit af16a6f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 15 deletions.
40 changes: 31 additions & 9 deletions applicationset/services/scm_provider/gitlab.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
pathpkg "path"

gitlab "github.com/xanzy/go-gitlab"
)
Expand Down Expand Up @@ -105,17 +106,38 @@ func (g *GitlabProvider) RepoHasPath(_ context.Context, repo *Repository, path s
if err != nil {
return false, err
}
_, resp, err := g.client.Repositories.ListTree(p.ID, &gitlab.ListTreeOptions{
Path: &path,
Ref: &repo.Branch,
})
if err != nil {
return false, err
directories := []string{
path,
pathpkg.Dir(path),
}
if resp.TotalItems == 0 {
return false, nil
for _, directory := range directories {
options := gitlab.ListTreeOptions{
Path: &directory,
Ref: &repo.Branch,
}
for {
treeNode, resp, err := g.client.Repositories.ListTree(p.ID, &options)
if err != nil {
return false, err
}
if path == directory {
if resp.TotalItems > 0 {
return true, nil
}
}
for i := range treeNode {
if treeNode[i].Path == path {
return true, nil
}
}
if resp.NextPage == 0 {
// no future pages
break
}
options.Page = resp.NextPage
}
}
return true, nil
return false, nil
}

func (g *GitlabProvider) listBranches(_ context.Context, repo *Repository) ([]gitlab.Branch, error) {
Expand Down
39 changes: 33 additions & 6 deletions applicationset/services/scm_provider/gitlab_test.go
Expand Up @@ -79,11 +79,38 @@ func TestGitlabHasPath(t *testing.T) {
Repository: "argocd",
Branch: "master",
}
ok, err := host.RepoHasPath(context.Background(), repo, "argocd")
assert.Nil(t, err)
assert.True(t, ok)

ok, err = host.RepoHasPath(context.Background(), repo, "notathing")
assert.Nil(t, err)
assert.False(t, ok)
cases := []struct {
name, path string
exists bool
}{
{
name: "directory exists",
path: "argocd",
exists: true,
},
{
name: "file exists",
path: "argocd/install.yaml",
exists: true,
},
{
name: "directory does not exist",
path: "notathing",
exists: false,
},
{
name: "file does not exist",
path: "argocd/notathing.yaml",
exists: false,
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
ok, err := host.RepoHasPath(context.Background(), repo, c.path)
assert.Nil(t, err)
assert.Equal(t, c.exists, ok)
})
}
}

0 comments on commit af16a6f

Please sign in to comment.