Skip to content

Commit f2eaaa9

Browse files
committed
updating tests
Signed-off-by: Michael Hoang <mhoang@redhat.com>
1 parent b2357c7 commit f2eaaa9

File tree

9 files changed

+278
-284
lines changed

9 files changed

+278
-284
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

+33-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"
@@ -47,36 +48,55 @@ import (
4748
"github.com/pkg/errors"
4849
)
4950

51+
const (
52+
DevfileName = "devfile.yaml"
53+
)
54+
5055
// downloadGitRepoResources is exposed as a global variable for the purpose of running mock tests
5156
var downloadGitRepoResources = func(url string, destDir string, httpTimeout *int, token string) error {
57+
var returnedErr error
58+
5259
gitUrl, err := git.NewGitUrlWithURL(url)
5360
if err != nil {
5461
return err
5562
}
5663

57-
if gitUrl.IsGitProviderRepo() && gitUrl.IsFile {
58-
stackDir, err := ioutil.TempDir(os.TempDir(), fmt.Sprintf("git-resources"))
64+
if gitUrl.IsGitProviderRepo() {
65+
if !gitUrl.IsFile || gitUrl.Revision == "" || !strings.Contains(gitUrl.Path, DevfileName) {
66+
return fmt.Errorf("error getting devfile from url: failed to retrieve %s", url)
67+
}
68+
69+
stackDir, err := os.MkdirTemp("", fmt.Sprintf("git-resources"))
5970
if err != nil {
6071
return fmt.Errorf("failed to create dir: %s, error: %v", stackDir, err)
6172
}
62-
defer os.RemoveAll(stackDir)
73+
74+
defer func(path string) {
75+
err := os.RemoveAll(path)
76+
if err != nil {
77+
returnedErr = multierror.Append(returnedErr, err)
78+
}
79+
}(stackDir)
6380

6481
if !gitUrl.IsPublic(httpTimeout) {
6582
err = gitUrl.SetToken(token, httpTimeout)
6683
if err != nil {
67-
return err
84+
returnedErr = multierror.Append(returnedErr, err)
85+
return returnedErr
6886
}
6987
}
7088

7189
err = gitUrl.CloneGitRepo(stackDir)
7290
if err != nil {
73-
return err
91+
returnedErr = multierror.Append(returnedErr, err)
92+
return returnedErr
7493
}
7594

7695
dir := path.Dir(path.Join(stackDir, gitUrl.Path))
7796
err = git.CopyAllDirFiles(dir, destDir)
7897
if err != nil {
79-
return err
98+
returnedErr = multierror.Append(returnedErr, err)
99+
return returnedErr
80100
}
81101
}
82102

@@ -163,15 +183,15 @@ func ParseDevfile(args ParserArgs) (d DevfileObj, err error) {
163183
} else if args.Path != "" {
164184
d.Ctx = devfileCtx.NewDevfileCtx(args.Path)
165185
} 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-
}
186+
d.Ctx = devfileCtx.NewURLDevfileCtx(args.URL)
171187
} else {
172188
return d, errors.Wrap(err, "the devfile source is not provided")
173189
}
174190

191+
if args.Token != "" {
192+
d.Ctx.SetToken(args.Token)
193+
}
194+
175195
tool := resolverTools{
176196
defaultNamespace: args.DefaultNamespace,
177197
registryURLs: args.RegistryURLs,
@@ -475,10 +495,9 @@ func parseFromURI(importReference v1.ImportReference, curDevfileCtx devfileCtx.D
475495
}
476496

477497
token := curDevfileCtx.GetToken()
498+
d.Ctx = devfileCtx.NewURLDevfileCtx(newUri)
478499
if token != "" {
479-
d.Ctx = devfileCtx.NewPrivateURLDevfileCtx(newUri, token)
480-
} else {
481-
d.Ctx = devfileCtx.NewURLDevfileCtx(newUri)
500+
d.Ctx.SetToken(token)
482501
}
483502

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

0 commit comments

Comments
 (0)