Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: xray watch #211

Merged
merged 30 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
7809b5a
feat(xray): setup service manager for xray endpoint
josh-barker-coles Oct 9, 2020
0e6132d
feat(xray): support xray watch apis
josh-barker-coles Oct 9, 2020
d4a29b7
feat(xray): support update on xray watch
josh-barker-coles Oct 7, 2020
09fd8fd
test(xray): refactor tests to separate functions
josh-barker-coles Oct 7, 2020
69afc2e
refactor(xray): tidy up names and remove redundant methods
josh-barker-coles Oct 8, 2020
1cd9175
refactor(xray): move watch to xray package
josh-barker-coles Oct 9, 2020
8ecf4f7
test(xray): add more update test cases
josh-barker-coles Oct 9, 2020
070f421
docs(xray): update readme
josh-barker-coles Oct 9, 2020
d1d2252
chore(xray): fix golint issues
josh-barker-coles Oct 11, 2020
4ef0b47
refactor(xray): move structs and functions to utils file
josh-barker-coles Oct 12, 2020
75a9cbc
test(xray): add tests cases for repository and built types
josh-barker-coles Oct 12, 2020
ccb28a7
docs(xray): document structs, funcs and update readme for watch
josh-barker-coles Oct 12, 2020
1139426
chore(*): ignore go coverage file
josh-barker-coles Oct 12, 2020
df5fdbe
test(xray): fix unit tests
josh-barker-coles Oct 12, 2020
8969dab
test(xray): only test xray when an xray url is set
josh-barker-coles Oct 14, 2020
0a61652
fix(xray): check error when creating body for update
josh-barker-coles Oct 14, 2020
8f319d8
refactory(xray): rename StringFilters to Filters
josh-barker-coles Oct 14, 2020
c17b085
chore(xray): reorder structs into exported and unexported
josh-barker-coles Oct 14, 2020
0a615d2
chore(xray): update comments in code
josh-barker-coles Oct 20, 2020
14efc5a
test(xray): rename test functions and throw response errors separately
josh-barker-coles Oct 20, 2020
128e9d0
fix(xray): use checkError to wrap errors from different sources
josh-barker-coles Oct 20, 2020
38a9f41
chore(xray): document that bundles are not supported and remove place…
josh-barker-coles Oct 20, 2020
dee3850
chore(xray): tidy up logging for watch functions
josh-barker-coles Oct 20, 2020
908ea61
fix(xray): ensure watch url is in the correct format
josh-barker-coles Oct 20, 2020
366943b
refactor(xray): remove Xray prefix on structs and functions
josh-barker-coles Oct 20, 2020
9d0aa86
refactor(xray): return http responses for watch functions
josh-barker-coles Oct 29, 2020
226ebc3
fix(xray): fix bug where multiple properties were not set
josh-barker-coles Oct 29, 2020
f141060
fix(xray): create default parameters for watch
josh-barker-coles Oct 29, 2020
1921286
fix(xray): add xray details on watch service
josh-barker-coles Nov 8, 2020
bc2aa2f
docs(xray): update examples in readme for watch functions
josh-barker-coles Nov 8, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@

# IOS
*.DS_Store

