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

Commit

Permalink
Merge pull request #2027 from github/fixes/draft-appears-after-submit
Browse files Browse the repository at this point in the history
Fix draft appearing after sucessful submit.
  • Loading branch information
jcansdale authored Nov 5, 2018
2 parents 2c79611 + 18cc2db commit 8cea51b
Showing 1 changed file with 44 additions and 28 deletions.
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);
}
}
}

0 comments on commit 8cea51b

Please sign in to comment.