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

Commit 8cea51b

Browse files
authored
Merge pull request #2027 from github/fixes/draft-appears-after-submit
Fix draft appearing after sucessful submit.
2 parents 2c79611 + 18cc2db commit 8cea51b

File tree

1 file changed

+44
-28
lines changed

1 file changed

+44
-28
lines changed

src/GitHub.App/ViewModels/PullRequestReviewCommentThreadViewModel.cs

Lines changed: 44 additions & 28 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)
@@ -219,12 +227,13 @@ public static (string key, string secondaryKey) GetDraftKeys(
219227

220228
protected override CommentDraft BuildDraft(ICommentViewModel comment)
221229
{
222-
return new PullRequestReviewCommentDraft
223-
{
224-
Body = comment.Body,
225-
Side = Side,
226-
UpdatedAt = DateTimeOffset.UtcNow,
227-
};
230+
return !string.IsNullOrEmpty(comment.Body) ?
231+
new PullRequestReviewCommentDraft
232+
{
233+
Body = comment.Body,
234+
Side = Side,
235+
UpdatedAt = DateTimeOffset.UtcNow,
236+
} : null;
228237
}
229238

230239
protected override (string key, string secondaryKey) GetDraftKeys(ICommentViewModel comment)
@@ -235,5 +244,12 @@ protected override (string key, string secondaryKey) GetDraftKeys(ICommentViewMo
235244
File.RelativePath,
236245
LineNumber);
237246
}
247+
248+
async Task UpdateDraft(ICommentViewModel comment)
249+
{
250+
var draft = BuildDraft(comment);
251+
var (key, secondaryKey) = GetDraftKeys(comment);
252+
await DraftStore.UpdateDraft(key, secondaryKey, draft).ConfigureAwait(true);
253+
}
238254
}
239255
}

0 commit comments

Comments
 (0)