Skip to content

Commit

Permalink
updating tests
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Hoang <[email protected]>
  • Loading branch information
mike-hoang committed Apr 27, 2023
1 parent b2357c7 commit f601e19
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 262 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ The Devfile Parser library is a Golang module that:
2. writes to the devfile.yaml with the updated data.
3. generates Kubernetes objects for the various devfile resources.
4. defines util functions for the devfile.
5. downloads resources from a parent devfile if specified in the devfile.yaml

## Private Repository Support
## Private repository support

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

[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.

[2] In this scenario, the token will be used to authenticate the main devfile.

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

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**
```yaml
schemaVersion: 2.2.0
...
parent:
uri: <uri-to-parent-devfile>/devfile.yaml
...
```

## Projects using devfile/library

The following projects are consuming this library as a Golang dependency
Expand Down
32 changes: 26 additions & 6 deletions pkg/devfile/parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"github.com/devfile/library/v2/pkg/git"
"github.com/hashicorp/go-multierror"
"io/ioutil"
"net/url"
"os"
Expand Down Expand Up @@ -47,36 +48,55 @@ import (
"github.com/pkg/errors"
)

const (
DevfileName = "devfile.yaml"
)

// downloadGitRepoResources is exposed as a global variable for the purpose of running mock tests
var downloadGitRepoResources = func(url string, destDir string, httpTimeout *int, token string) error {
var returnedErr error

gitUrl, err := git.NewGitUrlWithURL(url)
if err != nil {
return err
}

if gitUrl.IsGitProviderRepo() && gitUrl.IsFile {
stackDir, err := ioutil.TempDir(os.TempDir(), fmt.Sprintf("git-resources"))
if gitUrl.IsGitProviderRepo() {
if !gitUrl.IsFile || gitUrl.Revision == "" || !strings.Contains(gitUrl.Path, DevfileName) {
return fmt.Errorf("error getting devfile from url: failed to retrieve %s", url)
}

stackDir, err := os.MkdirTemp("", fmt.Sprintf("git-resources"))
if err != nil {
return fmt.Errorf("failed to create dir: %s, error: %v", stackDir, err)
}
defer os.RemoveAll(stackDir)

defer func(path string) {
err := os.RemoveAll(path)
if err != nil {
returnedErr = multierror.Append(returnedErr, err)
}
}(stackDir)

if !gitUrl.IsPublic(httpTimeout) {
err = gitUrl.SetToken(token, httpTimeout)
if err != nil {
return err
returnedErr = multierror.Append(returnedErr, err)
return returnedErr
}
}

err = gitUrl.CloneGitRepo(stackDir)
if err != nil {
return err
returnedErr = multierror.Append(returnedErr, err)
return returnedErr
}

dir := path.Dir(path.Join(stackDir, gitUrl.Path))
err = git.CopyAllDirFiles(dir, destDir)
if err != nil {
return err
returnedErr = multierror.Append(returnedErr, err)
return returnedErr
}
}

Expand Down
Loading

0 comments on commit f601e19

Please sign in to comment.