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

Commit e3d8de8

Browse files
committed
Allow PR sessions to close on non-PR branch
The CurrentSession will be set to null when user moves to non-PR branch.
1 parent 9eb8dfb commit e3d8de8

File tree

2 files changed

+72
-20
lines changed

2 files changed

+72
-20
lines changed

src/GitHub.InlineReviews/Services/PullRequestSessionManager.cs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -198,22 +198,31 @@ async Task RepoChanged(ILocalRepositoryModel localRepositoryModel)
198198
repository = localRepositoryModel;
199199
CurrentSession = null;
200200
sessions.Clear();
201-
}
202201

203-
if (string.IsNullOrWhiteSpace(localRepositoryModel?.CloneUrl)) return;
202+
if (localRepositoryModel == null)
203+
{
204+
return;
205+
}
206+
207+
if (string.IsNullOrWhiteSpace(localRepositoryModel.CloneUrl))
208+
{
209+
return;
210+
}
211+
}
204212

205-
var modelService = await connectionManager.GetModelService(repository, modelServiceFactory);
206213
var session = CurrentSession;
207214

208-
if (modelService != null)
215+
var pr = await service.GetPullRequestForCurrentBranch(localRepositoryModel).FirstOrDefaultAsync();
216+
if (pr != null)
209217
{
210-
var pr = await service.GetPullRequestForCurrentBranch(localRepositoryModel).FirstOrDefaultAsync();
218+
var changePR =
219+
pr.Item1 != (session?.PullRequest.Base.RepositoryCloneUrl.Owner) ||
220+
pr.Item2 != (session?.PullRequest.Number);
211221

212-
if (pr?.Item1 != (CurrentSession?.PullRequest.Base.RepositoryCloneUrl.Owner) ||
213-
pr?.Item2 != (CurrentSession?.PullRequest.Number))
222+
if (changePR)
214223
{
215-
var pullRequest = await GetPullRequestForTip(modelService, localRepositoryModel);
216-
224+
var modelService = await connectionManager.GetModelService(repository, modelServiceFactory);
225+
var pullRequest = await modelService?.GetPullRequest(pr.Item1, localRepositoryModel.Name, pr.Item2);
217226
if (pullRequest != null)
218227
{
219228
var newSession = await GetSessionInternal(pullRequest);
@@ -235,17 +244,6 @@ async Task RepoChanged(ILocalRepositoryModel localRepositoryModel)
235244
}
236245
}
237246

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-
249247
async Task<PullRequestSession> GetSessionInternal(IPullRequestModel pullRequest)
250248
{
251249
PullRequestSession session = null;

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,60 @@ public void CurrentSessionChangesWhenBranchChanges()
127127
Assert.That(session, Is.Not.SameAs(target.CurrentSession));
128128
}
129129

130+
[Test]
131+
public void LocalRepositoryModelNull()
132+
{
133+
var repositoryModel = null as LocalRepositoryModel;
134+
var target = new PullRequestSessionManager(
135+
CreatePullRequestService(),
136+
Substitute.For<IPullRequestSessionService>(),
137+
CreateConnectionManager(),
138+
CreateModelServiceFactory(),
139+
CreateTeamExplorerContext(null));
140+
141+
Assert.That(target.CurrentSession, Is.Null);
142+
}
143+
144+
[Test]
145+
public void CurrentSessionChangesToNullIfNoPullRequestForCurrentBranch()
146+
{
147+
var service = CreatePullRequestService();
148+
var teamExplorerContext = CreateTeamExplorerContext(CreateRepositoryModel());
149+
var target = new PullRequestSessionManager(
150+
service,
151+
Substitute.For<IPullRequestSessionService>(),
152+
CreateConnectionManager(),
153+
CreateModelServiceFactory(),
154+
teamExplorerContext);
155+
Assert.That(target.CurrentSession, Is.Not.Null);
156+
157+
Tuple<string, int> newPullRequest = null;
158+
service.GetPullRequestForCurrentBranch(null).ReturnsForAnyArgs(Observable.Return(newPullRequest));
159+
teamExplorerContext.StatusChanged += Raise.Event();
160+
161+
var session = target.CurrentSession;
162+
163+
Assert.That(session, Is.Null);
164+
}
165+
166+
[Test]
167+
public void CurrentSessionChangesToNullWhenRepoChangedToNull()
168+
{
169+
var teamExplorerContext = CreateTeamExplorerContext(CreateRepositoryModel());
170+
var target = new PullRequestSessionManager(
171+
CreatePullRequestService(),
172+
Substitute.For<IPullRequestSessionService>(),
173+
CreateConnectionManager(),
174+
CreateModelServiceFactory(),
175+
teamExplorerContext);
176+
Assert.That(target.CurrentSession, Is.Not.Null);
177+
178+
SetActiveRepository(teamExplorerContext, null);
179+
var session = target.CurrentSession;
180+
181+
Assert.That(session, Is.Null);
182+
}
183+
130184
[Test]
131185
public void CurrentSessionChangesWhenRepoChanged()
132186
{

0 commit comments

Comments
 (0)