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

Commit 2167aff

Browse files
committed
Correctly handle PRs from local repo.
When a PR comes from the same repo, simply check out the branch that it comes from.
1 parent 18535d5 commit 2167aff

File tree

4 files changed

+35
-20
lines changed

4 files changed

+35
-20
lines changed

src/GitHub.App/Services/PullRequestService.cs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Reactive;
1515
using System.Collections.Generic;
1616
using LibGit2Sharp;
17+
using PullRequest = Octokit.PullRequest;
1718

1819
namespace GitHub.Services
1920
{
@@ -136,22 +137,23 @@ public IObservable<HistoryDivergence> CalculateHistoryDivergence(ILocalRepositor
136137
});
137138
}
138139

139-
public IObservable<IBranch> GetLocalBranches(ILocalRepositoryModel repository, int number)
140+
public IObservable<IBranch> GetLocalBranches(ILocalRepositoryModel repository, PullRequest pullRequest)
140141
{
141142
return Observable.Defer(() =>
142143
{
143144
var repo = gitService.GetRepository(repository.LocalPath);
144-
var result = GetLocalBranchesInternal(repo, number).Select(x => new BranchModel(x, repository));
145+
var result = GetLocalBranchesInternal(repo, repository.CloneUrl, pullRequest).Select(x => new BranchModel(x, repository));
145146
return result.ToObservable();
146147
});
147148
}
148149

