• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

samsmithnz / GitHubActionsDotNet / 7900757945

14 Feb 2024 11:53AM CUT coverage: 76.33%. Remained the same
7900757945

Pull #133

github

web-flow
Merge 652e32ffc into 5ca82b379
Pull Request #133: Bump the core group in /src/GitHubActionsDotNet with 1 update

144 of 222 branches covered (64.86%)

Branch coverage included in aggregate %.

559 of 699 relevant lines covered (79.97%)

13.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/GitHubActionsDotNet/Common/ConversionUtility.cs
1
using System;
2
using System.Diagnostics;
3
using System.Linq;
4
using System.Text;
5

6
namespace GitHubActionsDotNet.Common
7
{
8
    public static class ConversionUtility
9
    {
10
        // Some elements have a simple, same line string, we need to make into a list
11
        // for example "trigger:none", becomes "trigger:\n\r- none"
12
        // This is a lot simplier in JSON, as it's already only returning the none string.
13
        public const string ProcessNoneJsonElement = "[ none ]";
14

15
        public static string GenerateSpaces(int number)
16
        {
17
            return new string(' ', number);
×
18
        }
19

20
        // https://stackoverflow.com/questions/20411812/count-the-spaces-at-start-of-a-string
21
        public static int CountSpacesBeforeText(string input)
22
        {
23
            input = input.Replace(Environment.NewLine, "");
×
24
            return input.TakeWhile(char.IsWhiteSpace).Count();
×
25
        }
26

27
        public static string CleanYamlBeforeDeserializationV2(string yaml)
28
        {
29
            string processedYaml = yaml;
×
30

31
            //test that the inputted yaml has a ":" in the yaml - this is present in even the simpliest yaml
32
            if (yaml.IndexOf(":") == -1)
×
33
            {
34
                throw new Exception("This appears to be invalid YAML");
×
35
            }
36

37
            //Process conditional insertions/ variables
38
            if (processedYaml.IndexOf("{{#if") >= 0 || processedYaml.IndexOf("{{ #if") >= 0 ||
×
39
                processedYaml.IndexOf("${{if") >= 0 || processedYaml.IndexOf("${{ if") >= 0)
×
40
            {
41
                StringBuilder sb = new StringBuilder();
×
42
                int spacePrefixCount = 0;
×
43
                foreach (string line in processedYaml.Split(Environment.NewLine))
×
44
                {
45
                    if (line.IndexOf("{{#if") >= 0 || line.IndexOf("{{ #if") >= 0 ||
×
46
                        line.IndexOf("${{if") >= 0 || line.IndexOf("${{ if") >= 0)
×
47
                    {
48
                        //don't add line, we want to remove it, but track the spaces
49
                        spacePrefixCount = ConversionUtility.CountSpacesBeforeText(line);
×
50
                    }
51
                    else if (line.IndexOf("{{/if") >= 0) //ending if 
×
52
                    {
53
                        //don't add line, remove
54
                        spacePrefixCount = 0;
×
55
                    }
56
                    else
57
                    {
58
                        //DANGER WILL ROBINSON - DANGER 
59
                        //This is meant for variables, but may affect much more than it should
60
                        int currentLinespaceFrefixCount = ConversionUtility.CountSpacesBeforeText(line);
×
61
                        if (spacePrefixCount > 0 && spacePrefixCount == (currentLinespaceFrefixCount - 2))
×
62
                        {
63
                            //Correct the location. For example:
64
                            //    var1: value1
65
                            //becomes:
66
                            //  var1: value1
67
                            sb.Append(GenerateSpaces(spacePrefixCount));
×
68
                            sb.Append(line.Trim());
×
69
                        }
70
                        else if (spacePrefixCount > 0 && spacePrefixCount > (currentLinespaceFrefixCount - 2))
×
71
                        {
72
                            spacePrefixCount = 0;
×
73
                            sb.Append(line);
×
74
                        }
75
                        else
76
                        {
77
                            sb.Append(line);
×
78
                        }
79
                        sb.Append(System.Environment.NewLine);
×
80
                    }
81
                }
82
                processedYaml = sb.ToString();
×
83
            }
84

85
            return processedYaml;
×
86

87
        }
88

89
        public static string ConvertMessageToYamlComment(string message)
90
        {
91
            //Append a comment to the message if one doesn't already exist
92
            if (!message.TrimStart().StartsWith("#"))
×
93
            {
94
                message = "#" + message;
×
95
            }
96
            return message;
×
97
        }
98

99
        //Add a steps parent, to allow the processing of an individual step to proceed
100
        public static string StepsPreProcessing(string input)
101
        {
102
            //If the step isn't wrapped in a "steps:" node, we need to add this, so we can process the step
103
            if (!input.Trim().StartsWith("steps:") && input.Trim().Length > 0)
×
104
            {
105
                //we need to add steps, before we do, we need to see if the task needs an indent
106
                string[] stepLines = input.Split(Environment.NewLine);
×
107
                if (stepLines.Length > 0)
×
108
                {
109
                    int i = 0;
×
110
                    //Search for the first non empty line
111
                    while (string.IsNullOrEmpty(stepLines[i].Trim()))
×
112
                    {
113
                        i++;
×
114
                    }
115
                    if (stepLines[i].Trim().StartsWith("-"))
×
116
                    {
117
                        int indentLevel = stepLines[i].IndexOf("-");
×
118
                        indentLevel += 2;
×
119
                        string buffer = ConversionUtility.GenerateSpaces(indentLevel);
×
120
                        StringBuilder newInput = new StringBuilder();
×
121
                        foreach (string line in stepLines)
×
122
                        {
123
                            newInput.Append(buffer);
×
124
                            newInput.Append(line);
×
125
                            newInput.Append(System.Environment.NewLine);
×
126
                        }
127
                        input = newInput.ToString();
×
128
                    }
129

130
                    input = "steps:" + System.Environment.NewLine + input;
×
131
                }
132
            }
133
            return input;
×
134
        }
135

136
        public static void WriteLine(string message, bool verbose)
137
        {
138
            if (verbose)
×
139
            {
140
                Debug.WriteLine(message);
141
            }
142
        }
×
143
    }
144
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc