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

Commit d3641f4

Browse files
authored
Merge pull request #1854 from github/fixes/1832-explicit-github-and-enterprise-gists
Allow user to choose between creating GitHub or GitHub Enterprise Gist
2 parents a119e89 + 785bc44 commit d3641f4

File tree

8 files changed

+141
-16
lines changed

8 files changed

+141
-16
lines changed

src/GitHub.App/Services/DialogService.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,19 @@ public async Task<string> ShowReCloneDialog(IRepositoryModel repository)
5151
return (string)await showDialog.ShowWithFirstConnection(viewModel);
5252
}
5353

54-
public async Task ShowCreateGist()
54+
public async Task ShowCreateGist(IConnection connection)
5555
{
5656
var viewModel = factory.CreateViewModel<IGistCreationViewModel>();
57-
await showDialog.ShowWithFirstConnection(viewModel);
57+
58+
if (connection != null)
59+
{
60+
await viewModel.InitializeAsync(connection);
61+
await showDialog.Show(viewModel);
62+
}
63+
else
64+
{
65+
await showDialog.ShowWithFirstConnection(viewModel);
66+
}
5867
}
5968

6069
public async Task ShowCreateRepositoryDialog(IConnection connection)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace GitHub.Commands
4+
{
5+
/// <summary>
6+
/// Creates a GitHub Enterprise gist from the currently selected text.
7+
/// </summary>
8+
public interface ICreateGistEnterpriseCommand : IVsCommand
9+
{
10+
}
11+
}

src/GitHub.Exports/GitHub.Exports.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
<ItemGroup>
150150
<Compile Include="Commands\ICopyLinkCommand.cs" />
151151
<Compile Include="Commands\IBlameLinkCommand.cs" />
152+
<Compile Include="Commands\ICreateGistEnterpriseCommand.cs" />
152153
<Compile Include="Commands\IOpenFromClipboardCommand.cs" />
153154
<Compile Include="Commands\IOpenFromUrlCommand.cs" />
154155
<Compile Include="Commands\IToggleInlineCommentMarginCommand.cs" />

src/GitHub.Exports/Services/IDialogService.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ public interface IDialogService
3939
/// <summary>
4040
/// Shows the Create Gist dialog.
4141
/// </summary>
42-
Task ShowCreateGist();
42+
/// <param name="connection">
43+
/// The connection to use. If null, the first connection will be used, or the user promted
44+
/// to log in if there are no connections.
45+
/// </param>
46+
Task ShowCreateGist(IConnection connection);
4347

4448
/// <summary>
4549
/// Shows the Create Repository dialog.

src/GitHub.Exports/Settings/PkgCmdID.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static class PkgCmdIDList
1919
public const int refreshCommand = 0x302;
2020
public const int pullRequestCommand = 0x310;
2121
public const int createGistCommand = 0x400;
22+
public const int createGistEnterpriseCommand = 0x401;
2223
public const int openLinkCommand = 0x100;
2324
public const int copyLinkCommand = 0x101;
2425
public const int goToSolutionOrPullRequestFileCommand = 0x102;
Lines changed: 93 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
using System;
2+
using System.Linq;
23
using System.ComponentModel.Composition;
34
using System.Threading.Tasks;
5+
using GitHub.Models;
46
using GitHub.Commands;
57
using GitHub.Logging;
68
using GitHub.Services;
9+
using GitHub.Extensions;
710
using GitHub.Services.Vssdk.Commands;
811

