Skip to content

Commit 4bdc1e7

Browse files
committed
adding support for public and private git providers
Signed-off-by: Michael Hoang <[email protected]>
1 parent 7beda08 commit 4bdc1e7

File tree

6 files changed

+768
-165
lines changed

6 files changed

+768
-165
lines changed

pkg/devfile/parser/parse.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -425,15 +425,17 @@ func parseFromURI(importReference v1.ImportReference, curDevfileCtx devfileCtx.D
425425
}
426426

427427
d.Ctx = devfileCtx.NewURLDevfileCtx(newUri)
428-
if strings.Contains(newUri, "raw.githubusercontent.com") {
429-
urlComponents, err := util.GetGitUrlComponentsFromRaw(newUri)
428+
if strings.Contains(newUri, util.RawGitHubHost) {
429+
gitUrl, err := util.ParseGitUrl(newUri)
430430
if err != nil {
431431
return DevfileObj{}, err
432432
}
433-
destDir := path.Dir(curDevfileCtx.GetAbsPath())
434-
err = getResourcesFromGit(urlComponents, destDir)
435-
if err != nil {
436-
return DevfileObj{}, err
433+
if gitUrl.IsFile {
434+
destDir := path.Dir(curDevfileCtx.GetAbsPath())
435+
err = getResourcesFromGit(gitUrl, destDir)
436+
if err != nil {
437+
return DevfileObj{}, err
438+
}
437439
}
438440
}
439441
}
@@ -443,19 +445,19 @@ func parseFromURI(importReference v1.ImportReference, curDevfileCtx devfileCtx.D
443445
return populateAndParseDevfile(d, newResolveCtx, tool, true)
444446
}
445447

446-
func getResourcesFromGit(gitUrlComponents map[string]string, destDir string) error {
448+
func getResourcesFromGit(g util.GitUrl, destDir string) error {
447449
stackDir, err := ioutil.TempDir(os.TempDir(), fmt.Sprintf("git-resources"))
448450
if err != nil {
449451
return fmt.Errorf("failed to create dir: %s, error: %v", stackDir, err)
450452
}
451453
defer os.RemoveAll(stackDir)
452454

453-
err = util.CloneGitRepo(gitUrlComponents, stackDir)
455+
err = util.CloneGitRepo(g, stackDir)
454456
if err != nil {
455457
return err
456458
}
457459

458-
dir := path.Dir(path.Join(stackDir, gitUrlComponents["file"]))
460+
dir := path.Dir(path.Join(stackDir, g.Path))
459461
err = util.CopyAllDirFiles(dir, destDir)
460462
if err != nil {
461463
return err

pkg/devfile/parser/parse_test.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package parser
1818
import (
1919
"context"
2020
"fmt"
21+
"github.com/devfile/library/v2/pkg/util"
2122
"io/ioutil"
2223
"net"
2324
"net/http"
@@ -4165,42 +4166,46 @@ func Test_getResourcesFromGit(t *testing.T) {
41654166
}
41664167
defer os.RemoveAll(destDir)
41674168

4168-
invalidGitUrl := map[string]string{
4169-
"username": "devfile",
4170-
"project": "nonexistent",
4171-
"branch": "nonexistent",
4169+
invalidGitHubUrl := util.GitUrl{
4170+
Protocol: "",
4171+
Host: "",
4172+
Owner: "devfile",
4173+
Repo: "nonexistent",
4174+
Branch: "nonexistent",
41724175
}
4173-
validGitUrl := map[string]string{
4174-
"host": "raw.githubusercontent.com",
4175-
"username": "devfile",
4176-
"project": "registry",
4177-
"branch": "main",
4178-
"file": "stacks/nodejs/devfile.yaml",
4176+
validGitHubUrl := util.GitUrl{
4177+
Protocol: "https",
4178+
Host: "raw.githubusercontent.com",
4179+
Owner: "devfile",
4180+
Repo: "registry",
4181+
Branch: "main",
4182+
Path: "stacks/nodejs/devfile.yaml",
4183+
IsFile: true,
41794184
}
41804185

41814186
tests := []struct {
4182-
name string
4183-
gitUrlComponents map[string]string
4184-
destDir string
4185-
wantErr bool
4187+
name string
4188+
gitUrl util.GitUrl
4189+
destDir string
4190+
wantErr bool
41864191
}{
41874192
{
4188-
name: "should fail with invalid git url",
4189-
gitUrlComponents: invalidGitUrl,
4190-
destDir: path.Join(os.TempDir(), "nonexistent"),
4191-
wantErr: true,
4193+
name: "should fail with invalid git url",
4194+
gitUrl: invalidGitHubUrl,
4195+
destDir: path.Join(os.TempDir(), "nonexistent"),
4196+
wantErr: true,
41924197
},
41934198
{
4194-
name: "should be able to get resources from valid git url",
4195-
gitUrlComponents: validGitUrl,
4196-
destDir: destDir,
4197-
wantErr: false,
4199+
name: "should be able to get resources from valid git url",
4200+
gitUrl: validGitHubUrl,
4201+
destDir: destDir,
4202+
wantErr: false,
41984203
},
41994204
}
42004205

42014206
for _, tt := range tests {
42024207
t.Run(tt.name, func(t *testing.T) {
4203-
err := getResourcesFromGit(tt.gitUrlComponents, tt.destDir)
4208+
err := getResourcesFromGit(tt.gitUrl, tt.destDir)
42044209
if (err != nil) != tt.wantErr {
42054210
t.Errorf("Expected error: %t, got error: %t", tt.wantErr, err)
42064211
}

0 commit comments

Comments
 (0)