Skip to content

Commit f601e19

Browse files
committed
updating tests
Signed-off-by: Michael Hoang <[email protected]>
1 parent b2357c7 commit f601e19

File tree

7 files changed

+263
-262
lines changed

7 files changed

+263
-262
lines changed

README.md

Lines changed: 14 additions & 2 deletions
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/parse.go

Lines changed: 26 additions & 6 deletions
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

0 commit comments

Comments
 (0)