Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supplemental: update to get the correct version from URL #512

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this??

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is like .vscode/ for jetbrains ide

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)
}
}
Loading