149-
public IObservable<Unit> SwitchToBranch(ILocalRepositoryModel repository, int number)
150+
public IObservable<Unit> SwitchToBranch(ILocalRepositoryModel repository, PullRequest pullRequest)
150151
{
151152
return Observable.Defer(() =>
152153
{
153154
var repo = gitService.GetRepository(repository.LocalPath);
154-
var branch = GetLocalBranchesInternal(repo, number).First();
155+
var branch = GetLocalBranchesInternal(repo, repository.CloneUrl, pullRequest).First();
156+
gitClient.Fetch(repo, "origin");
155157
gitClient.Checkout(repo, branch);
156158
return Observable.Empty<Unit>();
157159
});
@@ -177,13 +179,22 @@ async Task DoFetchAndCheckout(ILocalRepositoryModel repository, int pullRequestN
177179
await gitClient.SetConfig(repo, configKey, pullRequestNumber.ToString());
178180
}
179181

180-
IEnumerable<string> GetLocalBranchesInternal(IRepository repository, int number)
182+
IEnumerable<string> GetLocalBranchesInternal(IRepository repository, UriString cloneUrl, PullRequest pullRequest)
181183
{
182-
var pr = number.ToString(CultureInfo.InvariantCulture);
183-
return repository.Config
184-
.Select(x => new { Branch = BranchCapture.Match(x.Key).Groups["branch"].Value, Value = x.Value })
185-
.Where(x => !string.IsNullOrWhiteSpace(x.Branch) && x.Value == pr)
186-
.Select(x => x.Branch);
184+
var sourceUrl = new UriString(pullRequest.Head.Repository.CloneUrl);
185+
186+
if (sourceUrl.ToRepositoryUrl() == cloneUrl.ToRepositoryUrl())
187+
{
188+
return new[] { pullRequest.Head.Ref };
189+
}
190+
else
191+
{
192+
var pr = pullRequest.Number.ToString(CultureInfo.InvariantCulture);
193+
return repository.Config
194+
.Select(x => new { Branch = BranchCapture.Match(x.Key).Groups["branch"].Value, Value = x.Value })
195+
.Where(x => !string.IsNullOrWhiteSpace(x.Branch) && x.Value == pr)
196+
.Select(x => x.Branch);
197+
}
187198
}
188199

189200
async Task<IPullRequestModel> PushAndCreatePR(IRepositoryHost host,

src/GitHub.App/ViewModels/PullRequestDetailViewModel.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class PullRequestDetailViewModel : BaseViewModel, IPullRequestDetailViewM
3535
readonly ILocalRepositoryModel repository;
3636
readonly IPullRequestService pullRequestsService;
3737
readonly IAvatarProvider avatarProvider;
38+
PullRequest model;
3839
PullRequestState state;
3940
string sourceBranchDisplayName;
4041
string targetBranchDisplayName;
@@ -304,6 +305,7 @@ public override void Initialize([AllowNull] ViewWithData data)
304305
/// <param name="files">The pull request's changed files.</param>
305306
public async Task Load(PullRequest pullRequest, IList<PullRequestFile> files)
306307
{
308+
model = pullRequest;
307309
State = CreatePullRequestState(pullRequest);
308310
SourceBranchDisplayName = GetBranchDisplayName(pullRequest.Head.Label);
309311
TargetBranchDisplayName = GetBranchDisplayName(pullRequest.Base.Label);
@@ -329,7 +331,7 @@ public async Task Load(PullRequest pullRequest, IList<PullRequestFile> files)
329331
ChangedFilesTree.Add(change);
330332
}
331333

332-
var localBranches = await pullRequestsService.GetLocalBranches(repository, Number).ToList();
334+
var localBranches = await pullRequestsService.GetLocalBranches(repository, pullRequest).ToList();
333335

334336
if (localBranches.Contains(repository.CurrentBranch))
335337
{
@@ -464,7 +466,7 @@ IObservable<Unit> DoCheckout(object unused)
464466
.SelectMany(x => pullRequestsService.FetchAndCheckout(repository, Number, x));
465467
break;
466468
case CheckoutMode.Switch:
467-
operation = pullRequestsService.SwitchToBranch(repository, Number);
469+
operation = pullRequestsService.SwitchToBranch(repository, model);
468470
break;
469471
case CheckoutMode.InvalidState:
470472
operation = pullRequestsService

src/GitHub.Exports.Reactive/Services/IPullRequestService.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Reactive;
33
using GitHub.Models;
44
using LibGit2Sharp;
5+
using Octokit;
56

67
namespace GitHub.Services
78
{
@@ -40,17 +41,17 @@ IObservable<IPullRequestModel> CreatePullRequest(IRepositoryHost host,
4041
/// <summary>
4142
/// Gets the local branches that exist for the specified pull request.
4243
/// </summary>
43-
/// <param name="pullRequestNumber">The pull request number.</param>
44+
/// <param name="pullRequest">The octokit pull request details.</param>
4445
/// <returns></returns>
45-
IObservable<IBranch> GetLocalBranches(ILocalRepositoryModel repository, int number);
46+
IObservable<IBranch> GetLocalBranches(ILocalRepositoryModel repository, PullRequest pullRequest);
4647

4748
/// <summary>
4849
/// Switches to an existing branch for the specified pull request.
4950
/// </summary>
5051
/// <param name="repository">The repository.</param>
51-
/// <param name="pullRequestNumber">The pull request number.</param>
52+
/// <param name="pullRequest">The octokit pull request details.</param>
5253
/// <returns></returns>
53-
IObservable<Unit> SwitchToBranch(ILocalRepositoryModel repository, int number);
54+
IObservable<Unit> SwitchToBranch(ILocalRepositoryModel repository, PullRequest pullRequest);
5455

5556
/// <summary>
5657
/// Gets the history divergence between the current HEAD and the specified pull request.

src/UnitTests/GitHub.App/ViewModels/PullRequestDetailViewModelTests.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,15 @@ public async Task CheckoutSwitchShouldCallService()
216216
var target = CreateTargetAndService(
217217
currentBranch: "master",
218218
existingPrBranch: "pr/123");
219+
var pr = CreatePullRequest();
219220

220-
await target.Item1.Load(CreatePullRequest(), new PullRequestFile[0]);
221+
await target.Item1.Load(pr, new PullRequestFile[0]);
221222

222223
Assert.Equal(CheckoutMode.Switch, target.Item1.CheckoutMode);
223224

224225
target.Item1.Checkout.Execute(null);
225226

226-
var unused = target.Item2.Received().SwitchToBranch(Arg.Any<ILocalRepositoryModel>(), 1);
227+
var unused = target.Item2.Received().SwitchToBranch(Arg.Any<ILocalRepositoryModel>(), pr);
227228
}
228229

229230
[Fact]
@@ -295,12 +296,12 @@ Tuple<PullRequestDetailViewModel, IPullRequestService> CreateTargetAndService(
295296
if (existingPrBranch != null)
296297
{
297298
var existingBranchModel = new BranchModel(existingPrBranch, repository);
298-
pullRequestService.GetLocalBranches(repository, Arg.Any<int>())
299+
pullRequestService.GetLocalBranches(repository, Arg.Any<PullRequest>())
299300
.Returns(Observable.Return(existingBranchModel));
300301
}
301302
else
302303
{
303-
pullRequestService.GetLocalBranches(repository, Arg.Any<int>())
304+
pullRequestService.GetLocalBranches(repository, Arg.Any<PullRequest>())
304305
.Returns(Observable.Empty<IBranch>());
305306
}
306307

0 commit comments

Comments
 (0)