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

Commit b62034a

Browse files
authored
Merge pull request #1442 from github/fixes/1440-clear-PR-session
Allow PR session to close when user changes to non-PR branch
2 parents 2dd3ce5 + 96ee564 commit b62034a

File tree

2 files changed

+80
-33
lines changed

2 files changed

+80
-33
lines changed

src/GitHub.InlineReviews/Services/PullRequestSessionManager.cs

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,10 @@ public PullRequestSessionManager(
6767
this.modelServiceFactory = modelServiceFactory;
6868

6969
Observable.FromEventPattern(teamExplorerContext, nameof(teamExplorerContext.StatusChanged))
70-
.StartWith((EventPattern<object>)null)
7170
.ObserveOn(RxApp.MainThreadScheduler)
72-
.Subscribe(_ => RepoChanged(teamExplorerContext.ActiveRepository).Forget());
71+
.Subscribe(_ => StatusChanged().Forget());
7372

7473
teamExplorerContext.WhenAnyValue(x => x.ActiveRepository)
75-
.Skip(1)
7674
.ObserveOn(RxApp.MainThreadScheduler)
7775
.Subscribe(x => RepoChanged(x).Forget());
7876
}
@@ -159,8 +157,8 @@ public async Task<IPullRequestSession> GetSession(IPullRequestModel pullRequest)
159157
{
160158
// The branch for the PR was not previously marked with the PR number in the git
161159
// config so we didn't pick up that the current branch is a PR branch. That has
162-
// now been corrected, so call RepoChanged to make sure everything is up-to-date.
163-
await RepoChanged(repository);
160+
// now been corrected, so call StatusChanged to make sure everything is up-to-date.
161+
await StatusChanged();
164162
}
165163

166164
return await GetSessionInternal(pullRequest);
@@ -191,29 +189,33 @@ public PullRequestTextBufferInfo GetTextBufferInfo(ITextBuffer buffer)
191189

