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

Commit 428a590

Browse files
Merge pull request #1822 from github/donokuda/not-delete-nooooooo
Show a confirmation when attempting to delete a comment
2 parents 5227859 + 9eba416 commit 428a590

18 files changed

+174
-53
lines changed

src/GitHub.InlineReviews/GitHub.InlineReviews.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
<Compile Include="Margins\InlineCommentMargin.cs" />
8585
<Compile Include="Margins\InlineCommentMarginVisible.cs" />
8686
<Compile Include="Margins\InlineCommentMarginEnabled.cs" />
87+
<Compile Include="Services\CommentService.cs" />
88+
<Compile Include="Services\ICommentService.cs" />
8789
<Compile Include="PullRequestStatusBarPackage.cs" />
8890
<Compile Include="InlineReviewsPackage.cs" />
8991
<Compile Include="Models\InlineCommentThreadModel.cs" />

src/GitHub.InlineReviews/Peek/InlineCommentPeekableItemSource.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ class InlineCommentPeekableItemSource : IPeekableItemSource
1616
readonly IPullRequestSessionManager sessionManager;
1717
readonly INextInlineCommentCommand nextCommentCommand;
1818
readonly IPreviousInlineCommentCommand previousCommentCommand;
19+
readonly ICommentService commentService;
1920

20-
public InlineCommentPeekableItemSource(
21-
IInlineCommentPeekService peekService,
21+
public InlineCommentPeekableItemSource(IInlineCommentPeekService peekService,
2222
IPullRequestSessionManager sessionManager,
2323
INextInlineCommentCommand nextCommentCommand,
24-
IPreviousInlineCommentCommand previousCommentCommand)
24+
IPreviousInlineCommentCommand previousCommentCommand,
25+
ICommentService commentService)
2526
{
2627
this.peekService = peekService;
2728
this.sessionManager = sessionManager;
2829
this.nextCommentCommand = nextCommentCommand;
2930
this.previousCommentCommand = previousCommentCommand;
31+
this.commentService = commentService;
3032
}
3133

3234
public void AugmentPeekSession(IPeekSession session, IList<IPeekableItem> peekableItems)
@@ -38,7 +40,8 @@ public void AugmentPeekSession(IPeekSession session, IList<IPeekableItem> peekab
3840
session,
3941
sessionManager,
4042
nextCommentCommand,
41-
previousCommentCommand);
43+
previousCommentCommand,
44+
commentService);
4245
viewModel.Initialize().Forget();
4346
peekableItems.Add(new InlineCommentPeekableItem(viewModel));
4447
}

src/GitHub.InlineReviews/Peek/InlineCommentPeekableItemSourceProvider.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@ class InlineCommentPeekableItemSourceProvider : IPeekableItemSourceProvider
2020
readonly IPullRequestSessionManager sessionManager;
2121
readonly INextInlineCommentCommand nextCommentCommand;
2222
readonly IPreviousInlineCommentCommand previousCommentCommand;
23+
readonly ICommentService commentService;
2324

2425
[ImportingConstructor]
2526
public InlineCommentPeekableItemSourceProvider(
2627
IInlineCommentPeekService peekService,
2728
IPullRequestSessionManager sessionManager,
2829
INextInlineCommentCommand nextCommentCommand,
29-
IPreviousInlineCommentCommand previousCommentCommand)
30+
IPreviousInlineCommentCommand previousCommentCommand,
31+
ICommentService commentService)
3032
{
3133
this.peekService = peekService;
3234
this.sessionManager = sessionManager;
3335
this.nextCommentCommand = nextCommentCommand;
3436
this.previousCommentCommand = previousCommentCommand;
37+
this.commentService = commentService;
3538
}
3639

