Skip to content

Commit

Permalink
chore: add folder path to analytics event (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawkyZ authored Jun 5, 2024
1 parent e3b5f18 commit fd86f66
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Snyk Security Changelog

## [1.1.56]

### Fixed
- Send current solution folder path when sending Analytics.

## [1.1.55]

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion Snyk.VisualStudio.Extension.Shared/Service/SnykService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public SnykIdeAnalyticsService SnykIdeAnalyticsService
if (ideAnalyticsService == null)
{
ideAnalyticsService =
new SnykIdeAnalyticsService(Package, this, TasksService);
new SnykIdeAnalyticsService(Package, this, TasksService, SolutionService);
}

return ideAnalyticsService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public Attributes(ISnykOptions options)
IntegrationEnvironment = options.IntegrationEnvironment;
IntegrationEnvironmentVersion = options.IntegrationEnvironmentVersion;
}


public string Path { get; set; }

public string DeviceId { get; }

public string Application { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ public class SnykIdeAnalyticsService
private readonly ISnykOptionsProvider optionsProvider;
private readonly ICliProvider cliProvider;
private readonly ISnykScanTopicProvider scanTopicProvider;
private readonly ISolutionService solutionService;

private DateTime codeScanningStarted;
private DateTime ossScanningStarted;

public SnykIdeAnalyticsService(
ISnykOptionsProvider optionsProvider,
ICliProvider cliProvider,
ISnykScanTopicProvider scanTopicProvider
ISnykScanTopicProvider scanTopicProvider,
ISolutionService solutionService
)
{
this.optionsProvider = optionsProvider;
this.cliProvider = cliProvider;
this.scanTopicProvider = scanTopicProvider;
this.solutionService = solutionService;
}

public void Initialize()
Expand All @@ -41,7 +44,7 @@ public void Initialize()
scanTopicProvider.SnykCodeScanningFinished += OnSnykCodeScanningFinished;
}

