Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Hoang <mhoang@redhat.com>
  • Loading branch information
mike-hoang committed Mar 24, 2023
1 parent 046c37b commit af566e9
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 54 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ The function documentation can be accessed via [pkg.go.dev](https://pkg.go.dev/g
url = "https://gitlab.com/<owner>/<repo name>"

// Parse the repo url
gitUrl, err := util.ParseGitUrl(url)
gitUrl, err := util.NewGitUrl(url)

// Clone the repo to a destination dir
err = util.CloneGitRepo(gitUrl, destDir)
err = util.CloneGitRepo(*gitUrl, destDir)
```

## Projects using devfile/library
Expand Down
6 changes: 3 additions & 3 deletions pkg/devfile/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ func parseFromURI(importReference v1.ImportReference, curDevfileCtx devfileCtx.D

d.Ctx = devfileCtx.NewURLDevfileCtx(newUri)
if util.IsGitProviderRepo(newUri) {
gitUrl, err := util.ParseGitUrl(newUri)
gitUrl, err := util.NewGitUrl(newUri)
if err != nil {
return DevfileObj{}, err
}
Expand All @@ -450,7 +450,7 @@ func parseFromURI(importReference v1.ImportReference, curDevfileCtx devfileCtx.D
return populateAndParseDevfile(d, newResolveCtx, tool, true)
}

func getResourcesFromGit(g util.GitUrl, destDir string, httpTimeout *int, repoToken string) error {
func getResourcesFromGit(g *util.GitUrl, destDir string, httpTimeout *int, repoToken string) error {
stackDir, err := ioutil.TempDir(os.TempDir(), fmt.Sprintf("git-resources"))
if err != nil {
return fmt.Errorf("failed to create dir: %s, error: %v", stackDir, err)
Expand All @@ -464,7 +464,7 @@ func getResourcesFromGit(g util.GitUrl, destDir string, httpTimeout *int, repoTo
}
}

err = util.CloneGitRepo(g, stackDir)
err = util.CloneGitRepo(*g, stackDir)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/devfile/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4207,7 +4207,7 @@ func Test_getResourcesFromGit(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := getResourcesFromGit(tt.gitUrl, tt.destDir, &httpTimeout, "")
err := getResourcesFromGit(&tt.gitUrl, tt.destDir, &httpTimeout, "")
if (err != nil) != tt.wantErr {
t.Errorf("Expected error: %t, got error: %t", tt.wantErr, err)
}
Expand Down
44 changes: 37 additions & 7 deletions pkg/util/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ const (
BitbucketHost string = "bitbucket.org"
)

type IGitUrl interface {
ParseGitUrl(fullUrl string) error
GetGitRawFileAPI() string
SetToken(token string, httpTimeout *int) error
IsPublic(httpTimeout *int) bool
}

type GitUrl struct {
Protocol string // URL scheme
Host string // URL domain name
Expand All @@ -42,23 +49,30 @@ type GitUrl struct {
IsFile bool // defines if the URL points to a file in the repo
}

// NewGitUrl creates a GitUrl from a string url
func NewGitUrl(url string) (*GitUrl, error) {
g := &GitUrl{}
if err := g.ParseGitUrl(url); err != nil {
return g, err
}
return g, nil
}

// ParseGitUrl extracts information from a support git url
// Only supports git repositories hosted on GitHub, GitLab, and Bitbucket
func ParseGitUrl(fullUrl string) (GitUrl, error) {
var g GitUrl

func (g *GitUrl) ParseGitUrl(fullUrl string) error {
err := ValidateURL(fullUrl)
if err != nil {
return g, err
return err
}

parsedUrl, err := url.Parse(fullUrl)
if err != nil {
return g, err
return err
}

if len(parsedUrl.Path) == 0 {
return g, fmt.Errorf("url path should not be empty")
return fmt.Errorf("url path should not be empty")
}

if parsedUrl.Host == RawGitHubHost || parsedUrl.Host == GitHubHost {
Expand All @@ -71,7 +85,7 @@ func ParseGitUrl(fullUrl string) (GitUrl, error) {
err = fmt.Errorf("url host should be a valid GitHub, GitLab, or Bitbucket host; received: %s", parsedUrl.Host)
}

return g, err
return err
}

func (g *GitUrl) parseGitHubUrl(url *url.URL) error {
Expand Down Expand Up @@ -242,6 +256,22 @@ func (g *GitUrl) validateToken(params HTTPRequestParams) error {
return nil
}

// GetGitRawFileAPI returns the endpoint for the git providers raw file
func (g *GitUrl) GetGitRawFileAPI() string {
var apiRawFile string

switch g.Host {
case GitHubHost, RawGitHubHost:
apiRawFile = fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/%s/%s", g.Owner, g.Repo, g.Branch, g.Path)
case GitLabHost:
apiRawFile = fmt.Sprintf("https://gitlab.com/api/v4/projects/%s%%2F%s/repository/files/%s/raw", g.Owner, g.Repo, g.Path)
case BitbucketHost:
apiRawFile = fmt.Sprintf("https://api.bitbucket.org/2.0/repositories/%s/%s/src/%s/%s", g.Owner, g.Repo, g.Branch, g.Path)
}

return apiRawFile
}

// IsGitProviderRepo checks if the url matches a repo from a supported git provider
func IsGitProviderRepo(url string) bool {
if strings.Contains(url, RawGitHubHost) || strings.Contains(url, GitHubHost) ||
Expand Down
106 changes: 73 additions & 33 deletions pkg/util/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ var (
bitbucketToken = "fake-bitbucket-token"
)

func Test_ParseGitUrl(t *testing.T) {
func Test_NewGitUrl(t *testing.T) {
tests := []struct {
name string
url string
wantUrl GitUrl
wantUrl *GitUrl
wantErr string
}{
{
Expand All @@ -51,7 +51,7 @@ func Test_ParseGitUrl(t *testing.T) {
{
name: "should parse public GitHub repo with root path",
url: "https://github.com/devfile/library",
wantUrl: GitUrl{
wantUrl: &GitUrl{
Protocol: "https",
Host: "github.com",
Owner: "devfile",
Expand All @@ -70,7 +70,7 @@ func Test_ParseGitUrl(t *testing.T) {
{
name: "should parse public GitHub repo with file path",
url: "https://github.com/devfile/library/blob/main/devfile.yaml",
wantUrl: GitUrl{
wantUrl: &GitUrl{
Protocol: "https",
Host: "github.com",
Owner: "devfile",
Expand All @@ -84,7 +84,7 @@ func Test_ParseGitUrl(t *testing.T) {
{
name: "should parse public GitHub repo with raw file path",
url: "https://raw.githubusercontent.com/devfile/library/main/devfile.yaml",
wantUrl: GitUrl{
wantUrl: &GitUrl{
Protocol: "https",
Host: "raw.githubusercontent.com",
Owner: "devfile",
Expand All @@ -108,7 +108,7 @@ func Test_ParseGitUrl(t *testing.T) {
{
name: "should parse private GitHub repo with token",
url: "https://github.com/fake-owner/fake-private-repo",
wantUrl: GitUrl{
wantUrl: &GitUrl{
Protocol: "https",
Host: "github.com",
Owner: "fake-owner",
Expand All @@ -122,7 +122,7 @@ func Test_ParseGitUrl(t *testing.T) {
{
name: "should parse private raw GitHub file path with token",
url: "https://raw.githubusercontent.com/fake-owner/fake-private-repo/main/README.md",
wantUrl: GitUrl{
wantUrl: &GitUrl{
Protocol: "https",
Host: "raw.githubusercontent.com",
Owner: "fake-owner",
Expand All @@ -137,7 +137,7 @@ func Test_ParseGitUrl(t *testing.T) {
{
name: "should parse public GitLab repo with root path",
url: "https://gitlab.com/gitlab-org/gitlab-foss",
wantUrl: GitUrl{
wantUrl: &GitUrl{
Protocol: "https",
Host: "gitlab.com",
Owner: "gitlab-org",
Expand All @@ -156,7 +156,7 @@ func Test_ParseGitUrl(t *testing.T) {
{
name: "should parse public GitLab repo with file path",
url: "https://gitlab.com/gitlab-org/gitlab-foss/-/blob/master/README.md",
wantUrl: GitUrl{
wantUrl: &GitUrl{
Protocol: "https",
Host: "gitlab.com",
Owner: "gitlab-org",
Expand All @@ -180,7 +180,7 @@ func Test_ParseGitUrl(t *testing.T) {
{
name: "should parse private GitLab repo with token",
url: "https://gitlab.com/fake-owner/fake-private-repo",
wantUrl: GitUrl{
wantUrl: &GitUrl{
Protocol: "https",
Host: "gitlab.com",
Owner: "fake-owner",
Expand All @@ -194,7 +194,7 @@ func Test_ParseGitUrl(t *testing.T) {
{
name: "should parse private raw GitLab file path with token",
url: "https://gitlab.com/fake-owner/fake-private-repo/-/raw/main/README.md",
wantUrl: GitUrl{
wantUrl: &GitUrl{
Protocol: "https",
Host: "gitlab.com",
Owner: "fake-owner",
Expand All @@ -209,7 +209,7 @@ func Test_ParseGitUrl(t *testing.T) {
{
name: "should parse public Bitbucket repo with root path",
url: "https://bitbucket.org/fake-owner/fake-public-repo",
wantUrl: GitUrl{
wantUrl: &GitUrl{
Protocol: "https",
Host: "bitbucket.org",
Owner: "fake-owner",
Expand All @@ -228,7 +228,7 @@ func Test_ParseGitUrl(t *testing.T) {
{
name: "should parse public Bitbucket repo with file path",
url: "https://bitbucket.org/fake-owner/fake-public-repo/src/main/README.md",
wantUrl: GitUrl{
wantUrl: &GitUrl{
Protocol: "https",
Host: "bitbucket.org",
Owner: "fake-owner",
Expand All @@ -242,7 +242,7 @@ func Test_ParseGitUrl(t *testing.T) {
{
name: "should parse public Bitbucket file path with nested path",
url: "https://bitbucket.org/fake-owner/fake-public-repo/src/main/directory/test.txt",
wantUrl: GitUrl{
wantUrl: &GitUrl{
Protocol: "https",
Host: "bitbucket.org",
Owner: "fake-owner",
Expand All @@ -256,7 +256,7 @@ func Test_ParseGitUrl(t *testing.T) {
{
name: "should parse public Bitbucket repo with raw file path",
url: "https://bitbucket.org/fake-owner/fake-public-repo/raw/main/README.md",
wantUrl: GitUrl{
wantUrl: &GitUrl{
Protocol: "https",
Host: "bitbucket.org",
Owner: "fake-owner",
Expand Down Expand Up @@ -285,7 +285,7 @@ func Test_ParseGitUrl(t *testing.T) {
{
name: "should parse private Bitbucket repo with token",
url: "https://bitbucket.org/fake-owner/fake-private-repo",
wantUrl: GitUrl{
wantUrl: &GitUrl{
Protocol: "https",
Host: "bitbucket.org",
Owner: "fake-owner",
Expand All @@ -299,7 +299,7 @@ func Test_ParseGitUrl(t *testing.T) {
{
name: "should parse private raw Bitbucket file path with token",
url: "https://bitbucket.org/fake-owner/fake-private-repo/raw/main/README.md",
wantUrl: GitUrl{
wantUrl: &GitUrl{
Protocol: "https",
Host: "bitbucket.org",
Owner: "fake-owner",
Expand All @@ -314,7 +314,7 @@ func Test_ParseGitUrl(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseGitUrl(tt.url)
got, err := NewGitUrl(tt.url)
if (err != nil) != (tt.wantErr != "") {
t.Errorf("Unxpected error: %t, want: %v", err, tt.wantUrl)
} else if err == nil && !reflect.DeepEqual(got, tt.wantUrl) {
Expand All @@ -326,23 +326,63 @@ func Test_ParseGitUrl(t *testing.T) {
}
}

// todo: try mocking
func Test_SetToken(t *testing.T) {
g := GitUrl{
Protocol: "https",
Host: "github.com",
Owner: "devfile",
Repo: "library",
Branch: "main",
token: "",
func Test_GetGitRawFileAPI(t *testing.T) {
tests := []struct {
name string
g GitUrl
want string
}{
{
name: "Github url",
g: GitUrl{
Protocol: "https",
Host: "github.com",
Owner: "devfile",
Repo: "library",
Branch: "main",
Path: "tests/README.md",
},
want: "https://raw.githubusercontent.com/devfile/library/main/tests/README.md",
},
{
name: "GitLab url",
g: GitUrl{
Protocol: "https",
Host: "gitlab.com",
Owner: "gitlab-org",
Repo: "gitlab",
Branch: "master",
Path: "README.md",
},
want: "https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab/repository/files/README.md/raw",
},
{
name: "Bitbucket url",
g: GitUrl{
Protocol: "https",
Host: "bitbucket.org",
Owner: "owner",
Repo: "repo-name",
Branch: "main",
Path: "path/to/file.md",
},
want: "https://api.bitbucket.org/2.0/repositories/owner/repo-name/src/main/path/to/file.md",
},
{
name: "Empty GitUrl",
g: GitUrl{},
want: "",
},
}

httpTimeout := 0
token := "fake-git-token"

err := g.SetToken(token, &httpTimeout)
assert.NoError(t, err)
assert.Equal(t, token, g.token)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.g.GetGitRawFileAPI()
if !reflect.DeepEqual(result, tt.want) {
t.Errorf("Got: %v, want: %v", result, tt.want)
}
})
}
}

func Test_IsPublic(t *testing.T) {
Expand Down
Loading

0 comments on commit af566e9

Please sign in to comment.