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

Commit 4600c80

Browse files
authored
Merge branch 'master' into feature/navigate-diff-to-editor
2 parents ad08473 + 204c548 commit 4600c80

File tree

5 files changed

+96
-22
lines changed

5 files changed

+96
-22
lines changed

src/GitHub.App/ViewModels/GitHubPane/NavigationViewModel.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public class NavigationViewModel : ViewModelBase, INavigationViewModel
2525
public NavigationViewModel()
2626
{
2727
history = new ReactiveList<IPanePageViewModel>();
28-
history.BeforeItemsAdded.Subscribe(BeforeItemAdded);
29-
history.CollectionChanged += CollectionChanged;
28+
history.Changing.Subscribe(CollectionChanging);
29+
history.Changed.Subscribe(CollectionChanged);
3030

3131
var pos = this.WhenAnyValue(
3232
x => x.Index,
@@ -107,7 +107,7 @@ public bool Forward()
107107
public void Clear()
108108
{
109109
Index = -1;
110-
history.RemoveRange(0, history.Count);
110+
history.Clear();
111111
}
112112

113113
public int RemoveAll(IPanePageViewModel page)
@@ -139,27 +139,34 @@ void ItemRemoved(int removedIndex, IPanePageViewModel page)
139139
}
140140
}
141141

142-
void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
142+
void CollectionChanging(NotifyCollectionChangedEventArgs e)
143143
{
144-
using (DelayChangeNotifications())
144+
if (e.Action == NotifyCollectionChangedAction.Add)
145145
{
146-
switch (e.Action)
146+
foreach (IPanePageViewModel page in e.NewItems)
147147
{
148-
case NotifyCollectionChangedAction.Add:
149-
break;
150-
151-
case NotifyCollectionChangedAction.Remove:
152-
for (var i = 0; i < e.OldItems.Count; ++i)
153-
{
154-
ItemRemoved(e.OldStartingIndex + i, (IPanePageViewModel)e.OldItems[i]);
155-
}
156-
break;
157-
158-
case NotifyCollectionChangedAction.Reset:
159-
throw new NotSupportedException();
148+
BeforeItemAdded(page);
149+
}
150+
}
151+
else if (e.Action == NotifyCollectionChangedAction.Reset)
152+
{
153+
foreach (var page in history)
154+
{
155+
page.Dispose();
156+
}
157+
}
158+
}
160159

161-
default:
162-
throw new NotImplementedException();
160+
void CollectionChanged(NotifyCollectionChangedEventArgs e)
161+
{
162+
using (DelayChangeNotifications())
163+
{
164+
if (e.Action == NotifyCollectionChangedAction.Remove)
165+
{
166+
for (var i = 0; i < e.OldItems.Count; ++i)
167+
{
168+
ItemRemoved(e.OldStartingIndex + i, (IPanePageViewModel)e.OldItems[i]);
169+
}
163170
}
164171
}
165172
}

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)

test/UnitTests/GitHub.App/ViewModels/GitHubPane/NavigationViewModelTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using System.Reactive;
35
using System.Reactive.Linq;
46
using System.Reactive.Subjects;
@@ -309,6 +311,36 @@ public void CallsDeactivatedAndThenDisposedOnPages()
309311
first.Dispose();
310312
});
311313
}
314+
315+
[Test]
316+
public void DoesntThrowWhenHistoryHasMoreThan10Items()
317+
{
318+
var target = new NavigationViewModel();
319+
var pages = new List<IPanePageViewModel>();
320+
321+
for (var i = 0; i < 11; ++i)
322+
{
323+
var page = CreatePage();
324+
pages.Add(page);
325+
target.NavigateTo(page);
326+
}
327+
328+
foreach (var page in pages)
329+
{
330+
page.ClearReceivedCalls();
331+
}
332+
333+
target.Clear();
334+
335+
pages.Last().Received().Deactivated();
336+
pages.Last().Received().Dispose();
337+
338+
foreach (var page in pages.Take(pages.Count - 1))
339+
{
340+
page.DidNotReceive().Deactivated();
341+
page.Received().Dispose();
342+
}
343+
}
312344
}
313345

314346
public class TheRemoveMethod

0 commit comments

Comments
 (0)