11package gitlab
22
33import (
4+ "context"
5+ "fmt"
6+ "net/http"
7+ "net/http/httptest"
48 "testing"
59
10+ "github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
11+ gl "gitlab.com/gitlab-org/api/client-go"
612 "gotest.tools/v3/assert"
713)
814
@@ -16,6 +22,7 @@ func TestExtractGitLabInfo(t *testing.T) {
1622 name : "custom host" ,
1723 url : "https://gitlab.chmouel.com/group/subgroup/repo/-/blob/main/README.md?ref_type=heads" ,
1824 expected : & gitLabInfo {
25+ Scheme : "https" ,
1926 Host : "gitlab.chmouel.com" ,
2027 GroupOrUser : "group/subgroup" ,
2128 Repository : "repo" ,
@@ -27,6 +34,7 @@ func TestExtractGitLabInfo(t *testing.T) {
2734 name : "org repo" ,
2835 url : "https://gitlab.com/org/repo/-/blob/main/README.md" ,
2936 expected : & gitLabInfo {
37+ Scheme : "https" ,
3038 Host : "gitlab.com" ,
3139 GroupOrUser : "org" ,
3240 Repository : "repo" ,
@@ -38,6 +46,7 @@ func TestExtractGitLabInfo(t *testing.T) {
3846 name : "long group and subgroups" ,
3947 url : "https://gitlab.com/gitlab-com/partners/alliance/corp/sandbox/another/foo-foo/-/raw/main/hello.txt?ref_type=heads" ,
4048 expected : & gitLabInfo {
49+ Scheme : "https" ,
4150 Host : "gitlab.com" ,
4251 GroupOrUser : "gitlab-com/partners/alliance/corp/sandbox/another" ,
4352 Repository : "foo-foo" ,
@@ -55,3 +64,178 @@ func TestExtractGitLabInfo(t *testing.T) {
5564 })
5665 }
5766}
67+
68+ func TestGetTaskURI (t * testing.T ) {
69+ ctx := context .Background ()
70+ expectedContent := "apiVersion: tekton.dev/v1beta1\n kind: Pipeline\n metadata:\n name: test-pipeline\n spec:\n tasks:\n - name: echo-task\n taskSpec:\n steps:\n - name: echo\n image: ubuntu\n script: |\n echo \" Hello from remote pipeline!\" \n "
71+
72+ tests := []struct {
73+ name string
74+ setup func (t * testing.T ) (* httptest.Server , string , string )
75+ wantFound bool
76+ wantErr bool
77+ wantErrContains string
78+ wantContent string
79+ }{
80+ {
81+ name : "success" ,
82+ setup : func (t * testing.T ) (* httptest.Server , string , string ) {
83+ projectID := 12345
84+ projectSlug := "chmouel/dazgo"
85+ revision := "main"
86+ filePath := "task.yaml"
87+
88+ mux := http .NewServeMux ()
89+ mux .HandleFunc (fmt .Sprintf ("/api/v4/projects/%s" , gl .PathEscape (projectSlug )), func (w http.ResponseWriter , _ * http.Request ) {
90+ fmt .Fprintf (w , `{"id": %d, "path_with_namespace": "%s"}` , projectID , projectSlug )
91+ })
92+
93+ mux .HandleFunc (fmt .Sprintf ("/api/v4/projects/%d/repository/files/%s/raw" , projectID , gl .PathEscape (filePath )), func (w http.ResponseWriter , r * http.Request ) {
94+ assert .Equal (t , "token" , r .Header .Get ("Private-Token" ), "Expected Private-Token header to be 'token'" )
95+ assert .Equal (t , revision , r .URL .Query ().Get ("ref" ), "Expected 'ref' query parameter to be 'main'" )
96+ fmt .Fprint (w , expectedContent )
97+ })
98+
99+ server := httptest .NewServer (mux )
100+ remotePipelineURL := fmt .Sprintf ("%s/%s/-/raw/%s/%s" , server .URL , projectSlug , revision , filePath )
101+ return server , server .URL , remotePipelineURL
102+ },
103+ wantFound : true ,
104+ wantErr : false ,
105+ wantContent : expectedContent ,
106+ },
107+ {
108+ name : "file not found (404)" ,
109+ setup : func (_ * testing.T ) (* httptest.Server , string , string ) {
110+ mux := http .NewServeMux ()
111+ projectID := 12345
112+ projectSlug := "chmouel/dazgo"
113+ filePath := "nonexistent.yaml"
114+
115+ mux .HandleFunc (fmt .Sprintf ("/api/v4/projects/%s" , gl .PathEscape (projectSlug )), func (w http.ResponseWriter , _ * http.Request ) {
116+ fmt .Fprintf (w , `{"id": %d, "path_with_namespace": "%s"}` , projectID , projectSlug )
117+ })
118+
119+ mux .HandleFunc (fmt .Sprintf ("/api/v4/projects/%d/repository/files/%s/raw" , projectID , gl .PathEscape (filePath )), func (w http.ResponseWriter , _ * http.Request ) {
120+ w .WriteHeader (http .StatusNotFound )
121+ fmt .Fprint (w , `{"message": "404 File Not Found"}` )
122+ })
123+
124+ server := httptest .NewServer (mux )
125+ remotePipelineURL := fmt .Sprintf ("%s/%s/-/raw/main/%s" , server .URL , projectSlug , filePath )
126+ return server , server .URL , remotePipelineURL
127+ },
128+ wantFound : false ,
129+ wantErr : false ,
130+ },
131+ {
132+ name : "project not found (404)" ,
133+ setup : func (_ * testing.T ) (* httptest.Server , string , string ) {
134+ mux := http .NewServeMux ()
135+ projectSlug := "nonexistent/project"
136+
137+ mux .HandleFunc (fmt .Sprintf ("/api/v4/projects/%s" , gl .PathEscape (projectSlug )), func (w http.ResponseWriter , _ * http.Request ) {
138+ w .WriteHeader (http .StatusNotFound )
139+ fmt .Fprint (w , `{"message": "404 Project Not Found"}` )
140+ })
141+
142+ server := httptest .NewServer (mux )
143+ remotePipelineURL := fmt .Sprintf ("%s/%s/-/raw/main/task.yaml" , server .URL , projectSlug )
144+ return server , server .URL , remotePipelineURL
145+ },
146+ wantFound : false ,
147+ wantErr : false ,
148+ },
149+ {
150+ name : "invalid gitlab URL format" ,
151+ setup : func (_ * testing.T ) (* httptest.Server , string , string ) {
152+ return nil , "https://example.com" , "https://example.com/invalid/url/format"
153+ },
154+ wantFound : false ,
155+ wantErr : true ,
156+ wantErrContains : "URL does not match the expected GitLab pattern" ,
157+ },
158+ {
159+ name : "different host - should return not found" ,
160+ setup : func (_ * testing.T ) (* httptest.Server , string , string ) {
161+ return nil , "https://gitlab.com" , "https://different-host.com/chmouel/dazgo/-/raw/main/task.yaml"
162+ },
163+ wantFound : false ,
164+ wantErr : false ,
165+ },
166+ {
167+ name : "API error on GetProject" ,
168+ setup : func (_ * testing.T ) (* httptest.Server , string , string ) {
169+ mux := http .NewServeMux ()
170+ projectSlug := "chmouel/dazgo"
171+
172+ mux .HandleFunc (fmt .Sprintf ("/api/v4/projects/%s" , gl .PathEscape (projectSlug )), func (w http.ResponseWriter , _ * http.Request ) {
173+ w .WriteHeader (http .StatusInternalServerError )
174+ fmt .Fprint (w , `{"message": "Internal Server Error"}` )
175+ })
176+
177+ server := httptest .NewServer (mux )
178+ remotePipelineURL := fmt .Sprintf ("%s/%s/-/raw/main/task.yaml" , server .URL , projectSlug )
179+ return server , server .URL , remotePipelineURL
180+ },
181+ wantFound : false ,
182+ wantErr : true ,
183+ wantErrContains : "failed to get project ID" ,
184+ },
185+ {
186+ name : "API error on GetRawFile" ,
187+ setup : func (_ * testing.T ) (* httptest.Server , string , string ) {
188+ mux := http .NewServeMux ()
189+ projectID := 12345
190+ projectSlug := "chmouel/dazgo"
191+ filePath := "task.yaml"
192+
193+ mux .HandleFunc (fmt .Sprintf ("/api/v4/projects/%s" , gl .PathEscape (projectSlug )), func (w http.ResponseWriter , _ * http.Request ) {
194+ fmt .Fprintf (w , `{"id": %d, "path_with_namespace": "%s"}` , projectID , projectSlug )
195+ })
196+
197+ mux .HandleFunc (fmt .Sprintf ("/api/v4/projects/%d/repository/files/%s/raw" , projectID , gl .PathEscape (filePath )), func (w http.ResponseWriter , _ * http.Request ) {
198+ w .WriteHeader (http .StatusInternalServerError )
199+ fmt .Fprint (w , `{"message": "Internal Server Error"}` )
200+ })
201+
202+ server := httptest .NewServer (mux )
203+ remotePipelineURL := fmt .Sprintf ("%s/%s/-/raw/main/%s" , server .URL , projectSlug , filePath )
204+ return server , server .URL , remotePipelineURL
205+ },
206+ wantFound : false ,
207+ wantErr : true ,
208+ wantErrContains : "failed to get file" ,
209+ },
210+ }
211+
212+ for _ , tt := range tests {
213+ t .Run (tt .name , func (t * testing.T ) {
214+ server , eventURL , remotePipelineURL := tt .setup (t )
215+ if server != nil {
216+ defer server .Close ()
217+ }
218+
219+ v := & Provider {}
220+ event := info .NewEvent ()
221+ event .URL = eventURL
222+ event .Provider = & info.Provider {
223+ Token : "token" ,
224+ }
225+
226+ found , content , err := v .GetTaskURI (ctx , event , remotePipelineURL )
227+
228+ if tt .wantErr {
229+ assert .Assert (t , err != nil , "expected error but got nil" )
230+ if tt .wantErrContains != "" {
231+ assert .ErrorContains (t , err , tt .wantErrContains )
232+ }
233+ } else {
234+ assert .NilError (t , err )
235+ }
236+
237+ assert .Equal (t , tt .wantFound , found )
238+ assert .Equal (t , tt .wantContent , content )
239+ })
240+ }
241+ }
0 commit comments