private bool FetchArgumentsAndRulesets(ISonarQubeServer server, ProcessedArgs args, TeamBuildSettings settings, out IDictionary <string, string> serverSettings, out List <AnalyzerSettings> analyzersSettings) { serverSettings = null; analyzersSettings = new List <AnalyzerSettings>(); try { logger.LogInfo(Resources.MSG_FetchingAnalysisConfiguration); // Respect sonar.branch setting if set args.TryGetSetting(SonarProperties.ProjectBranch, out string projectBranch); // Fetch the SonarQube project properties serverSettings = server.GetProperties(args.ProjectKey, projectBranch); // Fetch installed plugins var availableLanguages = server.GetAllLanguages(); foreach (var plugin in plugins) { if (!availableLanguages.Contains(plugin.Language)) { continue; } // Fetch project quality profile if (!server.TryGetQualityProfile(args.ProjectKey, projectBranch, args.Organization, plugin.Language, out string qualityProfile)) { logger.LogDebug(Resources.RAP_NoQualityProfile, plugin.Language, args.ProjectKey); continue; } // Fetch rules (active and not active) var activeRules = server.GetActiveRules(qualityProfile); if (!activeRules.Any()) { logger.LogDebug(Resources.RAP_NoActiveRules, plugin.Language); continue; } var inactiveRules = server.GetInactiveRules(qualityProfile, plugin.Language); // Generate Roslyn analyzers settings and rulesets var analyzerProvider = factory.CreateRoslynAnalyzerProvider(); Debug.Assert(analyzerProvider != null, "Factory should not return null"); // Will be null if the processing of server settings and active rules resulted in an empty ruleset var analyzer = analyzerProvider.SetupAnalyzer(settings, serverSettings, activeRules, inactiveRules, plugin.Language); if (analyzer != null) { analyzersSettings.Add(analyzer); } } } catch (WebException ex) { if (Utilities.HandleHostUrlWebException(ex, args.SonarQubeUrl, logger)) { return(false); } throw; } finally { Utilities.SafeDispose(server); } return(true); }
private async Task <ArgumentsAndRuleSets> FetchArgumentsAndRulesets(ISonarQubeServer server, ProcessedArgs args, TeamBuildSettings settings) { ArgumentsAndRuleSets argumentsAndRuleSets = new ArgumentsAndRuleSets(); try { this.logger.LogInfo(Resources.MSG_FetchingAnalysisConfiguration); // Respect sonar.branch setting if set args.TryGetSetting(SonarProperties.ProjectBranch, out var projectBranch); // Fetch the SonarQube project properties argumentsAndRuleSets.ServerSettings = await server.GetProperties(args.ProjectKey, projectBranch); // Fetch installed plugins var availableLanguages = await server.GetAllLanguages(); foreach (var plugin in plugins) { if (!availableLanguages.Contains(plugin.Language)) { continue; } var qualityProfile = await server.TryGetQualityProfile(args.ProjectKey, projectBranch, args.Organization, plugin.Language); // Fetch project quality profile if (!qualityProfile.Item1) { this.logger.LogDebug(Resources.RAP_NoQualityProfile, plugin.Language, args.ProjectKey); continue; } // Fetch rules (active and not active) var activeRules = await server.GetActiveRules(qualityProfile.Item2); if (!activeRules.Any()) { this.logger.LogDebug(Resources.RAP_NoActiveRules, plugin.Language); } var inactiveRules = await server.GetInactiveRules(qualityProfile.Item2, plugin.Language); // Generate Roslyn analyzers settings and rulesets var analyzerProvider = this.factory.CreateRoslynAnalyzerProvider(); Debug.Assert(analyzerProvider != null, "Factory should not return null"); // Will be null if the processing of server settings and active rules resulted in an empty ruleset // Use the aggregate of local and server properties when generating the analyzer configuration // See bug 699: https://github.com/SonarSource/sonar-scanner-msbuild/issues/699 var serverProperties = new ListPropertiesProvider(argumentsAndRuleSets.ServerSettings); var allProperties = new AggregatePropertiesProvider(args.AggregateProperties, serverProperties); var analyzer = analyzerProvider.SetupAnalyzer(settings, allProperties, activeRules, inactiveRules, plugin.Language); if (analyzer != null) { argumentsAndRuleSets.AnalyzersSettings.Add(analyzer); } } } catch (WebException ex) { if (Utilities.HandleHostUrlWebException(ex, args.SonarQubeUrl, this.logger)) { argumentsAndRuleSets.IsSuccess = false; return(argumentsAndRuleSets); } throw; } finally { Utilities.SafeDispose(server); } argumentsAndRuleSets.IsSuccess = true; return(argumentsAndRuleSets); }
private bool FetchArgumentsAndRulesets(ISonarQubeServer server, ProcessedArgs args, TeamBuildSettings settings, out IDictionary <string, string> serverSettings, out List <AnalyzerSettings> analyzersSettings) { serverSettings = null; analyzersSettings = new List <AnalyzerSettings>(); try { this.logger.LogInfo(Resources.MSG_FetchingAnalysisConfiguration); // Respect sonar.branch setting if set string projectBranch = null; args.TryGetSetting(SonarProperties.ProjectBranch, out projectBranch); // Fetch the SonarQube project properties serverSettings = server.GetProperties(args.ProjectKey, projectBranch); // Fetch installed plugins IEnumerable <string> availableLanguages = server.GetAllLanguages(); foreach (PluginDefinition plugin in plugins) { if (!availableLanguages.Contains(plugin.Language)) { continue; } // Fetch project quality profile string qualityProfile; if (!server.TryGetQualityProfile(args.ProjectKey, projectBranch, args.Organization, plugin.Language, out qualityProfile)) { this.logger.LogDebug(Resources.RAP_NoQualityProfile, plugin.Language, args.ProjectKey); continue; } // Fetch rules (active and not active) IList <ActiveRule> activeRules = server.GetActiveRules(qualityProfile); if (!activeRules.Any()) { this.logger.LogDebug(Resources.RAP_NoActiveRules, plugin.Language); continue; } IList <string> inactiveRules = server.GetInactiveRules(qualityProfile, plugin.Language); // Generate fxcop rulesets this.logger.LogInfo(Resources.MSG_GeneratingRulesets); string fxCopPath = Path.Combine(settings.SonarConfigDirectory, string.Format(FxCopRulesetName, plugin.Language)); if (plugin.Language.Equals(VBNetLanguage)) { GenerateFxCopRuleset("fxcop-vbnet", activeRules, fxCopPath); } else { GenerateFxCopRuleset("fxcop", activeRules, fxCopPath); } // Generate Roslyn analyzers settings and rulesets IAnalyzerProvider analyzerProvider = this.factory.CreateRoslynAnalyzerProvider(this.logger); Debug.Assert(analyzerProvider != null, "Factory should not return null"); // Will be null if the processing of server settings and active rules resulted in an empty ruleset AnalyzerSettings analyzer = analyzerProvider.SetupAnalyzer(settings, serverSettings, activeRules, inactiveRules, plugin.Language); if (analyzer != null) { analyzersSettings.Add(analyzer); } } } catch (WebException ex) { if (Utilities.HandleHostUrlWebException(ex, args.SonarQubeUrl, this.logger)) { return(false); } throw; } finally { Utilities.SafeDispose(server); } return(true); }