Skip to content

Commit ebf754e

Browse files
gcp-cherry-pick-bot[bot]prune998
andauthoredJan 15, 2025··
fix(appset): update gitlab SCM provider to search on parent folder (#16253) (#21491) (#21503)
* (fix:appset) update gitlab SCM provider to search on parent folder fix #16253 * adding test-case that replicated the new Gitlab API behaviour * add comments to the case --------- Signed-off-by: Prune <prune@lecentre.net> Co-authored-by: Prune Sebastien THOMAS <prune@lecentre.net>

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed
 

‎applicationset/services/scm_provider/gitlab.go

+19-26
Original file line numberDiff line numberDiff line change
@@ -131,37 +131,30 @@ func (g *GitlabProvider) RepoHasPath(_ context.Context, repo *Repository, path s
131131
if err != nil {
132132
return false, err
133133
}
134-
directories := []string{
135-
path,
136-
pathpkg.Dir(path),
134+
135+
options := gitlab.ListTreeOptions{
136+
Path: gitlab.Ptr(pathpkg.Dir(path)), // search parent folder
137+
Ref: &repo.Branch,
137138
}
138-
for _, directory := range directories {
139-
options := gitlab.ListTreeOptions{
140-
Path: &directory,
141-
Ref: &repo.Branch,
139+
for {
140+
treeNode, resp, err := g.client.Repositories.ListTree(p.ID, &options)
141+
if err != nil {
142+
return false, err
142143
}
143-
for {
144-
treeNode, resp, err := g.client.Repositories.ListTree(p.ID, &options)
145-
if err != nil {
146-
return false, err
147-
}
148-
if path == directory {
149-
if resp.TotalItems > 0 {
150-
return true, nil
151-
}
152-
}
153-
for i := range treeNode {
154-
if treeNode[i].Path == path {
155-
return true, nil
156-
}
157-
}
158-
if resp.NextPage == 0 {
159-
// no future pages
160-
break
144+
145+
// search for presence of the requested file in the parent folder
146+
for i := range treeNode {
147+
if treeNode[i].Path == path {
148+
return true, nil
161149
}
162-
options.Page = resp.NextPage
163150
}
151+
if resp.NextPage == 0 {
152+
// no future pages
153+
break
154+
}
155+
options.Page = resp.NextPage
164156
}
157+
165158
return false, nil
166159
}
167160

‎applicationset/services/scm_provider/gitlab_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,12 @@ func gitlabMockHandler(t *testing.T) func(http.ResponseWriter, *http.Request) {
10401040
if err != nil {
10411041
t.Fail()
10421042
}
1043+
// Recent versions of the Gitlab API (v17.7+) return 404 not only when a file doesn't exist, but also
1044+
// when a path is to a file instead of a directory. Our code should not hit this path, because
1045+
// we should only send requests for parent directories. But we leave this handler in place
1046+
// to prevent regressions.
1047+
case "/api/v4/projects/27084533/repository/tree?path=argocd/filepath.yaml&ref=master":
1048+
w.WriteHeader(http.StatusNotFound)
10431049
case "/api/v4/projects/27084533/repository/branches/foo":
10441050
w.WriteHeader(http.StatusNotFound)
10451051
default:
@@ -1194,6 +1200,11 @@ func TestGitlabHasPath(t *testing.T) {
11941200
path: "argocd/notathing.yaml",
11951201
exists: false,
11961202
},
1203+
{
1204+
name: "send a file path",
1205+
path: "argocd/filepath.yaml",
1206+
exists: false,
1207+
},
11971208
}
11981209

11991210
for _, c := range cases {

0 commit comments

Comments
 (0)
Please sign in to comment.