Skip to content

Commit

Permalink
Feature: xray watch (#211)
Browse files Browse the repository at this point in the history
* feat(xray): setup service manager for xray endpoint

* feat(xray): support xray watch apis

* feat(xray): support update on xray watch

* test(xray): refactor tests to separate functions

* refactor(xray): tidy up names and remove redundant methods

* refactor(xray): move watch to xray package

* test(xray): add more update test cases

* docs(xray): update readme

* chore(xray): fix golint issues

* refactor(xray): move structs and functions to utils file

* test(xray): add tests cases for repository and built types

* docs(xray): document structs, funcs and update readme for watch

* chore(*): ignore go coverage file

* test(xray): fix unit tests

* test(xray): only test xray when an xray url is set

* fix(xray): check error when creating body for update

* refactory(xray): rename StringFilters to Filters

* chore(xray): reorder structs into exported and unexported

* chore(xray): update comments in code

* test(xray): rename test functions and throw response errors separately

* fix(xray): use checkError to wrap errors from different sources

* chore(xray): document that bundles are not supported and remove placeholder function

* chore(xray): tidy up logging for watch functions

* fix(xray): ensure watch url is in the correct format

* refactor(xray): remove Xray prefix on structs and functions

* refactor(xray): return http responses for watch functions

* fix(xray): fix bug where multiple properties were not set

* fix(xray): create default parameters for watch

* fix(xray): add xray details on watch service

* docs(xray): update examples in readme for watch functions
  • Loading branch information
josh-barker-coles authored Nov 9, 2020
1 parent 3b8f189 commit 5bbc84f
Show file tree
Hide file tree
Showing 12 changed files with 1,618 additions and 4 deletions.
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 != "" {
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()
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

0 comments on commit 5bbc84f

Please sign in to comment.