Skip to content

Commit a06223f

Browse files
committed
Merge remote-tracking branch 'origin/dev'
2 parents 40364ef + d9b18ee commit a06223f

9 files changed

Lines changed: 133 additions & 52 deletions

File tree

artifactory/services/utils/tests/xray/consts.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,14 +1440,18 @@ const XscGitInfoResponse = `{"multi_scan_id": "3472b4e2-bddc-11ee-a9c9-acde48001
14401440
const XscGitInfoBadResponse = `"failed create git info request: git_repo_url field must contain value"`
14411441

14421442
var GitInfoContextWithMinimalRequiredFields = xscServices.XscGitInfoContext{
1443-
GitRepoHttpsCloneUrl: "https://git.jfrog.info/projects/XSC/repos/xsc-service",
1444-
BranchName: "feature/XRAY-123-cool-feature",
1445-
LastCommitHash: "acc5e24e69a-d3c1-4022-62eb-69e4a1e5",
1443+
Source: xscServices.CommitContext{
1444+
GitRepoHttpsCloneUrl: "https://git.jfrog.info/projects/XSC/repos/xsc-service",
1445+
BranchName: "feature/XRAY-123-cool-feature",
1446+
CommitHash: "acc5e24e69a-d3c1-4022-62eb-69e4a1e5",
1447+
},
14461448
}
14471449

14481450
var GitInfoContextWithMissingFields = xscServices.XscGitInfoContext{
1449-
GitRepoHttpsCloneUrl: "https://git.jfrog.info/projects/XSC/repos/xsc-service",
1450-
BranchName: "feature/XRAY-123-cool-feature",
1451+
Source: xscServices.CommitContext{
1452+
GitRepoHttpsCloneUrl: "https://git.jfrog.info/projects/XSC/repos/xsc-service",
1453+
BranchName: "feature/XRAY-123-cool-feature",
1454+
},
14511455
}
14521456

14531457
const TestMultiScanId = "3472b4e2-bddc-11ee-a9c9-acde48001122"

http/httpclient/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (jc *HttpClient) Send(method, url string, content []byte, followRedirect, c
135135
if err != nil {
136136
if strings.Contains(err.Error(), "unsupported protocol scheme") {
137137
// Wrong URL, so no need to retry
138-
return false, fmt.Errorf("%w\nThe recieved error indicats an invalid URL: %q, Please ensure the URL includes a valid scheme like 'http://' or 'https://'.", err, url)
138+
return false, fmt.Errorf("%w\nThe received error indicates an invalid URL: %q, Please ensure the URL includes a valid scheme like 'http://' or 'https://'.", err, url)
139139
}
140140
return true, err
141141
}

tests/xscanalyticsevent_test.go

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,53 @@ var finalEvent = services.XscAnalyticsGeneralEvent{XscAnalyticsBasicGeneralEvent
4747

4848
func TestXscAddAndUpdateGeneralEvent(t *testing.T) {
4949
initXscTest(t, services.LogErrorMinXscVersion, utils.MinXrayVersionXscTransitionToXray)
50-
mockServer, analyticsService := createXscMockServerForGeneralEvent(t)
51-
defer mockServer.Close()
5250

53-
msi, err := analyticsService.AddGeneralEvent(initialEvent)
54-
assert.NoError(t, err)
55-
56-
// Validate that the event sent and saved properly in XSC.
57-
resp, err := analyticsService.GetGeneralEvent(msi)
58-
require.NoError(t, err)
59-
assert.Equal(t, initialEvent, *resp)
60-
61-
finalizeEvent := services.XscAnalyticsGeneralEventFinalize{
62-
MultiScanId: msi,
63-
XscAnalyticsBasicGeneralEvent: services.XscAnalyticsBasicGeneralEvent{
64-
EventStatus: services.Completed,
65-
TotalFindings: 10,
66-
TotalIgnoredFindings: 5,
67-
TotalScanDuration: "15s",
51+
testCases := []struct {
52+
name string
53+
xrayVersion string
54+
}{
55+
{
56+
name: "Xray version with deprecated AddGeneralEvent",
57+
xrayVersion: "3.115.0",
58+
},
59+
{
60+
name: "Xray version with new AddGeneralEvent",
61+
xrayVersion: "3.116.0",
6862
},
6963
}
64+
for _, tc := range testCases {
65+
t.Run(tc.name, func(t *testing.T) {
66+
mockServer, analyticsService := createXscMockServerForGeneralEvent(t)
67+
defer mockServer.Close()
7068

71-
err = analyticsService.UpdateGeneralEvent(finalizeEvent)
72-
assert.NoError(t, err)
69+
msi, err := analyticsService.AddGeneralEvent(initialEvent, tc.xrayVersion)
70+
assert.NoError(t, err)
7371

74-
// Validate that the event's update sent and saved properly in XSC.
75-
// We add suffix to the msi to enable the mock server to differentiate between the initial response to the final response
76-
resp, err = analyticsService.GetGeneralEvent(msi + "-final")
77-
assert.NoError(t, err)
78-
assert.Equal(t, finalEvent, *resp)
72+
// Validate that the event sent and saved properly in XSC.
73+
resp, err := analyticsService.GetGeneralEvent(msi)
74+
require.NoError(t, err)
75+
assert.Equal(t, initialEvent, *resp)
76+
77+
finalizeEvent := services.XscAnalyticsGeneralEventFinalize{
78+
MultiScanId: msi,
79+
XscAnalyticsBasicGeneralEvent: services.XscAnalyticsBasicGeneralEvent{
80+
EventStatus: services.Completed,
81+
TotalFindings: 10,
82+
TotalIgnoredFindings: 5,
83+
TotalScanDuration: "15s",
84+
},
85+
}
86+
87+
err = analyticsService.UpdateGeneralEvent(finalizeEvent)
88+
assert.NoError(t, err)
89+
90+
// Validate that the event's update sent and saved properly in XSC.
91+
// We add suffix to the msi to enable the mock server to differentiate between the initial response to the final response
92+
resp, err = analyticsService.GetGeneralEvent(msi + "-final")
93+
assert.NoError(t, err)
94+
assert.Equal(t, finalEvent, *resp)
95+
})
96+
}
7997
}
8098

8199
func createXscMockServerForGeneralEvent(t *testing.T) (mockServer *httptest.Server, analyticsService *services.AnalyticsEventService) {

utils/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import (
2828
const (
2929
Development = "development"
3030
Agent = "jfrog-client-go"
31-
Version = "1.51.0"
31+
Version = "1.51.1"
3232
)
3333

3434
const xrayDevVersion = "3.x-dev"

xray/services/xsc/xsc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ func (xs *XscInnerService) GetVersion() (string, error) {
2424
return versionService.GetVersion()
2525
}
2626

27-
func (xs *XscInnerService) AddAnalyticsGeneralEvent(event services.XscAnalyticsGeneralEvent) (string, error) {
27+
func (xs *XscInnerService) AddAnalyticsGeneralEvent(event services.XscAnalyticsGeneralEvent, xrayVersion string) (string, error) {
2828
eventService := services.NewAnalyticsEventService(xs.client)
2929
eventService.XrayDetails = xs.XrayDetails
30-
return eventService.AddGeneralEvent(event)
30+
return eventService.AddGeneralEvent(event, xrayVersion)
3131
}
3232

3333
func (xs *XscInnerService) SendXscLogErrorRequest(errorLog *services.ExternalErrorLog) error {

xsc/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ func (sm *XscServicesManager) GetVersion() (string, error) {
5050
}
5151

5252
// AddAnalyticsGeneralEvent will send an analytics metrics general event to Xsc and return MSI (multi scan id) generated by Xsc.
53-
func (sm *XscServicesManager) AddAnalyticsGeneralEvent(event services.XscAnalyticsGeneralEvent) (string, error) {
53+
func (sm *XscServicesManager) AddAnalyticsGeneralEvent(event services.XscAnalyticsGeneralEvent, xrayVersion string) (string, error) {
5454
eventService := services.NewAnalyticsEventService(sm.client)
5555
eventService.XscDetails = sm.config.GetServiceDetails()
56-
return eventService.AddGeneralEvent(event)
56+
return eventService.AddGeneralEvent(event, xrayVersion)
5757
}
5858

5959
func (sm *XscServicesManager) SendXscLogErrorRequest(errorLog *services.ExternalErrorLog) error {

xsc/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type XscService interface {
77
// GetVersion will return the Xsc version
88
GetVersion() (string, error)
99
// AddAnalyticsGeneralEvent will send an analytics metrics general event to Xsc and return MSI (multi scan id) generated by Xsc.
10-
AddAnalyticsGeneralEvent(event services.XscAnalyticsGeneralEvent) (string, error)
10+
AddAnalyticsGeneralEvent(event services.XscAnalyticsGeneralEvent, xrayVersion string) (string, error)
1111
// SendXscLogErrorRequest will send an error log to Xsc
1212
SendXscLogErrorRequest(errorLog *services.ExternalErrorLog) error
1313
// UpdateAnalyticsGeneralEvent upon completion of the scan and we have all the results to report on,

xsc/services/analytics.go

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package services
33
import (
44
"encoding/json"
55
"fmt"
6-
"net/http"
7-
86
"github.com/jfrog/jfrog-client-go/auth"
97
"github.com/jfrog/jfrog-client-go/http/jfroghttpclient"
108
"github.com/jfrog/jfrog-client-go/utils"
119
"github.com/jfrog/jfrog-client-go/utils/errorutils"
10+
servicesutils "github.com/jfrog/jfrog-client-go/xsc/services/utils"
1211
xscutils "github.com/jfrog/jfrog-client-go/xsc/services/utils"
12+
"net/http"
1313
)
1414

1515
const (
@@ -66,10 +66,17 @@ func (vs *AnalyticsEventService) sendGetRequest(msi string) (resp *http.Response
6666
}
6767

6868
// AddGeneralEvent add general event in Xsc and returns msi generated by Xsc.
69-
func (vs *AnalyticsEventService) AddGeneralEvent(event XscAnalyticsGeneralEvent) (string, error) {
70-
requestContent, err := json.Marshal(event)
71-
if err != nil {
72-
return "", errorutils.CheckError(err)
69+
func (vs *AnalyticsEventService) AddGeneralEvent(event XscAnalyticsGeneralEvent, xrayVersion string) (string, error) {
70+
var requestContent []byte
71+
if err := utils.ValidateMinimumVersion(utils.Xray, xrayVersion, servicesutils.MinXrayVersionNewGitInfoContext); err != nil {
72+
// use deprecated event struct
73+
if requestContent, err = json.Marshal(convertToDeprecatedEventStruct(event)); err != nil {
74+
return "", errorutils.CheckError(err)
75+
}
76+
} else {
77+
if requestContent, err = json.Marshal(event); err != nil {
78+
return "", errorutils.CheckError(err)
79+
}
7380
}
7481
resp, body, err := vs.sendPostRequest(requestContent)
7582
if err != nil {
@@ -113,24 +120,75 @@ func (vs *AnalyticsEventService) GetGeneralEvent(msi string) (*XscAnalyticsGener
113120
return &response, errorutils.CheckError(err)
114121
}
115122

123+
func convertToDeprecatedEventStruct(event XscAnalyticsGeneralEvent) XscAnalyticsGeneralEventDeprecated {
124+
var deprecatedGitInfo XscGitInfoContextDeprecated
125+
if event.GitInfo != nil {
126+
deprecatedGitInfo = XscGitInfoContextDeprecated{
127+
GitRepoUrl: event.GitInfo.Source.GitRepoHttpsCloneUrl,
128+
GitRepoName: event.GitInfo.Source.GitRepoName,
129+
GitProject: event.GitInfo.Source.GitProject,
130+
BranchName: event.GitInfo.Source.BranchName,
131+
CommitHash: event.GitInfo.Source.CommitHash,
132+
CommitMessage: event.GitInfo.Source.CommitMessage,
133+
CommitAuthor: event.GitInfo.Source.CommitAuthor,
134+
GitProvider: event.GitInfo.GitProvider,
135+
Technologies: event.GitInfo.Technologies,
136+
}
137+
}
138+
return XscAnalyticsGeneralEventDeprecated{
139+
XscAnalyticsBasicGeneralEvent: event.XscAnalyticsBasicGeneralEvent,
140+
GitInfo: &deprecatedGitInfo,
141+
IsGitInfoFlow: event.IsGitInfoFlow,
142+
}
143+
}
144+
116145
// XscAnalyticsGeneralEvent extend the basic struct with Frogbot related info.
146+
type XscAnalyticsGeneralEventDeprecated struct {
147+
XscAnalyticsBasicGeneralEvent
148+
GitInfo *XscGitInfoContextDeprecated `json:"gitinfo,omitempty"`
149+
IsGitInfoFlow bool `json:"is_gitinfo_flow,omitempty"`
150+
}
151+
152+
type XscGitInfoContextDeprecated struct {
153+
GitRepoUrl string `json:"git_repo_url"`
154+
GitRepoName string `json:"git_repo_name,omitempty"`
155+
GitProject string `json:"git_project,omitempty"`
156+
GitProvider string `json:"git_provider,omitempty"`
157+
Technologies []string `json:"technologies,omitempty"`
158+
BranchName string `json:"branch_name"`
159+
LastCommit string `json:"last_commit,omitempty"`
160+
CommitHash string `json:"commit_hash"`
161+
CommitMessage string `json:"commit_message,omitempty"`
162+
CommitAuthor string `json:"commit_author,omitempty"`
163+
}
164+
117165
type XscAnalyticsGeneralEvent struct {
118166
XscAnalyticsBasicGeneralEvent
119167
GitInfo *XscGitInfoContext `json:"gitinfo,omitempty"`
120168
IsGitInfoFlow bool `json:"is_gitinfo_flow,omitempty"`
121169
}
122170

123171
type XscGitInfoContext struct {
124-
GitRepoHttpsCloneUrl string `json:"git_repo_url"`
125-
GitRepoName string `json:"git_repo_name,omitempty"`
126-
GitProject string `json:"git_project,omitempty"`
127-
GitProvider string `json:"git_provider,omitempty"`
128-
Technologies []string `json:"technologies,omitempty"`
129-
BranchName string `json:"branch_name"`
130-
LastCommitUrl string `json:"last_commit,omitempty"`
131-
LastCommitHash string `json:"commit_hash"`
132-
LastCommitMessage string `json:"commit_message,omitempty"`
133-
LastCommitAuthor string `json:"commit_author,omitempty"`
172+
Source CommitContext `json:"source"`
173+
Target *CommitContext `json:"target,omitempty"`
174+
PullRequest *PullRequestContext `json:"pull_request,omitempty"`
175+
GitProvider string `json:"git_provider,omitempty"`
176+
Technologies []string `json:"technologies,omitempty"`
177+
}
178+
179+
type CommitContext struct {
180+
GitRepoHttpsCloneUrl string `json:"git_repo_url"`
181+
GitRepoName string `json:"git_repo_name,omitempty"`
182+
GitProject string `json:"git_project,omitempty"`
183+
BranchName string `json:"branch_name"`
184+
CommitHash string `json:"commit_hash"`
185+
CommitMessage string `json:"commit_message,omitempty"`
186+
CommitAuthor string `json:"commit_author,omitempty"`
187+
}
188+
189+
type PullRequestContext struct {
190+
PullRequestId int `json:"pull_request_id,omitempty"`
191+
PullRequestTitle string `json:"pull_request_title,omitempty"`
134192
}
135193

136194
type XscAnalyticsGeneralEventFinalize struct {

xsc/services/utils/utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
apiV1Suffix = "api/v1"
1515
XscInXraySuffix = apiV1Suffix + xscSuffix
1616
MinXrayVersionXscTransitionToXray = "3.107.13"
17+
MinXrayVersionNewGitInfoContext = "3.117.0"
1718
)
1819

1920
// From Xray version 3.107.13, XSC is transitioning to Xray as inner service. This function will return compatible URL.

0 commit comments

Comments
 (0)