Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: settings sections #335

Merged
merged 16 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ private void OkButton_OnClick(object sender, RoutedEventArgs e)
var currentList = folderConfigList.Where(x => x.FolderPath != FolderConfig.FolderPath).ToList();
currentList.Add(FolderConfig);

SnykVSPackage.ServiceProvider.Options.FolderConfigs = currentList;

var options = SnykVSPackage.ServiceProvider.Options;
options.FolderConfigs = currentList;
SnykVSPackage.ServiceProvider.SnykOptionsManager.Save(options);
this.CloseDialog();
}

Expand Down
2 changes: 1 addition & 1 deletion Snyk.VisualStudio.Extension.2022/Language/LsSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public SnykLsInitializationOptions GetInitializationOptions()
},
ScanningMode = options.AutoScan ? "auto" : "manual",
#pragma warning disable VSTHRD104
AdditionalParams = ThreadHelper.JoinableTaskFactory.Run(() => options.GetAdditionalOptionsAsync()),
AdditionalParams = ThreadHelper.JoinableTaskFactory.Run(() => this.serviceProvider.SnykOptionsManager.GetAdditionalOptionsAsync()),
#pragma warning restore VSTHRD104
AuthenticationMethod = options.AuthenticationMethod == AuthenticationType.OAuth ? "oauth" : "token",
CliPath = SnykCli.GetCliFilePath(options.CliCustomPath),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ public object GetInitializationOptions()
public async Task<Connection> ActivateAsync(CancellationToken token)
{
await Task.Yield();
if (SnykVSPackage.ServiceProvider?.Options == null)
var serviceProvider = SnykVSPackage.ServiceProvider;
if (serviceProvider?.Options == null)
{
Logger.Error("Could not activate Language Server because ServiceProvider is null. Is the extension initialized?");
return null;
}
var options = SnykVSPackage.ServiceProvider.Options;
var options = serviceProvider.Options;
// ReSharper disable once RedundantAssignment
var lsDebugLevel = await GetLsDebugLevelAsync(options);
var lsDebugLevel = await GetLsDebugLevelAsync(serviceProvider.SnykOptionsManager);
#if DEBUG
lsDebugLevel = "debug";
#endif
Expand Down Expand Up @@ -221,10 +222,10 @@ public Task OnServerInitializedAsync()
return Task.CompletedTask;
}

private async Task<string> GetLsDebugLevelAsync(ISnykOptions options)
private async Task<string> GetLsDebugLevelAsync(ISnykOptionsManager optionsManger)
{
var logLevel = "info";
var additionalCliParameters = await options.GetAdditionalOptionsAsync();
var additionalCliParameters = await optionsManger.GetAdditionalOptionsAsync();
if (!string.IsNullOrEmpty(additionalCliParameters) && (additionalCliParameters.Contains("-d") || additionalCliParameters.Contains("--debug")))
{
logLevel = "debug";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualStudio.Shell;
using Newtonsoft.Json.Linq;
using Snyk.VisualStudio.Extension.Authentication;
Expand Down Expand Up @@ -109,6 +110,7 @@ public async Task OnFolderConfig(JToken arg)
var folderConfigs = arg.TryParse<FolderConfigsParam>();
if (folderConfigs == null) return;
serviceProvider.Options.FolderConfigs = folderConfigs.FolderConfigs;
serviceProvider.SnykOptionsManager.Save(serviceProvider.Options);
}

[JsonRpcMethod(LsConstants.SnykHasAuthenticated)]
Expand All @@ -121,10 +123,6 @@ public async Task OnHasAuthenticated(JToken arg)
}

var token = arg["token"].ToString();
if (string.IsNullOrEmpty(token))
{
return;
}

var apiUrl = arg["apiUrl"]?.ToString();
if (!string.IsNullOrEmpty(apiUrl))
Expand All @@ -133,10 +131,14 @@ public async Task OnHasAuthenticated(JToken arg)
}

serviceProvider.Options.ApiToken = new AuthenticationToken(serviceProvider.Options.AuthenticationMethod, token);
serviceProvider.SnykOptionsManager.Save(serviceProvider.Options);

