Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -219,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)
Expand All @@ -235,5 +244,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);
}
}
}