Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/jfrog/jfrog-client-go
Browse files Browse the repository at this point in the history
# Conflicts:
#	artifactory/services/security.go
  • Loading branch information
sverdlov93 committed Sep 9, 2024
2 parents c94bcc7 + b1cffdc commit f2056b4
Show file tree
Hide file tree
Showing 20 changed files with 383 additions and 51 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@
- [Using XSC Services](#using-xsc-services)
- [Fetching XSC's Version](#fetching-xscs-version)
- [Report XSC analytics metrics](#report-xsc-analytics-metrics)
- [Add analytics general event](#add-analytics-general-event)
- [Update analytics general event](#update-analytics-general-event)
- [Get analytics general event](#get-analytics-general-event)
- [Add Analytics General Event](#add-analytics-general-event)
- [Update Analytics General Event](#update-analytics-general-event)
- [Get Analytics General Event](#get-analytics-general-event)
- [Pipelines APIs](#pipelines-apis)
- [Creating Pipelines Service Manager](#creating-pipelines-service-manager)
- [Creating Pipelines Details](#creating-pipelines-details)
Expand Down Expand Up @@ -1081,6 +1081,7 @@ You can get all repositories from Artifactory filtered according to theirs type
params := services.NewRepositoriesFilterParams()
params.RepoType = "remote"
params.PackageType = "maven"
params.ProjectKey = "project-key"
err := servicesManager.GetAllRepositoriesFiltered(params)
```

Expand Down Expand Up @@ -2468,8 +2469,8 @@ xscManager, err := xsc.New(serviceConfig)
version, err := xscManager.GetVersion()
```
#### Report XSC analytics metrics
##### Add analytics general event
#### Report XSC Analytics Metrics
##### Add Analytics General Event
Sent XSC a new event which contains analytics data, and get multi-scan id back from XSC.
```go
event := services.XscAnalyticsGeneralEvent{
Expand All @@ -2486,7 +2487,7 @@ event := services.XscAnalyticsGeneralEvent{
}}
msi, err := xscManager.AddAnalyticsGeneralEvent(event)
```
##### Update analytics general event
##### Update Analytics General Event
Sent XSC a finalized analytics metrics event with information matching an existing event's msi.
```go
finalizeEvent := services.XscAnalyticsGeneralEventFinalize{
Expand All @@ -2501,7 +2502,7 @@ finalizeEvent := services.XscAnalyticsGeneralEventFinalize{
err := xscManager.UpdateAnalyticsGeneralEvent(finalizeEvent)
```
##### Get analytics general event
##### Get Analytics General Event
Get a general event from XSC matching the provided msi.
```go
event, err := xscManager.GetAnalyticsGeneralEvent(msi)
Expand Down
2 changes: 1 addition & 1 deletion artifactory/services/buildinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (bis *BuildInfoService) PublishBuildInfo(build *buildinfo.BuildInfo, projec
if bis.IsDryRun() {
log.Info("[Dry run] Logging Build info preview...")
log.Output(clientutils.IndentJson(content))
return summary, err
return summary, nil
}
httpClientsDetails := bis.GetArtifactoryDetails().CreateHttpClientDetails()
utils.SetContentType("application/vnd.org.jfrog.artifactory+json", &httpClientsDetails.Headers)
Expand Down
31 changes: 27 additions & 4 deletions artifactory/services/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package services

import (
"encoding/json"
"fmt"
"net/http"
"net/url"

"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
Expand Down Expand Up @@ -46,12 +46,11 @@ func (rs *RepositoriesService) IsExists(repoKey string) (exists bool, err error)

func (rs *RepositoriesService) GetAll() (*[]RepositoryDetails, error) {
log.Info("Getting all repositories ...")
return rs.GetWithFilter(RepositoriesFilterParams{RepoType: "", PackageType: ""})
return rs.GetWithFilter(RepositoriesFilterParams{})
}

func (rs *RepositoriesService) GetWithFilter(params RepositoriesFilterParams) (*[]RepositoryDetails, error) {
url := fmt.Sprintf("%s?type=%s&packageType=%s", apiRepositories, params.RepoType, params.PackageType)
body, err := rs.sendGet(url)
body, err := rs.sendGet(rs.createUrlWithFilter(params))
if err != nil {
return nil, err
}
Expand All @@ -60,6 +59,29 @@ func (rs *RepositoriesService) GetWithFilter(params RepositoriesFilterParams) (*
return repoDetails, errorutils.CheckError(err)
}

// This function is used to create the URL for the repositories API with the given filter params.
// The function expects to get a RepositoriesFilterParams struct that contains the desired filter params.
// The function returns the URL string.
func (rs *RepositoriesService)createUrlWithFilter(params RepositoriesFilterParams) string {
u := url.URL{
Path: apiRepositories,
}

queryParams := url.Values{}
if params.RepoType != "" {
queryParams.Add("type", params.RepoType)
}
if params.PackageType != "" {
queryParams.Add("packageType", params.PackageType)
}
if params.ProjectKey != "" {
queryParams.Add("project", params.ProjectKey)
}

u.RawQuery = queryParams.Encode()
return u.String()
}

func (rs *RepositoriesService) sendGet(api string) ([]byte, error) {
httpClientsDetails := rs.ArtDetails.CreateHttpClientDetails()
resp, body, _, err := rs.client.SendGet(rs.ArtDetails.GetUrl()+api, true, &httpClientsDetails)
Expand Down Expand Up @@ -113,6 +135,7 @@ func (rd RepositoryDetails) GetRepoType() string {
type RepositoriesFilterParams struct {
RepoType string
PackageType string
ProjectKey string
}

func NewRepositoriesFilterParams() RepositoriesFilterParams {
Expand Down
27 changes: 27 additions & 0 deletions artifactory/services/repositories_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package services

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCreateUrlWithFilter(t *testing.T) {
rs := RepositoriesService{}
testCases := []struct {
params RepositoriesFilterParams
expected string
}{
{RepositoriesFilterParams{RepoType: "git", PackageType: "npm", ProjectKey: "123"}, "api/repositories?packageType=npm&project=123&type=git"},
{RepositoriesFilterParams{RepoType: "git", PackageType: "npm"}, "api/repositories?packageType=npm&type=git"},
{RepositoriesFilterParams{RepoType: "git"}, "api/repositories?type=git"},
{RepositoriesFilterParams{PackageType: "npm"}, "api/repositories?packageType=npm"},
{RepositoriesFilterParams{ProjectKey: "123"}, "api/repositories?project=123"},
{RepositoriesFilterParams{}, "api/repositories"},
}

for _, testCase := range testCases {
result := rs.createUrlWithFilter(testCase.params)
assert.Equal(t, testCase.expected, result, "For params %+v, expected %s, but got %s", testCase.params, testCase.expected, result)
}
}
26 changes: 13 additions & 13 deletions artifactory/services/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
)

const (
tokenPath = "api/security/token"
APIKeyPath = "api/security/apiKey"
errorMsgPrefix = "error occurred while attempting to"
unexpectedServerResponse = "got unexpected server response while attempting to"
couldntParseServerResponse = "couldn't parse server response while attempting to"
tokenPath = "api/security/token"
APIKeyPath = "api/security/apiKey"
errorMsgPrefix = "error occurred while attempting to"
unexpectedServerResponsePrefix = "got unexpected server response while attempting to"
couldntParseServerResponsePrefix = "couldn't parse server response while attempting to"
)

type SecurityService struct {
Expand Down Expand Up @@ -100,7 +100,7 @@ func (ss *SecurityService) GetAPIKey() (string, error) {
func getApiKeyFromBody(body []byte) (string, error) {
var data = make(map[string]interface{})
if err := json.Unmarshal(body, &data); err != nil {
return "", errorutils.CheckErrorf("unable to decode json. Error: %w Upstream response: %s", err, string(body))
return "", errorutils.CheckErrorf("unable to decode json. Error: %s Upstream response: %s", err.Error(), string(body))
}

if len(data) == 0 {
Expand All @@ -123,10 +123,10 @@ func (ss *SecurityService) CreateToken(params CreateTokenParams) (auth.CreateTok
return tokenInfo, fmt.Errorf("%s create token: %w", errorMsgPrefix, err)
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil {
return tokenInfo, fmt.Errorf("%s create token: %w", unexpectedServerResponse, err)
return tokenInfo, fmt.Errorf("%s create token: %w", unexpectedServerResponsePrefix, err)
}
if err = json.Unmarshal(body, &tokenInfo); err != nil {
return tokenInfo, errorutils.CheckErrorf("%s create token: %w", couldntParseServerResponse, err.Error())
return tokenInfo, errorutils.CheckErrorf("%s create token: %s", couldntParseServerResponsePrefix, err.Error())
}
return tokenInfo, nil
}
Expand All @@ -140,10 +140,10 @@ func (ss *SecurityService) GetTokens() (GetTokensResponseData, error) {
return tokens, fmt.Errorf("%s get tokens: %w", errorMsgPrefix, err)
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil {
return tokens, fmt.Errorf("%s get tokens: %w", unexpectedServerResponse, err)
return tokens, fmt.Errorf("%s get tokens: %w", unexpectedServerResponsePrefix, err)
}
if err = json.Unmarshal(body, &tokens); err != nil {
return tokens, errorutils.CheckErrorf("%s get tokens: %s", couldntParseServerResponse, err.Error())
return tokens, errorutils.CheckErrorf("%s get tokens: %s", couldntParseServerResponsePrefix, err.Error())
}
return tokens, nil
}
Expand Down Expand Up @@ -172,10 +172,10 @@ func (ss *SecurityService) RefreshToken(params ArtifactoryRefreshTokenParams) (a
return tokenInfo, fmt.Errorf("%s refresh token: %w", errorMsgPrefix, err)
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil {
return tokenInfo, fmt.Errorf("%s refresh token: %w", unexpectedServerResponse, err)
return tokenInfo, fmt.Errorf("%s refresh token: %w", unexpectedServerResponsePrefix, err)
}
if err = json.Unmarshal(body, &tokenInfo); err != nil {
return tokenInfo, errorutils.CheckErrorf("%s refresh token: %s", couldntParseServerResponse, err.Error())
return tokenInfo, errorutils.CheckErrorf("%s refresh token: %s", couldntParseServerResponsePrefix, err.Error())
}
return tokenInfo, nil
}
Expand All @@ -190,7 +190,7 @@ func (ss *SecurityService) RevokeToken(params RevokeTokenParams) (string, error)
return "", fmt.Errorf("%s revoke token: %w", errorMsgPrefix, err)
}
if err = errorutils.CheckResponseStatusWithBody(resp, body, http.StatusOK); err != nil {
return "", fmt.Errorf("%s revoke token: %w", unexpectedServerResponse, err)
return "", fmt.Errorf("%s revoke token: %w", unexpectedServerResponsePrefix, err)
}
return string(body), nil
}
Expand Down
4 changes: 2 additions & 2 deletions artifactory/services/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ const (
defaultUploadMinSplit = utils.SizeMiB * 200
// The default maximum number of parts that can be concurrently uploaded per file during a multipart upload
defaultUploadSplitCount = 5
// Minimal file size to show progress bar
minFileSizeForProgressInKb = 250 * utils.SizeKib
// Minimal file size to show progress bar (to avoid polluting the terminal with a lot of progress lines)
minFileSizeForProgressInKb = 500 * utils.SizeKib
)

type UploadService struct {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/gookit/color v1.5.4
github.com/jfrog/archiver/v3 v3.6.1
github.com/jfrog/build-info-go v1.9.33
github.com/jfrog/build-info-go v1.9.35
github.com/jfrog/gofrog v1.7.5
github.com/minio/sha256-simd v1.0.1
github.com/stretchr/testify v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOl
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/jfrog/archiver/v3 v3.6.1 h1:LOxnkw9pOn45DzCbZNFV6K0+6dCsQ0L8mR3ZcujO5eI=
github.com/jfrog/archiver/v3 v3.6.1/go.mod h1:VgR+3WZS4N+i9FaDwLZbq+jeU4B4zctXL+gL4EMzfLw=
github.com/jfrog/build-info-go v1.9.33 h1:TEeTHDc3tEwZe/7kKhm1hQDd5vA/HnVhp1ZczUOWExk=
github.com/jfrog/build-info-go v1.9.33/go.mod h1:JTGnENexG1jRhKWCkQtZuDb0PerlzlSzF5OmMLG9kfc=
github.com/jfrog/build-info-go v1.9.35 h1:P53Ckbuin0GYrq0LWMY0GZSptJcQwiUyW6lqTbXKdcc=
github.com/jfrog/build-info-go v1.9.35/go.mod h1:6mdtqjREK76bHNODXakqKR/+ksJ9dvfLS7H57BZtnLY=
github.com/jfrog/gofrog v1.7.5 h1:dFgtEDefJdlq9cqTRoe09RLxS5Bxbe1Ev5+E6SmZHcg=
github.com/jfrog/gofrog v1.7.5/go.mod h1:jyGiCgiqSSR7k86hcUSu67XVvmvkkgWTmPsH25wI298=
github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4=
Expand Down
7 changes: 2 additions & 5 deletions tests/artifactoryctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tests

import (
"context"
"errors"
"testing"
"time"

Expand Down Expand Up @@ -40,8 +39,6 @@ func testCtxTimeout(t *testing.T) {
assert.NoError(t, err)
time.Sleep(time.Millisecond * 300)
_, err = sm.GetVersion()
assert.Error(t, err)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fail()
}
// Expect timeout error
assert.ErrorContains(t, err, context.DeadlineExceeded.Error())
}
49 changes: 49 additions & 0 deletions tests/testdata/configprofile/configProfileExample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"profile_name": "default-profile",
"frogbot_config": {
"email_author": "[email protected]",
"aggregate_fixes": true,
"avoid_previous_pr_comments_deletion": true,
"branch_name_template": "frogbot-${IMPACTED_PACKAGE}-${BRANCH_NAME_HASH}",
"pr_title_template": "[🐸 Frogbot] Upgrade {IMPACTED_PACKAGE} to {FIX_VERSION}",
"pr_comment_title": "Frogbot notes:",
"commit_message_template": "Upgrade {IMPACTED_PACKAGE} to {FIX_VERSION}",
"show_secrets_as_pr_comment": false
},
"modules": [
{
"module_name": "default-module",
"path_from_root": ".",
"releases_repo": "nuget-remote",
"analyzer_manager_version": "1.8.1",
"additional_paths_for_module": ["lib1", "utils/lib2"],
"exclude_paths": ["**/.git/**", "**/*test*/**", "**/*venv*/**", "**/*node_modules*/**", "**/target/**"],
"scan_config": {
"scan_timeout": 600,
"exclude_pattern": "*.md",
"enable_sca_scan": true,
"enable_contextual_analysis_scan": true,
"sast_scanner_config": {
"enable_sast_scan": true
},
"secrets_scanner_config": {
"enable_secrets_scan": true
},
"iac_scanner_config": {
"enable_iac_scan": true
},
"applications_scanner_config": {
"enable_applications_scan": true
},
"services_scanner_config": {
"enable_services_scan": true
}
},
"protected_branches": ["main", "master"],
"include_exclude_mode": 0,
"include_exclude_pattern": "*test*",
"report_analytics": true
}
],
"is_default": true
}
6 changes: 3 additions & 3 deletions tests/xraywatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,17 +314,17 @@ func testXrayWatchUpdateMissingWatch(t *testing.T) {
paramsMissingWatch.Policies = []utils.AssignedPolicy{}

err := testsXrayWatchService.Update(paramsMissingWatch)
assert.EqualError(t, err, "server response: 404 Not Found\n{\n \"error\": \"Failed to update Watch: Watch was not found\"\n}")
assert.EqualError(t, err, "server response: 404 Not Found")
}

func testXrayWatchDeleteMissingWatch(t *testing.T) {
err := testsXrayWatchService.Delete("client-go-tests-watch-builds-missing")
assert.EqualError(t, err, "server response: 404 Not Found\n{\n \"error\": \"Failed to delete Watch: Watch was not found\"\n}")
assert.EqualError(t, err, "server response: 404 Not Found")
}

func testXrayWatchGetMissingWatch(t *testing.T) {
_, err := testsXrayWatchService.Get("client-go-tests-watch-builds-missing")
assert.EqualError(t, err, "server response: 404 Not Found\n{\n \"error\": \"Watch was not found\"\n}")
assert.EqualError(t, err, "server response: 404 Not Found")
}

func validateWatchGeneralSettings(t *testing.T, params utils.WatchParams) {
Expand Down
Loading

0 comments on commit f2056b4

Please sign in to comment.