1818using GitHub . Extensions ;
1919using System . Reactive . Disposables ;
2020using System . Reactive ;
21- using GitHub . Logging ;
21+ using System . Threading . Tasks ;
2222using Serilog ;
23+ using GitHub . Logging ;
2324
2425namespace GitHub . ViewModels
2526{
@@ -72,8 +73,6 @@ public PullRequestCreationViewModel(IRepositoryHost repositoryHost, ILocalReposi
7273 } ) ;
7374
7475 SourceBranch = activeRepo . CurrentBranch ;
75- service . GetPullRequestTemplate ( activeRepo )
76- . Subscribe ( x => Description = x ?? String . Empty , ( ) => Description = Description ?? String . Empty ) ;
7776
7877 this . WhenAnyValue ( x => x . Branches )
7978 . WhereNotNull ( )
@@ -86,6 +85,31 @@ public PullRequestCreationViewModel(IRepositoryHost repositoryHost, ILocalReposi
8685
8786 SetupValidators ( ) ;
8887
88+ var uniqueCommits = this . WhenAnyValue (
89+ x => x . SourceBranch ,
90+ x => x . TargetBranch )
91+ . Where ( x => x . Item1 != null && x . Item2 != null )
92+ . Select ( branches =>
93+ {
94+ var baseBranch = branches . Item1 . Name ;
95+ var compareBranch = branches . Item2 . Name ;
96+
97+ // We only need to get max two commits for what we're trying to achieve here.
98+ // If there's no commits we want to block creation of the PR, if there's one commits
99+ // we wan't to use its commit message as the PR title/body and finally if there's more
100+ // than one we'll use the branch name for the title.
101+ return service . GetMessagesForUniqueCommits ( activeRepo , baseBranch , compareBranch , maxCommits : 2 )
102+ . Catch < IReadOnlyList < CommitMessage > , Exception > ( ex =>
103+ {
104+ log . Warning ( ex , "Could not load unique commits" ) ;
105+ return Observable . Empty < IReadOnlyList < CommitMessage > > ( ) ;
106+ } ) ;
107+ } )
108+ . Switch ( )
109+ . ObserveOn ( RxApp . MainThreadScheduler )
110+ . Replay ( 1 )
111+ . RefCount ( ) ;
112+
89113 var whenAnyValidationResultChanges = this . WhenAny (
90114 x => x . TitleValidator . ValidationResult ,
91115 x => x . BranchValidator . ValidationResult ,
@@ -121,6 +145,37 @@ public PullRequestCreationViewModel(IRepositoryHost repositoryHost, ILocalReposi
121145 this . WhenAnyValue ( x => x . Initialized , x => x . GitHubRepository , x => x . Description , x => x . IsExecuting )
122146 . Select ( x => ! ( x . Item1 && x . Item2 != null && x . Item3 != null && ! x . Item4 ) )
123147 . Subscribe ( x => IsBusy = x ) ;
148+
149+ Observable . CombineLatest (
150+ this . WhenAnyValue ( x => x . SourceBranch ) ,
151+ uniqueCommits ,
152+ service . GetPullRequestTemplate ( activeRepo ) . DefaultIfEmpty ( string . Empty ) ,
153+ ( compare , commits , template ) => new { compare , commits , template } )
154+ . Subscribe ( x =>
155+ {
156+ var prTitle = string . Empty ;
157+ var prDescription = string . Empty ;
158+
159+ if ( x . commits . Count == 1 )
160+ {
161+ prTitle = x . commits [ 0 ] . Summary ;
162+ prDescription = x . commits [ 0 ] . Details ;
163+ }
164+ else
165+ {
166+ prTitle = x . compare . Name . Humanize ( ) ;
167+ }
168+
169+ if ( ! string . IsNullOrWhiteSpace ( x . template ) )
170+ {
171+ if ( ! string . IsNullOrEmpty ( prDescription ) )
172+ prDescription += "\n \n " ;
173+ prDescription += x . template ;
174+ }
175+
176+ PRTitle = prTitle ;
177+ Description = prDescription ;
178+ } ) ;
124179 }
125180
126181 public override void Initialize ( ViewWithData data = null )
0 commit comments