Skip to content

Commit b99e86b

Browse files
authored
Clean up cancelled review comments (#7993)
Fixes #7992
1 parent 3b5839a commit b99e86b

1 file changed

Lines changed: 33 additions & 3 deletions

File tree

src/github/pullRequestModel.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,10 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
626626
*/
627627
async deleteReview(): Promise<{ deletedReviewId: number; deletedReviewComments: IComment[] }> {
628628
const pendingReviewId = await this.getPendingReviewId();
629+
if (!pendingReviewId) {
630+
throw new Error(`No pending review found for pull request #${this.number}.`);
631+
}
632+
629633
const { mutate, schema } = await this.githubRepository.ensure();
630634
const { data } = await mutate<DeleteReviewResponse>({
631635
mutation: schema.DeleteReview,
@@ -635,15 +639,41 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
635639
});
636640

637641
const { comments, databaseId } = data!.deletePullRequestReview.pullRequestReview;
642+
const deletedReviewComments = comments.nodes.map(comment => parseGraphQLComment(comment, false, this.githubRepository));
643+
644+
// Update local state: remove all draft comments (and their threads if emptied) that belonged to the deleted review
645+
const deletedCommentIds = new Set(deletedReviewComments.map(c => c.id));
646+
const changedThreads: IReviewThread[] = [];
647+
const removedThreads: IReviewThread[] = [];
648+
for (let i = this._reviewThreadsCache.length - 1; i >= 0; i--) {
649+
const thread = this._reviewThreadsCache[i];
650+
const originalLength = thread.comments.length;
651+
thread.comments = thread.comments.filter(c => !deletedCommentIds.has(c.id));
652+
if (thread.comments.length === 0 && originalLength > 0) {
653+
// Entire thread was composed only of comments from the deleted review; remove it.
654+
this._reviewThreadsCache.splice(i, 1);
655+
removedThreads.push(thread);
656+
} else if (thread.comments.length !== originalLength) {
657+
changedThreads.push(thread);
658+
}
659+
}
660+
if (changedThreads.length > 0 || removedThreads.length > 0) {
661+
this._onDidChangeReviewThreads.fire({ added: [], changed: changedThreads, removed: removedThreads });
662+
}
663+
664+
// Remove from flat comments collection
665+
if (this._comments) {
666+
this.comments = this._comments.filter(c => !deletedCommentIds.has(c.id));
667+
}
638668

639669
this.hasPendingReview = false;
640670
await this.updateDraftModeContext();
641671

642-
this.getReviewThreads();
643-
this._onDidChange.fire({ timeline: true });
672+
// Fire change event to update timeline & comment views
673+
this._onDidChange.fire({ timeline: true, comments: true });
644674
return {
645675
deletedReviewId: databaseId,
646-
deletedReviewComments: comments.nodes.map(comment => parseGraphQLComment(comment, false, this.githubRepository)),
676+
deletedReviewComments,
647677
};
648678
}
649679

0 commit comments

Comments
 (0)