Skip to content

Commit cb23572

Browse files
chetan-rnswtam2018
andauthored
fix: extract ref from GitOps URL query param (#19)
The backend should extract the ref query param from the GitOps URL and not from the request URL. Co-authored-by: William Tam <[email protected]>
1 parent ed63abe commit cb23572

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

pkg/httpapi/api.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (a *APIRouter) GetPipelines(w http.ResponseWriter, r *http.Request) {
6060

6161
// TODO: replace this with logr or sugar.
6262
log.Printf("urlToFetch = %#v\n", urlToFetch)
63-
repo, err := parseURL(urlToFetch)
63+
repo, parsedRepo, err := parseURL(urlToFetch)
6464
if err != nil {
6565
log.Printf("ERROR: failed to parse the URL: %s", err)
6666
http.Error(w, err.Error(), http.StatusBadRequest)
@@ -85,7 +85,7 @@ func (a *APIRouter) GetPipelines(w http.ResponseWriter, r *http.Request) {
8585
// Add a "not found" error that can be returned, otherwise it's a
8686
// StatusInternalServerError.
8787
log.Println("got an authenticated client")
88-
body, err := client.FileContents(r.Context(), repo, "pipelines.yaml", refFromQuery(r.URL.Query()))
88+
body, err := client.FileContents(r.Context(), repo, "pipelines.yaml", refFromQuery(parsedRepo.Query()))
8989
if err != nil {
9090
log.Printf("ERROR: failed to get file contents for repo %#v: %s", repo, err)
9191
http.Error(w, err.Error(), http.StatusBadRequest)
@@ -114,7 +114,7 @@ func (a *APIRouter) GetApplication(w http.ResponseWriter, r *http.Request) {
114114

115115
// TODO: replace this with logr or sugar.
116116
log.Printf("urlToFetch = %#v\n", urlToFetch)
117-
repo, err := parseURL(urlToFetch)
117+
repo, parsedRepo, err := parseURL(urlToFetch)
118118
if err != nil {
119119
log.Printf("ERROR: failed to parse the URL: %s", err)
120120
http.Error(w, err.Error(), http.StatusBadRequest)
@@ -138,7 +138,7 @@ func (a *APIRouter) GetApplication(w http.ResponseWriter, r *http.Request) {
138138
//
139139
// Add a "not found" error that can be returned, otherwise it's a
140140
// StatusInternalServerError.
141-
body, err := client.FileContents(r.Context(), repo, "pipelines.yaml", refFromQuery(r.URL.Query()))
141+
body, err := client.FileContents(r.Context(), repo, "pipelines.yaml", refFromQuery(parsedRepo.Query()))
142142
if err != nil {
143143
log.Printf("ERROR: failed to get file contents for repo %#v: %s", repo, err)
144144
http.Error(w, err.Error(), http.StatusBadRequest)
@@ -179,12 +179,12 @@ func (a *APIRouter) getAuthenticatedGitClient(fetchURL, token string) (git.SCM,
179179
return a.gitClientFactory.Create(fetchURL, token)
180180
}
181181

182-
func parseURL(s string) (string, error) {
182+
func parseURL(s string) (string, *url.URL, error) {
183183
parsed, err := url.Parse(s)
184184
if err != nil {
185-
return "", fmt.Errorf("failed to parse %#v: %w", s, err)
185+
return "", nil, fmt.Errorf("failed to parse %#v: %w", s, err)
186186
}
187-
return strings.TrimLeft(strings.TrimSuffix(parsed.Path, ".git"), "/"), nil
187+
return strings.TrimLeft(strings.TrimSuffix(parsed.Path, ".git"), "/"), parsed, nil
188188
}
189189

190190
func secretRefFromQuery(v url.Values) (types.NamespacedName, bool) {

pkg/httpapi/api_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ func TestGetPipelines(t *testing.T) {
5050
func TestGetPipelinesWithASpecificRef(t *testing.T) {
5151
ts, c := makeServer(t)
5252
c.addContents("example/gitops", "pipelines.yaml", testRef, "testdata/pipelines.yaml")
53-
pipelinesURL := "https://github.com/example/gitops.git"
53+
pipelinesURL := fmt.Sprintf("https://github.com/example/gitops.git?ref=%s", testRef)
5454

55-
req := makeClientRequest(t, "Bearer testing", fmt.Sprintf("%s/pipelines?url=%s&ref=%s", ts.URL, pipelinesURL, testRef))
55+
req := makeClientRequest(t, "Bearer testing", fmt.Sprintf("%s/pipelines?url=%s", ts.URL, pipelinesURL))
5656
res, err := ts.Client().Do(req)
5757
if err != nil {
5858
t.Fatal(err)
@@ -225,10 +225,9 @@ func TestGetPipelineApplicationWithRef(t *testing.T) {
225225
a.resourceParser = stubResourceParser(testResource)
226226
})
227227
c.addContents("example/gitops", "pipelines.yaml", testRef, "testdata/pipelines.yaml")
228-
pipelinesURL := "https://github.com/example/gitops.git"
228+
pipelinesURL := fmt.Sprintf("https://github.com/example/gitops.git?ref=%s", testRef)
229229
options := url.Values{
230230
"url": []string{pipelinesURL},
231-
"ref": []string{testRef},
232231
}
233232
req := makeClientRequest(t, "Bearer testing",
234233
fmt.Sprintf("%s/environments/%s/application/%s?%s", ts.URL, "dev", "taxi", options.Encode()))
@@ -273,11 +272,18 @@ func TestParseURL(t *testing.T) {
273272
}
274273

275274
for _, tt := range urlTests {
276-
repo, err := parseURL(tt.u)
275+
repo, got, err := parseURL(tt.u)
277276
if !test.MatchError(t, tt.wantErr, err) {
278277
t.Errorf("got an unexpected error: %v", err)
279278
continue
280279
}
280+
if err == nil {
281+
want, err := url.Parse(tt.u)
282+
assertNoError(t, err)
283+
if got.String() != want.String() {
284+
t.Errorf("Parsed URL mismatch: got %v, want %v", got.String(), want.String())
285+
}
286+
}
281287
if repo != tt.wantRepo {
282288
t.Errorf("repo got %s, want %s", repo, tt.wantRepo)
283289
}

0 commit comments

Comments
 (0)