Merge pull request #1854 from github/fixes/1832-explicit-github-and-enterprise-gists

Allow user to choose between creating GitHub or GitHub Enterprise Gist
This commit is contained in:
Jamie Cansdale 2018-08-10 17:11:00 +01:00 коммит произвёл GitHub
Родитель a119e893ff 785bc4453a
Коммит d3641f4a63
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
8 изменённых файлов: 141 добавлений и 16 удалений

Просмотреть файл

@ -51,11 +51,20 @@ namespace GitHub.Services
return (string)await showDialog.ShowWithFirstConnection(viewModel); return (string)await showDialog.ShowWithFirstConnection(viewModel);
} }
public async Task ShowCreateGist() public async Task ShowCreateGist(IConnection connection)
{ {
var viewModel = factory.CreateViewModel<IGistCreationViewModel>(); var viewModel = factory.CreateViewModel<IGistCreationViewModel>();
if (connection != null)
{
await viewModel.InitializeAsync(connection);
await showDialog.Show(viewModel);
}
else
{
await showDialog.ShowWithFirstConnection(viewModel); await showDialog.ShowWithFirstConnection(viewModel);
} }
}
public async Task ShowCreateRepositoryDialog(IConnection connection) public async Task ShowCreateRepositoryDialog(IConnection connection)
{ {

Просмотреть файл

@ -0,0 +1,11 @@
using System;
namespace GitHub.Commands
{
/// <summary>
/// Creates a GitHub Enterprise gist from the currently selected text.
/// </summary>
public interface ICreateGistEnterpriseCommand : IVsCommand
{
}
}

Просмотреть файл

@ -149,6 +149,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="Commands\ICopyLinkCommand.cs" /> <Compile Include="Commands\ICopyLinkCommand.cs" />
<Compile Include="Commands\IBlameLinkCommand.cs" /> <Compile Include="Commands\IBlameLinkCommand.cs" />
<Compile Include="Commands\ICreateGistEnterpriseCommand.cs" />
<Compile Include="Commands\IOpenFromClipboardCommand.cs" /> <Compile Include="Commands\IOpenFromClipboardCommand.cs" />
<Compile Include="Commands\IOpenFromUrlCommand.cs" /> <Compile Include="Commands\IOpenFromUrlCommand.cs" />
<Compile Include="Commands\IToggleInlineCommentMarginCommand.cs" /> <Compile Include="Commands\IToggleInlineCommentMarginCommand.cs" />

Просмотреть файл

@ -39,7 +39,11 @@ namespace GitHub.Services
/// <summary> /// <summary>
/// Shows the Create Gist dialog. /// Shows the Create Gist dialog.
/// </summary> /// </summary>
Task ShowCreateGist(); /// <param name="connection">
/// The connection to use. If null, the first connection will be used, or the user promted
/// to log in if there are no connections.
/// </param>
Task ShowCreateGist(IConnection connection);
/// <summary> /// <summary>
/// Shows the Create Repository dialog. /// Shows the Create Repository dialog.

Просмотреть файл

@ -19,6 +19,7 @@ namespace GitHub.VisualStudio
public const int refreshCommand = 0x302; public const int refreshCommand = 0x302;
public const int pullRequestCommand = 0x310; public const int pullRequestCommand = 0x310;
public const int createGistCommand = 0x400; public const int createGistCommand = 0x400;
public const int createGistEnterpriseCommand = 0x401;
public const int openLinkCommand = 0x100; public const int openLinkCommand = 0x100;
public const int copyLinkCommand = 0x101; public const int copyLinkCommand = 0x101;
public const int goToSolutionOrPullRequestFileCommand = 0x102; public const int goToSolutionOrPullRequestFileCommand = 0x102;

Просмотреть файл

@ -1,28 +1,30 @@
using System; using System;
using System.Linq;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.Threading.Tasks; using System.Threading.Tasks;
using GitHub.Models;
using GitHub.Commands; using GitHub.Commands;
using GitHub.Logging; using GitHub.Logging;
using GitHub.Services; using GitHub.Services;
using GitHub.Extensions;
using GitHub.Services.Vssdk.Commands; using GitHub.Services.Vssdk.Commands;
namespace GitHub.VisualStudio.Commands namespace GitHub.VisualStudio.Commands
{ {
/// <summary> /// <summary>
/// Creates a gist from the currently selected text. /// Creates a GitHub Gist from the currently selected text.
/// </summary> /// </summary>
[Export(typeof(ICreateGistCommand))] [Export(typeof(ICreateGistCommand))]
public class CreateGistCommand : VsCommand, ICreateGistCommand public class CreateGistCommand : CreateGistCommandBase, ICreateGistCommand
{ {
readonly Lazy<IDialogService> dialogService;
readonly Lazy<ISelectedTextProvider> selectedTextProvider;
[ImportingConstructor] [ImportingConstructor]
protected CreateGistCommand(Lazy<IDialogService> dialogService, Lazy<ISelectedTextProvider> selectedTextProvider) protected CreateGistCommand(
: base(CommandSet, CommandId) Lazy<IDialogService> dialogService,
Lazy<ISelectedTextProvider> selectedTextProvider,
Lazy<IConnectionManager> connectionManager)
: base(CommandSet, CommandId, dialogService, selectedTextProvider, connectionManager, true,
isNotLoggedInDefault: true)
{ {
this.dialogService = dialogService;
this.selectedTextProvider = selectedTextProvider;
} }
/// <summary> /// <summary>
@ -34,21 +36,100 @@ namespace GitHub.VisualStudio.Commands
/// Gets the numeric identifier of the command. /// Gets the numeric identifier of the command.
/// </summary> /// </summary>
public const int CommandId = PkgCmdIDList.createGistCommand; public const int CommandId = PkgCmdIDList.createGistCommand;
}
/// <summary>
/// Creates a GitHub Enterprise Gist from the currently selected text.
/// </summary>
[Export(typeof(ICreateGistEnterpriseCommand))]
public class CreateGistEnterpriseCommand : CreateGistCommandBase, ICreateGistEnterpriseCommand
{
[ImportingConstructor]
protected CreateGistEnterpriseCommand(
Lazy<IDialogService> dialogService,
Lazy<ISelectedTextProvider> selectedTextProvider,
Lazy<IConnectionManager> connectionManager)
: base(CommandSet, CommandId, dialogService, selectedTextProvider, connectionManager, false)
{
}
/// <summary>
/// Gets the GUID of the group the command belongs to.
/// </summary>
public static readonly Guid CommandSet = Guids.guidContextMenuSet;
/// <summary>
/// Gets the numeric identifier of the command.
/// </summary>
public const int CommandId = PkgCmdIDList.createGistEnterpriseCommand;
}
/// <summary>
/// Creates a GitHub or GitHub Enterprise Gist from the currently selected text.
/// </summary>
public abstract class CreateGistCommandBase : VsCommand
{
readonly bool isGitHubDotCom;
readonly bool isNotLoggedInDefault;
readonly Lazy<IDialogService> dialogService;
readonly Lazy<ISelectedTextProvider> selectedTextProvider;
readonly Lazy<IConnectionManager> connectionManager;
protected CreateGistCommandBase(
Guid commandSet, int commandId,
Lazy<IDialogService> dialogService,
Lazy<ISelectedTextProvider> selectedTextProvider,
Lazy<IConnectionManager> connectionManager,
bool isGitHubDotCom,
bool isNotLoggedInDefault = false)
: base(commandSet, commandId)
{
this.dialogService = dialogService;
this.selectedTextProvider = selectedTextProvider;
this.connectionManager = connectionManager;
this.isGitHubDotCom = isGitHubDotCom;
this.isNotLoggedInDefault = isNotLoggedInDefault;
}
ISelectedTextProvider SelectedTextProvider => selectedTextProvider.Value; ISelectedTextProvider SelectedTextProvider => selectedTextProvider.Value;
/// <summary> /// <summary>
/// Shows the Create Gist dialog. /// Shows the Create Gist dialog.
/// </summary> /// </summary>
public override Task Execute() public override async Task Execute()
{ {
return dialogService.Value.ShowCreateGist(); var connection = await FindConnectionAsync();
await dialogService.Value.ShowCreateGist(connection);
} }
protected override void QueryStatus() protected override void QueryStatus()
{ {
Log.Assert(SelectedTextProvider != null, "Could not get an instance of ISelectedTextProvider"); Log.Assert(SelectedTextProvider != null, "Could not get an instance of ISelectedTextProvider");
Visible = !string.IsNullOrWhiteSpace(SelectedTextProvider?.GetSelectedText()); Visible = !string.IsNullOrWhiteSpace(SelectedTextProvider?.GetSelectedText()) &&
(HasConnection() || isNotLoggedInDefault && IsLoggedIn() == false);
}
bool HasConnection()
{
var task = FindConnectionAsync();
return task.IsCompleted && task.Result != null;
}
async Task<IConnection> FindConnectionAsync()
{
var connections = await connectionManager.Value.GetLoadedConnections();
return connections.FirstOrDefault(x => x.IsLoggedIn && x.HostAddress.IsGitHubDotCom() == isGitHubDotCom);
}
bool? IsLoggedIn()
{
var task = connectionManager.Value.IsLoggedIn();
if (task.IsCompleted)
{
return task.Result;
}
return null;
} }
} }
} }

Просмотреть файл

@ -214,6 +214,18 @@
</Strings> </Strings>
</Button> </Button>
<Button guid="guidContextMenuSet" id="idCreateGistEnterpriseCommand" priority="0x0101" type="Button">
<Icon guid="guidImages" id="logo" />
<CommandFlag>IconIsMoniker</CommandFlag>
<CommandFlag>DefaultInvisible</CommandFlag>
<CommandFlag>DynamicVisibility</CommandFlag>
<Strings>
<ButtonText>Create an Enterprise Gist</ButtonText>
<CanonicalName>.GitHub.CreateGistEnterprise</CanonicalName>
<LocCanonicalName>.GitHub.CreateGistEnterprise</LocCanonicalName>
</Strings>
</Button>
<Button guid="guidContextMenuSet" id="openLinkCommand" type="Button"> <Button guid="guidContextMenuSet" id="openLinkCommand" type="Button">
<Icon guid="guidImages" id="link_external" /> <Icon guid="guidImages" id="link_external" />
<CommandFlag>IconIsMoniker</CommandFlag> <CommandFlag>IconIsMoniker</CommandFlag>
@ -303,7 +315,11 @@
<Parent guid="guidContextMenuSet" id="idGitHubContextSubMenuGroup"/> <Parent guid="guidContextMenuSet" id="idGitHubContextSubMenuGroup"/>
</CommandPlacement> </CommandPlacement>
<CommandPlacement guid="guidContextMenuSet" id="idBlameCommand" priority="0x104"> <CommandPlacement guid="guidContextMenuSet" id="idCreateGistEnterpriseCommand" priority="0x104">
<Parent guid="guidContextMenuSet" id="idGitHubContextSubMenuGroup"/>
</CommandPlacement>
<CommandPlacement guid="guidContextMenuSet" id="idBlameCommand" priority="0x105">
<Parent guid="guidContextMenuSet" id="idGitHubContextSubMenuGroup"/> <Parent guid="guidContextMenuSet" id="idGitHubContextSubMenuGroup"/>
</CommandPlacement> </CommandPlacement>
@ -410,6 +426,7 @@
<IDSymbol name="copyLinkCommand" value="0x101"/> <IDSymbol name="copyLinkCommand" value="0x101"/>
<IDSymbol name="goToSolutionOrPullRequestFileCommand" value="0x0102" /> <IDSymbol name="goToSolutionOrPullRequestFileCommand" value="0x0102" />
<IDSymbol name="idCreateGistCommand" value="0x0400" /> <IDSymbol name="idCreateGistCommand" value="0x0400" />
<IDSymbol name="idCreateGistEnterpriseCommand" value="0x0401" />
<IDSymbol name="idBlameCommand" value="0x0500" /> <IDSymbol name="idBlameCommand" value="0x0500" />
</GuidSymbol> </GuidSymbol>

Просмотреть файл

@ -85,6 +85,7 @@ namespace GitHub.VisualStudio
exports.GetExportedValue<IBlameLinkCommand>(), exports.GetExportedValue<IBlameLinkCommand>(),
exports.GetExportedValue<ICopyLinkCommand>(), exports.GetExportedValue<ICopyLinkCommand>(),
exports.GetExportedValue<ICreateGistCommand>(), exports.GetExportedValue<ICreateGistCommand>(),
exports.GetExportedValue<ICreateGistEnterpriseCommand>(),
exports.GetExportedValue<IOpenLinkCommand>(), exports.GetExportedValue<IOpenLinkCommand>(),
exports.GetExportedValue<IOpenPullRequestsCommand>(), exports.GetExportedValue<IOpenPullRequestsCommand>(),
exports.GetExportedValue<IShowCurrentPullRequestCommand>(), exports.GetExportedValue<IShowCurrentPullRequestCommand>(),