Ejemplo n.º 1
0
        private static async Task Main()
        {
            var client = new HttpClient();
            HttpResponseMessage ruleSetResponse = await client.GetAsync(IpMaturityMatrixRuleset.RuleSetDefinitionsUrl).ConfigureAwait(false);

            ruleSetResponse.EnsureSuccessStatusCode();
            string yamlRuleText = await ruleSetResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

            var yamlRules       = IpMaturityMatrixRuleset.FromYaml(yamlRuleText);
            var immProjectScore = IpMaturityMatrix.FromYaml(File.ReadAllText("imm-default.yaml"));

            var evaluationEngine = new EvaluationEngine(new RuleDefinitionRepository(yamlRules));

            var col1 = new StringBuilder();
            var col2 = new StringBuilder();

            ImmEvaluation evalutionResult = evaluationEngine.Evaluate(immProjectScore);

            foreach (var result in evalutionResult.RuleEvaluations)
            {
                Console.WriteLine($"{result.RuleAssertion.Name} {result.Percentage}% Score: {result.Score}");

#pragma warning disable RCS1197 // Optimize StringBuilder.Append/AppendLine call.
                col1.AppendLine($"<tspan x='30' dy='1.5em'>{WebUtility.HtmlEncode(result.RuleAssertion.Name)}</tspan>");
                col2.AppendLine($"<tspan x='310' dy='1.5em'>{result.Percentage}%</tspan>");
#pragma warning restore RCS1197 // Optimize StringBuilder.Append/AppendLine call.

                File.WriteAllText($"imm-{result.RuleAssertion.Id}.svg", BadgePainter.DrawSVG(WebUtility.HtmlEncode(result.RuleAssertion.Name !), $"{result.Percentage}%", ColorScheme.Red, Style.FlatSquare));
            }

            Console.WriteLine($"{evalutionResult.TotalScore} / {evalutionResult.MaximumPossibleTotalScore}");

            File.WriteAllText("imm.svg", BadgePainter.DrawSVG("IMM", $"{evalutionResult.TotalScore} / {evalutionResult.MaximumPossibleTotalScore}", ColorScheme.Red, Style.Flat));
            File.WriteAllText("imm-table.svg", string.Format(Resources.Table, col1.ToString(), col2.ToString()));
        }
Ejemplo n.º 2
0
        public async Task <HttpResponseMessage> GitHubImmTotalScore(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "imm/github/{org}/{project}/total")] HttpRequest request,
            string org,
            string project)
        {
            (string rulesObjectName, string projectObjectName) = this.GetGitHubBranchOrObjectNames(request);
            (IRuleDefinitionRepository ruleSet, IpMaturityMatrix ruleAssertions) =
                await this.GetImmRulesFromGitHubAsync(org, project, rulesObjectName, projectObjectName).ConfigureAwait(false);

            var evaluationEngine = new EvaluationEngine(ruleSet);

            ImmEvaluation evaluationResult = evaluationEngine.Evaluate(ruleAssertions);

            string svg = BadgePainter.DrawSVG(
                "IMM",
                $"{evaluationResult.TotalScore} / {evaluationResult.MaximumPossibleTotalScore}",
                GetColourSchemeForPercentage(100M * evaluationResult.TotalScore / evaluationResult.MaximumPossibleTotalScore),
                Style.Flat);

            return(this.CreateUncacheResponse(
                       new ByteArrayContent(Encoding.ASCII.GetBytes(svg)),
                       "image/svg+xml"));
        }
Ejemplo n.º 3
0
        public async Task <HttpResponseMessage> GitHubImmAllBadges(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "imm/github/{org}/{project}/showbadges")] HttpRequest request,
            string org,
            string project)
        {
            (string rulesObjectName, string projectObjectName) = this.GetGitHubBranchOrObjectNames(request);
            (IRuleDefinitionRepository ruleSet, IpMaturityMatrix ruleAssertions) =
                await this.GetImmRulesFromGitHubAsync(org, project, rulesObjectName, projectObjectName).ConfigureAwait(false);

            var evaluationEngine = new EvaluationEngine(ruleSet);

            ImmEvaluation evaluationResult = evaluationEngine.Evaluate(ruleAssertions);

            string encodedRulesObjectName   = WebUtility.UrlEncode(rulesObjectName);
            string encodedProjectObjectName = WebUtility.UrlEncode(projectObjectName);

            var sb = new StringBuilder();

            using (var sw = new StringWriter(sb))
            {
                sw.WriteLine("<!DOCTYPE html>");
                sw.WriteLine("<html>");
                sw.WriteLine("<head>");
                sw.WriteLine("    <meta charset=\"utf-8\" />");
                sw.Write("    <title>IP Maturity Matrix for");
                sw.Write(org);
                sw.Write("/");
                sw.Write(org);
                sw.Write(" (");
                sw.Write(projectObjectName);
                if (rulesObjectName != "master")
                {
                    sw.Write(", rules: ");
                    sw.Write(WebUtility.HtmlEncode(rulesObjectName));
                }
                sw.WriteLine(")</title>");
                sw.WriteLine("<body>");

                sw.WriteLine("  <div>");
                sw.WriteLine("  <p>Total score</p>");

                sw.Write("    <img src=\"/api/imm/github/");
                sw.Write(org);
                sw.Write("/");
                sw.Write(project);
                sw.Write("/total");
                sw.Write("?cache=false&definitionsBranch=");
                sw.Write(encodedRulesObjectName);
                sw.Write("&projectBranch=");
                sw.Write(encodedProjectObjectName);
                sw.WriteLine("\" />");

                sw.WriteLine("  </div>");

                sw.WriteLine("  <div>");
                sw.WriteLine("  <p>Rules</p>");

                sw.WriteLine("  <table>");
                sw.WriteLine("    <tbody>");

                foreach (var evaluation in evaluationResult.RuleEvaluations)
                {
                    sw.WriteLine("      <tr>");
                    sw.WriteLine("        <td>");
                    sw.Write("          <img src=\"/api/imm/github/");
                    sw.Write(org);
                    sw.Write("/");
                    sw.Write(project);
                    sw.Write("/rule/");
                    sw.Write(evaluation.RuleAssertion.Id);
                    sw.Write("?cache=false&definitionsBranch=");
                    sw.Write(encodedRulesObjectName);
                    sw.Write("&projectBranch=");
                    sw.Write(encodedProjectObjectName);
                    sw.WriteLine("\" />");
                    sw.WriteLine("        </td>");
                    sw.WriteLine("      </tr>");
                }

                sw.WriteLine("    </tbody>");
                sw.WriteLine("  </table>");
                sw.WriteLine("  </div>");
                sw.WriteLine("</body>");
                sw.WriteLine("</html>");
            }

            //string svg = BadgePainter.DrawSVG("IMM", $"{evaluationResult.TotalScore} / {evaluationResult.MaximumPossibleTotalScore}", ColorScheme.Red, Style.Flat);
            return(this.CreateUncacheResponse(
                       new StringContent(sb.ToString(), Encoding.UTF8),
                       "text/html"));
        }