Skip to content

Commit a6ddad4

Browse files
zakisksavitaashture
authored andcommitted
Fix: PLR Definition Issue When Provenance is Set in GitLab
Issue: In GitLab, when provenance is set to default_branch, the API call to get .tekton directory tree is correct and fetching the tree as per provenance but when each is file is fetched as raw its using `source` as provenance no matter what value has been set in Repository CR. Solution: This pull request use revision correctly in concatAllYamlFiles func to make it obey provenance settings. Signed-off-by: Zaki Shaikh <[email protected]>
1 parent ab853a5 commit a6ddad4

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

pkg/provider/gitlab/acl_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestIsAllowed(t *testing.T) {
108108
thelp.MuxDisallowUserID(mux, tt.fields.targetProjectID, tt.allowMemberID)
109109
}
110110
if tt.ownerFile != "" {
111-
thelp.MuxGetFile(mux, tt.fields.targetProjectID, "OWNERS", tt.ownerFile)
111+
thelp.MuxGetFile(mux, tt.fields.targetProjectID, "OWNERS", tt.ownerFile, false)
112112
}
113113
if tt.commentContent != "" {
114114
thelp.MuxDiscussionsNote(mux, tt.fields.targetProjectID,

pkg/provider/gitlab/gitlab.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,16 @@ func (v *Provider) GetTektonDir(_ context.Context, event *info.Event, path, prov
295295
}
296296
}
297297

298-
return v.concatAllYamlFiles(nodes, event)
298+
return v.concatAllYamlFiles(nodes, revision)
299299
}
300300

301301
// concatAllYamlFiles concat all yaml files from a directory as one big multi document yaml string.
302-
func (v *Provider) concatAllYamlFiles(objects []*gitlab.TreeNode, runevent *info.Event) (string, error) {
302+
func (v *Provider) concatAllYamlFiles(objects []*gitlab.TreeNode, revision string) (string, error) {
303303
var allTemplates string
304304
for _, value := range objects {
305305
if strings.HasSuffix(value.Name, ".yaml") ||
306306
strings.HasSuffix(value.Name, ".yml") {
307-
data, _, err := v.getObject(value.Path, runevent.HeadBranch, v.sourceProjectID)
307+
data, _, err := v.getObject(value.Path, revision, v.sourceProjectID)
308308
if err != nil {
309309
return "", err
310310
}

pkg/provider/gitlab/gitlab_test.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,8 @@ func TestGetTektonDir(t *testing.T) {
399399
args args
400400
wantStr string
401401
wantErr string
402+
wantTreeAPIErr bool
403+
wantFilesAPIErr bool
402404
wantClient bool
403405
prcontent string
404406
filterMessageSnippet string
@@ -472,6 +474,38 @@ func TestGetTektonDir(t *testing.T) {
472474
wantClient: true,
473475
wantStr: "kind: PipelineRun",
474476
},
477+
{
478+
name: "list tekton dir tree api call error",
479+
prcontent: strings.TrimPrefix(string(samplePR), "---"),
480+
args: args{
481+
path: ".tekton",
482+
event: &info.Event{
483+
HeadBranch: "main",
484+
},
485+
},
486+
fields: fields{
487+
sourceProjectID: 100,
488+
},
489+
wantClient: true,
490+
wantTreeAPIErr: true,
491+
wantErr: "failed to list .tekton dir",
492+
},
493+
{
494+
name: "get file raw api call error",
495+
prcontent: strings.TrimPrefix(string(samplePR), "---"),
496+
args: args{
497+
path: ".tekton",
498+
event: &info.Event{
499+
HeadBranch: "main",
500+
},
501+
},
502+
fields: fields{
503+
sourceProjectID: 100,
504+
},
505+
wantClient: true,
506+
wantFilesAPIErr: true,
507+
wantErr: "failed to get filename from api pr.yaml dir",
508+
},
475509
}
476510
for _, tt := range tests {
477511
t.Run(tt.name, func(t *testing.T) {
@@ -493,7 +527,7 @@ func TestGetTektonDir(t *testing.T) {
493527
muxbranch = tt.args.event.DefaultBranch
494528
}
495529
if tt.args.path != "" && tt.prcontent != "" {
496-
thelp.MuxListTektonDir(t, mux, tt.fields.sourceProjectID, muxbranch, tt.prcontent)
530+
thelp.MuxListTektonDir(t, mux, tt.fields.sourceProjectID, muxbranch, tt.prcontent, tt.wantTreeAPIErr, tt.wantFilesAPIErr)
497531
}
498532
defer tearDown()
499533
}
@@ -528,7 +562,7 @@ func TestGetFileInsideRepo(t *testing.T) {
528562
sourceProjectID: 10,
529563
Client: client,
530564
}
531-
thelp.MuxListTektonDir(t, mux, v.sourceProjectID, event.HeadBranch, content)
565+
thelp.MuxListTektonDir(t, mux, v.sourceProjectID, event.HeadBranch, content, false, false)
532566
got, err := v.GetFileInsideRepo(ctx, event, "pr.yaml", "")
533567
assert.NilError(t, err)
534568
assert.Equal(t, content, got)

pkg/provider/gitlab/test/test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,12 @@ func MuxDisallowUserID(mux *http.ServeMux, projectID, userID int) {
6666
})
6767
}
6868

69-
func MuxListTektonDir(_ *testing.T, mux *http.ServeMux, pid int, ref, prs string) {
69+
func MuxListTektonDir(_ *testing.T, mux *http.ServeMux, pid int, ref, prs string, wantTreeAPIErr, wantFilesAPIErr bool) {
7070
mux.HandleFunc(fmt.Sprintf("/projects/%d/repository/tree", pid), func(rw http.ResponseWriter, r *http.Request) {
71+
if wantTreeAPIErr {
72+
rw.WriteHeader(http.StatusUnauthorized)
73+
return
74+
}
7175
if r.URL.Query().Get("pagination") == "keyset" {
7276
if r.URL.Query().Get("ref") == ref {
7377
if r.URL.Query().Get("page_token") != "page2" {
@@ -87,8 +91,8 @@ func MuxListTektonDir(_ *testing.T, mux *http.ServeMux, pid int, ref, prs string
8791
}
8892
})
8993

90-
MuxGetFile(mux, pid, "pr.yaml", prs)
91-
MuxGetFile(mux, pid, "random.yaml", `foo:bar`)
94+
MuxGetFile(mux, pid, "pr.yaml", prs, wantFilesAPIErr)
95+
MuxGetFile(mux, pid, "random.yaml", `foo:bar`, wantTreeAPIErr)
9296
}
9397

9498
func MuxDiscussionsNoteEmpty(mux *http.ServeMux, pid, mrID int) {
@@ -120,8 +124,12 @@ func MuxDiscussionsNote(mux *http.ServeMux, pid, mrID int, author string, author
120124
})
121125
}
122126

123-
func MuxGetFile(mux *http.ServeMux, pid int, fname, content string) {
127+
func MuxGetFile(mux *http.ServeMux, pid int, fname, content string, wantErr bool) {
124128
mux.HandleFunc(fmt.Sprintf("/projects/%d/repository/files/%s/raw", pid, fname), func(rw http.ResponseWriter, _ *http.Request) {
129+
if wantErr {
130+
rw.WriteHeader(http.StatusUnauthorized)
131+
return
132+
}
125133
fmt.Fprint(rw, content)
126134
})
127135
}

0 commit comments

Comments
 (0)