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

samsmithnz / GitHubActionsDotNet / 6016288227

29 Aug 2023 06:45PM UTC coverage: 75.791% (-0.1%) from 75.904%
6016288227

push

github

web-flow
Merge pull request #104 from samsmithnz/NewFixToDependabotFile

Updated actions creation

140 of 220 branches covered (0.0%)

Branch coverage included in aggregate %.

2 of 2 new or added lines in 1 file covered. (100.0%)

555 of 697 relevant lines covered (79.63%)

13.09 hits per line

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

90.85
/src/GitHubActionsDotNet/Serialization/DependabotSerialization.cs
1
using GitHubActionsDotNet.Common;
2
using GitHubActionsDotNet.Models.Dependabot;
3
using System.Collections.Generic;
4
using System.IO;
5
using System.Text.Json;
6

7
namespace GitHubActionsDotNet.Serialization
8
{
9
    public static class DependabotSerialization
10
    {
11
        public static string Serialize(string startingDirectory,
12
            List<string> files,
13
            string interval = null,
14
            string time = null,
15
            string timezone = null,
16
            List<string> assignees = null,
17
            int openPRLimit = 0,
18
            bool includeActions = true,
19
            string groupName = null,
20
            string[] groupPatterns = null,
21
            string[] groupUpdateTypes = null)
22
        {
23
            if (startingDirectory == null)
7!
24
            {
25
                return "";
×
26
            }
27

28
            DependabotRoot root = new DependabotRoot();
7✔
29
            List<Package> packages = new List<Package>();
7✔
30
            foreach (string file in files)
38✔
31
            {
32
                FileInfo fileInfo = new FileInfo(file);
12✔
33
                string cleanedFilePath = file.Replace(startingDirectory + "/", "");
12✔
34
                cleanedFilePath = cleanedFilePath.Replace(startingDirectory + "\\", "");
12✔
35
                cleanedFilePath = cleanedFilePath.Replace(fileInfo.Name, "");
12✔
36
                cleanedFilePath = "/" + cleanedFilePath.Replace("\\", "/");
12✔
37
                string packageEcoSystem = DependabotCommon.GetPackageEcoSystemFromFileName(fileInfo.Name);
12✔
38
                Package package = CreatePackage(cleanedFilePath, packageEcoSystem, interval, time, timezone, assignees, openPRLimit);
12✔
39
                if (groupName != null)
12!
40
                {
41
                    package.groups.Add(groupName, new Group() { patterns = groupPatterns, update_types = groupUpdateTypes });
×
42
                }
43
                packages.Add(package);
12✔
44
            }
45
            //Add actions
46
            if (includeActions == true)
7✔
47
            {
48
                Package actionsPackage = CreatePackage("/", "github-actions", interval, time, timezone, assignees, openPRLimit);
7✔
49
                if (groupName != null)
7!
50
                {
51
                    actionsPackage.groups.Add("actions", new Group() { patterns = groupPatterns, update_types = groupUpdateTypes });
×
52
                }
53
                packages.Add(actionsPackage);
7✔
54
            }
55
            root.updates = packages;
7✔
56

57
            //Serialize the object into YAML
58
            string yaml = YamlSerialization.SerializeYaml(root);
7✔
59

60
            //I can't use - in variable names, so replace _ with -
61
            yaml = yaml.Replace("package_ecosystem", "package-ecosystem");
7✔
62
            yaml = yaml.Replace("open_pull_requests_limit", "open-pull-requests-limit");
7✔
63
            yaml = yaml.Replace("replaces_base", "replaces-base");
7✔
64
            yaml = yaml.Replace("dependency_name", "dependency-name");
7✔
65
            yaml = yaml.Replace("dependency_type", "dependency-type");
7✔
66
            yaml = yaml.Replace("prefix_development", "prefix-development");
7✔
67
            yaml = yaml.Replace("commit_message", "commit-message");
7✔
68
            yaml = yaml.Replace("update_types", "update-types");
7✔
69
            yaml = yaml.Replace("insecure_external_code_execution", "insecure-external-code-execution");
7✔
70
            yaml = yaml.Replace("pull_request_branch_name", "pull-request-branch-name");
7✔
71
            yaml = yaml.Replace("rebase_strategy", "rebase-strategy");
7✔
72
            yaml = yaml.Replace("target_branch", "target-branch");
7✔
73
            yaml = yaml.Replace("versioning_strategy", "versioning-strategy");
7✔
74
            yaml = yaml.Replace("update_types", "update-types");
7✔
75

76
            return yaml;
7✔
77
        }
78

79
        public static DependabotRoot Deserialize(string yaml)
80
        {
81
            yaml = yaml.Replace("package-ecosystem", "package_ecosystem");
31✔
82
            yaml = yaml.Replace("open-pull-requests-limit", "open_pull_requests_limit");
31✔
83
            yaml = yaml.Replace("replaces-base", "replaces_base");
31✔
84
            yaml = yaml.Replace("dependency-name", "dependency_name");
31✔
85
            yaml = yaml.Replace("dependency-type", "dependency_type");
31✔
86
            yaml = yaml.Replace("prefix-development", "prefix_development");
31✔
87
            yaml = yaml.Replace("commit-message", "commit_message");
31✔
88
            yaml = yaml.Replace("update-types", "update_types");
31✔
89
            yaml = yaml.Replace("insecure-external-code-execution", "insecure_external_code_execution");
31✔
90
            yaml = yaml.Replace("pull-request-branch-name", "pull_request_branch_name");
31✔
91
            yaml = yaml.Replace("rebase-strategy", "rebase_strategy");
31✔
92
            yaml = yaml.Replace("target-branch", "target_branch");
31✔
93
            yaml = yaml.Replace("versioning-strategy", "versioning_strategy");
31✔
94
            yaml = yaml.Replace("update-types", "update_types");
31✔
95

96
            //DependabotRoot root = YamlSerialization.DeserializeYaml<DependabotRoot>(yaml);
97
            //convert the yaml into json, it's easier to parse
98
            JsonElement jsonObject = new JsonElement();
31✔
99
            if (yaml != null)
31✔
100
            {
101
                jsonObject = JsonSerialization.DeserializeStringToJsonElement(yaml);
31✔
102
            }
103

104
            //Build up the GitHub object piece by piece
105
            DependabotRoot root = new DependabotRoot();
31✔
106

107
            if (jsonObject.ValueKind != JsonValueKind.Undefined)
31✔
108
            {
109
                //Version
110
                if (jsonObject.TryGetProperty("name", out JsonElement jsonElement))
31!
111
                {
112
                    root.version = jsonElement.ToString().Replace("version:", "").Replace(System.Environment.NewLine, "").Trim();
×
113
                }
114

115
                //Registries
116
                if (jsonObject.TryGetProperty("registries", out jsonElement))
31✔
117
                {
118
                    root.registries = YamlSerialization.DeserializeYaml<IDictionary<string, Registry>>(jsonElement.ToString()); //JsonSerialization.(jsonElement.ToString());
12✔
119
                }
120

121
                //Packages
122
                if (jsonObject.TryGetProperty("updates", out jsonElement))
31✔
123
                {
124
                    foreach (JsonElement packagesItem in jsonElement.EnumerateArray())
112✔
125
                    {
126
                        string packageYaml = packagesItem.ToString();
35✔
127
                        if (root.updates == null)
35✔
128
                        {
129
                            root.updates = new List<Package>();
21✔
130
                        }
131
                        root.updates.Add(ProcessPackage(packageYaml));
35✔
132
                    }
133
                }
134
            }
135

136
            return root;
31✔
137
        }
138

139
        private static Package ProcessPackage(string packageYaml)
140
        {
141
            Package package = null;
35✔
142
            if (packageYaml != null)
35✔
143
            {
144
                //Try the string[] variable first - I think that will be the most common
145
                try
146
                {
147
                    package = YamlSerialization.DeserializeYaml<PackageStringArray>(packageYaml);
35✔
148
                }
34✔
149
                catch
1✔
150
                {
151
                    //If it didn't work, try the simple string one, the next most common
152
                    package = YamlSerialization.DeserializeYaml<PackageString>(packageYaml);
1✔
153
                }
1✔
154
            }
155
            return package;
35✔
156
        }
157

158
        private static Package CreatePackage(string filePath,
159
           string packageEcoSystem,
160
           string interval = null,
161
           string time = null,
162
           string timezone = null,
163
           List<string> assignees = null,
164
           int openPRLimit = 0,
165
           string registryString = null,
166
           string[] registryStringArray = null)
167
        {
168
            Package package;
169
            if (registryString != null)
19!
170
            {
171
                package = new PackageString
×
172
                {
×
173
                    registries = registryString
×
174
                };
×
175
            }
176
            else
177
            {
178
                package = new PackageStringArray
19✔
179
                {
19✔
180
                    registries = registryStringArray
19✔
181
                };
19✔
182
            }
183
            package.package_ecosystem = packageEcoSystem;
19✔
184
            package.directory = filePath;
19✔
185
            package.assignees = assignees;
19✔
186
            if (interval != null ||
19✔
187
                time != null ||
19✔
188
                timezone != null)
19✔
189
            {
190
                package.schedule = new Schedule();
18✔
191
                if (interval != null)
18✔
192
                {
193
                    package.schedule.interval = interval;
18✔
194
                }
195
                if (time != null)
18✔
196
                {
197
                    package.schedule.time = time;
2✔
198
                }
199
                if (timezone != null)
18✔
200
                {
201
                    package.schedule.timezone = timezone;
2✔
202
                }
203
            }
204
            if (openPRLimit > 0)
19✔
205
            {
206
                package.open_pull_requests_limit = openPRLimit.ToString();
10✔
207
            }
208
            return package;
19✔
209
        }
210
    }
211
}
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

© 2026 Coveralls, Inc