public ConventionalCommit Parse(Commit commit)
        {
            var conventionalCommit = new ConventionalCommit
            {
                Sha = commit.Sha
            };

            var commitMessageLines = commit.Message.Split(
                new[] { "\r\n", "\r", "\n" },
                StringSplitOptions.None
                )
                                     .Select(line => line.Trim())
                                     .Where(line => !string.IsNullOrWhiteSpace(line))
                                     .ToList();

            var header = commitMessageLines.FirstOrDefault();

            if (header == null)
            {
                return(conventionalCommit);
            }

            var match = HeaderPattern.Match(header);

            if (match.Success)
            {
                conventionalCommit.Scope   = match.Groups["scope"].Value;
                conventionalCommit.Type    = match.Groups["type"].Value;
                conventionalCommit.Subject = match.Groups["subject"].Value;
            }
            else
            {
                conventionalCommit.Subject = header;
            }

            for (var i = 1; i < commitMessageLines.Count; i++)
            {
                foreach (var noteKeyword in noteKeywords)
                {
                    var line = commitMessageLines[i];
                    if (line.StartsWith($"{noteKeyword}:"))
                    {
                        conventionalCommit.Notes.Add(new ConventionalCommitNote
                        {
                            Title = noteKeyword,
                            Text  = line.Substring($"{noteKeyword}:".Length).TrimStart()
                        });
                    }
                }
            }

            return(conventionalCommit);
        }
Example #2
0
        public static string BuildCommit(ConventionalCommit commit)
        {
            var sb = new StringBuilder("* ");

            if (!string.IsNullOrWhiteSpace(commit.Scope))
            {
                sb.Append($"**{commit.Scope}:** ");
            }
#pragma warning disable CA1304 // Specify CultureInfo
            var formattedSubject = char.ToUpper(commit.Subject[0]) +
                                   ((commit.Subject.Length > 1) ? commit.Subject.Substring(1) : string.Empty);
#pragma warning restore CA1304 // Specify CultureInfo

            sb.Append(formattedSubject);

            //var commitLink = linkBuilder.BuildCommitLink(commit);

            //if (!string.IsNullOrWhiteSpace(commitLink))
            //{
            //    sb.Append($" ([{commit.Sha.Substring(0, 7)}]({commitLink}))");
            //}

            return(sb.ToString());
        }