public string GetAnalyticsPayload(string product, int durationMs, int critical, int high, int medium, int low)
public string GetAnalyticsPayload(string product, int durationMs, int critical, int high, int medium, int low, string directoryPath)
{
ScanDoneEvent e = new()
{
Expand All @@ -51,6 +54,7 @@ public string GetAnalyticsPayload(string product, int durationMs, int critical,
{
DurationMs = durationMs.ToString(),
ScanType = product,
Path = directoryPath,
UniqueIssueCount = new UniqueIssueCount
{
Critical = critical,
Expand Down Expand Up @@ -86,7 +90,8 @@ public async Task OnOssScanningUpdateAsync(object sender, SnykCliScanEventArgs e
low = e.Result.LowSeverityCount;
}

var payload = GetAnalyticsPayload("Snyk Open Source", durationMs, critical, high, medium, low);
var directoryPath = await solutionService.GetSolutionFolderAsync();
var payload = GetAnalyticsPayload("Snyk Open Source", durationMs, critical, high, medium, low, directoryPath);
await cliProvider.Cli.ReportAnalyticsAsync(payload);
}
catch (Exception exception)
Expand Down Expand Up @@ -137,7 +142,8 @@ public async Task OnSnykCodeScanningUpdateAsync(object sender, SnykCodeScanEvent
}
}

var payload = GetAnalyticsPayload("Snyk Code", durationMs, critical, high, medium, low);
var directoryPath = await solutionService.GetSolutionFolderAsync();
var payload = GetAnalyticsPayload("Snyk Code", durationMs, critical, high, medium, low, directoryPath);
await cliProvider.Cli.ReportAnalyticsAsync(payload);
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Moq;
using Snyk.Code.Library.Api.Dto.Analysis;
using Snyk.Code.Library.Domain.Analysis;
using Snyk.Common;
using Snyk.Common.Settings;
using Snyk.VisualStudio.Extension.Shared;
using Snyk.VisualStudio.Extension.Shared.CLI;
Expand Down Expand Up @@ -36,10 +37,13 @@ public void SnykIdeAnalyticsServiceTest_shouldCreateTheCorrectPayloadString()
var optionsMock = new Mock<ISnykOptions>();
var scanTopicMock = new Mock<ISnykScanTopicProvider>();
var cliProviderMock = new Mock<ICliProvider>();
var service = new SnykIdeAnalyticsService(optionsProviderMock.Object, cliProviderMock.Object, scanTopicMock.Object);
var solutionServiceMock = new Mock<ISolutionService>();
const string folderPath = "C:\\Users\\user\\project";

var service = new SnykIdeAnalyticsService(optionsProviderMock.Object, cliProviderMock.Object, scanTopicMock.Object, solutionServiceMock.Object);
SetupOptionsMock(optionsProviderMock, optionsMock);

var payload = service.GetAnalyticsPayload("product", 100, 1, 2, 3, 4);
var payload = service.GetAnalyticsPayload("product", 100, 1, 2, 3, 4, folderPath);

// assert payload json
Assert.Contains("\"device_id\":\"anonymousId\"", payload);
Expand All @@ -56,6 +60,7 @@ public void SnykIdeAnalyticsServiceTest_shouldCreateTheCorrectPayloadString()
Assert.Contains("\"unique_issue_count\":{\"critical\":1,\"high\":2,\"medium\":3,\"low\":4}", payload);
Assert.Contains("\"duration_ms\":\"100\"", payload);
Assert.Contains("\"type\":\"analytics\"", payload);
Assert.Contains("\"path\":\"C:\\\\Users\\\\user\\\\project\"", payload);
}

[Fact]
Expand All @@ -66,9 +71,15 @@ public async Task SnykIdeAnalyticsServiceTest_shouldReportAnalyticsOnSnykCodeSca
var scanTopicMock = new Mock<ISnykScanTopicProvider>();
var cliProviderMock = new Mock<ICliProvider>();
var cliMock = new Mock<ICli>();
var solutionServiceMock = new Mock<ISolutionService>();
const string folderPath = "C:\\Users\\user\\project";

cliProviderMock.SetupGet(cli => cli.Cli).Returns(cliMock.Object);
var service = new SnykIdeAnalyticsService(optionsProviderMock.Object, cliProviderMock.Object, scanTopicMock.Object);
var service = new SnykIdeAnalyticsService(optionsProviderMock.Object, cliProviderMock.Object, scanTopicMock.Object, solutionServiceMock.Object);
SetupOptionsMock(optionsProviderMock, optionsMock);
solutionServiceMock.Setup(solutionService => solutionService.GetSolutionFolderAsync())
.ReturnsAsync(folderPath);

var analysisResult = SetupAnalysisResult();
var e = new SnykCodeScanEventArgs(analysisResult);

Expand All @@ -77,7 +88,8 @@ public async Task SnykIdeAnalyticsServiceTest_shouldReportAnalyticsOnSnykCodeSca
// assert payload is generated (anonymousID called) and cli reportAnalytics is called
optionsMock.VerifyGet(options => options.AnonymousId, Times.Once);
cliProviderMock.Verify(cli => cli.Cli, Times.Once);
cliMock.Verify( c => c.ReportAnalyticsAsync(It.IsAny<string>()), Times.Once);
cliMock.Verify(c => c.ReportAnalyticsAsync(It.IsAny<string>()), Times.Once);
solutionServiceMock.Verify(s => s.GetSolutionFolderAsync(), Times.Once);
}

[Fact]
Expand All @@ -88,9 +100,14 @@ public async Task SnykIdeAnalyticsServiceTest_shouldReportAnalyticsOnOssScanning
var scanTopicMock = new Mock<ISnykScanTopicProvider>();
var cliProviderMock = new Mock<ICliProvider>();
var cliMock = new Mock<ICli>();
var solutionServiceMock = new Mock<ISolutionService>();
const string folderPath = "C:\\Users\\user\\project";

cliProviderMock.SetupGet(cli => cli.Cli).Returns(cliMock.Object);
var service = new SnykIdeAnalyticsService(optionsProviderMock.Object, cliProviderMock.Object, scanTopicMock.Object);
var service = new SnykIdeAnalyticsService(optionsProviderMock.Object, cliProviderMock.Object, scanTopicMock.Object, solutionServiceMock.Object);
SetupOptionsMock(optionsProviderMock, optionsMock);
solutionServiceMock.Setup(solutionService => solutionService.GetSolutionFolderAsync())
.ReturnsAsync(folderPath);
var cliResult = SetupCliResult();

var e = new SnykCliScanEventArgs(cliResult);
Expand All @@ -99,7 +116,8 @@ public async Task SnykIdeAnalyticsServiceTest_shouldReportAnalyticsOnOssScanning
// assert payload is generated (anonymousID called) and cli reportAnalytics is called
optionsMock.VerifyGet(options => options.AnonymousId, Times.Once);
cliProviderMock.Verify(cli => cli.Cli, Times.Once);
cliMock.Verify( c => c.ReportAnalyticsAsync(It.IsAny<string>()), Times.Once);
cliMock.Verify(c => c.ReportAnalyticsAsync(It.IsAny<string>()), Times.Once);
solutionServiceMock.Verify(s => s.GetSolutionFolderAsync(), Times.Once);
}

private static CliResult SetupCliResult()
Expand Down

0 comments on commit fd86f66

Please sign in to comment.