# go coverage
coverage.out
96 changes: 95 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@
- [Downloading Logs](#downloading-logs)
- [Syncing Content To Maven Central](#syncing-content-to-maven-central)
- [Using ContentReader](#using-contentreader)
- [Xray APIs](#xray-apis)
- [Creating Xray Service Manager](#creating-xray-service-manager)
- [Creating Xray Details](#creating-xray-details)
- [Creating Xray Service Config](#creating-xray-service-config)
- [Creating New Xray Service Manager](#creating-new-xray-service-manager)
- [Using Xray Services](#using-xray-services)
- [Creating an Xray Watch](#creating-an-xray-watch)
- [Get an Xray Watch](#get-an-xray-watch)
- [Update an Xray Watch](#update-an-xray-watch)
- [Delete an Xray Watch](#delete-an-xray-watch)

## General
_jfrog-client-go_ is a library which provides Go APIs to performs actions on JFrog Artifactory or Bintray from your Go application.
Expand Down Expand Up @@ -126,6 +136,7 @@ Optional flags:
| `-rt.user` | [Default: admin] Artifactory username. |
| `-rt.password` | [Default: password] Artifactory password. |
| `-rt.distUrl` | [Optional] JFrog Distribution URL. |
| `-rt.xrayUrl` | [Optional] JFrog Xray URL. |
| `-rt.apikey` | [Optional] Artifactory API key. |
| `-rt.sshKeyPath` | [Optional] Ssh key file path. Should be used only if the Artifactory URL format is ssh://[domain]:port |
| `-rt.sshPassphrase` | [Optional] Ssh key passphrase. |
Expand Down Expand Up @@ -1110,4 +1121,87 @@ reader.Reset()

* `reader.GetError()` returns any error that might have occurd during `NextRecord()`.

* `reader.Reset()` resets the reader back to the beginning of the output.
* `reader.Reset()` resets the reader back to the beginning of the output.

## Xray APIs
### Creating Xray Service Manager
#### Creating Xray Details
```go
xrayDetails := auth.NewXrayDetails()
xrayDetails.SetUrl("http://localhost:8081/xray")
xrayDetails.SetSshKeysPath("path/to/.ssh/")
xrayDetails.SetApiKey("apikey")
xrayDetails.SetUser("user")
xrayDetails.SetPassword("password")
xrayDetails.SetAccessToken("accesstoken")
// if client certificates are required
xrayDetails.SetClientCertPath("path/to/.cer")
xrayDetails.SetClientCertKeyPath("path/to/.key")
```

#### Creating Xray Service Config
```go
serviceConfig, err := config.NewConfigBuilder().
SetServiceDetails(xrayDetails).
SetCertificatesPath(certPath).
Build()
```

#### Creating New Xray Service Manager
```go
xrayManager, err := xray.New(&xrayDetails, serviceConfig)
```

### Using Xray Services
#### Creating an Xray Watch

This uses API version 2.

You are able to configure repositories and builds on a watch.
However, bundles are not supported.

```go
params := utils.NewWatchParams()
params.Name = "example-watch-all"
params.Description = "All Repos"
params.Active = true

params.Repositories.Type = utils.WatchRepositoriesAll
params.Repositories.All.Filters.PackageTypes = []string{"Npm", "maven"}
params.Repositories.ExcludePatterns = []string{"excludePath1", "excludePath2"}
params.Repositories.IncludePatterns = []string{"includePath1", "includePath2"}

params.Builds.Type = utils.WatchBuildAll
params.Builds.All.Bin_Mgr_ID = "default"

params.Policies = []utils.AssignedPolicy{
{
Name: policy1Name,
Type: "security",
},
{
Name: policy2Name,
Type: "security",
},
}

resp, err := xrayManager.CreateWatch(*params)
```

#### Get an Xray Watch
```go
watch, resp, err := xrayManager.GetWatch("example-watch-all")
```

#### Update an Xray Watch
```go
watch, resp, err := xrayManager.GetWatch("example-watch-all")
watch.Description = "Updated description"

resp, err := xrayManager.UpdateWatch(*watch)
```

#### Delete an Xray Watch
```go
resp, err := xrayManager.DeleteWatch("example-watch-all")
```
9 changes: 7 additions & 2 deletions tests/jfrogclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ const (
)

func TestMain(m *testing.M) {
InitArtifactoryServiceManager()
InitServiceManagers()
result := m.Run()
os.Exit(result)
}

func InitArtifactoryServiceManager() {
func InitServiceManagers() {
flag.Parse()
log.SetLogger(log.NewLogger(log.DEBUG, nil))
createArtifactoryUploadManager()
Expand All @@ -43,9 +43,14 @@ func InitArtifactoryServiceManager() {
createArtifactoryReplicationGetManager()
createArtifactoryReplicationDeleteManager()
createArtifactoryPermissionTargetManager()

if *DistUrl != "" {
createDistributionManager()
}
if *XrayUrl != "" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that this has been added as part of the InitArtifactoryServiceManager function, we should probably rename the function from InitArtifactoryServiceManager to InitServiceManagers.

createXrayVersionManager()
createXrayWatchManager()
}
createReposIfNeeded()
}

Expand Down
34 changes: 33 additions & 1 deletion tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"flag"
"fmt"
"github.com/jfrog/jfrog-client-go/artifactory/services/utils/tests"
"io/ioutil"
"net/http"
"os"
Expand All @@ -14,6 +13,8 @@ import (
"testing"
"time"

"github.com/jfrog/jfrog-client-go/artifactory/services/utils/tests"

artifactoryAuth "github.com/jfrog/jfrog-client-go/artifactory/auth"
rthttpclient "github.com/jfrog/jfrog-client-go/artifactory/httpclient"
"github.com/jfrog/jfrog-client-go/artifactory/services"
Expand All @@ -25,12 +26,15 @@ import (
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/io/fileutils"
"github.com/jfrog/jfrog-client-go/utils/log"
xrayAuth "github.com/jfrog/jfrog-client-go/xray/auth"
xrayServices "github.com/jfrog/jfrog-client-go/xray/services"
"github.com/mholt/archiver"
"github.com/stretchr/testify/assert"
)

var RtUrl *string
var DistUrl *string
var XrayUrl *string
var RtUser *string
var RtPassword *string
var RtApiKey *string
Expand Down Expand Up @@ -69,6 +73,10 @@ var testsBundleDistributionStatusService *distributionServices.DistributionStatu
var testsBundleDeleteLocalService *distributionServices.DeleteLocalReleaseBundleService
var testsBundleDeleteRemoteService *distributionServices.DeleteReleaseBundleService

// Xray Services
var testsXrayVersionService *xrayServices.VersionService
var testsXrayWatchService *xrayServices.WatchService

var timestamp = time.Now().Unix()
var trueValue = true
var falseValue = false
Expand All @@ -84,6 +92,7 @@ const (
func init() {
RtUrl = flag.String("rt.url", "http://localhost:8081/artifactory/", "Artifactory url")
DistUrl = flag.String("rt.distUrl", "", "Distribution url")
XrayUrl = flag.String("rt.xrayUrl", "", "Xray url")
RtUser = flag.String("rt.user", "admin", "Artifactory username")
RtPassword = flag.String("rt.password", "password", "Artifactory password")
RtApiKey = flag.String("rt.apikey", "", "Artifactory user API key")
Expand Down Expand Up @@ -158,6 +167,14 @@ func createDistributionManager() {
testsBundleDeleteRemoteService.DistDetails = distDetails
}

func createXrayVersionManager() {
xrayDetails := GetXrayDetails()
client, err := rthttpclient.ArtifactoryClientBuilder().SetServiceDetails(&xrayDetails).Build()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that ArtifactoryClientBuilder also needs to be used for Xray, we should rename it to HttpClientBuilder.
We should also move the httpclient package, which is currently located under the artifactory directory, to be one level up, next to the artifactory dir.
rthttpclient should be renamed to httpclient, or if the the httpclient is already taken as an alias in the file, maybe jfroghttpclient.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @eyalbe4,

I understand why you want to move that functionality, but I've noticed a few things.

This sounds like a big change that should go in a separate PR, as it would effect many files under artifactory/*, distribution/*, tests/* and xray/*.

As the httpclient directory & package is already taken, what did you want to do?

Cheers

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it @josh-barker-coles. We'll create an issue for this and handle this in a follow up PR.

failOnHttpClientCreation(err)
testsXrayVersionService = xrayServices.NewVersionService(client)
testsXrayVersionService.XrayDetails = xrayDetails
}

func createArtifactoryCreateLocalRepositoryManager() {
artDetails := GetRtDetails()
client, err := rthttpclient.ArtifactoryClientBuilder().SetServiceDetails(&artDetails).Build()
Expand Down Expand Up @@ -262,6 +279,14 @@ func createArtifactoryPermissionTargetManager() {
testsPermissionTargetService.ArtDetails = artDetails
}

func createXrayWatchManager() {
XrayDetails := GetXrayDetails()
client, err := rthttpclient.ArtifactoryClientBuilder().SetServiceDetails(&XrayDetails).Build()
failOnHttpClientCreation(err)
testsXrayWatchService = xrayServices.NewWatchService(client)
testsXrayWatchService.XrayDetails = XrayDetails
}

func failOnHttpClientCreation(err error) {
if err != nil {
log.Error(fmt.Sprintf(HttpClientCreationFailureMessage, err.Error()))
Expand All @@ -283,6 +308,13 @@ func GetDistDetails() auth.ServiceDetails {
return distDetails
}

func GetXrayDetails() auth.ServiceDetails {
xrayDetails := xrayAuth.NewXrayDetails()
xrayDetails.SetUrl(clientutils.AddTrailingSlashIfNeeded(*XrayUrl))
setAuthenticationDetail(xrayDetails)
return xrayDetails
}

func setAuthenticationDetail(details auth.ServiceDetails) {
if !fileutils.IsSshUrl(details.GetUrl()) {
if *RtApiKey != "" {
Expand Down
19 changes: 19 additions & 0 deletions tests/xray_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tests

import (
"testing"
)

func TestXrayVersion(t *testing.T) {
if *XrayUrl == "" {
t.Skip("Xray is not being tested, skipping...")
}

version, err := GetXrayDetails().GetVersion()
if err != nil {
t.Error(err)
}
if version == "" {
t.Error("Expected a version, got empty string")
}
}
Loading