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

Commit 204c548

Browse files
authored
Merge pull request #1439 from github/fixes/1435-comment-no-diff-lines
Ignore inline comment if it contains no diff line context
2 parents 3b3ee04 + a7dcbee commit 204c548

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/GitHub.InlineReviews/Models/InlineCommentThreadModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff 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

src/GitHub.InlineReviews/Services/PullRequestSessionService.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
using GitHub.InlineReviews.Models;
1212
using GitHub.Models;
1313
using GitHub.Primitives;
14+
using GitHub.Logging;
1415
using GitHub.Services;
1516
using LibGit2Sharp;
1617
using Microsoft.VisualStudio.Text;
1718
using Microsoft.VisualStudio.Text.Projection;
1819
using ReactiveUI;
20+
using Serilog;
1921

2022
namespace 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,

test/GitHub.InlineReviews.UnitTests/Services/PullRequestSessionServiceTests.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff 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)

0 commit comments

Comments
 (0)