From 8e2afe278130959bf352d96a870a6192210c60f0 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 2 Nov 2018 11:03:04 +0100 Subject: [PATCH 1/2] Fix draft appearing after sucessful submit. In certain situations, the inline comment thread was refreshed before the `DeleteDraft(comment)` line in `PullRequestReviewCommentThreadViewModel.PostComment` was run, meaning that the draft would be displayed _after_ the comment was succesfully submitted. Work around this by deleting the draft before submitting the comment, and if we get an error re-insert the draft. --- ...PullRequestReviewCommentThreadViewModel.cs | 59 ++++++++++++------- 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs b/src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs index e0802b8fac..22b2793360 100644 --- a/src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs +++ b/src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs @@ -161,35 +161,43 @@ public override async Task PostComment(ICommentViewModel comment) { Guard.ArgumentNotNull(comment, nameof(comment)); - if (IsNewThread) + await DeleteDraft(comment).ConfigureAwait(false); + + try { - var diffPosition = File.Diff - .SelectMany(x => x.Lines) - .FirstOrDefault(x => + if (IsNewThread) + { + var diffPosition = File.Diff + .SelectMany(x => x.Lines) + .FirstOrDefault(x => + { + var line = Side == DiffSide.Left ? x.OldLineNumber : x.NewLineNumber; + return line == LineNumber + 1; + }); + + if (diffPosition == null) { - var line = Side == DiffSide.Left ? x.OldLineNumber : x.NewLineNumber; - return line == LineNumber + 1; - }); - - if (diffPosition == null) + throw new InvalidOperationException("Unable to locate line in diff."); + } + + await Session.PostReviewComment( + comment.Body, + File.CommitSha, + File.RelativePath.Replace("\\", "/"), + File.Diff, + diffPosition.DiffLineNumber).ConfigureAwait(false); + } + else { - throw new InvalidOperationException("Unable to locate line in diff."); + var replyId = Comments[0].Id; + await Session.PostReviewComment(comment.Body, replyId).ConfigureAwait(false); } - - await Session.PostReviewComment( - comment.Body, - File.CommitSha, - File.RelativePath.Replace("\\", "/"), - File.Diff, - diffPosition.DiffLineNumber).ConfigureAwait(false); } - else + catch { - var replyId = Comments[0].Id; - await Session.PostReviewComment(comment.Body, replyId).ConfigureAwait(false); + UpdateDraft(comment).Forget(); + throw; } - - await DeleteDraft(comment).ConfigureAwait(false); } public override async Task EditComment(ICommentViewModel comment) @@ -235,5 +243,12 @@ protected override (string key, string secondaryKey) GetDraftKeys(ICommentViewMo File.RelativePath, LineNumber); } + + async Task UpdateDraft(ICommentViewModel comment) + { + var draft = BuildDraft(comment); + var (key, secondaryKey) = GetDraftKeys(comment); + await DraftStore.UpdateDraft(key, secondaryKey, draft).ConfigureAwait(true); + } } } From 18cc2db74c70af98ffdf2bd1360839889764c459 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 2 Nov 2018 11:28:32 +0100 Subject: [PATCH 2/2] Don't save drafts of empty comments. --- .../PullRequestReviewCommentThreadViewModel.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs b/src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs index 22b2793360..43b0d299ed 100644 --- a/src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs +++ b/src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs @@ -227,12 +227,13 @@ public static (string key, string secondaryKey) GetDraftKeys( protected override CommentDraft BuildDraft(ICommentViewModel comment) { - return new PullRequestReviewCommentDraft - { - Body = comment.Body, - Side = Side, - UpdatedAt = DateTimeOffset.UtcNow, - }; + return !string.IsNullOrEmpty(comment.Body) ? + new PullRequestReviewCommentDraft + { + Body = comment.Body, + Side = Side, + UpdatedAt = DateTimeOffset.UtcNow, + } : null; } protected override (string key, string secondaryKey) GetDraftKeys(ICommentViewModel comment)