Example #1
0
        public Task <CodeOwnerConfig> ParserConfig(string content)
        {
            var result = new CodeOwnerConfig();

            if (string.IsNullOrEmpty(content))
            {
                return(Task.FromResult(result));
            }

            using (var reader = new StringReader(content))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    if (line.StartsWith("#"))
                    {
                        continue;
                    }

                    var entries = line.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)
                                  .SelectMany(x => x.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries))
                                  .ToList();
                    var pattern = entries.First();
                    var users   = entries.Skip(1)
                                  // CODEOWNERS could also started with "@".
                                  .Select(x => x.TrimStart('@'))
                                  .ToList();

                    if (users.Count == 0)
                    {
                        throw new Exception($"You must provide a user for pattern {pattern}");
                    }

                    result.Entries.Add(new CodeOwnerConfig.Entry
                    {
                        Pattern = pattern,
                        Users   = users
                    });
                }
            }

            return(Task.FromResult(result));
        }
Example #2
0
        public static void WriteCodeOwners(string path, CodeOwnerConfig codeOwners)
        {
            var content = new StringBuilder();

            foreach (var entry in codeOwners.Entries)
            {
                content.AppendLine($"{entry.Pattern} {string.Join(" ", entry.Users)}");
            }

            var parentDirectory = Path.GetDirectoryName(path);

            if (!string.IsNullOrEmpty(parentDirectory))
            {
                if (!Directory.Exists(parentDirectory))
                {
                    Directory.CreateDirectory(parentDirectory);
                }
            }
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            File.WriteAllText(path, content.ToString());
        }