Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit f67582d

Browse files
committed
Allow starting a review from and inline comment.
1 parent be8fb3f commit f67582d

File tree

14 files changed

+344
-30
lines changed

14 files changed

+344
-30
lines changed

src/GitHub.App/ViewModels/GitHubPane/PullRequestReviewAuthoringViewModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ async Task DoSubmit(object arg)
246246

247247
if (Enum.TryParse(arg.ToString(), out e))
248248
{
249-
await Task.Delay(1);
250-
//await session.PostReview(Body, e);
249+
await session.PostReview(Body, e);
251250
Close();
252251
}
253252
}

src/GitHub.Exports/GitHub.Exports.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@
168168
<Compile Include="Models\ILocalRepositoryModelFactory.cs" />
169169
<Compile Include="Models\IPullRequestReviewCommentModel.cs" />
170170
<Compile Include="Models\IPullRequestReviewModel.cs" />
171-
<Compile Include="Models\PullRequestReviewEvent.cs" />
172171
<Compile Include="Services\IEnterpriseCapabilitiesService.cs" />
173172
<Compile Include="Services\IGlobalConnection.cs" />
174173
<Compile Include="Services\ILocalRepositories.cs" />

src/GitHub.InlineReviews/GitHub.InlineReviews.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@
104104
<Compile Include="ViewModels\ICommentThreadViewModel.cs" />
105105
<Compile Include="ViewModels\CommentThreadViewModel.cs" />
106106
<Compile Include="ViewModels\InlineCommentPeekViewModel.cs" />
107+
<Compile Include="ViewModels\IPullRequestReviewCommentViewModel.cs" />
107108
<Compile Include="ViewModels\NewInlineCommentThreadViewModel.cs" />
109+
<Compile Include="ViewModels\PullRequestReviewCommentViewModel.cs" />
108110
<Compile Include="ViewModels\PullRequestStatusViewModel.cs" />
109111
<Compile Include="Views\GlyphMarginGrid.xaml.cs">
110112
<DependentUpon>GlyphMarginGrid.xaml</DependentUpon>

