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

samsmithnz / GitHubActionsDotNet / 6016164427

29 Aug 2023 06:31PM UTC coverage: 75.904% (-0.1%) from 76.018%
6016164427

push

github

web-flow
Merge pull request #103 from samsmithnz/UpdateToSerializationForGroups

Update to serialization function for dependabot

139 of 218 branches covered (0.0%)

Branch coverage included in aggregate %.

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

554 of 695 relevant lines covered (79.71%)

13.11 hits per line

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

92.03
/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
                packages.Add(actionsPackage);
7✔
50
            }
51
            root.updates = packages;
7✔
52

53
            //Serialize the object into YAML
54
            string yaml = YamlSerialization.SerializeYaml(root);
7✔
55

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

72
            return yaml;
7✔
73
        }
74

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

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

100
            //Build up the GitHub object piece by piece
101
            DependabotRoot root = new DependabotRoot();
31✔
102

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

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

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

132
            return root;
31✔
133
        }
134

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

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