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

Commit 8e2afe2

Browse files
committed
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.
1 parent 2c79611 commit 8e2afe2

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -161,35 +161,43 @@ public override async Task PostComment(ICommentViewModel comment)
161161
{
162162
Guard.ArgumentNotNull(comment, nameof(comment));
163163

164-
if (IsNewThread)
164+
await DeleteDraft(comment).ConfigureAwait(false);
165+
166+
try
165167
{
166-
var diffPosition = File.Diff
167-
.SelectMany(x => x.Lines)
168-
.FirstOrDefault(x =>
168+
if (IsNewThread)
169+
{
170+
var diffPosition = File.Diff
171+
.SelectMany(x => x.Lines)
172+
.FirstOrDefault(x =>
173+
{
174+
var line = Side == DiffSide.Left ? x.OldLineNumber : x.NewLineNumber;
175+
return line == LineNumber + 1;
176+
});
177+
178+
if (diffPosition == null)
169179
{
170-
var line = Side == DiffSide.Left ? x.OldLineNumber : x.NewLineNumber;
171-
return line == LineNumber + 1;
172-
});
173-
174-
if (diffPosition == null)
180+
throw new InvalidOperationException("Unable to locate line in diff.");
181+
}
182+
183+
await Session.PostReviewComment(
184+
comment.Body,
185+
File.CommitSha,
186+
File.RelativePath.Replace("\\", "/"),
187+
File.Diff,
188+
diffPosition.DiffLineNumber).ConfigureAwait(false);
189+
}
190+
else
175191
{
176-
throw new InvalidOperationException("Unable to locate line in diff.");
192+
var replyId = Comments[0].Id;
193+
await Session.PostReviewComment(comment.Body, replyId).ConfigureAwait(false);
177194
}
178-
179-
await Session.PostReviewComment(
180-
comment.Body,
181-
File.CommitSha,
182-
File.RelativePath.Replace("\\", "/"),
183-
File.Diff,
184-
diffPosition.DiffLineNumber).ConfigureAwait(false);
185195
}
186-
else
196+
catch
187197
{
188-
var replyId = Comments[0].Id;
189-
await Session.PostReviewComment(comment.Body, replyId).ConfigureAwait(false);
198+
UpdateDraft(comment).Forget();
199+
throw;
190200
}
191-
192-
await DeleteDraft(comment).ConfigureAwait(false);
193201
}
194202

195203
public override async Task EditComment(ICommentViewModel comment)
@@ -235,5 +243,12 @@ protected override (string key, string secondaryKey) GetDraftKeys(ICommentViewMo
235243
File.RelativePath,
236244
LineNumber);
237245
}
246+
247+
async Task UpdateDraft(ICommentViewModel comment)
248+
{
249+
var draft = BuildDraft(comment);
250+
var (key, secondaryKey) = GetDraftKeys(comment);
251+
await DraftStore.UpdateDraft(key, secondaryKey, draft).ConfigureAwait(true);
252+
}
238253
}
239254
}

0 commit comments

Comments
 (0)