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

Commit df93480

Browse files
Merge branch 'features/check-suite-annotations' into features/check-suite-annotations-inline
2 parents 2b0970a + e705559 commit df93480

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
@@ -163,35 +163,43 @@ public override async Task PostComment(ICommentViewModel comment)
163163
{
164164
Guard.ArgumentNotNull(comment, nameof(comment));
165165

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

197205
public override async Task EditComment(ICommentViewModel comment)
@@ -221,12 +229,13 @@ public static (string key, string secondaryKey) GetDraftKeys(
221229

222230
protected override CommentDraft BuildDraft(ICommentViewModel comment)
223231
{
224-
return new PullRequestReviewCommentDraft
225-
{
226-
Body = comment.Body,
227-
Side = Side,
228-
UpdatedAt = DateTimeOffset.UtcNow,
229-
};
232+
return !string.IsNullOrEmpty(comment.Body) ?
233+
new PullRequestReviewCommentDraft
234+
{
235+
Body = comment.Body,
236+
Side = Side,
237+
UpdatedAt = DateTimeOffset.UtcNow,
238+
} : null;
230239
}
231240

232241
protected override (string key, string secondaryKey) GetDraftKeys(ICommentViewModel comment)
@@ -237,5 +246,12 @@ protected override (string key, string secondaryKey) GetDraftKeys(ICommentViewMo
237246
File.RelativePath,
238247
LineNumber);
239248
}
249+
250+
async Task UpdateDraft(ICommentViewModel comment)
251+
{
252+
var draft = BuildDraft(comment);
253+
var (key, secondaryKey) = GetDraftKeys(comment);
254+
await DraftStore.UpdateDraft(key, secondaryKey, draft).ConfigureAwait(true);
255+
}
240256
}
241257
}

0 commit comments

Comments
 (0)