Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
28 changes: 28 additions & 0 deletions pkg/analytics/instrumentation_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/snyk/go-application-framework/pkg/logging"
"os/user"
"strings"
"time"

"github.com/rs/zerolog"
Expand Down Expand Up @@ -38,6 +39,8 @@ type InstrumentationCollector interface {
SetStatus(s Status)
SetTestSummary(s json_schemas.TestSummary)
SetTargetId(t string) // maybe use package-url library and types
SetProjectIds(projectIds []string)
SetMonitorIds(monitorIds []string)
AddError(err error)
AddExtension(key string, value interface{})
}
Expand All @@ -62,6 +65,8 @@ type instrumentationCollectorImpl struct {
status Status
testSummary json_schemas.TestSummary
targetId string
projectIds []string
monitorIds []string
instrumentationErr []error
extension map[string]interface{}
}
Expand Down Expand Up @@ -129,6 +134,14 @@ func (ic *instrumentationCollectorImpl) SetTargetId(t string) {
ic.targetId = t
}

func (ic *instrumentationCollectorImpl) SetProjectIds(ids []string) {
ic.projectIds = ids
}

func (ic *instrumentationCollectorImpl) SetMonitorIds(ids []string) {
ic.monitorIds = ids
}

func (ic *instrumentationCollectorImpl) AddError(err error) {
ic.instrumentationErr = append(ic.instrumentationErr, err)
}
Expand Down Expand Up @@ -219,6 +232,21 @@ func (ic *instrumentationCollectorImpl) getV2Attributes() api.AnalyticsAttribute

func (ic *instrumentationCollectorImpl) getV2Interaction() api.Interaction {
stage := toInteractionStage(ic.stage)

if len(ic.projectIds) > 0 {
if ic.extension == nil {
ic.extension = make(map[string]interface{})
}
ic.extension["project_ids"] = strings.Join(ic.projectIds, ",")
}

if len(ic.monitorIds) > 0 {
if ic.extension == nil {
ic.extension = make(map[string]interface{})
}
ic.extension["monitor_ids"] = strings.Join(ic.monitorIds, ",")
}

return api.Interaction{
Categories: &ic.category,
Errors: toInteractionErrors(ic.instrumentationErr),
Expand Down
30 changes: 30 additions & 0 deletions pkg/analytics/instrumentation_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,36 @@ func Test_InstrumentationCollector(t *testing.T) {
actualCategory := ic.GetCategory()
assert.Equal(t, mockCategory, actualCategory)
})

t.Run("it should add project id to extension", func(t *testing.T) {
ic := setupBaseCollector(t)
expectedV2InstrumentationObject := buildExpectedBaseObject(t)

mockProjectId := "123e4567-e89b-12d3-a456-426614174000"
mockMonitorId := "89674498-4fcc-4af3-9a80-0582a74614d5"
mockProjectIds := []string{mockProjectId}
mockMonitorIds := []string{mockMonitorId}
ic.SetProjectIds(mockProjectIds)
ic.SetMonitorIds(mockMonitorIds)

mockExtension := map[string]interface{}{
"strings": "hello world",
"project_ids": mockProjectId,
"monitor_ids": mockMonitorId,
}

expectedV2InstrumentationObject.Data.Attributes.Interaction.Extension = &mockExtension

actualV2InstrumentationObject, err := GetV2InstrumentationObject(ic)
assert.NoError(t, err)

expectedV2InstrumentationJson, err := json.Marshal(expectedV2InstrumentationObject)
assert.NoError(t, err)
actualV2InstrumentationJson, err := json.Marshal(actualV2InstrumentationObject)
assert.NoError(t, err)

assert.JSONEq(t, string(expectedV2InstrumentationJson), string(actualV2InstrumentationJson))
})
}

func setupBaseCollector(t *testing.T) InstrumentationCollector {
Expand Down
25 changes: 25 additions & 0 deletions pkg/instrumentation/get-project-id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package instrumentation

import (
"regexp"
)

func GetProjectIdAndMonitorIdFromText(text string) ([][2]string, error) {
// Pattern: /org/{org-slug}/project/{project-uuid}/history/{history-uuid}
re := regexp.MustCompile(`/org/[^/]+/project/([0-9a-fA-F-]{36})/history/([0-9a-fA-F-]{36})`)
matches := re.FindAllStringSubmatch(text, -1)

if len(matches) == 0 {
return nil, nil
}

result := make([][2]string, 0, len(matches))
for _, m := range matches {
if len(m) < 3 {
continue
}
result = append(result, [2]string{m[1], m[2]})
}

return result, nil
}
Loading