912
namespace GitHub.VisualStudio.Commands
1013
{
1114
/// <summary>
12-
/// Creates a gist from the currently selected text.
15+
/// Creates a GitHub Gist from the currently selected text.
1316
/// </summary>
1417
[Export(typeof(ICreateGistCommand))]
15-
public class CreateGistCommand : VsCommand, ICreateGistCommand
18+
public class CreateGistCommand : CreateGistCommandBase, ICreateGistCommand
1619
{
17-
readonly Lazy<IDialogService> dialogService;
18-
readonly Lazy<ISelectedTextProvider> selectedTextProvider;
19-
2020
[ImportingConstructor]
21-
protected CreateGistCommand(Lazy<IDialogService> dialogService, Lazy<ISelectedTextProvider> selectedTextProvider)
22-
: base(CommandSet, CommandId)
21+
protected CreateGistCommand(
22+
Lazy<IDialogService> dialogService,
23+
Lazy<ISelectedTextProvider> selectedTextProvider,
24+
Lazy<IConnectionManager> connectionManager)
25+
: base(CommandSet, CommandId, dialogService, selectedTextProvider, connectionManager, true,
26+
isNotLoggedInDefault: true)
2327
{
24-
this.dialogService = dialogService;
25-
this.selectedTextProvider = selectedTextProvider;
2628
}
2729

2830
/// <summary>
@@ -34,21 +36,100 @@ protected CreateGistCommand(Lazy<IDialogService> dialogService, Lazy<ISelectedTe
3436
/// Gets the numeric identifier of the command.
3537
/// </summary>
3638
public const int CommandId = PkgCmdIDList.createGistCommand;
39+
}
40+
41+
/// <summary>
42+
/// Creates a GitHub Enterprise Gist from the currently selected text.
43+
/// </summary>
44+
[Export(typeof(ICreateGistEnterpriseCommand))]
45+
public class CreateGistEnterpriseCommand : CreateGistCommandBase, ICreateGistEnterpriseCommand
46+
{
47+
[ImportingConstructor]
48+
protected CreateGistEnterpriseCommand(
49+
Lazy<IDialogService> dialogService,
50+
Lazy<ISelectedTextProvider> selectedTextProvider,
51+
Lazy<IConnectionManager> connectionManager)
52+
: base(CommandSet, CommandId, dialogService, selectedTextProvider, connectionManager, false)
53+
{
54+
}
55+
56+
/// <summary>
57+
/// Gets the GUID of the group the command belongs to.
58+
/// </summary>
59+
public static readonly Guid CommandSet = Guids.guidContextMenuSet;
60+
61+
/// <summary>
62+
/// Gets the numeric identifier of the command.
63+
/// </summary>
64+
public const int CommandId = PkgCmdIDList.createGistEnterpriseCommand;
65+
}
66+
67+
/// <summary>
68+
/// Creates a GitHub or GitHub Enterprise Gist from the currently selected text.
69+
/// </summary>
70+
public abstract class CreateGistCommandBase : VsCommand
71+
{
72+
readonly bool isGitHubDotCom;
73+
readonly bool isNotLoggedInDefault;
74+
readonly Lazy<IDialogService> dialogService;
75+
readonly Lazy<ISelectedTextProvider> selectedTextProvider;
76+
readonly Lazy<IConnectionManager> connectionManager;
77+
78+
protected CreateGistCommandBase(
79+
Guid commandSet, int commandId,
80+
Lazy<IDialogService> dialogService,
81+
Lazy<ISelectedTextProvider> selectedTextProvider,
82+
Lazy<IConnectionManager> connectionManager,
83+
bool isGitHubDotCom,
84+
bool isNotLoggedInDefault = false)
85+
: base(commandSet, commandId)
86+
{
87+
this.dialogService = dialogService;
88+
this.selectedTextProvider = selectedTextProvider;
89+
this.connectionManager = connectionManager;
90+
this.isGitHubDotCom = isGitHubDotCom;
91+
this.isNotLoggedInDefault = isNotLoggedInDefault;
92+
}
3793

3894
ISelectedTextProvider SelectedTextProvider => selectedTextProvider.Value;
3995

4096
/// <summary>
4197
/// Shows the Create Gist dialog.
4298
/// </summary>
43-
public override Task Execute()
99+
public override async Task Execute()
44100
{
45-
return dialogService.Value.ShowCreateGist();
101+
var connection = await FindConnectionAsync();
102+
await dialogService.Value.ShowCreateGist(connection);
46103
}
47104

48105
protected override void QueryStatus()
49106
{
50107
Log.Assert(SelectedTextProvider != null, "Could not get an instance of ISelectedTextProvider");
51-
Visible = !string.IsNullOrWhiteSpace(SelectedTextProvider?.GetSelectedText());
108+
Visible = !string.IsNullOrWhiteSpace(SelectedTextProvider?.GetSelectedText()) &&
109+
(HasConnection() || isNotLoggedInDefault && IsLoggedIn() == false);
110+
}
111+
112+
bool HasConnection()
113+
{
114+
var task = FindConnectionAsync();
115+
return task.IsCompleted && task.Result != null;
116+
}
117+
118+
async Task<IConnection> FindConnectionAsync()
119+
{
120+
var connections = await connectionManager.Value.GetLoadedConnections();
121+
return connections.FirstOrDefault(x => x.IsLoggedIn && x.HostAddress.IsGitHubDotCom() == isGitHubDotCom);
122+
}
123+
124+
bool? IsLoggedIn()
125+
{
126+
var task = connectionManager.Value.IsLoggedIn();
127+
if (task.IsCompleted)
128+
{
129+
return task.Result;
130+
}
131+
132+
return null;
52133
}
53134
}
54135
}

src/GitHub.VisualStudio/GitHub.VisualStudio.vsct

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,18 @@
214214
</Strings>
215215
</Button>
216216

217+
<Button guid="guidContextMenuSet" id="idCreateGistEnterpriseCommand" priority="0x0101" type="Button">
218+
<Icon guid="guidImages" id="logo" />
219+
<CommandFlag>IconIsMoniker</CommandFlag>
220+
<CommandFlag>DefaultInvisible</CommandFlag>
221+
<CommandFlag>DynamicVisibility</CommandFlag>
222+
<Strings>
223+
<ButtonText>Create an Enterprise Gist</ButtonText>
224+
<CanonicalName>.GitHub.CreateGistEnterprise</CanonicalName>
225+
<LocCanonicalName>.GitHub.CreateGistEnterprise</LocCanonicalName>
226+
</Strings>
227+
</Button>
228+
217229
<Button guid="guidContextMenuSet" id="openLinkCommand" type="Button">
218230
<Icon guid="guidImages" id="link_external" />
219231
<CommandFlag>IconIsMoniker</CommandFlag>
@@ -303,7 +315,11 @@
303315
<Parent guid="guidContextMenuSet" id="idGitHubContextSubMenuGroup"/>
304316
</CommandPlacement>
305317

306-
<CommandPlacement guid="guidContextMenuSet" id="idBlameCommand" priority="0x104">
318+
<CommandPlacement guid="guidContextMenuSet" id="idCreateGistEnterpriseCommand" priority="0x104">
319+
<Parent guid="guidContextMenuSet" id="idGitHubContextSubMenuGroup"/>
320+
</CommandPlacement>
321+
322+
<CommandPlacement guid="guidContextMenuSet" id="idBlameCommand" priority="0x105">
307323
<Parent guid="guidContextMenuSet" id="idGitHubContextSubMenuGroup"/>
308324
</CommandPlacement>
309325

@@ -410,6 +426,7 @@
410426
<IDSymbol name="copyLinkCommand" value="0x101"/>
411427
<IDSymbol name="goToSolutionOrPullRequestFileCommand" value="0x0102" />
412428
<IDSymbol name="idCreateGistCommand" value="0x0400" />
429+
<IDSymbol name="idCreateGistEnterpriseCommand" value="0x0401" />
413430
<IDSymbol name="idBlameCommand" value="0x0500" />
414431
</GuidSymbol>
415432

src/GitHub.VisualStudio/GitHubPackage.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ async Task InitializeMenus()
8585
exports.GetExportedValue<IBlameLinkCommand>(),
8686
exports.GetExportedValue<ICopyLinkCommand>(),
8787
exports.GetExportedValue<ICreateGistCommand>(),
88+
exports.GetExportedValue<ICreateGistEnterpriseCommand>(),
8889
exports.GetExportedValue<IOpenLinkCommand>(),
8990
exports.GetExportedValue<IOpenPullRequestsCommand>(),
9091
exports.GetExportedValue<IShowCurrentPullRequestCommand>(),

0 commit comments

Comments
 (0)