This repository was archived by the owner on Jun 21, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +37
-2
lines changed
test/GitHub.InlineReviews.UnitTests/Services Expand file tree Collapse file tree 3 files changed +37
-2
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ public InlineCommentThreadModel(
3939
4040 Comments = comments . ToList ( ) ;
4141 DiffMatch = diffMatch ;
42+ DiffLineType = diffMatch [ 0 ] . Type ;
4243 OriginalCommitSha = originalCommitSha ;
4344 OriginalPosition = originalPosition ;
4445 RelativePath = relativePath ;
@@ -51,7 +52,7 @@ public InlineCommentThreadModel(
5152 public IList < DiffLine > DiffMatch { get ; }
5253
5354 /// <inheritdoc/>
54- public DiffChangeType DiffLineType => DiffMatch . First ( ) . Type ;
55+ public DiffChangeType DiffLineType { get ; }
5556
5657 /// <inheritdoc/>
5758 public bool IsStale
Original file line number Diff line number Diff line change 1111using GitHub . InlineReviews . Models ;
1212using GitHub . Models ;
1313using GitHub . Primitives ;
14+ using GitHub . Logging ;
1415using GitHub . Services ;
1516using LibGit2Sharp ;
1617using Microsoft . VisualStudio . Text ;
1718using Microsoft . VisualStudio . Text . Projection ;
1819using ReactiveUI ;
20+ using Serilog ;
1921
2022namespace GitHub . InlineReviews . Services
2123{
@@ -25,6 +27,8 @@ namespace GitHub.InlineReviews.Services
2527 [ Export ( typeof ( IPullRequestSessionService ) ) ]
2628 public class PullRequestSessionService : IPullRequestSessionService
2729 {
30+ static readonly ILogger log = LogManager . ForContext < PullRequestSessionService > ( ) ;
31+
2832 readonly IGitService gitService ;
2933 readonly IGitClient gitClient ;
3034 readonly IDiffService diffService ;
@@ -88,6 +92,13 @@ public IReadOnlyList<IInlineCommentThreadModel> BuildCommentThreads(
8892 var chunks = DiffUtilities . ParseFragment ( hunk ) ;
8993 var chunk = chunks . Last ( ) ;
9094 var diffLines = chunk . Lines . Reverse ( ) . Take ( 5 ) . ToList ( ) ;
95+ var firstLine = diffLines . FirstOrDefault ( ) ;
96+ if ( firstLine == null )
97+ {
98+ log . Warning ( "Ignoring in-line comment in {RelativePath} with no diff line context" , relativePath ) ;
99+ continue ;
100+ }
101+
91102 var thread = new InlineCommentThreadModel (
92103 relativePath ,
93104 comments . Key . Item1 ,
Original file line number Diff line number Diff line change @@ -53,6 +53,29 @@ Line 2
5353 }
5454 }
5555
56+ [ Test ]
57+ public async Task IgnoreCommentsWithNoDiffLineContext ( )
58+ {
59+ var baseContents = "Line 1" ;
60+ var headContents = "Line 1" ;
61+
62+ var comment = CreateComment ( @"@@ -10,7 +10,6 @@ class Program" ) ;
63+
64+ using ( var diffService = new FakeDiffService ( FilePath , baseContents ) )
65+ {
66+ var diff = await diffService . Diff ( FilePath , headContents ) ;
67+ var pullRequest = CreatePullRequest ( FilePath , comment ) ;
68+ var target = CreateTarget ( diffService ) ;
69+
70+ var result = target . BuildCommentThreads (
71+ pullRequest ,
72+ FilePath ,
73+ diff ) ;
74+
75+ Assert . That ( result , Is . Empty ) ;
76+ }
77+ }
78+
5679 [ Test ]
5780 public async Task MatchesReviewCommentOnDifferentLine ( )
5881 {
@@ -212,7 +235,7 @@ Line 2
212235
213236 Assert . That ( 3 , Is . EqualTo ( threads [ 0 ] . LineNumber ) ) ;
214237 Assert . That (
215- new [ ]
238+ new [ ]
216239 {
217240 Tuple . Create ( 2 , DiffSide . Right ) ,
218241 Tuple . Create ( 3 , DiffSide . Right )
You can’t perform that action at this time.
0 commit comments