Cmdlets/src/XpandPosh.Cmdlets/PublishGitHubRelease/New-GithubReleaseNotes.cs

using System;
using System.Linq;
using System.Management.Automation;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using XpandPosh.Cmdlets.GetGitHubCommitIssue;
using XpandPosh.CmdLets;
 
namespace XpandPosh.Cmdlets.PublishGitHubRelease{
    [CmdletBinding(SupportsShouldProcess = true)]
    [Cmdlet(VerbsCommon.New, "GithubReleaseNotes",SupportsShouldProcess = true)]
    [OutputType(typeof(string))]
    public class NewGitHubReleaseNotes : GitHubCmdlet{
        [Parameter(Mandatory = true)]
        public ICommitIssues[] CommitIssues{ get; set; }
        [Parameter(Mandatory = true)]
        public IReleaseNotesTemplate ReleaseNotesTemplate{ get; set; } = Cmdlets.PublishGitHubRelease.ReleaseNotesTemplate.Default;
         
        protected override Task ProcessRecordAsync(){
            return Observable.Return(string.Join(Environment.NewLine,ReleaseNotesTemplate.Parts.Select(GetBody)))
                .WriteObject(this)
                .ToTask();
        }
 
        private string GetBody( ITemplatePart templatePart){
            return string.Join(Environment.NewLine, CommitIssues
                .Where(tuple => templatePart.Labels.Intersect(tuple.Issues.SelectMany(issue => issue.Labels).Select(label => label.Name)).Any())
                .Select(tuple => {
                    var commitMessage = Regex.Replace(tuple.GitHubCommit.Commit.Message, @"(#\d*)", "", RegexOptions.IgnoreCase);
                    var numbers = string.Join(" ",tuple.Issues.Select(issue => $"#[{issue.Number}]({tuple.Repository2.HtmlUrl}/issues/{issue.Number})"));
                    return $"1. {numbers} {commitMessage}";
                }));
        }
    }
 
}