Skip to content

Commit

Permalink
Supplemental: update to get the correct version from URL
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin222004 <[email protected]>
  • Loading branch information
Kevin222004 committed Jul 12, 2024
1 parent f3af398 commit 0995d15
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ cmd/errorutil/errorutil
**errorutil_errors_export.json
*.DS_Store
coverage.txt
.idea
21 changes: 21 additions & 0 deletions generators/github/git_repo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package github

import (
"net/url"
"testing"
)

func TestGetVersion(t *testing.T) {
gitProtoString := "git://github.com/kubernetes-sigs/metrics-server/master/charts"
parsedUrl, _ := url.Parse(gitProtoString)
testData := NewDownloaderForScheme("git", parsedUrl, "metrics-server")
output, _ := testData.GetContent()

if output == nil {
t.Fatalf("Expected non-nil data, got nil")
}

if version := output.GetVersion(); version != "metrics-server-helm-chart-3.12.1" {
t.Errorf("Expected version metrics-server-helm-chart-3.12.1, got %s", version)
}
}
23 changes: 21 additions & 2 deletions generators/github/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package github

import (
"bufio"
"fmt"
"io"

"net/url"
Expand All @@ -19,15 +20,21 @@ type URL struct {
PackageName string
}

// < http/https://url/version>
// < http/https://url/ >
//close the descriptors

func (u URL) GetContent() (models.Package, error) {
downloadDirPath := filepath.Join(os.TempDir(), utils.GetRandomAlphabetsOfDigit(5))
_ = os.MkdirAll(downloadDirPath, 0755)

url := u.URL.String()
version := url[strings.LastIndex(url, "/")+1:]
owner, repo, errr := extractDetail(url)
versions, errr := utils.GetLatestReleaseTagsSorted(owner, repo)
if errr != nil {
return nil, ErrInvalidGitHubSourceURL(errr)
}
version := versions[len(versions)-1]

url, _ = strings.CutSuffix(url, "/"+version)

fileName := utils.GetRandomAlphabetsOfDigit(6)
Expand Down Expand Up @@ -61,6 +68,18 @@ func (u URL) GetContent() (models.Package, error) {
}, nil
}

func extractDetail(url string) (owner string, repo string, err error) {
parts := strings.SplitN(strings.TrimPrefix(url, "/"), "/", 5)
size := len(parts)
if size > 4 {
owner = parts[3]
repo = parts[4]
} else {
err = ErrInvalidGitHubSourceURL(fmt.Errorf("Source URL %s is invalid", url))
}
return
}

func ProcessContent(w io.Writer, downloadDirPath, downloadfilePath string) error {
var err error
if utils.IsTarGz(downloadfilePath) {
Expand Down
21 changes: 21 additions & 0 deletions generators/github/url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package github

import (
"net/url"
"testing"
)

func TestURL_GetVersion(t *testing.T) {
UrlString := "https://github.com/kubernetes-sigs/metrics-server"
parsedUrl, _ := url.Parse(UrlString)
testData := NewDownloaderForScheme("https", parsedUrl, "metrics-server")
output, _ := testData.GetContent()

if output == nil {
t.Fatalf("Expected non-nil data, got nil")
}

if version := output.GetVersion(); version != "metrics-server-helm-chart-3.12.1" {
t.Errorf("Expected version metrics-server-helm-chart-3.12.1, got %s", version)
}
}

0 comments on commit 0995d15

Please sign in to comment.