Skip to content

Commit

Permalink
fix(kamelets): parse the url used to download kamelet
Browse files Browse the repository at this point in the history
Closes #5173
  • Loading branch information
squakez committed May 6, 2024
1 parent 6fee6d6 commit 6addee9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 11 additions & 2 deletions pkg/kamelet/repository/github_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"io"
"net/http"
neturl "net/url"
"os"
"sort"
"strings"
Expand Down Expand Up @@ -112,12 +113,20 @@ func (c *githubKameletRepository) listFiles(ctx context.Context) ([]*github.Repo
}

func (c *githubKameletRepository) downloadKamelet(ctx context.Context, url string) (*v1.Kamelet, error) {
return downloadGithubKamelet(ctx, url, c.httpClient)
}

func downloadGithubKamelet(ctx context.Context, url string, httpClient *http.Client) (*v1.Kamelet, error) {
parsedUrl, err := neturl.Parse(url)
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}

resp, err := c.httpClient.Do(req)
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
Expand All @@ -131,7 +140,7 @@ func (c *githubKameletRepository) downloadKamelet(ctx context.Context, url strin
if err != nil {
return nil, err
}
if strings.HasSuffix(url, ".yaml") || strings.HasSuffix(url, ".yml") {
if strings.HasSuffix(parsedUrl.Path, ".yaml") || strings.HasSuffix(parsedUrl.Path, ".yml") {
content, err = yaml.ToJSON(content)
if err != nil {
return nil, err
Expand Down
14 changes: 14 additions & 0 deletions pkg/kamelet/repository/github_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package repository

import (
"context"
"net/http"
"os"
"testing"

Expand Down Expand Up @@ -50,3 +51,16 @@ func TestGithubRepository(t *testing.T) {
}

}

func TestDownloadKamelet(t *testing.T) {
c := &http.Client{}
kamelet, err := downloadGithubKamelet(
context.Background(),
// the appended parameter test the strength of the func which should load the kamelet regardless any
// additional parameter provided
"https://raw.githubusercontent.com/apache/camel-kamelets/main/kamelets/timer-source.kamelet.yaml?test=test",
c,
)
require.NoError(t, err)
assert.Equal(t, "timer-source", kamelet.Name)
}

0 comments on commit 6addee9

Please sign in to comment.