await serviceProvider.GeneralOptionsDialogPage.HandleAuthenticationSuccess(token, apiUrl);
serviceProvider.FeatureFlagService.RefreshAsync(SnykVSPackage.Instance.DisposalToken).FireAndForget();

if (!serviceProvider.Options.ApiToken.IsValid())
return;

serviceProvider.FeatureFlagService.RefreshAsync(SnykVSPackage.Instance.DisposalToken).FireAndForget();
if (serviceProvider.Options.AutoScan)
{
await serviceProvider.TasksService.ScanAsync();
Expand All @@ -150,7 +152,7 @@ public async Task OnAddTrustedFolders(JToken arg)
if (trustedFolders == null) return;

serviceProvider.Options.TrustedFolders = new HashSet<string>(trustedFolders.TrustedFolders);
this.serviceProvider.UserStorageSettingsService?.SaveSettings();
this.serviceProvider.SnykOptionsManager.Save(serviceProvider.Options);
await serviceProvider.LanguageClientManager.DidChangeConfigurationAsync(SnykVSPackage.Instance.DisposalToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public interface ISnykServiceProvider
/// </summary>
ISnykOptions Options { get; }
ISnykGeneralOptionsDialogPage GeneralOptionsDialogPage { get; }

ISnykOptionsManager SnykOptionsManager { get; }
/// <summary>
/// Gets Visual Studio Settiings Manager instance.
/// </summary>
Expand All @@ -58,11 +58,6 @@ public interface ISnykServiceProvider
/// </summary>
SnykVsThemeService VsThemeService { get; }

/// <summary>
/// Gets user storage settings service instance.
/// </summary>
IUserStorageSettingsService UserStorageSettingsService { get; }

/// <summary>
/// Gets <see cref="SnykToolWindowControl"/> instance.
/// </summary>
Expand Down
17 changes: 6 additions & 11 deletions Snyk.VisualStudio.Extension.2022/Service/SnykService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Threading.Tasks;
using EnvDTE;
using EnvDTE80;
using Microsoft;
using Microsoft.VisualStudio.ComponentModelHost;
using Microsoft.VisualStudio.Settings;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Settings;
Expand Down Expand Up @@ -36,7 +34,7 @@ public class SnykService : ISnykServiceProvider, ISnykService

private DTE2 dte;

private SnykUserStorageSettingsService userStorageSettingsService;
private SnykOptionsManager snykOptionsManager;
private SnykFeatureFlagService featureFlagService;

private IWorkspaceTrustService workspaceTrustService;
Expand Down Expand Up @@ -101,21 +99,18 @@ public SnykService(IAsyncServiceProvider serviceProvider, string vsVersion = "")
/// </summary>
public SnykVsThemeService VsThemeService => this.vsThemeService;

/// <summary>
/// Gets user storage settings service instance.
/// </summary>
public IUserStorageSettingsService UserStorageSettingsService
public ISnykOptionsManager SnykOptionsManager
{
get
{
if (this.userStorageSettingsService == null)
if (this.snykOptionsManager == null)
{
string settingsFilePath = Path.Combine(SnykExtension.GetExtensionDirectoryPath(), "settings.json");

this.userStorageSettingsService = new SnykUserStorageSettingsService(settingsFilePath, this);
this.snykOptionsManager = new SnykOptionsManager(settingsFilePath, this);
}

return this.userStorageSettingsService;
return this.snykOptionsManager;
}
}

Expand Down Expand Up @@ -167,7 +162,7 @@ public async Task InitializeAsync(CancellationToken cancellationToken)
await SnykSolutionService.Instance.InitializeAsync(this);

this.tasksService = SnykTasksService.Instance;
this.workspaceTrustService = new WorkspaceTrustService(this.UserStorageSettingsService);
this.workspaceTrustService = new WorkspaceTrustService(this);

NotificationService.Initialize(this);
VsStatusBar.Initialize(this);
Expand Down
16 changes: 7 additions & 9 deletions Snyk.VisualStudio.Extension.2022/Service/SnykTasksService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,7 @@ private void CancelTask(CancellationTokenSource tokenSource)

public bool ShouldDownloadCli()
{
var userSettingsStorageService = this.serviceProvider.UserStorageSettingsService;
if (!userSettingsStorageService.BinariesAutoUpdate)
if (!this.serviceProvider.Options.BinariesAutoUpdate)
{
return false;
}
Expand All @@ -751,8 +750,8 @@ public bool ShouldDownloadCli()
private async Task DownloadAsync(CliDownloadFinishedCallback downloadFinishedCallback,
ISnykProgressWorker progressWorker)
{
var userSettingsStorageService = this.serviceProvider.UserStorageSettingsService;
if (!userSettingsStorageService.BinariesAutoUpdate)
var options = this.serviceProvider.Options;
if (!options.BinariesAutoUpdate)
{
Logger.Information("CLI auto-update is disabled, CLI download is skipped.");
this.DownloadCancelled?.Invoke(this, new SnykCliDownloadEventArgs());
Expand All @@ -762,8 +761,7 @@ private async Task DownloadAsync(CliDownloadFinishedCallback downloadFinishedCal
this.isCliDownloading = true;
try
{
var serviceProviderOptions = this.serviceProvider.Options;
var cliDownloader = new SnykCliDownloader(serviceProviderOptions);
var cliDownloader = new SnykCliDownloader(this.serviceProvider.Options);

var downloadFinishedCallbacks = new List<CliDownloadFinishedCallback>();

Expand All @@ -774,12 +772,12 @@ private async Task DownloadAsync(CliDownloadFinishedCallback downloadFinishedCal

downloadFinishedCallbacks.Add(() =>
{
serviceProviderOptions.CurrentCliVersion = cliDownloader.GetLatestReleaseInfo().Name;
userSettingsStorageService.SaveSettings();
this.serviceProvider.Options.CurrentCliVersion = cliDownloader.GetLatestReleaseInfo().Name;
this.serviceProvider.SnykOptionsManager.Save(this.serviceProvider.Options);
DisposeCancellationTokenSource(this.downloadCliTokenSource);
});

var downloadPath = serviceProviderOptions.CliCustomPath;
var downloadPath = this.serviceProvider.Options.CliCustomPath;
await cliDownloader.AutoUpdateCliAsync(
progressWorker,
downloadPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public class WorkspaceTrustService : IWorkspaceTrustService
{
private static readonly ILogger Logger = LogManager.ForContext<WorkspaceTrustService>();

private readonly IUserStorageSettingsService settingsService;
private readonly ISnykServiceProvider serviceProvider;

public WorkspaceTrustService(IUserStorageSettingsService settingsService)
public WorkspaceTrustService(ISnykServiceProvider serviceProvider)
{
this.settingsService = settingsService;
this.serviceProvider = serviceProvider;
}

public void AddFolderToTrusted(string absoluteFolderPath)
Expand All @@ -30,10 +30,10 @@ public void AddFolderToTrusted(string absoluteFolderPath)

try
{
var trustedFolders = this.settingsService.TrustedFolders;
var trustedFolders = this.serviceProvider.Options.TrustedFolders;
trustedFolders.Add(absoluteFolderPath);
this.settingsService.TrustedFolders = trustedFolders;
this.settingsService.SaveSettings();
this.serviceProvider.Options.TrustedFolders = trustedFolders;
this.serviceProvider.SnykOptionsManager.Save(this.serviceProvider.Options);
}
catch (Exception e)
{
Expand All @@ -45,7 +45,7 @@ public bool IsFolderTrusted(string absoluteFolderPath)
{
if (string.IsNullOrEmpty(absoluteFolderPath))
return true;
var trustedFolders = this.settingsService.TrustedFolders;
var trustedFolders = this.serviceProvider.Options.TrustedFolders;

foreach (var trustedFolder in trustedFolders)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,74 +1,78 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Snyk.VisualStudio.Extension.Authentication;
using Snyk.VisualStudio.Extension.Language;

namespace Snyk.VisualStudio.Extension.Settings;

public interface IUserStorageSettingsService
public interface IPersistableOptions
{
void SaveSettings();
bool BinariesAutoUpdate { get; set; }
string CliCustomPath { get; set; }
AuthenticationType AuthenticationMethod { get; set; }
string DeviceId { get; set; }
bool AutoScan { get; set; }

bool OpenIssuesEnabled { get; set; }
bool IgnoredIssuesEnabled { get; set; }

/// <summary>
/// Gets or sets trusted folders list.
/// Gets or sets a value indicating whether Snyk user API token.
/// </summary>
ISet<string> TrustedFolders { get; set; }
AuthenticationToken ApiToken { get; set; }

/// <summary>
/// Get Auto Scan option
/// Gets Value of Authentication Token Type.
/// </summary>
/// <returns>bool.</returns>
bool AutoScan { get; set; }
AuthenticationType AuthenticationMethod { get; set; }

/// <summary>
/// Gets or sets a value indicating whether CLI custom endpoint parameter.
/// </summary>
string CustomEndpoint { get; set; }

/// <summary>
/// Get Or Set Auth Token
/// Gets a value indicating whether Snyk Code settings URL.
/// </summary>
/// <returns>string.</returns>
string Token { get; set; }
string SnykCodeSettingsUrl { get; }

string CliReleaseChannel { get; set; }
string CliDownloadUrl { get; set; }
bool IgnoreUnknownCa { get; set; }
/// <summary>
/// Gets or sets a value indicating whether CLI organization parameter.
/// </summary>
string Organization { get; set; }
string CustomEndpoint { get; set; }
bool SnykCodeSecurityEnabled { get; set; }
bool SnykCodeQualityEnabled { get; set; }

/// <summary>
/// Gets or sets a value indicating whether CLI ignore unknown CA parameter.
/// </summary>
bool IgnoreUnknownCA { get; set; }

/// <summary>
/// Gets a value indicating whether is Oss scan enabled.
/// </summary>
bool OssEnabled { get; set; }
bool IacEnabled { get; set; }
string CurrentCliVersion { get; set; }
string DeviceId { get; set; }
bool AnalyticsPluginInstalledSent { get; set; }
bool OpenIssuesEnabled { get; set; }
bool IgnoredIssuesEnabled { get; set; }
List<FolderConfig> FolderConfigs { get; set; }
bool EnableDeltaFindings { get; set; }

/// <summary>
/// Get is all projects enabled.
/// Gets a value indicating whether is Oss scan enabled.
/// </summary>
/// <returns>Bool.</returns>
Task<bool> GetIsAllProjectsEnabledAsync();
bool SnykCodeSecurityEnabled { get; set; }

/// <summary>
/// Get CLI additional options string.
/// Gets a value indicating whether is Oss scan enabled.
/// </summary>
/// <returns>string.</returns>
Task<string> GetAdditionalOptionsAsync();
bool SnykCodeQualityEnabled { get; set; }

/// <summary>
/// Save additional options string.
/// Gets or sets a value indicating whether the CLI should be automatically updated.
/// </summary>
/// <param name="additionalOptions">CLI options string.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task SaveAdditionalOptionsAsync(string additionalOptions);
bool BinariesAutoUpdate { get; set; }

/// <summary>
/// Sace is all projects scan enabled.
/// Gets or sets the value of the CLI custom path. If empty, the default path from AppData would be used.
/// </summary>
/// <param name="isAllProjectsEnabled">Bool param.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task SaveIsAllProjectsScanEnabledAsync(bool isAllProjectsEnabled);
string CliCustomPath { get; set; }
string CliReleaseChannel { get; set; }
string CliDownloadUrl { get; set; }
ISet<string> TrustedFolders { get; set; }

bool EnableDeltaFindings { get; set; }
List<FolderConfig> FolderConfigs { get; set; }
string CurrentCliVersion { get; set; }
bool AnalyticsPluginInstalledSent { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Snyk.VisualStudio.Extension.Service;

namespace Snyk.VisualStudio.Extension.Settings;

public interface ISnykCliOptionsDialogPage
{
void Initialize(ISnykServiceProvider provider);
}
Loading
Loading