Skip to content

Commit af566e9

Browse files
committed
cleanup
Signed-off-by: Michael Hoang <mhoang@redhat.com>
1 parent 046c37b commit af566e9

File tree

8 files changed

+310
-54
lines changed

8 files changed

+310
-54
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ The function documentation can be accessed via [pkg.go.dev](https://pkg.go.dev/g
198198
url = "https://gitlab.com/<owner>/<repo name>"
199199

200200
// Parse the repo url
201-
gitUrl, err := util.ParseGitUrl(url)
201+
gitUrl, err := util.NewGitUrl(url)
202202

203203
// Clone the repo to a destination dir
204-
err = util.CloneGitRepo(gitUrl, destDir)
204+
err = util.CloneGitRepo(*gitUrl, destDir)
205205
```
206206

207207
## Projects using devfile/library

pkg/devfile/parser/parse.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ func parseFromURI(importReference v1.ImportReference, curDevfileCtx devfileCtx.D
431431

432432
d.Ctx = devfileCtx.NewURLDevfileCtx(newUri)
433433
if util.IsGitProviderRepo(newUri) {
434-
gitUrl, err := util.ParseGitUrl(newUri)
434+
gitUrl, err := util.NewGitUrl(newUri)
435435
if err != nil {
436436
return DevfileObj{}, err
437437
}
@@ -450,7 +450,7 @@ func parseFromURI(importReference v1.ImportReference, curDevfileCtx devfileCtx.D
450450
return populateAndParseDevfile(d, newResolveCtx, tool, true)
451451
}
452452

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

467-
err = util.CloneGitRepo(g, stackDir)
467+
err = util.CloneGitRepo(*g, stackDir)
468468
if err != nil {
469469
return err
470470
}

pkg/devfile/parser/parse_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4207,7 +4207,7 @@ func Test_getResourcesFromGit(t *testing.T) {
42074207

42084208
for _, tt := range tests {
42094209
t.Run(tt.name, func(t *testing.T) {
4210-
err := getResourcesFromGit(tt.gitUrl, tt.destDir, &httpTimeout, "")
4210+
err := getResourcesFromGit(&tt.gitUrl, tt.destDir, &httpTimeout, "")
42114211
if (err != nil) != tt.wantErr {
42124212
t.Errorf("Expected error: %t, got error: %t", tt.wantErr, err)
42134213
}

pkg/util/git.go

+37-7
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ const (
3131
BitbucketHost string = "bitbucket.org"
3232
)
3333

34+
type IGitUrl interface {
35+
ParseGitUrl(fullUrl string) error
36+
GetGitRawFileAPI() string
37+
SetToken(token string, httpTimeout *int) error
38+
IsPublic(httpTimeout *int) bool
39+
}
40+
3441
type GitUrl struct {
3542
Protocol string // URL scheme
3643
Host string // URL domain name
@@ -42,23 +49,30 @@ type GitUrl struct {
4249
IsFile bool // defines if the URL points to a file in the repo
4350
}
4451

52+
// NewGitUrl creates a GitUrl from a string url
53+
func NewGitUrl(url string) (*GitUrl, error) {
54+
g := &GitUrl{}
55+
if err := g.ParseGitUrl(url); err != nil {
56+
return g, err
57+
}
58+
return g, nil
59+
}
60+
4561
// ParseGitUrl extracts information from a support git url
4662
// Only supports git repositories hosted on GitHub, GitLab, and Bitbucket
47-
func ParseGitUrl(fullUrl string) (GitUrl, error) {
48-
var g GitUrl
49-
63+
func (g *GitUrl) ParseGitUrl(fullUrl string) error {
5064
err := ValidateURL(fullUrl)
5165
if err != nil {
52-
return g, err
66+
return err
5367
}
5468

5569
parsedUrl, err := url.Parse(fullUrl)
5670
if err != nil {
57-
return g, err
71+
return err
5872
}
5973

6074
if len(parsedUrl.Path) == 0 {
61-
return g, fmt.Errorf("url path should not be empty")
75+
return fmt.Errorf("url path should not be empty")
6276
}
6377

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

74-
return g, err
88+
return err
7589
}
7690

7791
func (g *GitUrl) parseGitHubUrl(url *url.URL) error {
@@ -242,6 +256,22 @@ func (g *GitUrl) validateToken(params HTTPRequestParams) error {
242256
return nil
243257
}
244258

259+
// GetGitRawFileAPI returns the endpoint for the git providers raw file
260+
func (g *GitUrl) GetGitRawFileAPI() string {
261+
var apiRawFile string
262+
263+
switch g.Host {
264+
case GitHubHost, RawGitHubHost:
265+
apiRawFile = fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/%s/%s", g.Owner, g.Repo, g.Branch, g.Path)
266+
case GitLabHost:
267+
apiRawFile = fmt.Sprintf("https://gitlab.com/api/v4/projects/%s%%2F%s/repository/files/%s/raw", g.Owner, g.Repo, g.Path)
268+
case BitbucketHost:
269+
apiRawFile = fmt.Sprintf("https://api.bitbucket.org/2.0/repositories/%s/%s/src/%s/%s", g.Owner, g.Repo, g.Branch, g.Path)
270+
}
271+
272+
return apiRawFile
273+
}
274+
245275
// IsGitProviderRepo checks if the url matches a repo from a supported git provider
246276
func IsGitProviderRepo(url string) bool {
247277
if strings.Contains(url, RawGitHubHost) || strings.Contains(url, GitHubHost) ||

pkg/util/git_test.go

+73-33
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ var (
3030
bitbucketToken = "fake-bitbucket-token"
3131
)
3232

33-
func Test_ParseGitUrl(t *testing.T) {
33+
func Test_NewGitUrl(t *testing.T) {
3434
tests := []struct {
3535
name string
3636
url string
37-
wantUrl GitUrl
37+
wantUrl *GitUrl
3838
wantErr string
3939
}{
4040
{
@@ -51,7 +51,7 @@ func Test_ParseGitUrl(t *testing.T) {
5151
{
5252
name: "should parse public GitHub repo with root path",
5353
url: "https://github.com/devfile/library",
54-
wantUrl: GitUrl{
54+
wantUrl: &GitUrl{
5555
Protocol: "https",
5656
Host: "github.com",
5757
Owner: "devfile",
@@ -70,7 +70,7 @@ func Test_ParseGitUrl(t *testing.T) {
7070
{
7171
name: "should parse public GitHub repo with file path",
7272
url: "https://github.com/devfile/library/blob/main/devfile.yaml",
73-
wantUrl: GitUrl{
73+
wantUrl: &GitUrl{
7474
Protocol: "https",
7575
Host: "github.com",
7676
Owner: "devfile",
@@ -84,7 +84,7 @@ func Test_ParseGitUrl(t *testing.T) {
8484
{
8585
name: "should parse public GitHub repo with raw file path",
8686
url: "https://raw.githubusercontent.com/devfile/library/main/devfile.yaml",
87-
wantUrl: GitUrl{
87+
wantUrl: &GitUrl{
8888
Protocol: "https",
8989
Host: "raw.githubusercontent.com",
9090
Owner: "devfile",
@@ -108,7 +108,7 @@ func Test_ParseGitUrl(t *testing.T) {
108108
{
109109
name: "should parse private GitHub repo with token",
110110
url: "https://github.com/fake-owner/fake-private-repo",
111-
wantUrl: GitUrl{
111+
wantUrl: &GitUrl{
112112
Protocol: "https",
113113
Host: "github.com",
114114
Owner: "fake-owner",
@@ -122,7 +122,7 @@ func Test_ParseGitUrl(t *testing.T) {
122122
{
123123
name: "should parse private raw GitHub file path with token",
124124
url: "https://raw.githubusercontent.com/fake-owner/fake-private-repo/main/README.md",
125-
wantUrl: GitUrl{
125+
wantUrl: &GitUrl{
126126
Protocol: "https",
127127
Host: "raw.githubusercontent.com",
128128
Owner: "fake-owner",
@@ -137,7 +137,7 @@ func Test_ParseGitUrl(t *testing.T) {
137137
{
138138
name: "should parse public GitLab repo with root path",
139139
url: "https://gitlab.com/gitlab-org/gitlab-foss",
140-
wantUrl: GitUrl{
140+
wantUrl: &GitUrl{
141141
Protocol: "https",
142142
Host: "gitlab.com",
143143
Owner: "gitlab-org",
@@ -156,7 +156,7 @@ func Test_ParseGitUrl(t *testing.T) {
156156
{
157157
name: "should parse public GitLab repo with file path",
158158
url: "https://gitlab.com/gitlab-org/gitlab-foss/-/blob/master/README.md",
159-
wantUrl: GitUrl{
159+
wantUrl: &GitUrl{
160160
Protocol: "https",
161161
Host: "gitlab.com",
162162
Owner: "gitlab-org",
@@ -180,7 +180,7 @@ func Test_ParseGitUrl(t *testing.T) {
180180
{
181181
name: "should parse private GitLab repo with token",
182182
url: "https://gitlab.com/fake-owner/fake-private-repo",
183-
wantUrl: GitUrl{
183+
wantUrl: &GitUrl{
184184
Protocol: "https",
185185
Host: "gitlab.com",
186186
Owner: "fake-owner",
@@ -194,7 +194,7 @@ func Test_ParseGitUrl(t *testing.T) {
194194
{
195195
name: "should parse private raw GitLab file path with token",
196196
url: "https://gitlab.com/fake-owner/fake-private-repo/-/raw/main/README.md",
197-
wantUrl: GitUrl{
197+
wantUrl: &GitUrl{
198198
Protocol: "https",
199199
Host: "gitlab.com",
200200
Owner: "fake-owner",
@@ -209,7 +209,7 @@ func Test_ParseGitUrl(t *testing.T) {
209209
{
210210
name: "should parse public Bitbucket repo with root path",
211211
url: "https://bitbucket.org/fake-owner/fake-public-repo",
212-
wantUrl: GitUrl{
212+
wantUrl: &GitUrl{
213213
Protocol: "https",
214214
Host: "bitbucket.org",
215215
Owner: "fake-owner",
@@ -228,7 +228,7 @@ func Test_ParseGitUrl(t *testing.T) {
228228
{
229229
name: "should parse public Bitbucket repo with file path",
230230
url: "https://bitbucket.org/fake-owner/fake-public-repo/src/main/README.md",
231-
wantUrl: GitUrl{
231+
wantUrl: &GitUrl{
232232
Protocol: "https",
233233
Host: "bitbucket.org",
234234
Owner: "fake-owner",
@@ -242,7 +242,7 @@ func Test_ParseGitUrl(t *testing.T) {
242242
{
243243
name: "should parse public Bitbucket file path with nested path",
244244
url: "https://bitbucket.org/fake-owner/fake-public-repo/src/main/directory/test.txt",
245-
wantUrl: GitUrl{
245+
wantUrl: &GitUrl{
246246
Protocol: "https",
247247
Host: "bitbucket.org",
248248
Owner: "fake-owner",
@@ -256,7 +256,7 @@ func Test_ParseGitUrl(t *testing.T) {
256256
{
257257
name: "should parse public Bitbucket repo with raw file path",
258258
url: "https://bitbucket.org/fake-owner/fake-public-repo/raw/main/README.md",
259-
wantUrl: GitUrl{
259+
wantUrl: &GitUrl{
260260
Protocol: "https",
261261
Host: "bitbucket.org",
262262
Owner: "fake-owner",
@@ -285,7 +285,7 @@ func Test_ParseGitUrl(t *testing.T) {
285285
{
286286
name: "should parse private Bitbucket repo with token",
287287
url: "https://bitbucket.org/fake-owner/fake-private-repo",
288-
wantUrl: GitUrl{
288+
wantUrl: &GitUrl{
289289
Protocol: "https",
290290
Host: "bitbucket.org",
291291
Owner: "fake-owner",
@@ -299,7 +299,7 @@ func Test_ParseGitUrl(t *testing.T) {
299299
{
300300
name: "should parse private raw Bitbucket file path with token",
301301
url: "https://bitbucket.org/fake-owner/fake-private-repo/raw/main/README.md",
302-
wantUrl: GitUrl{
302+
wantUrl: &GitUrl{
303303
Protocol: "https",
304304
Host: "bitbucket.org",
305305
Owner: "fake-owner",
@@ -314,7 +314,7 @@ func Test_ParseGitUrl(t *testing.T) {
314314

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

329-
// todo: try mocking
330-
func Test_SetToken(t *testing.T) {
331-
g := GitUrl{
332-
Protocol: "https",
333-
Host: "github.com",
334-
Owner: "devfile",
335-
Repo: "library",
336-
Branch: "main",
337-
token: "",
329+
func Test_GetGitRawFileAPI(t *testing.T) {
330+
tests := []struct {
331+
name string
332+
g GitUrl
333+
want string
334+
}{
335+
{
336+
name: "Github url",
337+
g: GitUrl{
338+
Protocol: "https",
339+
Host: "github.com",
340+
Owner: "devfile",
341+
Repo: "library",
342+
Branch: "main",
343+
Path: "tests/README.md",
344+
},
345+
want: "https://raw.githubusercontent.com/devfile/library/main/tests/README.md",
346+
},
347+
{
348+
name: "GitLab url",
349+
g: GitUrl{
350+
Protocol: "https",
351+
Host: "gitlab.com",
352+
Owner: "gitlab-org",
353+
Repo: "gitlab",
354+
Branch: "master",
355+
Path: "README.md",
356+
},
357+
want: "https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab/repository/files/README.md/raw",
358+
},
359+
{
360+
name: "Bitbucket url",
361+
g: GitUrl{
362+
Protocol: "https",
363+
Host: "bitbucket.org",
364+
Owner: "owner",
365+
Repo: "repo-name",
366+
Branch: "main",
367+
Path: "path/to/file.md",
368+
},
369+
want: "https://api.bitbucket.org/2.0/repositories/owner/repo-name/src/main/path/to/file.md",
370+
},
371+
{
372+
name: "Empty GitUrl",
373+
g: GitUrl{},
374+
want: "",
375+
},
338376
}
339377

340-
httpTimeout := 0
341-
token := "fake-git-token"
342-
343-
err := g.SetToken(token, &httpTimeout)
344-
assert.NoError(t, err)
345-
assert.Equal(t, token, g.token)
378+
for _, tt := range tests {
379+
t.Run(tt.name, func(t *testing.T) {
380+
result := tt.g.GetGitRawFileAPI()
381+
if !reflect.DeepEqual(result, tt.want) {
382+
t.Errorf("Got: %v, want: %v", result, tt.want)
383+
}
384+
})
385+
}
346386
}
347387

348388
func Test_IsPublic(t *testing.T) {

0 commit comments

Comments
 (0)