From f45e20bf36b5dd984c8460ec464edf41c79153b8 Mon Sep 17 00:00:00 2001 From: Gabriela Trutan Date: Mon, 23 Dec 2024 17:12:16 +0100 Subject: [PATCH] CaYC: cleanup code applied in touched files --- .../BoundSonarQubeProjectExtensionsTests.cs | 3 +- .../Binding/IUnintrusiveBindingController.cs | 9 +- .../BoundSonarQubeProjectExtensions.cs | 2 +- .../AuthenticationHeaderFactoryTests.cs | 4 +- .../Models/ConnectionInformationTests.cs | 16 ++-- .../SonarQubeService_TestBase.cs | 6 +- src/SonarQube.Client/SonarQubeService.cs | 85 ++++++++++++------- 7 files changed, 79 insertions(+), 46 deletions(-) diff --git a/src/ConnectedMode.UnitTests/Persistence/BoundSonarQubeProjectExtensionsTests.cs b/src/ConnectedMode.UnitTests/Persistence/BoundSonarQubeProjectExtensionsTests.cs index 3a3831058..dfcd94f9a 100644 --- a/src/ConnectedMode.UnitTests/Persistence/BoundSonarQubeProjectExtensionsTests.cs +++ b/src/ConnectedMode.UnitTests/Persistence/BoundSonarQubeProjectExtensionsTests.cs @@ -88,7 +88,7 @@ public void BoundSonarQubeProject_CreateConnectionInformation_NoOrganizationNoAu conn.Credentials.Should().BeAssignableTo(); conn.Organization.Should().BeNull(); } - + [TestMethod] public void BoundServerProject_CreateConnectionInformation_ArgCheck() { @@ -109,7 +109,6 @@ public void BoundServerProject_CreateConnectionInformation_NoCredentials() conn.Credentials.Should().BeAssignableTo(); conn.Organization.Key.Should().Be("org_key"); } - [TestMethod] public void BoundServerProject_CreateConnectionInformation_BasicAuthCredentials() diff --git a/src/ConnectedMode/Binding/IUnintrusiveBindingController.cs b/src/ConnectedMode/Binding/IUnintrusiveBindingController.cs index f4df18162..86330146b 100644 --- a/src/ConnectedMode/Binding/IUnintrusiveBindingController.cs +++ b/src/ConnectedMode/Binding/IUnintrusiveBindingController.cs @@ -29,9 +29,10 @@ namespace SonarLint.VisualStudio.ConnectedMode.Binding public interface IBindingController { Task BindAsync(BoundServerProject project, CancellationToken cancellationToken); + bool Unbind(string localBindingKey); } - + internal interface IUnintrusiveBindingController { Task BindAsync(BoundServerProject project, IProgress progress, CancellationToken token); @@ -48,7 +49,11 @@ internal class UnintrusiveBindingController : IUnintrusiveBindingController, IBi private readonly ISolutionBindingRepository solutionBindingRepository; [ImportingConstructor] - public UnintrusiveBindingController(IBindingProcessFactory bindingProcessFactory, ISonarQubeService sonarQubeService, IActiveSolutionChangedHandler activeSolutionChangedHandler, ISolutionBindingRepository solutionBindingRepository) + public UnintrusiveBindingController( + IBindingProcessFactory bindingProcessFactory, + ISonarQubeService sonarQubeService, + IActiveSolutionChangedHandler activeSolutionChangedHandler, + ISolutionBindingRepository solutionBindingRepository) { this.bindingProcessFactory = bindingProcessFactory; this.sonarQubeService = sonarQubeService; diff --git a/src/Core/Binding/BoundSonarQubeProjectExtensions.cs b/src/Core/Binding/BoundSonarQubeProjectExtensions.cs index af90addb7..29af79091 100644 --- a/src/Core/Binding/BoundSonarQubeProjectExtensions.cs +++ b/src/Core/Binding/BoundSonarQubeProjectExtensions.cs @@ -37,7 +37,7 @@ public static ConnectionInformation CreateConnectionInformation(this BoundSonarQ connection.Organization = binding.Organization; return connection; } - + public static ConnectionInformation CreateConnectionInformation(this BoundServerProject binding) { if (binding == null) diff --git a/src/SonarQube.Client.Tests/Helpers/AuthenticationHeaderFactoryTests.cs b/src/SonarQube.Client.Tests/Helpers/AuthenticationHeaderFactoryTests.cs index 81a291977..9653ae7d7 100644 --- a/src/SonarQube.Client.Tests/Helpers/AuthenticationHeaderFactoryTests.cs +++ b/src/SonarQube.Client.Tests/Helpers/AuthenticationHeaderFactoryTests.cs @@ -100,7 +100,9 @@ public void Create_BasicAuth_CredentialsProvided_ReturnsBasicScheme() AssertAreEqualUserNameAndPassword(Username, Password, authenticationHeaderValue.Parameter); } - private void AssertAreEqualUserNameAndPassword(string expectedUser, string expectedPassword, + private void AssertAreEqualUserNameAndPassword( + string expectedUser, + string expectedPassword, string userAndPasswordBase64String) { diff --git a/src/SonarQube.Client.Tests/Models/ConnectionInformationTests.cs b/src/SonarQube.Client.Tests/Models/ConnectionInformationTests.cs index 7d5e4d36e..6f4959a00 100644 --- a/src/SonarQube.Client.Tests/Models/ConnectionInformationTests.cs +++ b/src/SonarQube.Client.Tests/Models/ConnectionInformationTests.cs @@ -73,22 +73,23 @@ public void Ctor_SonarCloudUrl_IsProcessedCorrectly(string inputUrl) [DataRow("http://localhost", "user1", "secret", null)] [DataRow("http://sonarcloud.io", null, null, "myorg")] [DataRow("http://sonarcloud.io", "a token", null, "myorg")] - public void Clone_PropertiesAreCopiedCorrectly(string serverUrl, string userName, string password, string orgKey) + public void Clone_PropertiesAreCopiedCorrectly( + string serverUrl, + string userName, + string password, + string orgKey) { var securePwd = InitializeSecureString(password); var org = InitializeOrganization(orgKey); var credentials = MockBasicAuthCredentials(userName, securePwd); - var testSubject = new ConnectionInformation(new Uri(serverUrl), credentials) - { - Organization = org - }; + var testSubject = new ConnectionInformation(new Uri(serverUrl), credentials) { Organization = org }; var cloneObj = ((ICloneable)testSubject).Clone(); cloneObj.Should().BeOfType(); CheckPropertiesMatch(testSubject, (ConnectionInformation)cloneObj); - _= credentials.Received().Clone(); + _ = credentials.Received().Clone(); } [TestMethod] @@ -108,8 +109,7 @@ private static SecureString InitializeSecureString(string password) => // The "ToSecureString" doesn't expect nulls, which we want to use in the tests password?.ToSecureString(); - private static SonarQubeOrganization InitializeOrganization(string orgKey) => - orgKey == null ? null : new SonarQubeOrganization(orgKey, Guid.NewGuid().ToString()); + private static SonarQubeOrganization InitializeOrganization(string orgKey) => orgKey == null ? null : new SonarQubeOrganization(orgKey, Guid.NewGuid().ToString()); private static void CheckPropertiesMatch(ConnectionInformation item1, ConnectionInformation item2) { diff --git a/src/SonarQube.Client.Tests/SonarQubeService_TestBase.cs b/src/SonarQube.Client.Tests/SonarQubeService_TestBase.cs index bf63208f8..72a5e0230 100644 --- a/src/SonarQube.Client.Tests/SonarQubeService_TestBase.cs +++ b/src/SonarQube.Client.Tests/SonarQubeService_TestBase.cs @@ -66,7 +66,11 @@ public void TestInitialize() ResetService(); } - protected void SetupRequest(string relativePath, string response, HttpStatusCode statusCode = HttpStatusCode.OK, string serverUrl = DefaultBasePath) => + protected void SetupRequest( + string relativePath, + string response, + HttpStatusCode statusCode = HttpStatusCode.OK, + string serverUrl = DefaultBasePath) => MocksHelper.SetupHttpRequest(messageHandler, relativePath, response, statusCode, serverUrl); protected void SetupRequest(string relativePath, HttpResponseMessage response, params MediaTypeHeaderValue[] expectedHeaderValues) => diff --git a/src/SonarQube.Client/SonarQubeService.cs b/src/SonarQube.Client/SonarQubeService.cs index 63a3f7276..eeb48a4d4 100644 --- a/src/SonarQube.Client/SonarQubeService.cs +++ b/src/SonarQube.Client/SonarQubeService.cs @@ -67,7 +67,10 @@ public SonarQubeService(HttpMessageHandler messageHandler, string userAgent, ILo { } - internal /* for testing */ SonarQubeService(HttpMessageHandler messageHandler, string userAgent, ILogger logger, + internal /* for testing */ SonarQubeService( + HttpMessageHandler messageHandler, + string userAgent, + ILogger logger, IRequestFactorySelector requestFactorySelector, ISecondaryIssueHashUpdater secondaryIssueHashUpdater, ISSEStreamReaderFactory sseStreamReaderFactory) @@ -110,7 +113,8 @@ private Task InvokeCheckedRequestAsync(Cancellat /// Action that configures a type instance that implements TRequest. /// Cancellation token. /// Returns the result of the request invocation. - private Task InvokeCheckedRequestAsync(Action configure, + private Task InvokeCheckedRequestAsync( + Action configure, CancellationToken token) where TRequest : IRequest { @@ -141,11 +145,7 @@ public async Task ConnectAsync(ConnectionInformation connection, CancellationTok httpClient = new HttpClient(messageHandler) { - BaseAddress = connection.ServerUri, - DefaultRequestHeaders = - { - Authorization = AuthenticationHeaderFactory.Create(connection.Credentials), - }, + BaseAddress = connection.ServerUri, DefaultRequestHeaders = { Authorization = AuthenticationHeaderFactory.Create(connection.Credentials), }, }; httpClient.DefaultRequestHeaders.Add("User-Agent", userAgent); @@ -214,8 +214,7 @@ await InvokeCheckedRequestAsync> GetAllLanguagesAsync(CancellationToken token) => - await InvokeCheckedRequestAsync(token); + public async Task> GetAllLanguagesAsync(CancellationToken token) => await InvokeCheckedRequestAsync(token); public async Task DownloadStaticFileAsync(string pluginKey, string fileName, CancellationToken token) => await InvokeCheckedRequestAsync( @@ -226,8 +225,7 @@ await InvokeCheckedRequestAsync( }, token); - public async Task> GetAllPluginsAsync(CancellationToken token) => - await InvokeCheckedRequestAsync(token); + public async Task> GetAllPluginsAsync(CancellationToken token) => await InvokeCheckedRequestAsync(token); public async Task> GetAllProjectsAsync(string organizationKey, CancellationToken token) => await InvokeCheckedRequestAsync( @@ -268,7 +266,11 @@ public async Task> GetAllQualityProfilesAsync(str token); } - public async Task GetQualityProfileAsync(string projectKey, string organizationKey, SonarQubeLanguage language, CancellationToken token) + public async Task GetQualityProfileAsync( + string projectKey, + string organizationKey, + SonarQubeLanguage language, + CancellationToken token) { var qualityProfiles = await InvokeCheckedRequestAsync( request => @@ -309,8 +311,11 @@ public async Task GetQualityProfileAsync(string project qualityProfile.IsDefault, updatedDate); } - public async Task GetRoslynExportProfileAsync(string qualityProfileName, - string organizationKey, SonarQubeLanguage language, CancellationToken token) => + public async Task GetRoslynExportProfileAsync( + string qualityProfileName, + string organizationKey, + SonarQubeLanguage language, + CancellationToken token) => await InvokeCheckedRequestAsync( request => { @@ -320,8 +325,11 @@ await InvokeCheckedRequestAsync> GetSuppressedIssuesAsync(string projectKey, string branch, - string[] issueKeys, CancellationToken token) => + public async Task> GetSuppressedIssuesAsync( + string projectKey, + string branch, + string[] issueKeys, + CancellationToken token) => await InvokeCheckedRequestAsync( request => { @@ -332,7 +340,11 @@ await InvokeCheckedRequestAsync( }, token); - public async Task> GetIssuesForComponentAsync(string projectKey, string branch, string componentKey, string ruleId, + public async Task> GetIssuesForComponentAsync( + string projectKey, + string branch, + string componentKey, + string ruleId, CancellationToken token) { return await InvokeCheckedRequestAsync( @@ -346,7 +358,9 @@ public async Task> GetIssuesForComponentAsync(string proje token); } - public async Task> GetNotificationEventsAsync(string projectKey, DateTimeOffset eventsSince, + public async Task> GetNotificationEventsAsync( + string projectKey, + DateTimeOffset eventsSince, CancellationToken token) => await InvokeCheckedRequestAsync( request => @@ -365,17 +379,21 @@ await InvokeCheckedRequestAsync( }, token); - public async Task> SearchFilesByNameAsync(string projectKey, string branch, string fileName, CancellationToken token) + public async Task> SearchFilesByNameAsync( + string projectKey, + string branch, + string fileName, + CancellationToken token) { return await InvokeCheckedRequestAsync( - request => - { - request.ProjectKey = projectKey; - request.BranchName = branch; - request.FileName = fileName; - }, - token - ); + request => + { + request.ProjectKey = projectKey; + request.BranchName = branch; + request.FileName = fileName; + }, + token + ); } public async Task> GetRulesAsync(bool isActive, string qualityProfileKey, CancellationToken token) => @@ -385,7 +403,7 @@ await InvokeCheckedRequestAsync( request.IsActive = isActive; request.QualityProfileKey = qualityProfileKey; }, - token); + token); public async Task GetRuleByKeyAsync(string ruleKey, string qualityProfileKey, CancellationToken token) { @@ -395,7 +413,7 @@ public async Task GetRuleByKeyAsync(string ruleKey, string qualit request.RuleKey = ruleKey; request.QualityProfileKey = qualityProfileKey; }, - token); + token); Debug.Assert(rules.Length <= 1); return rules.FirstOrDefault(); @@ -409,7 +427,8 @@ await InvokeCheckedRequestAsync( }, token); - public async Task> SearchHotspotsAsync(string projectKey, string branch, CancellationToken token) => await InvokeCheckedRequestAsync( + public async Task> SearchHotspotsAsync(string projectKey, string branch, CancellationToken token) => + await InvokeCheckedRequestAsync( request => { request.BranchKey = branch; @@ -417,7 +436,11 @@ public async Task> SearchHotspotsAsync(string proj }, token); - public async Task TransitionIssueAsync(string issueKey, SonarQubeIssueTransition transition, string optionalComment, CancellationToken token) + public async Task TransitionIssueAsync( + string issueKey, + SonarQubeIssueTransition transition, + string optionalComment, + CancellationToken token) { var transitionResult = await InvokeCheckedRequestAsync( request =>