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

adding support for public and private git providers #160

Merged
merged 17 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 11 additions & 9 deletions pkg/devfile/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,15 +425,17 @@ func parseFromURI(importReference v1.ImportReference, curDevfileCtx devfileCtx.D
}

d.Ctx = devfileCtx.NewURLDevfileCtx(newUri)
mike-hoang marked this conversation as resolved.
Show resolved Hide resolved
if strings.Contains(newUri, "raw.githubusercontent.com") {
urlComponents, err := util.GetGitUrlComponentsFromRaw(newUri)
if strings.Contains(newUri, util.RawGitHubHost) {
gitUrl, err := util.ParseGitUrl(newUri)
if err != nil {
return DevfileObj{}, err
}
destDir := path.Dir(curDevfileCtx.GetAbsPath())
err = getResourcesFromGit(urlComponents, destDir)
if err != nil {
return DevfileObj{}, err
if gitUrl.IsFile {
destDir := path.Dir(curDevfileCtx.GetAbsPath())
err = getResourcesFromGit(gitUrl, destDir)
mike-hoang marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return DevfileObj{}, err
}
}
}
}
Expand All @@ -443,19 +445,19 @@ func parseFromURI(importReference v1.ImportReference, curDevfileCtx devfileCtx.D
return populateAndParseDevfile(d, newResolveCtx, tool, true)
}

func getResourcesFromGit(gitUrlComponents map[string]string, destDir string) error {
func getResourcesFromGit(g util.GitUrl, destDir 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)
}
defer os.RemoveAll(stackDir)

err = util.CloneGitRepo(gitUrlComponents, stackDir)
err = util.CloneGitRepo(g, stackDir)
if err != nil {
return err
}

dir := path.Dir(path.Join(stackDir, gitUrlComponents["file"]))
dir := path.Dir(path.Join(stackDir, g.Path))
err = util.CopyAllDirFiles(dir, destDir)
if err != nil {
return err
Expand Down
51 changes: 28 additions & 23 deletions pkg/devfile/parser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package parser
import (
"context"
"fmt"
"github.com/devfile/library/v2/pkg/util"
"io/ioutil"
"net"
"net/http"
Expand Down Expand Up @@ -4165,42 +4166,46 @@ func Test_getResourcesFromGit(t *testing.T) {
}
defer os.RemoveAll(destDir)

invalidGitUrl := map[string]string{
"username": "devfile",
"project": "nonexistent",
"branch": "nonexistent",
invalidGitHubUrl := util.GitUrl{
Protocol: "",
Host: "",
Owner: "devfile",
Repo: "nonexistent",
Branch: "nonexistent",
}
validGitUrl := map[string]string{
"host": "raw.githubusercontent.com",
"username": "devfile",
"project": "registry",
"branch": "main",
"file": "stacks/nodejs/devfile.yaml",
validGitHubUrl := util.GitUrl{
Protocol: "https",
Host: "raw.githubusercontent.com",
Owner: "devfile",
Repo: "registry",
Branch: "main",
Path: "stacks/nodejs/devfile.yaml",
IsFile: true,
}

tests := []struct {
name string
gitUrlComponents map[string]string
destDir string
wantErr bool
name string
gitUrl util.GitUrl
destDir string
wantErr bool
}{
{
name: "should fail with invalid git url",
gitUrlComponents: invalidGitUrl,
destDir: path.Join(os.TempDir(), "nonexistent"),
wantErr: true,
name: "should fail with invalid git url",
gitUrl: invalidGitHubUrl,
destDir: path.Join(os.TempDir(), "nonexistent"),
wantErr: true,
},
{
name: "should be able to get resources from valid git url",
gitUrlComponents: validGitUrl,
destDir: destDir,
wantErr: false,
name: "should be able to get resources from valid git url",
gitUrl: validGitHubUrl,
destDir: destDir,
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := getResourcesFromGit(tt.gitUrlComponents, tt.destDir)
err := getResourcesFromGit(tt.gitUrl, tt.destDir)
if (err != nil) != tt.wantErr {
t.Errorf("Expected error: %t, got error: %t", tt.wantErr, err)
}
Expand Down
Loading