예제 #1
0
        private List <SonarRule> createActiveRules()
        {
            /*
             * <Rules AnalyzerId=""SonarLint.CSharp"" RuleNamespace=""SonarLint.CSharp"">
             * <Rule Id=""S1116"" Action=""Warning""/>
             * <Rule Id=""S1125"" Action=""Warning""/>
             * </Rules>
             * <Rules AnalyzerId=""Wintellect.Analyzers"" RuleNamespace=""Wintellect.Analyzers"">
             * <Rule Id=""Wintellect003"" Action=""Warning""/>
             * </Rules>
             */
            var rules             = new List <SonarRule>();
            var ruleWithParameter = new SonarRule("csharpsquid", "S1116", true);
            var p = new Dictionary <string, string>
            {
                { "key", "value" }
            };

            ruleWithParameter.Parameters = p;
            rules.Add(ruleWithParameter);
            rules.Add(new SonarRule("csharpsquid", "S1125", true));
            rules.Add(new SonarRule("roslyn.wintellect", "Wintellect003", true));

            return(rules);
        }
        private static SonarRule FilterRule(JObject r, JToken actives)
        {
            var activeRulesForRuleKey = actives.Value <JArray>(r["key"].ToString());

            if (activeRulesForRuleKey == null ||
                activeRulesForRuleKey.Count != 1)
            {
                // Because of the parameters we use we expect to have only actives rules. So rules and actives
                // should both contain the same number of elements (i.e. the same rules).
                throw new JsonException($"Malformed json response, \"actives\" field should contain rule '{r["key"].ToString()}'");
            }

            var activeRule = new SonarRule(r["repo"].ToString(), ParseRuleKey(r["key"].ToString()), true);

            if (r["internalKey"] != null)
            {
                activeRule.InternalKey = r["internalKey"].ToString();
            }
            if (r["templateKey"] != null)
            {
                activeRule.TemplateKey = r["templateKey"].ToString();
            }

            var activeRuleParams = activeRulesForRuleKey[0]["params"].Children <JObject>();

            activeRule.Parameters = activeRuleParams.ToDictionary(pair => pair["key"].ToString(), pair => pair["value"].ToString());

            if (activeRule.Parameters.ContainsKey("CheckId"))
            {
                activeRule.RuleKey = activeRule.Parameters["CheckId"];
            }

            return(activeRule);
        }
예제 #3
0
        /// <summary>
        /// Retrieves active rules from the quality profile with the given ID, including their parameters and template keys.
        ///
        /// </summary>
        /// <param name="qprofile">Quality profile id.</param>
        /// <returns>List of active rules</returns>
        public IList <SonarRule> GetActiveRules(string qprofile)
        {
            var fetched        = 0;
            var page           = 1;
            var total          = 0;
            var activeRuleList = new List <SonarRule>();

            do
            {
                var ws = GetUrl("/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey,params,actives&ps=500&activation=true&qprofile={0}&p={1}", qprofile, page.ToString());
                this.logger.LogDebug(Resources.MSG_FetchingActiveRules, qprofile, ws);

                activeRuleList.AddRange(DoLogExceptions(() =>
                {
                    var contents = this.downloader.Download(ws);
                    var json     = JObject.Parse(contents);
                    total        = json["total"].ToObject <int>();
                    fetched     += json["ps"].ToObject <int>();
                    page++;

                    var rules   = json["rules"].Children <JObject>();
                    var actives = json["actives"];

                    return(rules.Select(r =>
                    {
                        var activeRulesForRuleKey = actives.Value <JArray>(r["key"].ToString());

                        if (activeRulesForRuleKey == null ||
                            activeRulesForRuleKey.Count != 1)
                        {
                            // Because of the parameters we use we expect to have only actives rules. So rules and actives
                            // should both contain the same number of elements (i.e. the same rules).
                            throw new JsonException($"Malformed json response, \"actives\" field should contain rule '{r["key"].ToString()}'");
                        }

                        var activeRule = new SonarRule(r["repo"].ToString(), ParseRuleKey(r["key"].ToString()), true);
                        if (r["internalKey"] != null)
                        {
                            activeRule.InternalKey = r["internalKey"].ToString();
                        }
                        if (r["templateKey"] != null)
                        {
                            activeRule.TemplateKey = r["templateKey"].ToString();
                        }

                        var activeRuleParams = activeRulesForRuleKey[0]["params"].Children <JObject>();
                        activeRule.Parameters = activeRuleParams.ToDictionary(pair => pair["key"].ToString(), pair => pair["value"].ToString());

                        if (activeRule.Parameters.ContainsKey("CheckId"))
                        {
                            activeRule.RuleKey = activeRule.Parameters["CheckId"];
                        }

                        return activeRule;
                    }));
                }, ws));
            } while (fetched < total);

            return(activeRuleList);
        }
예제 #4
0
        private static SonarRule CreateRule(JObject r, JToken actives)
        {
            var active = actives?.Value <JArray>(r["key"].ToString())?.FirstOrDefault();
            var rule   = new SonarRule(r["repo"].ToString(), ParseRuleKey(r["key"].ToString()), r["internalKey"]?.ToString(), r["templateKey"]?.ToString(), active != null);

            if (active != null)
            {
                rule.Parameters = active["params"].Children <JObject>().ToDictionary(pair => pair["key"].ToString(), pair => pair["value"].ToString());
                if (rule.Parameters.ContainsKey("CheckId"))
                {
                    rule.RuleKey = rule.Parameters["CheckId"];
                }
            }
            return(rule);
        }
예제 #5
0
 public QualityProfile AddInactiveRule(SonarRule rule)
 {
     InactiveRules.Add(rule);
     return(this);
 }
예제 #6
0
        public void AddInactiveRuleToProfile(string qProfile, SonarRule rule)
        {
            var profile = FindProfile(qProfile);

            profile.InactiveRules.Add(rule);
        }
예제 #7
0
 public QualityProfile AddRule(SonarRule rule)
 {
     Rules.Add(rule);
     return(this);
 }