192190
async Task RepoChanged(ILocalRepositoryModel localRepositoryModel)
193191
{
194-
try
195-
{
196-
if (localRepositoryModel != repository)
197-
{
198-
repository = localRepositoryModel;
199-
CurrentSession = null;
200-
sessions.Clear();
201-
}
192+
repository = localRepositoryModel;
193+
CurrentSession = null;
194+
sessions.Clear();
202195

203-
if (string.IsNullOrWhiteSpace(localRepositoryModel?.CloneUrl)) return;
196+
if (localRepositoryModel != null)
197+
{
198+
await StatusChanged();
199+
}
200+
}
204201

205-
var modelService = await connectionManager.GetModelService(repository, modelServiceFactory);
202+
async Task StatusChanged()
203+
{
204+
try
205+
{
206206
var session = CurrentSession;
207207

208-
if (modelService != null)
208+
var pr = await service.GetPullRequestForCurrentBranch(repository).FirstOrDefaultAsync();
209+
if (pr != null)
209210
{
210-
var pr = await service.GetPullRequestForCurrentBranch(localRepositoryModel).FirstOrDefaultAsync();
211+
var changePR =
212+
pr.Item1 != (session?.PullRequest.Base.RepositoryCloneUrl.Owner) ||
213+
pr.Item2 != (session?.PullRequest.Number);
211214

212-
if (pr?.Item1 != (CurrentSession?.PullRequest.Base.RepositoryCloneUrl.Owner) ||
213-
pr?.Item2 != (CurrentSession?.PullRequest.Number))
215+
if (changePR)
214216
{
215-
var pullRequest = await GetPullRequestForTip(modelService, localRepositoryModel);
216-
217+
var modelService = await connectionManager.GetModelService(repository, modelServiceFactory);
218+
var pullRequest = await modelService?.GetPullRequest(pr.Item1, repository.Name, pr.Item2);
217219
if (pullRequest != null)
218220
{
219221
var newSession = await GetSessionInternal(pullRequest);
@@ -235,17 +237,6 @@ async Task RepoChanged(ILocalRepositoryModel localRepositoryModel)
235237
}
236238
}
237239

238-
async Task<IPullRequestModel> GetPullRequestForTip(IModelService modelService, ILocalRepositoryModel localRepositoryModel)
239-
{
240-
if (modelService != null)
241-
{
242-
var pr = await service.GetPullRequestForCurrentBranch(localRepositoryModel);
243-
if (pr != null) return await modelService.GetPullRequest(pr.Item1, localRepositoryModel.Name, pr.Item2).ToTask();
244-
}
245-
246-
return null;
247-
}
248-
249240
async Task<PullRequestSession> GetSessionInternal(IPullRequestModel pullRequest)
250241
{
251242
PullRequestSession session = null;

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

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Microsoft.VisualStudio.Utilities;
1919
using NSubstitute;
2020
using NUnit.Framework;
21+
using System.ComponentModel;
2122

2223
namespace GitHub.InlineReviews.UnitTests.Services
2324
{
@@ -127,6 +128,60 @@ public void CurrentSessionChangesWhenBranchChanges()
127128
Assert.That(session, Is.Not.SameAs(target.CurrentSession));
128129
}
129130

131+
[Test]
132+
public void LocalRepositoryModelNull()
133+
{
134+
var repositoryModel = null as LocalRepositoryModel;
135+
var target = new PullRequestSessionManager(
136+
CreatePullRequestService(),
137+
Substitute.For<IPullRequestSessionService>(),
138+
CreateConnectionManager(),
139+
CreateModelServiceFactory(),
140+
CreateTeamExplorerContext(null));
141+
142+
Assert.That(target.CurrentSession, Is.Null);
143+
}
144+
145+
[Test]
146+
public void CurrentSessionChangesToNullIfNoPullRequestForCurrentBranch()
147+
{
148+
var service = CreatePullRequestService();
149+
var teamExplorerContext = CreateTeamExplorerContext(CreateRepositoryModel());
150+
var target = new PullRequestSessionManager(
151+
service,
152+
Substitute.For<IPullRequestSessionService>(),
153+
CreateConnectionManager(),
154+
CreateModelServiceFactory(),
155+
teamExplorerContext);
156+
Assert.That(target.CurrentSession, Is.Not.Null);
157+
158+
Tuple<string, int> newPullRequest = null;
159+
service.GetPullRequestForCurrentBranch(null).ReturnsForAnyArgs(Observable.Return(newPullRequest));
160+
teamExplorerContext.StatusChanged += Raise.Event();
161+
162+
var session = target.CurrentSession;
163+
164+
Assert.That(session, Is.Null);
165+
}
166+
167+
[Test]
168+
public void CurrentSessionChangesToNullWhenRepoChangedToNull()
169+
{
170+
var teamExplorerContext = CreateTeamExplorerContext(CreateRepositoryModel());
171+
var target = new PullRequestSessionManager(
172+
CreatePullRequestService(),
173+
Substitute.For<IPullRequestSessionService>(),
174+
CreateConnectionManager(),
175+
CreateModelServiceFactory(),
176+
teamExplorerContext);
177+
Assert.That(target.CurrentSession, Is.Not.Null);
178+
179+
SetActiveRepository(teamExplorerContext, null);
180+
var session = target.CurrentSession;
181+
182+
Assert.That(session, Is.Null);
183+
}
184+
130185
[Test]
131186
public void CurrentSessionChangesWhenRepoChanged()
132187
{
@@ -1012,7 +1067,8 @@ static ITeamExplorerContext CreateTeamExplorerContext(ILocalRepositoryModel repo
10121067
static void SetActiveRepository(ITeamExplorerContext teamExplorerContext, ILocalRepositoryModel localRepositoryModel)
10131068
{
10141069
teamExplorerContext.ActiveRepository.Returns(localRepositoryModel);
1015-
teamExplorerContext.StatusChanged += Raise.Event();
1070+
var eventArgs = new PropertyChangedEventArgs(nameof(teamExplorerContext.ActiveRepository));
1071+
teamExplorerContext.PropertyChanged += Raise.Event<PropertyChangedEventHandler>(teamExplorerContext, eventArgs);
10161072
}
10171073
}
10181074
}

0 commit comments

Comments
 (0)