src/GitHub.InlineReviews/ViewModels/CommentViewModel.cs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class CommentViewModel : ReactiveObject, ICommentViewModel
3535
/// <param name="state">The comment edit state.</param>
3636
/// <param name="user">The author of the comment.</param>
3737
/// <param name="updatedAt">The modified date of the comment.</param>
38-
public CommentViewModel(
38+
protected CommentViewModel(
3939
ICommentThreadViewModel thread,
4040
IAccount currentUser,
4141
int commentId,
@@ -89,35 +89,14 @@ public CommentViewModel(
8989
/// <param name="thread">The thread that the comment is a part of.</param>
9090
/// <param name="currentUser">The current user.</param>
9191
/// <param name="model">The comment model.</param>
92-
public CommentViewModel(
92+
protected CommentViewModel(
9393
ICommentThreadViewModel thread,
9494
IAccount currentUser,
9595
ICommentModel model)
9696
: this(thread, currentUser, model.Id, model.NodeId, model.Body, CommentEditState.None, model.User, model.CreatedAt)
9797
{
9898
}
9999

100-
/// <summary>
101-
/// Creates a placeholder comment which can be used to add a new comment to a thread.
102-
/// </summary>
103-
/// <param name="thread">The comment thread.</param>
104-
/// <param name="currentUser">The current user.</param>
105-
/// <returns>THe placeholder comment.</returns>
106-
public static CommentViewModel CreatePlaceholder(
107-
ICommentThreadViewModel thread,
108-
IAccount currentUser)
109-
{
110-
return new CommentViewModel(
111-
thread,
112-
currentUser,
113-
0,
114-
null,
115-
string.Empty,
116-
CommentEditState.Placeholder,
117-
currentUser,
118-
DateTimeOffset.MinValue);
119-
}
120-
121100
protected void AddErrorHandler<T>(ReactiveCommand<T> command)
122101
{
123102
command.ThrownExceptions.Subscribe(x => ErrorMessage = x.Message);

src/GitHub.InlineReviews/ViewModels/ICommentViewModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public enum CommentEditState
1313
Placeholder,
1414
}
1515

16+
/// <summary>
17+
/// View model for an issue or pull request comment.
18+
/// </summary>
1619
public interface ICommentViewModel : IViewModel
1720
{
1821
/// <summary>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System;
2+
using System.Reactive;
3+
using ReactiveUI;
4+
5+
namespace GitHub.InlineReviews.ViewModels
6+
{
7+
/// <summary>
8+
/// View model for a pull request review comment.
9+
/// </summary>
10+
public interface IPullRequestReviewCommentViewModel : ICommentViewModel
11+
{
12+
/// <summary>
13+
/// Gets a value indicating whether the user can start a new review with this comment.
14+
/// </summary>
15+
bool CanStartReview { get; }
16+
17+
/// <summary>
18+
/// Gets the caption for the "Commit" button.
19+
/// </summary>
20+
/// <remarks>
21+
/// This will be "Add a single comment" when not in review mode and "Add review comment"
22+
/// when in review mode.
23+
/// </remarks>
24+
string CommitCaption { get; }
25+
26+
/// <summary>
27+
/// Gets a value indicating whether this comment is part of a pending pull request review.
28+
/// </summary>
29+
bool IsPending { get; }
30+
31+
/// <summary>
32+
/// Gets a command which will commit a new comment and start a review.
33+
/// </summary>
34+
ReactiveCommand<Unit> StartReview { get; }
35+
}
36+
}

src/GitHub.InlineReviews/ViewModels/InlineCommentThreadViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ public InlineCommentThreadViewModel(
3737

3838
foreach (var comment in comments)
3939
{
40-
Comments.Add(new CommentViewModel(this, CurrentUser, comment));
40+
Comments.Add(new PullRequestReviewCommentViewModel(session, this, CurrentUser, comment));
4141
}
4242

43-
Comments.Add(CommentViewModel.CreatePlaceholder(this, CurrentUser));
43+
Comments.Add(PullRequestReviewCommentViewModel.CreatePlaceholder(session, this, CurrentUser));
4444
}
4545

4646
/// <summary>

src/GitHub.InlineReviews/ViewModels/NewInlineCommentThreadViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq;
3+
using System.Reactive;
34
using System.Reactive.Linq;
45
using System.Threading.Tasks;
56
using GitHub.Extensions;
@@ -44,7 +45,7 @@ public NewInlineCommentThreadViewModel(
4445
this.WhenAnyValue(x => x.NeedsPush, x => !x),
4546
DoPostComment);
4647

47-
var placeholder = CommentViewModel.CreatePlaceholder(this, CurrentUser);
48+
var placeholder = PullRequestReviewCommentViewModel.CreatePlaceholder(session, this, CurrentUser);
4849
placeholder.BeginEdit.Execute(null);
4950
this.WhenAnyValue(x => x.NeedsPush).Subscribe(x => placeholder.IsReadOnly = x);
5051
Comments.Add(placeholder);
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reactive;
4+
using System.Reactive.Linq;
5+
using System.Threading.Tasks;
6+
using GitHub.Extensions;
7+
using GitHub.Logging;
8+
using GitHub.Models;
9+
using GitHub.Services;
10+
using GitHub.VisualStudio.UI;
11+
using ReactiveUI;
12+
using Serilog;
13+
14+
namespace GitHub.InlineReviews.ViewModels
15+
{
16+
/// <summary>
17+
/// View model for a pull request review comment.
18+
/// </summary>
19+
public class PullRequestReviewCommentViewModel : CommentViewModel, IPullRequestReviewCommentViewModel
20+
{
21+
readonly IPullRequestSession session;
22+
ObservableAsPropertyHelper<bool> canStartReview;
23+
ObservableAsPropertyHelper<string> commitCaption;
24+
25+
/// <summary>
26+
/// Initializes a new instance of the <see cref="PullRequestReviewCommentViewModel"/> class.
27+
/// </summary>
28+
/// <param name="session">The pull request session.</param>
29+
/// <param name="thread">The thread that the comment is a part of.</param>
30+
/// <param name="currentUser">The current user.</param>
31+
/// <param name="commentId">The REST ID of the comment.</param>
32+
/// <param name="commentNodeId">The GraphQL ID of the comment.</param>
33+
/// <param name="body">The comment body.</param>
34+
/// <param name="state">The comment edit state.</param>
35+
/// <param name="user">The author of the comment.</param>
36+
/// <param name="updatedAt">The modified date of the comment.</param>
37+
/// <param name="isPending">Whether this is a pending comment.</param>
38+
public PullRequestReviewCommentViewModel(
39+
IPullRequestSession session,
40+
ICommentThreadViewModel thread,
41+
IAccount currentUser,
42+
int commentId,
43+
string commentNodeId,
44+
string body,
45+
CommentEditState state,
46+
IAccount user,
47+
DateTimeOffset updatedAt,
48+
bool isPending)
49+
: base(thread, currentUser, commentId, commentNodeId, body, state, user, updatedAt)
50+
{
51+
Guard.ArgumentNotNull(session, nameof(session));
52+
53+
this.session = session;
54+
IsPending = isPending;
55+
56+
canStartReview = session.WhenAnyValue(x => x.HasPendingReview, x => !x)
57+
.ToProperty(this, x => x.CanStartReview);
58+
commitCaption = session.WhenAnyValue(
59+
x => x.HasPendingReview,
60+
x => x ? Resources.AddReviewComment : Resources.AddSingleComment)
61+
.ToProperty(this, x => x.CommitCaption);
62+
63+
StartReview = ReactiveCommand.CreateAsyncTask(
64+
CommitEdit.CanExecuteObservable,
65+
DoStartReview);
66+
AddErrorHandler(StartReview);
67+
}
68+
69+
/// <summary>
70+
/// Initializes a new instance of the <see cref="PullRequestReviewCommentViewModel"/> class.
71+
/// </summary>
72+
/// <param name="session">The pull request session.</param>
73+
/// <param name="thread">The thread that the comment is a part of.</param>
74+
/// <param name="currentUser">The current user.</param>
75+
/// <param name="model">The comment model.</param>
76+
public PullRequestReviewCommentViewModel(
77+
IPullRequestSession session,
78+
ICommentThreadViewModel thread,
79+
IAccount currentUser,
80+
IPullRequestReviewCommentModel model)
81+
: this(session, thread, currentUser, model.Id, model.NodeId, model.Body, CommentEditState.None, model.User, model.CreatedAt, model.IsPending)
82+
{
83+
}
84+
85+
/// <summary>
86+
/// Creates a placeholder comment which can be used to add a new comment to a thread.
87+
/// </summary>
88+
/// <param name="thread">The comment thread.</param>
89+
/// <param name="currentUser">The current user.</param>
90+
/// <returns>THe placeholder comment.</returns>
91+
public static CommentViewModel CreatePlaceholder(
92+
IPullRequestSession session,
93+
ICommentThreadViewModel thread,
94+
IAccount currentUser)
95+
{
96+
return new PullRequestReviewCommentViewModel(
97+
session,
98+
thread,
99+
currentUser,
100+
0,
101+
null,
102+
string.Empty,
103+
CommentEditState.Placeholder,
104+
currentUser,
105+
DateTimeOffset.MinValue,
106+
false);
107+
}
108+
109+
/// <inheritdoc/>
110+
public bool CanStartReview => canStartReview.Value;
111+
112+
/// <inheritdoc/>
113+
public string CommitCaption => commitCaption.Value;
114+
115+
/// <inheritdoc/>
116+
public bool IsPending { get; }
117+
118+
/// <inheritdoc/>
119+
public ReactiveCommand<Unit> StartReview { get; }
120+
121+
async Task DoStartReview(object unused)
122+
{
123+
await session.StartReview();
124+
await CommitEdit.ExecuteAsync(null);
125+
}
126+
}
127+
}

src/GitHub.InlineReviews/Views/CommentView.xaml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@
6060
Command="{Binding OpenOnGitHub}"
6161
Foreground="{DynamicResource GitHubVsToolWindowText}"
6262
Opacity="0.75" />
63+
<Border Background="{DynamicResource VsBrush.InfoBackground}"
64+
BorderBrush="{DynamicResource VsBrush.AccentPale}"
65+
BorderThickness="1"
66+
CornerRadius="3"
67+
Padding="2 1"
68+
Visibility="{Binding IsPending, Converter={ui:BooleanToVisibilityConverter}, FallbackValue=Collapsed}">
69+
<TextBlock FontSize="10">Pending</TextBlock>
70+
</Border>
6371
</StackPanel>
6472

6573

@@ -168,7 +176,12 @@
168176
</Style.Triggers>
169177
</Style>
170178
</StackPanel.Style>
171-
<Button Command="{Binding CommitEdit}">Comment</Button>
179+
<Button Command="{Binding CommitEdit}" Content="{Binding CommitCaption}"/>
180+
<Button Margin="4 0 0 0"
181+
Command="{Binding StartReview}"
182+
Visibility="{Binding CanStartReview, Converter={ui:BooleanToVisibilityConverter}}">
183+
Start a review
184+
</Button>
172185
<Button Margin="4 0 0 0" Command="{Binding CancelEdit}">Cancel</Button>
173186
</StackPanel>
174187
</Grid>

0 commit comments

Comments
 (0)