8000 Merge pull request #1128 from github/feature/open-current-pr-command · github/VisualStudio@d5cd104 · GitHub
[go: up one dir, main page]

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

Commit d5cd104

Browse files
authored
Merge pull request #1128 from github/feature/open-current-pr-command
Add VS command to show current PR details
2 parents 2ab326d + 6a31bc3 commit d5cd104

File tree

12 files changed

+113
-77
lines changed

12 files changed

+113
-77
lines changed

src/GitHub.App/Models/PullRequestDetailArgument.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using GitHub.ViewModels;
3+
using GitHub.Primitives;
34

45
namespace GitHub.Models
56
{
@@ -9,9 +10,9 @@ namespace GitHub.Models
910
public class PullRequestDetailArgument
1011
{
1112
/// <summary>
12-
/// Gets or sets the repository containing the pull request.
13+
/// Gets or sets the owner of the repository containing the pull request.
1314
/// </summary>
14-
public IRemoteRepositoryModel Repository { get; set; }
15+
public string RepositoryOwner { get; set; }
1516

1617
/// <summary>
1718
/// Gets or sets the number of the pull request.

src/GitHub.App/SampleData/PullRequestDetailViewModelDesigner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public PullRequestDetailViewModelDesigner()
3636
{
3737
var repoPath = @"C:\Repo";
3838

39-
Model = new PullRequestModel(419,
39+
Model = new PullRequestModel(419,
4040
"Error handling/bubbling from viewmodels to views to viewhosts",
4141
new AccountDesigner { Login = "shana", IsUser = true },
4242
DateTime.Now.Subtract(TimeSpan.FromDays(3)))
@@ -75,7 +75,7 @@ public PullRequestDetailViewModelDesigner()
7575
public IPullRequestModel Model { get; }
7676
public IPullRequestSession Session { get; }
7777
public ILocalRepositoryModel LocalRepository { get; }
78-
public IRemoteRepositoryModel RemoteRepository { get; }
78+
public string RemoteRepositoryOwner { get; }
7979
public string SourceBranchDisplayName { get; set; }
8080
public string TargetBranchDisplayName { get; set; }
8181
public int CommentCount { get; set; }

src/GitHub.App/ViewModels/PullRequestDetailViewModel.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,13 @@ private set
148148
public ILocalRepositoryModel LocalRepository { get; }
149149

150150
/// <summary>
151-
/// Gets the remote repository that contains the pull request.
151+
/// Gets the owner of the remote repository that contains the pull request.
152152
/// </summary>
153153
/// <remarks>
154154
/// The remote repository may be different from the local repository if the local
155155
/// repository is a fork and the user is viewing pull requests from the parent repository.
156156
/// </remarks>
157-
public IRemoteRepositoryModel RemoteRepository { get; private set; }
157+
public string RemoteRepositoryOwner { get; private set; }
158158

159159
/// <summary>
160160
/// Gets the session for the pull request.
@@ -326,13 +326,13 @@ public IReadOnlyList<IPullRequestChangeNode> ChangedFilesTree
326326
public override void Initialize(ViewWithData data)
327327
{
328328
int number;
329-
var repo = RemoteRepository;
329+
var repoOwner = RemoteRepositoryOwner;
330330

331331
if (data != null)
332332
{
333333
var arg = (PullRequestDetailArgument)data.Data;
334334
number = arg.Number;
335-
repo = arg.Repository;
335+
repoOwner = arg.RepositoryOwner;
336336
}
337337
else
338338
{
@@ -349,7 +349,7 @@ public override void Initialize(ViewWithData data)
349349
}
350350

351351
ErrorMessage = OperationError = null;
352-
modelService.GetPullRequest(repo, number)
352+
modelService.GetPullRequest(repoOwner, LocalRepository.Name, number)
353353
.TakeLast(1)
354354
.ObserveOn(RxApp.MainThreadScheduler)
355355
.Catch<IPullRequestModel, Exception>(ex =>
@@ -359,23 +359,23 @@ public override void Initialize(ViewWithData data)
359359
IsLoading = IsBusy = false;
360360
return Observable.Empty<IPullRequestModel>();
361361
})
362-
.Subscribe(x => Load(repo, x).Forget());
362+
.Subscribe(x => Load(repoOwner, x).Forget());
363363
}
364364

365365
/// <summary>
366366
/// Loads the view model from octokit models.
367367
/// </summary>
368-
/// <param name="remoteRepository">The remote repository.</param>
368+
/// <param name="remoteRepositoryOwner">The owner of the remote repository.</param>
369369
/// <param name="pullRequest">The pull request model.</param>
370-
public async Task Load(IRemoteRepositoryModel remoteRepository, IPullRequestModel pullRequest)
370+
public async Task Load(string remoteRepositoryOwner, IPullRequestModel pullRequest)
371371
{
372-
Guard.ArgumentNotNull(remoteRepository, nameof(remoteRepository));
372+
Guard.ArgumentNotNull(remoteRepositoryOwner, nameof(remoteRepositoryOwner));
373373

374374
try
375375
{
376376
var firstLoad = (Model == null);
377377
Model = pullRequest;
378-
RemoteRepository = remoteRepository;
378+
RemoteRepositoryOwner = remoteRepositoryOwner;
379379
Session = await sessionManager.GetSession(pullRequest);
380380
Title = Resources.PullRequestNavigationItemText + " #" + pullRequest.Number;
381381

src/GitHub.App/ViewModels/PullRequestListViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ void DoOpenPullRequest(object pullRequest)
331331
{
332332
Data = new PullRequestDetailArgument
333333
{
334-
Repository = SelectedRepository,
334+
RepositoryOwner = SelectedRepository.Owner,
335335
Number = (int)pullRequest,
336336
}
337337
};

src/GitHub.Exports.Reactive/ViewModels/IPullRequestDetailViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ public interface IPullRequestDetailViewModel : IViewModel, IHasLoading, IHasBusy
8383
ILocalRepositoryModel LocalRepository { get; }
8484

8585
/// <summary>
86-
/// Gets the remote repository that contains the pull request.
86+
/// Gets the owner of the remote repository that contains the pull request.
8787
/// </summary>
8888
/// <remarks>
8989
/// The remote repository may be different from the local repository if the local
9090
/// repository is a fork and the user is viewing pull requests from the parent repository.
9191
/// </remarks>
92-
IRemoteRepositoryModel RemoteRepository { get; }
92+
string RemoteRepositoryOwner { get; }
9393

9494
/// <summary>
9595
/// Gets a string describing how to display the pull request's source branch.

src/GitHub.Exports/Settings/PkgCmdID.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public static class PkgCmdIDList
99
public const int idGitHubToolbar = 0x1120;
1010
public const int showGitHubPaneCommand = 0x200;
1111
public const int openPullRequestsCommand = 0x201;
12+
public const int showCurrentPullRequestCommand = 0x202;
1213
public const int backCommand = 0x300;
1314
public const int forwardCommand = 0x301;
1415
public const int refreshCommand = 0x302;

src/GitHub.VisualStudio/GitHub.VisualStudio.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@
311311
<Compile Include="Helpers\ActiveDocumentSnapshot.cs" />
312312
<Compile Include="Menus\OpenLink.cs" />
313313
<Compile Include="Menus\LinkMenuBase.cs" />
314+
<Compile Include="Menus\ShowCurrentPullRequest.cs" />
314315
<Compile Include="Menus\OpenPullRequests.cs" />
315316
<Compile Include="Menus\ShowGitHubPane.cs" />
316317
<Compile Include="Menus\AddConnection.cs" />

src/GitHub.VisualStudio/GitHub.VisualStudio.vsct

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@
8888
</Strings>
8989
</Button>
9090

91+
<Button guid="guidGitHubCmdSet" id="showCurrentPullRequestCommand" type="Button">
92+
<Icon guid="guidImages" id="logo" />
93+
<CommandFlag>IconIsMoniker</CommandFlag>
94+
<Strings>
95+
<ButtonText>Show Current Pull Request</ButtonText>
96+
<CanonicalName>GitHub.ShowCurrentPullRequest</CanonicalName>
97+
<LocCanonicalName>GitHub.ShowCurrentPullRequest</LocCanonicalName>
98+
</Strings>
99+
</Button>
100+
91101
<!--- Toolbar buttons -->
92102
<Button guid="guidGitHubToolbarCmdSet" id="backCommand" type="Button">
93103
<Icon guid="guidImages" id="arrow_left" />
@@ -181,13 +191,13 @@
181191
<ButtonText>Blame</ButtonText>
182192
</Strings>
183193
</Button>
184-
194+
185195
</Buttons>
186196

187197
</Commands>
188198

189199
<CommandPlacements>
190-
200+
191201
<!-- context menu -->
192202
<CommandPlacement guid="guidContextMenuSet" id="idGitHubContextMenuGroup" priority="0x1000">
193203
<Parent guid="GUID_XAML_EDITOR" id="ID_XAML_CTXT"/>
@@ -260,7 +270,7 @@
260270
</CommandPlacement>
261271

262272
</CommandPlacements>
263-
273+
264274
<Symbols>
265275
<!-- This is the package guid. -->
266276
<GuidSymbol name="guidGitHubPkg" value="{c3d3dc68-c977-411f-b3e8-03b0dccf7dfc}" />
@@ -271,13 +281,14 @@
271281
<IDSymbol name="idGitHubMenuGroup" value="0x1020"/>
272282
<IDSymbol name="addConnectionCommand" value="0x110"/>
273283
<IDSymbol name="showGitHubPaneCommand" value="0x200"/>
284+
<IDSymbol name="showCurrentPullRequestCommand" value="0x202"/>
274285
</GuidSymbol>
275286

276287
<!-- This is the Manage Connections menu -->
277288
<GuidSymbol name="guidManageConnections" value="{0A014553-A0AA-46DD-8D6B-B8E3178CA435}">
278289
<IDSymbol name="idManageConnections" value="0x1009"/>
279290
</GuidSymbol>
280-
291+
281292
<GuidSymbol name="guidImages" value="{27841f47-070a-46d6-90be-a5cbbfc724ac}" >
282293
<IDSymbol name="logo" value="1" />
283294
<IDSymbol name="arrow_left" value="2" />
@@ -313,7 +324,7 @@
313324
<IDSymbol name="idBlameCommand" value="0x0500" />
314325

315326
</GuidSymbol>
316-
327+
317328
<GuidSymbol name="GUID_XAML_EDITOR" value="{4C87B692-1202-46AA-B64C-EF01FAEC53DA}">
318329
<IDSymbol name="ID_XAML_CTXT" value="259"/>
319330
</GuidSymbol>

src/GitHub.VisualStudio/Menus/MenuProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public MenuProvider(IGitHubServiceProvider serviceProvider)
4444
{
4545
new AddConnection(serviceProvider),
4646
new OpenPullRequests(serviceProvider),
47-
new ShowGitHubPane(serviceProvider)
47+
new ShowGitHubPane(serviceProvider),
48+
new ShowCurrentPullRequest(serviceProvider)
4849
};
4950

5051
DynamicMenus = new List<IDynamicMenuHandler>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using GitHub.Exports;
3+
using GitHub.UI;
4+
using GitHub.Services;
5+
using GitHub.Extensions;
6+
using GitHub.Models;
7+
8+
namespace GitHub.VisualStudio.Menus
9+
{
10+
[ExportMenu(MenuType = MenuType.OpenPullRequests)]
11+
public class ShowCurrentPullRequest : MenuBase, IMenuHandler
12+
{
13+
public ShowCurrentPullRequest(IGitHubServiceProvider serviceProvider)
14+
: base(serviceProvider)
15+
{
16+
Guard.ArgumentNotNull(serviceProvider, nameof(serviceProvider));
17+
}
18+
19+
public Guid Guid => Guids.guidGitHubCmdSet;
20+
public int CmdId => PkgCmdIDList.showCurrentPullRequestCommand;
21+
22+
public void Activate(object data = null)
23+
{
24+
var pullRequestSessionManager = ServiceProvider.ExportProvider.GetExportedValueOrDefault<IPullRequestSessionManager>();
25+
var session = pullRequestSessionManager?.CurrentSession;
26+
if (session == null)
27+
{
28+
return; // No active PR session.
29+
}
30+
31+
var pullRequest = session.PullRequest;
32+
var arg = new PullRequestDetailArgument { RepositoryOwner = session.RepositoryOwner, Number = pullRequest.Number };
33+
var viewWithData = new ViewWithData(UIControllerFlow.PullRequestDetail) { Data = arg };
34+
35+
var manager = ServiceProvider.TryGetService<IGitHubToolWindowManager>();
36+
var host = manager.ShowHomePane();
37+
host?.ShowView(viewWithData);
38+
}
39+
}
40+
}

src/GitHub.VisualStudio/UI/Views/PullRequestDetailView.xaml.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using Task = System.Threading.Tasks.Task;
2727
using Microsoft.VisualStudio.TextManager.Interop;
2828
using System.Text;
29+
using System.Globalization;
2930
using Microsoft.VisualStudio.Text.Projection;
3031

3132
namespace GitHub.VisualStudio.UI.Views
@@ -75,12 +76,18 @@ protected override void OnVisualParentChanged(DependencyObject oldParent)
7576

7677
void DoOpenOnGitHub()
7778
{
78-
var repo = ViewModel.RemoteRepository;
7979
var browser = VisualStudioBrowser;
80-
var url = repo.CloneUrl.ToRepositoryUrl().Append("pull/" + ViewModel.Model.Number);
80+
var cloneUrl = ViewModel.LocalRepository.CloneUrl;
81+
var url = ToPullRequestUrl(cloneUrl.Host, ViewModel.RemoteRepositoryOwner, ViewModel.LocalRepository.Name, ViewModel.Model.Number);
8182
browser.OpenUrl(url);
8283
}
8384

85+
static Uri ToPullRequestUrl(string host, string owner, string repositoryName, int number)
86+
{
87+
var url = string.Format(CultureInfo.InvariantCulture, "https://{0}/{1}/{2}/pull/{3}", host, owner, repositoryName, number);
88+
return new Uri(url);
89+
}
90+
8491
async Task DoOpenFile(IPullRequestFileNode file, bool workingDirectory)
8592
{
8693
try

0 commit comments

Comments
 (0)
0