Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3d8e991

Browse files
committedApr 28, 2023
updating tests
Signed-off-by: Michael Hoang <mhoang@redhat.com>
1 parent b2357c7 commit 3d8e991

File tree

9 files changed

+304
-291
lines changed

9 files changed

+304
-291
lines changed
 

‎README.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ The Devfile Parser library is a Golang module that:
1212
2. writes to the devfile.yaml with the updated data.
1313
3. generates Kubernetes objects for the various devfile resources.
1414
4. defines util functions for the devfile.
15+
5. downloads resources from a parent devfile if specified in the devfile.yaml
1516

16-
## Private Repository Support
17+
## Private repository support
1718

1819
Tokens are required to be set in the following cases:
1920
1. parsing a devfile from a private repository
@@ -24,7 +25,8 @@ Set the token for the repository:
2425
```go
2526
parser.ParserArgs{
2627
...
27-
URL: <url-to-devfile-on-supported-git-provider>
28+
// URL must point to a devfile.yaml
29+
URL: <url-to-devfile-on-supported-git-provider-repo>/devfile.yaml
2830
Token: <repo-personal-access-token>
2931
...
3032
}
@@ -37,6 +39,7 @@ For more information about personal access tokens:
3739
3. [Bitbucket docs](https://support.atlassian.com/bitbucket-cloud/docs/repository-access-tokens/)
3840

3941
[1] Currently, this works under the assumption that the token can authenticate the devfile and the parent devfile; both devfiles are in the same repository.
42+
4043
[2] In this scenario, the token will be used to authenticate the main devfile.
4144

4245
## Usage
@@ -199,6 +202,15 @@ The function documentation can be accessed via [pkg.go.dev](https://pkg.go.dev/g
199202
}
200203
```
201204

205+
9. When parsing a devfile that contains a parent reference, if the parent uri is a supported git provider repo url with the correct personal access token, all resources from the parent git repo excluding the parent devfile.yaml will be downloaded to the location of the devfile being parsed. **Note: The URL must point to a devfile.yaml**
206+
```yaml
207+
schemaVersion: 2.2.0
208+
...
209+
parent:
210+
uri: <uri-to-parent-devfile>/devfile.yaml
211+
...
212+
```
213+
202214
## Projects using devfile/library
203215

204216
The following projects are consuming this library as a Golang dependency

‎pkg/devfile/parser/context/context.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,6 @@ func NewURLDevfileCtx(url string) DevfileCtx {
7373
}
7474
}
7575

