33using System . Reactive ;
44using System . Threading . Tasks ;
55using GitHub . Extensions ;
6+ using GitHub . Logging ;
67using GitHub . Models ;
78using GitHub . Services ;
89using ReactiveUI ;
10+ using Serilog ;
911
1012namespace GitHub . ViewModels . GitHubPane
1113{
@@ -14,6 +16,8 @@ namespace GitHub.ViewModels.GitHubPane
1416 /// </summary>
1517 public class PullRequestReviewCommentViewModel : IPullRequestReviewFileCommentViewModel
1618 {
19+ static readonly ILogger log = LogManager . ForContext < PullRequestReviewCommentViewModel > ( ) ;
20+
1721 readonly IPullRequestEditorService editorService ;
1822 readonly IPullRequestSession session ;
1923 readonly PullRequestReviewCommentModel model ;
@@ -52,18 +56,34 @@ async Task DoOpen()
5256 {
5357 if ( thread == null )
5458 {
55- var file = await session . GetFile ( RelativePath , model . Thread . CommitSha ) ;
56- thread = file . InlineCommentThreads . FirstOrDefault ( t => t . Comments . Any ( c => c . Comment . Id == model . Id ) ) ;
59+ if ( model . Thread . IsOutdated )
60+ {
61+ var file = await session . GetFile ( RelativePath , model . Thread . OriginalCommitSha ) ;
62+ thread = file . InlineCommentThreads . FirstOrDefault ( t => t . Comments . Any ( c => c . Comment . Id == model . Id ) ) ;
63+ }
64+ else
65+ {
66+ var file = await session . GetFile ( RelativePath , model . Thread . CommitSha ) ;
67+ thread = file . InlineCommentThreads . FirstOrDefault ( t => t . Comments . Any ( c => c . Comment . Id == model . Id ) ) ;
68+
69+ if ( thread ? . LineNumber == - 1 )
70+ {
71+ log . Warning ( "Couldn't find line number for comment on {RelativePath} @ {CommitSha}" , RelativePath , model . Thread . CommitSha ) ;
72+ // Fall back to opening outdated file if we can't find a line number for the comment
73+ file = await session . GetFile ( RelativePath , model . Thread . OriginalCommitSha ) ;
74+ thread = file . InlineCommentThreads . FirstOrDefault ( t => t . Comments . Any ( c => c . Comment . Id == model . Id ) ) ;
75+ }
76+ }
5777 }
5878
5979 if ( thread != null && thread . LineNumber != - 1 )
6080 {
6181 await editorService . OpenDiff ( session , RelativePath , thread ) ;
6282 }
6383 }
64- catch ( Exception )
84+ catch ( Exception e )
6585 {
66- // TODO: Show error.
86+ log . Error ( e , nameof ( DoOpen ) ) ;
6787 }
6888 }
6989 }
0 commit comments