Skip to content

Commit 7acc246

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

File tree

9 files changed

+344
-309
lines changed

9 files changed

+344
-309
lines changed

README.md

+21-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,19 +25,28 @@ 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
}
3133
```
3234
Note: The url must also be set with a supported git provider repo url.
3335

36+
Minimum token scope required:
37+
1. GitHub: Read access to code
38+
2. GitLab: Read repository
39+
3. Bitbucket: Read repository
40+
41+
Note: To select token scopes for GitHub, a fine-grained token is required.
42+
3443
For more information about personal access tokens:
3544
1. [GitHub docs](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token)
3645
2. [GitLab docs](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#create-a-personal-access-token)
3746
3. [Bitbucket docs](https://support.atlassian.com/bitbucket-cloud/docs/repository-access-tokens/)
3847

3948
[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.
49+
4050
[2] In this scenario, the token will be used to authenticate the main devfile.
4151

4252
## Usage
@@ -199,6 +209,15 @@ The function documentation can be accessed via [pkg.go.dev](https://pkg.go.dev/g
199209
}
200210
```
201211

212+
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**
213+
```yaml
214+
schemaVersion: 2.2.0
215+
...
216+
parent:
217+
uri: <uri-to-parent-devfile>/devfile.yaml
218+
...
219+
```
220+
202221
## Projects using devfile/library
203222

204223
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())

0 commit comments

Comments
 (0)