76-
// NewPrivateURLDevfileCtx returns a new DevfileCtx type object
77-
func NewPrivateURLDevfileCtx(url string, token string) DevfileCtx {
78-
return DevfileCtx{
79-
url: url,
80-
token: token,
81-
}
82-
}
83-
8476
// NewByteContentDevfileCtx set devfile content from byte data and returns a new DevfileCtx type object and error
8577
func NewByteContentDevfileCtx(data []byte) (d DevfileCtx, err error) {
8678
err = d.SetDevfileContentFromBytes(data)
@@ -166,6 +158,11 @@ func (d *DevfileCtx) GetToken() string {
166158
return d.token
167159
}
168160

161+
// SetToken sets the token for the devfile
162+
func (d *DevfileCtx) SetToken(token string) {
163+
d.token = token
164+
}
165+
169166
// SetAbsPath sets absolute file path for devfile
170167
func (d *DevfileCtx) SetAbsPath() (err error) {
171168
// Set devfile absolute path

‎pkg/devfile/parser/context/context_test.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,12 @@ func TestNewURLDevfileCtx(t *testing.T) {
8888
token = "fake-token"
8989
url = "https://github.com/devfile/registry/blob/main/stacks/go/2.0.0/devfile.yaml"
9090
)
91-
92-
{
93-
d := NewPrivateURLDevfileCtx(url, token)
94-
assert.Equal(t, "https://github.com/devfile/registry/blob/main/stacks/go/2.0.0/devfile.yaml", d.GetURL())
95-
assert.Equal(t, "fake-token", d.GetToken())
96-
}
9791
{
9892
d := NewURLDevfileCtx(url)
9993
assert.Equal(t, "https://github.com/devfile/registry/blob/main/stacks/go/2.0.0/devfile.yaml", d.GetURL())
10094
assert.Equal(t, "", d.GetToken())
95+
d.SetToken(token)
96+
assert.Equal(t, "fake-token", d.GetToken())
10197
}
10298
}
10399

‎pkg/devfile/parser/parse.go

+29-14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"encoding/json"
2121
"fmt"
2222
"github.com/devfile/library/v2/pkg/git"
23+
"github.com/hashicorp/go-multierror"
2324
"io/ioutil"
2425
"net/url"
2526
"os"
@@ -49,34 +50,49 @@ import (
4950

5051
// downloadGitRepoResources is exposed as a global variable for the purpose of running mock tests
5152
var downloadGitRepoResources = func(url string, destDir string, httpTimeout *int, token string) error {
53+
var returnedErr error
54+
5255
gitUrl, err := git.NewGitUrlWithURL(url)
5356
if err != nil {
5457
return err
5558
}
5659

57-
if gitUrl.IsGitProviderRepo() && gitUrl.IsFile {
58-
stackDir, err := ioutil.TempDir(os.TempDir(), fmt.Sprintf("git-resources"))
60+
if gitUrl.IsGitProviderRepo() {
61+
if !gitUrl.IsFile || gitUrl.Revision == "" || !strings.Contains(gitUrl.Path, OutputDevfileYamlPath) {
62+
return fmt.Errorf("error getting devfile from url: failed to retrieve %s", url)
63+
}
64+
65+
stackDir, err := os.MkdirTemp("", fmt.Sprintf("git-resources"))
5966
if err != nil {
6067
return fmt.Errorf("failed to create dir: %s, error: %v", stackDir, err)
6168
}
62-
defer os.RemoveAll(stackDir)
69+
70+
defer func(path string) {
71+
err := os.RemoveAll(path)
72+
if err != nil {
73+
returnedErr = multierror.Append(returnedErr, err)
74+
}
75+
}(stackDir)
6376

6477
if !gitUrl.IsPublic(httpTimeout) {
6578
err = gitUrl.SetToken(token, httpTimeout)
6679
if err != nil {
67-
return err
80+
returnedErr = multierror.Append(returnedErr, err)
81+
return returnedErr
6882
}
6983
}
7084

7185
err = gitUrl.CloneGitRepo(stackDir)
7286
if err != nil {
73-
return err
87+
returnedErr = multierror.Append(returnedErr, err)
88+
return returnedErr
7489
}
7590

7691
dir := path.Dir(path.Join(stackDir, gitUrl.Path))
7792
err = git.CopyAllDirFiles(dir, destDir)
7893
if err != nil {
79-
return err
94+
returnedErr = multierror.Append(returnedErr, err)
95+
return returnedErr
8096
}
8197
}
8298

@@ -163,15 +179,15 @@ func ParseDevfile(args ParserArgs) (d DevfileObj, err error) {
163179
} else if args.Path != "" {
164180
d.Ctx = devfileCtx.NewDevfileCtx(args.Path)
165181
} else if args.URL != "" {
166-
if args.Token != "" {
167-
d.Ctx = devfileCtx.NewPrivateURLDevfileCtx(args.URL, args.Token)
168-
} else {
169-
d.Ctx = devfileCtx.NewURLDevfileCtx(args.URL)
170-
}
182+
d.Ctx = devfileCtx.NewURLDevfileCtx(args.URL)
171183
} else {
172184
return d, errors.Wrap(err, "the devfile source is not provided")
173185
}
174186

187+
if args.Token != "" {
188+
d.Ctx.SetToken(args.Token)
189+
}
190+
175191
tool := resolverTools{
176192
defaultNamespace: args.DefaultNamespace,
177193
registryURLs: args.RegistryURLs,
@@ -475,10 +491,9 @@ func parseFromURI(importReference v1.ImportReference, curDevfileCtx devfileCtx.D
475491
}
476492

477493
token := curDevfileCtx.GetToken()
494+
d.Ctx = devfileCtx.NewURLDevfileCtx(newUri)
478495
if token != "" {
479-
d.Ctx = devfileCtx.NewPrivateURLDevfileCtx(newUri, token)
480-
} else {
481-
d.Ctx = devfileCtx.NewURLDevfileCtx(newUri)
496+
d.Ctx.SetToken(token)
482497
}
483498

484499
destDir := path.Dir(curDevfileCtx.GetAbsPath())

‎pkg/devfile/parser/parse_test.go

+187-170
Large diffs are not rendered by default.

‎pkg/git/git.go

+20-15
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package git
1818
import (
1919
"fmt"
2020
"net/url"
21+
"os"
2122
"os/exec"
2223
"path/filepath"
2324
"strings"
@@ -30,7 +31,7 @@ const (
3031
BitbucketHost string = "bitbucket.org"
3132
)
3233

33-
type Url struct {
34+
type GitUrl struct {
3435
Protocol string // URL scheme
3536
Host string // URL domain name
3637
Owner string // name of the repo owner
@@ -42,7 +43,7 @@ type Url struct {
4243
}
4344

4445
// NewGitUrlWithURL NewGitUrl creates a GitUrl from a string url
45-
func NewGitUrlWithURL(url string) (Url, error) {
46+
func NewGitUrlWithURL(url string) (GitUrl, error) {
4647
gitUrl, err := ParseGitUrl(url)
4748
if err != nil {
4849
return gitUrl, err
@@ -52,8 +53,8 @@ func NewGitUrlWithURL(url string) (Url, error) {
5253

5354
// ParseGitUrl extracts information from a support git url
5455
// Only supports git repositories hosted on GitHub, GitLab, and Bitbucket
55-
func ParseGitUrl(fullUrl string) (Url, error) {
56-
var g Url
56+
func ParseGitUrl(fullUrl string) (GitUrl, error) {
57+
var g GitUrl
5758
err := ValidateURL(fullUrl)
5859
if err != nil {
5960
return g, err
@@ -81,7 +82,7 @@ func ParseGitUrl(fullUrl string) (Url, error) {
8182
return g, err
8283
}
8384

84-
func (g *Url) GetToken() string {
85+
func (g *GitUrl) GetToken() string {
8586
return g.token
8687
}
8788

@@ -106,7 +107,7 @@ var execute = func(baseDir string, cmd CommandType, args ...string) ([]byte, err
106107
return []byte(""), fmt.Errorf(unsupportedCmdMsg, string(cmd))
107108
}
108109

109-
func (g *Url) CloneGitRepo(destDir string) error {
110+
func (g *GitUrl) CloneGitRepo(destDir string) error {
110111
exist := CheckPathExists(destDir)
111112
if !exist {
112113
return fmt.Errorf("failed to clone repo, destination directory: '%s' does not exists", destDir)
@@ -140,14 +141,18 @@ func (g *Url) CloneGitRepo(destDir string) error {
140141
if g.Revision != "" {
141142
_, err := execute(destDir, "git", "switch", "--detach", "origin/"+g.Revision)
142143
if err != nil {
144+
err = os.RemoveAll(destDir)
145+
if err != nil {
146+
return err
147+
}
143148
return fmt.Errorf("failed to switch repo to revision. repo dir: %v, revision: %v", destDir, g.Revision)
144149
}
145150
}
146151

147152
return nil
148153
}
149154

150-
func (g *Url) parseGitHubUrl(url *url.URL) error {
155+
func (g *GitUrl) parseGitHubUrl(url *url.URL) error {
151156
var splitUrl []string
152157
var err error
153158

@@ -209,7 +214,7 @@ func (g *Url) parseGitHubUrl(url *url.URL) error {
209214
return err
210215
}
211216

212-
func (g *Url) parseGitLabUrl(url *url.URL) error {
217+
func (g *GitUrl) parseGitLabUrl(url *url.URL) error {
213218
var splitFile, splitOrg []string
214219
var err error
215220

@@ -257,7 +262,7 @@ func (g *Url) parseGitLabUrl(url *url.URL) error {
257262
return err
258263
}
259264

260-
func (g *Url) parseBitbucketUrl(url *url.URL) error {
265+
func (g *GitUrl) parseBitbucketUrl(url *url.URL) error {
261266
var splitUrl []string
262267
var err error
263268

@@ -295,7 +300,7 @@ func (g *Url) parseBitbucketUrl(url *url.URL) error {
295300

296301
// SetToken validates the token with a get request to the repo before setting the token
297302
// Defaults token to empty on failure.
298-
func (g *Url) SetToken(token string, httpTimeout *int) error {
303+
func (g *GitUrl) SetToken(token string, httpTimeout *int) error {
299304
err := g.validateToken(HTTPRequestParams{Token: token, Timeout: httpTimeout})
300305
if err != nil {
301306
g.token = ""
@@ -307,7 +312,7 @@ func (g *Url) SetToken(token string, httpTimeout *int) error {
307312

308313
// IsPublic checks if the GitUrl is public with a get request to the repo using an empty token
309314
// Returns true if the request succeeds
310-
func (g *Url) IsPublic(httpTimeout *int) bool {
315+
func (g *GitUrl) IsPublic(httpTimeout *int) bool {
311316
err := g.validateToken(HTTPRequestParams{Token: "", Timeout: httpTimeout})
312317
if err != nil {
313318
return false
@@ -317,7 +322,7 @@ func (g *Url) IsPublic(httpTimeout *int) bool {
317322

318323
// validateToken makes a http get request to the repo with the GitUrl token
319324
// Returns an error if the get request fails
320-
func (g *Url) validateToken(params HTTPRequestParams) error {
325+
func (g *GitUrl) validateToken(params HTTPRequestParams) error {
321326
var apiUrl string
322327

323328
switch g.Host {
@@ -341,14 +346,14 @@ func (g *Url) validateToken(params HTTPRequestParams) error {
341346
}
342347

343348
// GitRawFileAPI returns the endpoint for the git providers raw file
344-
func (g *Url) GitRawFileAPI() string {
349+
func (g *GitUrl) GitRawFileAPI() string {
345350
var apiRawFile string
346351

347352
switch g.Host {
348353
case GitHubHost, RawGitHubHost:
349354
apiRawFile = fmt.Sprintf("https://raw.githubusercontent.com/%s/%s/%s/%s", g.Owner, g.Repo, g.Revision, g.Path)
350355
case GitLabHost:
351-
apiRawFile = fmt.Sprintf("https://gitlab.com/api/v4/projects/%s%%2F%s/repository/files/%s/raw", g.Owner, g.Repo, g.Path)
356+
apiRawFile = fmt.Sprintf("https://gitlab.com/api/v4/projects/%s%%2F%s/repository/files/%s/raw?ref=%s", g.Owner, g.Repo, g.Path, g.Revision)
352357
case BitbucketHost:
353358
apiRawFile = fmt.Sprintf("https://api.bitbucket.org/2.0/repositories/%s/%s/src/%s/%s", g.Owner, g.Repo, g.Revision, g.Path)
354359
}
@@ -357,7 +362,7 @@ func (g *Url) GitRawFileAPI() string {
357362
}
358363

359364
// IsGitProviderRepo checks if the url matches a repo from a supported git provider
360-
func (g *Url) IsGitProviderRepo() bool {
365+
func (g *GitUrl) IsGitProviderRepo() bool {
361366
switch g.Host {
362367
case GitHubHost, RawGitHubHost, GitLabHost, BitbucketHost:
363368
return true

‎pkg/git/git_test.go

+25-67
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func Test_ParseGitUrl(t *testing.T) {
2828
tests := []struct {
2929
name string
3030
url string
31-
wantUrl Url
31+
wantUrl GitUrl
3232
wantErr string
3333
}{
3434
{
@@ -45,7 +45,7 @@ func Test_ParseGitUrl(t *testing.T) {
4545
{
4646
name: "should parse GitHub repo with root path",
4747
url: "https://github.com/devfile/library",
48-
wantUrl: Url{
48+
wantUrl: GitUrl{
4949
Protocol: "https",
5050
Host: "github.com",
5151
Owner: "devfile",
@@ -58,7 +58,7 @@ func Test_ParseGitUrl(t *testing.T) {
5858
{
5959
name: "should parse GitHub repo with root path and tag",
6060
url: "https://github.com/devfile/library/tree/v2.2.0",
61-
wantUrl: Url{
61+
wantUrl: GitUrl{
6262
Protocol: "https",
6363
Host: "github.com",
6464
Owner: "devfile",
@@ -71,7 +71,7 @@ func Test_ParseGitUrl(t *testing.T) {
7171
{
7272
name: "should parse GitHub repo with root path and revision",
7373
url: "https://github.com/devfile/library/tree/0ce592a416fb185564516353891a45016ac7f671",
74-
wantUrl: Url{
74+
wantUrl: GitUrl{
7575
Protocol: "https",
7676
Host: "github.com",
7777
Owner: "devfile",
@@ -89,7 +89,7 @@ func Test_ParseGitUrl(t *testing.T) {
8989
{
9090
name: "should parse GitHub repo with file path",
9191
url: "https://github.com/devfile/library/blob/main/devfile.yaml",
92-
wantUrl: Url{
92+
wantUrl: GitUrl{
9393
Protocol: "https",
9494
Host: "github.com",
9595
Owner: "devfile",
@@ -102,7 +102,7 @@ func Test_ParseGitUrl(t *testing.T) {
102102
{
103103
name: "should parse GitHub repo with raw file path",
104104
url: "https://raw.githubusercontent.com/devfile/library/main/devfile.yaml",
105-
wantUrl: Url{
105+
wantUrl: GitUrl{
106106
Protocol: "https",
107107
Host: "raw.githubusercontent.com",
108108
Owner: "devfile",
@@ -146,7 +146,7 @@ func Test_ParseGitUrl(t *testing.T) {
146146
{
147147
name: "should parse GitLab repo with root path",
148148
url: "https://gitlab.com/gitlab-org/gitlab-foss",
149-
wantUrl: Url{
149+
wantUrl: GitUrl{
150150
Protocol: "https",
151151
Host: "gitlab.com",
152152
Owner: "gitlab-org",
@@ -164,7 +164,7 @@ func Test_ParseGitUrl(t *testing.T) {
164164
{
165165
name: "should parse GitLab repo with file path",
166166
url: "https://gitlab.com/gitlab-org/gitlab-foss/-/blob/master/README.md",
167-
wantUrl: Url{
167+
wantUrl: GitUrl{
168168
Protocol: "https",
169169
Host: "gitlab.com",
170170
Owner: "gitlab-org",
@@ -193,7 +193,7 @@ func Test_ParseGitUrl(t *testing.T) {
193193
{
194194
name: "should parse Bitbucket repo with root path",
195195
url: "https://bitbucket.org/fake-owner/fake-public-repo",
196-
wantUrl: Url{
196+
wantUrl: GitUrl{
197197
Protocol: "https",
198198
Host: "bitbucket.org",
199199
Owner: "fake-owner",
@@ -211,7 +211,7 @@ func Test_ParseGitUrl(t *testing.T) {
211211
{
212212
name: "should parse Bitbucket repo with file path",
213213
url: "https://bitbucket.org/fake-owner/fake-public-repo/src/main/README.md",
214-
wantUrl: Url{
214+
wantUrl: GitUrl{
215215
Protocol: "https",
216216
Host: "bitbucket.org",
217217
Owner: "fake-owner",
@@ -224,7 +224,7 @@ func Test_ParseGitUrl(t *testing.T) {
224224
{
225225
name: "should parse Bitbucket file path with nested path",
226226
url: "https://bitbucket.org/fake-owner/fake-public-repo/src/main/directory/test.txt",
227-
wantUrl: Url{
227+
wantUrl: GitUrl{
228228
Protocol: "https",
229229
Host: "bitbucket.org",
230230
Owner: "fake-owner",
@@ -237,7 +237,7 @@ func Test_ParseGitUrl(t *testing.T) {
237237
{
238238
name: "should parse Bitbucket repo with raw file path",
239239
url: "https://bitbucket.org/fake-owner/fake-public-repo/raw/main/README.md",
240-
wantUrl: Url{
240+
wantUrl: GitUrl{
241241
Protocol: "https",
242242
Host: "bitbucket.org",
243243
Owner: "fake-owner",
@@ -281,12 +281,12 @@ func Test_ParseGitUrl(t *testing.T) {
281281
func Test_GetGitRawFileAPI(t *testing.T) {
282282
tests := []struct {
283283
name string
284-
g Url
284+
g GitUrl
285285
want string
286286
}{
287287
{
288288
name: "Github url",
289-
g: Url{
289+
g: GitUrl{
290290
Protocol: "https",
291291
Host: "github.com",
292292
Owner: "devfile",
@@ -298,19 +298,19 @@ func Test_GetGitRawFileAPI(t *testing.T) {
298298
},
299299
{
300300
name: "GitLab url",
301-
g: Url{
301+
g: GitUrl{
302302
Protocol: "https",
303303
Host: "gitlab.com",
304304
Owner: "gitlab-org",
305305
Repo: "gitlab",
306-
Revision: "master",
306+
Revision: "v15.11.0-ee",
307307
Path: "README.md",
308308
},
309-
want: "https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab/repository/files/README.md/raw",
309+
want: "https://gitlab.com/api/v4/projects/gitlab-org%2Fgitlab/repository/files/README.md/raw?ref=v15.11.0-ee",
310310
},
311311
{
312312
name: "Bitbucket url",
313-
g: Url{
313+
g: GitUrl{
314314
Protocol: "https",
315315
Host: "bitbucket.org",
316316
Owner: "owner",
@@ -322,7 +322,7 @@ func Test_GetGitRawFileAPI(t *testing.T) {
322322
},
323323
{
324324
name: "Empty GitUrl",
325-
g: Url{},
325+
g: GitUrl{},
326326
want: "",
327327
},
328328
}
@@ -338,7 +338,7 @@ func Test_GetGitRawFileAPI(t *testing.T) {
338338
}
339339

340340
func Test_IsPublic(t *testing.T) {
341-
publicGitUrl := Url{
341+
publicGitUrl := GitUrl{
342342
Protocol: "https",
343343
Host: "github.com",
344344
Owner: "devfile",
@@ -347,7 +347,7 @@ func Test_IsPublic(t *testing.T) {
347347
token: "fake-token",
348348
}
349349

350-
privateGitUrl := Url{
350+
privateGitUrl := GitUrl{
351351
Protocol: "https",
352352
Host: "github.com",
353353
Owner: "not",
@@ -360,7 +360,7 @@ func Test_IsPublic(t *testing.T) {
360360

361361
tests := []struct {
362362
name string
363-
g Url
363+
g GitUrl
364364
want bool
365365
}{
366366
{
@@ -387,43 +387,16 @@ func Test_IsPublic(t *testing.T) {
387387

388388
func Test_CloneGitRepo(t *testing.T) {
389389
tempInvalidDir := t.TempDir()
390-
tempDirGitHub := t.TempDir()
391-
tempDirGitLab := t.TempDir()
392-
tempDirBitbucket := t.TempDir()
393390

394-
invalidGitUrl := Url{
391+
invalidGitUrl := GitUrl{
395392
Protocol: "",
396393
Host: "",
397394
Owner: "nonexistent",
398395
Repo: "nonexistent",
399396
Revision: "nonexistent",
400397
}
401398

402-
validPublicGitHubUrl := Url{
403-
Protocol: "https",
404-
Host: "github.com",
405-
Owner: "devfile",
406-
Repo: "library",
407-
Revision: "main",
408-
}
409-
410-
validPublicGitLabUrl := Url{
411-
Protocol: "https",
412-
Host: "gitlab.com",
413-
Owner: "mike-hoang",
414-
Repo: "public-testing-repo",
415-
Revision: "main",
416-
}
417-
418-
validPublicBitbucketUrl := Url{
419-
Protocol: "https",
420-
Host: "bitbucket.org",
421-
Owner: "mike-hoang",
422-
Repo: "public-testing-repo",
423-
Revision: "master",
424-
}
425-
426-
invalidPrivateGitHubRepo := Url{
399+
invalidPrivateGitHubRepo := GitUrl{
427400
Protocol: "https",
428401
Host: "github.com",
429402
Owner: "fake-owner",
@@ -438,7 +411,7 @@ func Test_CloneGitRepo(t *testing.T) {
438411

439412
tests := []struct {
440413
name string
441-
gitUrl Url
414+
gitUrl GitUrl
442415
destDir string
443416
wantErr string
444417
}{
@@ -460,21 +433,6 @@ func Test_CloneGitRepo(t *testing.T) {
460433
destDir: tempInvalidDir,
461434
wantErr: privateRepoBadTokenErr,
462435
},
463-
{
464-
name: "should be able to clone valid public github url",
465-
gitUrl: validPublicGitHubUrl,
466-
destDir: tempDirGitHub,
467-
},
468-
{
469-
name: "should be able to clone valid public gitlab url",
470-
gitUrl: validPublicGitLabUrl,
471-
destDir: tempDirGitLab,
472-
},
473-
{
474-
name: "should be able to clone valid public bitbucket url",
475-
gitUrl: validPublicBitbucketUrl,
476-
destDir: tempDirBitbucket,
477-
},
478436
}
479437

480438
for _, tt := range tests {

‎pkg/git/mock.go

+20-7
Original file line numberDiff line numberDiff line change
@@ -44,30 +44,43 @@ var mockExecute = func(baseDir string, cmd CommandType, args ...string) ([]byte,
4444
u, _ := url.Parse(args[1])
4545
password, hasPassword := u.User.Password()
4646

47+
resourceFile, err := os.Create(filepath.Clean(baseDir) + "/resource.file")
48+
if err != nil {
49+
return nil, fmt.Errorf("failed to create test resource: %v", err)
50+
}
51+
4752
// private repository
4853
if hasPassword {
4954
switch password {
5055
case "valid-token":
51-
_, err := os.Create(filepath.Clean(baseDir) + "/private-repo-resource.txt")
56+
_, err := resourceFile.WriteString("private repo\n")
5257
if err != nil {
53-
return nil, fmt.Errorf("failed to create test resource: %v", err)
58+
return nil, fmt.Errorf("failed to write to test resource: %v", err)
5459
}
55-
return []byte("test"), nil
60+
return []byte(""), nil
5661
default:
5762
return []byte(""), fmt.Errorf("not a valid token")
5863
}
5964
}
60-
// public repository
61-
_, err := os.Create(filepath.Clean(baseDir) + "/public-repo-resource.txt")
65+
66+
_, err = resourceFile.WriteString("public repo\n")
6267
if err != nil {
63-
return nil, fmt.Errorf("failed to create test resource: %v", err)
68+
return nil, fmt.Errorf("failed to write to test resource: %v", err)
6469
}
65-
return []byte("test"), nil
70+
return []byte(""), nil
6671
}
6772

6873
if len(args) > 0 && args[0] == "switch" {
6974
revision := strings.TrimPrefix(args[2], "origin/")
7075
if revision != "invalid-revision" {
76+
resourceFile, err := os.OpenFile(filepath.Clean(baseDir)+"/resource.file", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
77+
if err != nil {
78+
return nil, fmt.Errorf("failed to open test resource: %v", err)
79+
}
80+
_, err = resourceFile.WriteString("git switched")
81+
if err != nil {
82+
return nil, fmt.Errorf("failed to write to test resource: %v", err)
83+
}
7184
return []byte("git switched to revision"), nil
7285
}
7386
return []byte(""), fmt.Errorf("failed to switch revision")

‎pkg/util/util.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ func DownloadInMemory(params HTTPRequestParams) ([]byte, error) {
11001100
ResponseHeaderTimeout: HTTPRequestResponseTimeout,
11011101
}, Timeout: HTTPRequestResponseTimeout}
11021102

1103-
var g git.Url
1103+
var g git.GitUrl
11041104
var err error
11051105

11061106
if IsGitProviderRepo(params.URL) {
@@ -1113,7 +1113,7 @@ func DownloadInMemory(params HTTPRequestParams) ([]byte, error) {
11131113
return downloadInMemoryWithClient(params, httpClient, g)
11141114
}
11151115

1116-
func downloadInMemoryWithClient(params HTTPRequestParams, httpClient HTTPClient, g git.Url) ([]byte, error) {
1116+
func downloadInMemoryWithClient(params HTTPRequestParams, httpClient HTTPClient, g git.GitUrl) ([]byte, error) {
11171117
var url string
11181118
url = params.URL
11191119
req, err := http.NewRequest("GET", url, nil)

0 commit comments

Comments
 (0)
Please sign in to comment.