コード例 #1
0
        /// <summary>
        /// Retrieves settings from the SonarQube server and generates a an FxCop file on disc
        /// </summary>
        /// <param name="server">SonarQube server instance</param>
        /// <param name="requiredPluginKey">The plugin key that defines the given language</param>
        /// <param name="language">The language of the FxCop repository</param>
        /// <param name="fxCopRepositoryKey">The key of the FxCop repository</param>
        /// <param name="sonarProjectKey">The key of the SonarQube project for which the ruleset should be generated</param>
        /// <param name="sonarProjectBranch">The branch of the SonarQube project for which the ruleset should be generated (optional).</param>
        /// <param name="outputFilePath">The full path to the file to be generated</param>
        public static void Generate(ISonarQubeServer server, string requiredPluginKey, string language, string fxCopRepositoryKey, string sonarProjectKey, string sonarProjectBranch, string outputFilePath)
        {
            if (server == null)
            {
                throw new ArgumentNullException("server");
            }
            if (string.IsNullOrWhiteSpace(requiredPluginKey))
            {
                throw new ArgumentNullException("requiredPluginKey");
            }
            if (string.IsNullOrWhiteSpace(language))
            {
                throw new ArgumentNullException("language");
            }
            if (string.IsNullOrWhiteSpace(fxCopRepositoryKey))
            {
                throw new ArgumentNullException("fxCopRepositoryKey");
            }
            if (string.IsNullOrWhiteSpace(sonarProjectKey))
            {
                throw new ArgumentNullException("sonarProjectKey");
            }
            if (string.IsNullOrWhiteSpace(outputFilePath))
            {
                throw new ArgumentNullException("outputFilePath");
            }

            IEnumerable <string> activeRuleKeys = Enumerable.Empty <string>();

            if (server.GetInstalledPlugins().Contains(requiredPluginKey))
            {
                string qualityProfile;
                if (server.TryGetQualityProfile(sonarProjectKey, sonarProjectBranch, language, out qualityProfile))
                {
                    activeRuleKeys = server.GetActiveRuleKeys(qualityProfile, language, fxCopRepositoryKey);
                }
            }

            if (activeRuleKeys.Any())
            {
                var internalKeys = server.GetInternalKeys(fxCopRepositoryKey);
                var ids          = activeRuleKeys.Select(
                    k =>
                {
                    var fullKey = fxCopRepositoryKey + ':' + k;
                    return(internalKeys.ContainsKey(fullKey) ? internalKeys[fullKey] : k);
                });

                File.WriteAllText(outputFilePath, RulesetWriter.ToString(ids));
            }
            else
            {
                File.Delete(outputFilePath);
            }
        }
コード例 #2
0
        /// <summary>
        /// Retrieves settings from the SonarQube server and generates a an FxCop file on disc
        /// </summary>
        /// <param name="server">SonarQube server instance</param>
        /// <param name="requiredPluginKey">The plugin key that defines the given language</param>
        /// <param name="language">The language of the FxCop repository</param>
        /// <param name="fxCopRepositoryKey">The key of the FxCop repository</param>
        /// <param name="sonarProjectKey">The key of the SonarQube project for which the ruleset should be generated</param>
        /// <param name="outputFilePath">The full path to the file to be generated</param>
        public static void Generate(ISonarQubeServer server, string requiredPluginKey, string language, string fxCopRepositoryKey, string sonarProjectKey, string outputFilePath)
        {
            if (server == null)
            {
                throw new ArgumentNullException("server");
            }
            if (string.IsNullOrWhiteSpace(requiredPluginKey))
            {
                throw new ArgumentNullException("requiredPluginKey");
            }
            if (string.IsNullOrWhiteSpace(language))
            {
                throw new ArgumentNullException("language");
            }
            if (string.IsNullOrWhiteSpace(fxCopRepositoryKey))
            {
                throw new ArgumentNullException("fxCopRepositoryKey");
            }
            if (string.IsNullOrWhiteSpace(sonarProjectKey))
            {
                throw new ArgumentNullException("sonarProjectKey");
            }
            if (string.IsNullOrWhiteSpace(outputFilePath))
            {
                throw new ArgumentNullException("outputFilePath");
            }

            IEnumerable<string> activeRuleKeys = Enumerable.Empty<string>();
            if (server.GetInstalledPlugins().Contains(requiredPluginKey))
            {
                string qualityProfile;
                if (server.TryGetQualityProfile(sonarProjectKey, language, out qualityProfile))
                {
                    activeRuleKeys = server.GetActiveRuleKeys(qualityProfile, language, fxCopRepositoryKey);
                }
            }

            if (activeRuleKeys.Any())
            {
                var internalKeys = server.GetInternalKeys(fxCopRepositoryKey);
                var ids = activeRuleKeys.Select(
                    k =>
                    {
                        var fullKey = fxCopRepositoryKey + ':' + k;
                        return internalKeys.ContainsKey(fullKey) ? internalKeys[fullKey] : k;
                    });

                File.WriteAllText(outputFilePath, RulesetWriter.ToString(ids));
            }
            else
            {
                File.Delete(outputFilePath);
            }
        }
コード例 #3
0
 private static bool IsCppPluginInstalled(ISonarQubeServer server)
 {
     return server.GetInstalledPlugins().Contains(CppPluginKey);
 }
コード例 #4
0
        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> installedPlugins = server.GetInstalledPlugins();

                foreach (PluginDefinition plugin in plugins)
                {
                    if (!installedPlugins.Contains(plugin.PluginKey))
                    {
                        continue;
                    }

                    // Fetch project quality profile
                    string qualityProfile;
                    if (!server.TryGetQualityProfile(args.ProjectKey, projectBranch, plugin.Language, out qualityProfile))
                    {
                        this.logger.LogDebug(Resources.RAP_NoQualityProfile, plugin.Language, args.ProjectKey);
                        continue;
                    }

                    // Fetch rules
                    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);

                    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 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)
            {
                string hostUrl = args.GetSetting(SonarProperties.HostUrl);
                if (Utilities.HandleHostUrlWebException(ex, hostUrl, this.logger))
                {
                    return(false);
                }

                throw;
            }
            finally
            {
                Utilities.SafeDispose(server);
            }

            return(true);
        }
コード例 #5
0
 private static bool IsCSharpPluginInstalled(ISonarQubeServer server)
 {
     return(server.GetInstalledPlugins().Contains(CSharpPluginKey));
 }