3740
public IPeekableItemSource TryCreatePeekableItemSource(ITextBuffer textBuffer)
@@ -40,7 +43,8 @@ public IPeekableItemSource TryCreatePeekableItemSource(ITextBuffer textBuffer)
4043
peekService,
4144
sessionManager,
4245
nextCommentCommand,
43-
previousCommentCommand);
46+
previousCommentCommand,
47+
commentService);
4448
}
4549
}
4650
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.ComponentModel.Composition;
2+
using System.Windows.Forms;
3+
4+
namespace GitHub.InlineReviews.Services
5+
{
6+
[Export(typeof(ICommentService))]
7+
[PartCreationPolicy(CreationPolicy.NonShared)]
8+
public class CommentService:ICommentService
9+
{
10+
public bool ConfirmCommentDelete()
11+
{
12+
return MessageBox.Show(
13+
VisualStudio.UI.Resources.DeleteCommentConfirmation,
14+
VisualStudio.UI.Resources.DeleteCommentConfirmationCaption,
15+
MessageBoxButtons.YesNo,
16+
MessageBoxIcon.Question) == DialogResult.Yes;
17+
}
18+
}
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace GitHub.InlineReviews.Services
2+
{
3+
/// <summary>
4+
/// This service allows for functionality to be injected into the chain of different peek Comment ViewModel types.
5+
/// </summary>
6+
public interface ICommentService
7+
{
8+
/// <summary>
9+
/// This function uses MessageBox.Show to display a confirmation if a comment should be deleted.
10+
/// </summary>
11+
/// <returns></returns>
12+
bool ConfirmCommentDelete();
13+
}
14+
}

src/GitHub.InlineReviews/ViewModels/CommentViewModel.cs

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using System.Reactive;
44
using System.Reactive.Linq;
55
using System.Threading.Tasks;
6+
using System.Windows;
67
using GitHub.Extensions;
8+
using GitHub.InlineReviews.Services;
79
using GitHub.Logging;
810
using GitHub.Models;
911
using GitHub.ViewModels;
@@ -18,6 +20,7 @@ namespace GitHub.InlineReviews.ViewModels
1820
public class CommentViewModel : ReactiveObject, ICommentViewModel
1921
{
2022
static readonly ILogger log = LogManager.ForContext<CommentViewModel>();
23+
ICommentService commentService;
2124
string body;
2225
string errorMessage;
2326
bool isReadOnly;
@@ -30,6 +33,7 @@ public class CommentViewModel : ReactiveObject, ICommentViewModel
3033
/// <summary>
3134
/// Initializes a new instance of the <see cref="CommentViewModel"/> class.
3235
/// </summary>
36+
/// <param name="commentService">The comment service</param>
3337
/// <param name="thread">The thread that the comment is a part of.</param>
3438
/// <param name="currentUser">The current user.</param>
3539
/// <param name="pullRequestId">The pull request id of the comment.</param>
@@ -41,6 +45,7 @@ public class CommentViewModel : ReactiveObject, ICommentViewModel
4145
/// <param name="updatedAt">The modified date of the comment.</param>
4246
/// <param name="webUrl"></param>
4347
protected CommentViewModel(
48+
ICommentService commentService,
4449
ICommentThreadViewModel thread,
4550
IActorViewModel currentUser,
4651
int pullRequestId,
@@ -52,6 +57,7 @@ protected CommentViewModel(
5257
DateTimeOffset updatedAt,
5358
Uri webUrl)
5459
{
60+
this.commentService = commentService;
5561
Guard.ArgumentNotNull(thread, nameof(thread));
5662
Guard.ArgumentNotNull(currentUser, nameof(currentUser));
5763
Guard.ArgumentNotNull(author, nameof(author));
@@ -102,14 +108,17 @@ protected CommentViewModel(
102108
/// <summary>
103109
/// Initializes a new instance of the <see cref="CommentViewModel"/> class.
104110
/// </summary>
111+
/// <param name="commentService">Comment Service</param>
105112
/// <param name="thread">The thread that the comment is a part of.</param>
106113
/// <param name="currentUser">The current user.</param>
107114
/// <param name="model">The comment model.</param>
108115
protected CommentViewModel(
116+
ICommentService commentService,
109117
ICommentThreadViewModel thread,
110118
ActorModel currentUser,
111119
CommentModel model)
112120
: this(
121+
commentService,
113122
thread,
114123
new ActorViewModel(currentUser),
115124
model.PullRequestId,
@@ -130,22 +139,25 @@ protected void AddErrorHandler<T>(ReactiveCommand<T> command)
130139

131140
async Task DoDelete(object unused)
132141
{
133-
try
142+
if (commentService.ConfirmCommentDelete())
134143
{
135-
ErrorMessage = null;
136-
IsSubmitting = true;
144+
try
145+
{
146+
ErrorMessage = null;
147+
IsSubmitting = true;
137148

138-
await Thread.DeleteComment.ExecuteAsyncTask(new Tuple<int, int>(PullRequestId, DatabaseId));
139-
}
140-
catch (Exception e)
141-
{
142-
var message = e.Message;
143-
ErrorMessage = message;
144-
log.Error(e, "Error Deleting comment");
145-
}
146-
finally
147-
{
148-
IsSubmitting = false;
149+
await Thread.DeleteComment.ExecuteAsyncTask(new Tuple<int, int>(PullRequestId, DatabaseId));
150+
}
151+
catch (Exception e)
152+
{
153+
var message = e.Message;
154+
ErrorMessage = message;
155+
log.Error(e, "Error Deleting comment");
156+
}
157+
finally
158+
{
159+
IsSubmitting = false;
160+
}
149161
}
150162
}
151163

src/GitHub.InlineReviews/ViewModels/InlineCommentPeekViewModel.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using GitHub.Extensions.Reactive;
1111
using GitHub.Factories;
1212
using GitHub.InlineReviews.Commands;
13+
using GitHub.InlineReviews.Peek;
1314
using GitHub.InlineReviews.Services;
1415
using GitHub.Logging;
1516
using GitHub.Models;
@@ -31,6 +32,7 @@ public sealed class InlineCommentPeekViewModel : ReactiveObject, IDisposable
3132
readonly IInlineCommentPeekService peekService;
3233
readonly IPeekSession peekSession;
3334
readonly IPullRequestSessionManager sessionManager;
35+
readonly ICommentService commentService;
3436
IPullRequestSession session;
3537
IPullRequestSessionFile file;
3638
ICommentThreadViewModel thread;
@@ -44,12 +46,12 @@ public sealed class InlineCommentPeekViewModel : ReactiveObject, IDisposable
4446
/// <summary>
4547
/// Initializes a new instance of the <see cref="InlineCommentPeekViewModel"/> class.
4648
/// </summary>
47-
public InlineCommentPeekViewModel(
48-
IInlineCommentPeekService peekService,
49+
public InlineCommentPeekViewModel(IInlineCommentPeekService peekService,
4950
IPeekSession peekSession,
5051
IPullRequestSessionManager sessionManager,
5152
INextInlineCommentCommand nextCommentCommand,
52-
IPreviousInlineCommentCommand previousCommentCommand)
53+
IPreviousInlineCommentCommand previousCommentCommand,
54+
ICommentService commentService)
5355
{
5456
Guard.ArgumentNotNull(peekService, nameof(peekService));
5557
Guard.ArgumentNotNull(peekSession, nameof(peekSession));
@@ -60,6 +62,7 @@ public InlineCommentPeekViewModel(
6062
this.peekService = peekService;
6163
this.peekSession = peekSession;
6264
this.sessionManager = sessionManager;
65+
this.commentService = commentService;
6366
triggerPoint = peekSession.GetTriggerPoint(peekSession.TextView.TextBuffer);
6467

6568
peekSession.Dismissed += (s, e) => Dispose();
@@ -180,11 +183,11 @@ async Task UpdateThread()
180183

181184
if (thread != null)
182185
{
183-
Thread = new InlineCommentThreadViewModel(session, thread.Comments);
186+
Thread = new InlineCommentThreadViewModel(commentService, session, thread.Comments);
184187
}
185188
else
186189
{
187-
Thread = new NewInlineCommentThreadViewModel(session, file, lineNumber, leftBuffer);
190+
Thread = new NewInlineCommentThreadViewModel(commentService, session, file, lineNumber, leftBuffer);
188191
}
189192

190193
if (!string.IsNullOrWhiteSpace(placeholderBody))

src/GitHub.InlineReviews/ViewModels/InlineCommentThreadViewModel.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reactive.Linq;
55
using System.Threading.Tasks;
66
using GitHub.Extensions;
7+
using GitHub.InlineReviews.Services;
78
using GitHub.Models;
89
using GitHub.Services;
910
using ReactiveUI;
@@ -18,10 +19,10 @@ public class InlineCommentThreadViewModel : CommentThreadViewModel
1819
/// <summary>
1920
/// Initializes a new instance of the <see cref="InlineCommentThreadViewModel"/> class.
2021
/// </summary>
22+
/// <param name="commentService">The comment service</param>
2123
/// <param name="session">The current PR review session.</param>
2224
/// <param name="comments">The comments to display in this inline review.</param>
23-
public InlineCommentThreadViewModel(
24-
IPullRequestSession session,
25+
public InlineCommentThreadViewModel(ICommentService commentService, IPullRequestSession session,
2526
IEnumerable<InlineCommentModel> comments)
2627
: base(session.User)
2728
{
@@ -45,13 +46,14 @@ public InlineCommentThreadViewModel(
4546
{
4647
Comments.Add(new PullRequestReviewCommentViewModel(
4748
session,
49+
commentService,
4850
this,
4951
CurrentUser,
5052
comment.Review,
5153
comment.Comment));
5254
}
5355

54-
Comments.Add(PullRequestReviewCommentViewModel.CreatePlaceholder(session, this, CurrentUser));
56+
Comments.Add(PullRequestReviewCommentViewModel.CreatePlaceholder(session, commentService, this, CurrentUser));
5557
}
5658

5759
/// <summary>

src/GitHub.InlineReviews/ViewModels/NewInlineCommentThreadViewModel.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Reactive.Linq;
55
using System.Threading.Tasks;
66
using GitHub.Extensions;
7+
using GitHub.InlineReviews.Services;
78
using GitHub.Models;
89
using GitHub.Services;
910
using ReactiveUI;
@@ -20,13 +21,14 @@ public class NewInlineCommentThreadViewModel : CommentThreadViewModel
2021
/// <summary>
2122
/// Initializes a new instance of the <see cref="InlineCommentThreadViewModel"/> class.
2223
/// </summary>
24+
/// <param name="commentService">The comment service</param>
2325
/// <param name="session">The current PR review session.</param>
2426
/// <param name="file">The file being commented on.</param>
2527
/// <param name="lineNumber">The 0-based line number in the file.</param>
2628
/// <param name="leftComparisonBuffer">
27-
/// True if the comment is being left on the left-hand-side of a diff; otherwise false.
29+
/// True if the comment is being left on the left-hand-side of a diff; otherwise false.
2830
/// </param>
29-
public NewInlineCommentThreadViewModel(
31+
public NewInlineCommentThreadViewModel(ICommentService commentService,
3032
IPullRequestSession session,
3133
IPullRequestSessionFile file,
3234
int lineNumber,
@@ -53,7 +55,7 @@ public NewInlineCommentThreadViewModel(
5355
Observable.Return(false),
5456
o => null);
5557

56-
var placeholder = PullRequestReviewCommentViewModel.CreatePlaceholder(session, this, CurrentUser);
58+
var placeholder = PullRequestReviewCommentViewModel.CreatePlaceholder(session, commentService, this, CurrentUser);
5759
placeholder.BeginEdit.Execute(null);
5860
this.WhenAnyValue(x => x.NeedsPush).Subscribe(x => placeholder.IsReadOnly = x);
5961
Comments.Add(placeholder);

0 commit comments

Comments
 (0)