зеркало из
1
0
Форкнуть 0

Updated SH subscription samples

This commit is contained in:
Will Smythe 2017-08-25 13:44:08 -04:00 коммит произвёл Sean Burgess
Родитель 6e5940828d
Коммит 5e400304a2
4 изменённых файлов: 162 добавлений и 53 удалений

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

@ -0,0 +1,101 @@
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Notifications.WebApi;
using Microsoft.VisualStudio.Services.Notifications.WebApi.Clients;
using Microsoft.VisualStudio.Services.ServiceHooks.WebApi;
using Microsoft.VisualStudio.Services.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Microsoft.TeamServices.Samples.Client.ServiceHooks
{
/// <summary>
///
/// Samples showing how to use the Service Hooks client to create and manage service hook subscriptions
///
/// For more details, see https://www.visualstudio.com/en-us/docs/integrate/api/hooks/subscriptions
///
/// </summary>
[ClientSample(ServiceHooksPublisherApiConstants.AreaName, "Subscriptions")]
public class SubscriptionsSample : ClientSample
{
/// <summary>
/// Create a new web hook subscription that triggers a notification on all new work items created in the specified project.
/// </summary>
[ClientSampleMethod]
public Subscription CreateWebHooksSubscription()
{
// Get the project to create the subscription in
TeamProjectReference project = ClientSampleHelpers.FindAnyProject(this.Context);
// Get the client
VssConnection connection = Context.Connection;
ServiceHooksPublisherHttpClient serviceHooksClient = connection.GetClient<ServiceHooksPublisherHttpClient>();
Subscription subscriptionParameters = new Subscription()
{
ConsumerId = "webHooks",
ConsumerActionId = "httpRequest",
ConsumerInputs = new Dictionary<string, string>
{
{ "url", "https://requestb.in/12h6lw21" }
},
EventType = "workitem.created",
PublisherId = "tfs",
PublisherInputs = new Dictionary<string, string>
{
{ "projectId", project.Id.ToString() }
},
};
Subscription newSubscription = serviceHooksClient.CreateSubscriptionAsync(subscriptionParameters).Result;
LogSubscription(newSubscription);
return newSubscription;
}
/// <summary>
/// Create a new web hook subscription that triggers a notification on all work item updates across the entire account (project collection).
///
/// This requires account/collection level administrator permissions.
///
/// </summary>
[ClientSampleMethod]
public Subscription CreateAccountWideWebHooksSubscription()
{
// Get the client
VssConnection connection = Context.Connection;
ServiceHooksPublisherHttpClient serviceHooksClient = connection.GetClient<ServiceHooksPublisherHttpClient>();
Subscription subscriptionParameters = new Subscription()
{
ConsumerId = "webHooks",
ConsumerActionId = "httpRequest",
ConsumerInputs = new Dictionary<string, string>
{
{ "url", "https://requestb.in/12h6lw21" }
},
EventType = "workitem.updated",
PublisherId = "tfs"
};
Subscription newSubscription = serviceHooksClient.CreateSubscriptionAsync(subscriptionParameters).Result;
LogSubscription(newSubscription);
return newSubscription;
}
protected void LogSubscription(Subscription subscription)
{
Context.Log(" {0} {1} {2} {3}",
subscription.Id.ToString().PadRight(8),
subscription.EventDescription.PadRight(40),
subscription.ConsumerId.PadRight(15),
subscription.ModifiedDate.ToShortDateString().PadRight(10),
subscription.ModifiedBy?.DisplayName);
}
}
}

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

@ -32,6 +32,9 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="HtmlAgilityPack, Version=1.4.9.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a, processorArchitecture=MSIL">
<HintPath>..\packages\HtmlAgilityPack.1.4.9\lib\Net45\HtmlAgilityPack.dll</HintPath>
</Reference>
<Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=3.13.9.1126, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"> <Reference Include="Microsoft.IdentityModel.Clients.ActiveDirectory, Version=3.13.9.1126, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.13.9\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath> <HintPath>..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.3.13.9\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll</HintPath>
</Reference> </Reference>
@ -98,6 +101,9 @@
<Reference Include="Microsoft.VisualStudio.Services.ReleaseManagement.WebApi, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="Microsoft.VisualStudio.Services.ReleaseManagement.WebApi, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.VisualStudio.Services.Release.Client.15.126.0-preview\lib\net45\Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.dll</HintPath> <HintPath>..\packages\Microsoft.VisualStudio.Services.Release.Client.15.126.0-preview\lib\net45\Microsoft.VisualStudio.Services.ReleaseManagement.WebApi.dll</HintPath>
</Reference> </Reference>
<Reference Include="Microsoft.VisualStudio.Services.ServiceHooks.WebApi, Version=16.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.VisualStudio.Services.ServiceHooks.WebApi.15.121.0-preview\lib\net45\Microsoft.VisualStudio.Services.ServiceHooks.WebApi.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.Services.WebApi, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="Microsoft.VisualStudio.Services.WebApi, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.VisualStudio.Services.Client.15.126.0-preview\lib\net45\Microsoft.VisualStudio.Services.WebApi.dll</HintPath> <HintPath>..\packages\Microsoft.VisualStudio.Services.Client.15.126.0-preview\lib\net45\Microsoft.VisualStudio.Services.WebApi.dll</HintPath>
</Reference> </Reference>
@ -156,6 +162,7 @@
<Compile Include="Security\AccessControlListsSample.cs" /> <Compile Include="Security\AccessControlListsSample.cs" />
<Compile Include="Security\TokenHelpers.cs" /> <Compile Include="Security\TokenHelpers.cs" />
<Compile Include="Security\SecurityNamespacesSample.cs" /> <Compile Include="Security\SecurityNamespacesSample.cs" />
<Compile Include="Hooks\SubscriptionsSample.cs" />
<Compile Include="Tfvc\ItemsSample.cs" /> <Compile Include="Tfvc\ItemsSample.cs" />
<Compile Include="Tfvc\ChangesetChangesSample.cs" /> <Compile Include="Tfvc\ChangesetChangesSample.cs" />
<Compile Include="Tfvc\BranchesSample.cs" /> <Compile Include="Tfvc\BranchesSample.cs" />

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

@ -1,59 +1,59 @@
using Microsoft.VisualStudio.Services.Security; using Microsoft.VisualStudio.Services.Security;
using Microsoft.VisualStudio.Services.Security.Client; using Microsoft.VisualStudio.Services.Security.Client;
using Microsoft.VisualStudio.Services.WebApi; using Microsoft.VisualStudio.Services.WebApi;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace Microsoft.TeamServices.Samples.Client.Security
{
[ClientSample(LocationResourceIds.SecurityServiceArea, "securitynamespaces")]
public class SecurityNamespacesSample : ClientSample
{
[ClientSampleMethod]
public IEnumerable<SecurityNamespaceDescription> ListSecurityNamespaces()
{
VssConnection connection = this.Context.Connection;
SecurityHttpClient securityClient = connection.GetClient<SecurityHttpClient>();
namespace Microsoft.TeamServices.Samples.Client.Security
{
[ClientSample(LocationResourceIds.SecurityServiceArea, "securitynamespaces")]
public class SecurityNamespacesSample : ClientSample
{
[ClientSampleMethod]
public IEnumerable<SecurityNamespaceDescription> ListSecurityNamespaces()
{
VssConnection connection = this.Context.Connection;
SecurityHttpClient securityClient = connection.GetClient<SecurityHttpClient>();
IEnumerable<SecurityNamespaceDescription> namespaces = securityClient.QuerySecurityNamespacesAsync(Guid.Empty).Result; IEnumerable<SecurityNamespaceDescription> namespaces = securityClient.QuerySecurityNamespacesAsync(Guid.Empty).Result;
Console.WriteLine("Listing all security namespaces"); Console.WriteLine("Listing all security namespaces");
foreach (SecurityNamespaceDescription ns in namespaces) foreach (SecurityNamespaceDescription ns in namespaces)
{ {
Console.WriteLine("{0} ({1}) - {2} permissions", ns.DisplayName ?? ns.Name, ns.NamespaceId, ns.Actions.Count()); Console.WriteLine("{0} ({1}) - {2} permissions", ns.DisplayName ?? ns.Name, ns.NamespaceId, ns.Actions.Count());
} }
return namespaces; return namespaces;
} }
[ClientSampleMethod] [ClientSampleMethod]
public IEnumerable<SecurityNamespaceDescription> ListLocalSecurityNamespaces() public IEnumerable<SecurityNamespaceDescription> ListLocalSecurityNamespaces()
{ {
VssConnection connection = this.Context.Connection; VssConnection connection = this.Context.Connection;
SecurityHttpClient securityClient = connection.GetClient<SecurityHttpClient>(); SecurityHttpClient securityClient = connection.GetClient<SecurityHttpClient>();
IEnumerable<SecurityNamespaceDescription> namespaces = securityClient.QuerySecurityNamespacesAsync(Guid.Empty, localOnly: true).Result; IEnumerable<SecurityNamespaceDescription> namespaces = securityClient.QuerySecurityNamespacesAsync(Guid.Empty, localOnly: true).Result;
Console.WriteLine("Listing local security namespaces"); Console.WriteLine("Listing local security namespaces");
foreach (SecurityNamespaceDescription ns in namespaces) foreach (SecurityNamespaceDescription ns in namespaces)
{ {
Console.WriteLine("{0} ({1}) - {2} permissions", ns.DisplayName ?? ns.Name, ns.NamespaceId, ns.Actions.Count()); Console.WriteLine("{0} ({1}) - {2} permissions", ns.DisplayName ?? ns.Name, ns.NamespaceId, ns.Actions.Count());
} }
return namespaces; return namespaces;
} }
[ClientSampleMethod] [ClientSampleMethod]
public SecurityNamespaceDescription GetGitSecurityNamespace() public SecurityNamespaceDescription GetGitSecurityNamespace()
{ {
VssConnection connection = this.Context.Connection; VssConnection connection = this.Context.Connection;
SecurityHttpClient securityClient = connection.GetClient<SecurityHttpClient>(); SecurityHttpClient securityClient = connection.GetClient<SecurityHttpClient>();
IEnumerable<SecurityNamespaceDescription> namespaces = securityClient.QuerySecurityNamespacesAsync(this.GitSecurityNamespace).Result; IEnumerable<SecurityNamespaceDescription> namespaces = securityClient.QuerySecurityNamespacesAsync(this.GitSecurityNamespace).Result;
SecurityNamespaceDescription gitNamespace = namespaces.First(); SecurityNamespaceDescription gitNamespace = namespaces.First();
Console.WriteLine("{0}", gitNamespace.DisplayName); Console.WriteLine("{0}", gitNamespace.DisplayName);
foreach (ActionDefinition actionDef in gitNamespace.Actions) foreach (ActionDefinition actionDef in gitNamespace.Actions)
{ {
string knownBit = ""; string knownBit = "";
@ -68,11 +68,11 @@ namespace Microsoft.TeamServices.Samples.Client.Security
} }
Console.WriteLine("\"{0}\" ({1}){2}", actionDef.DisplayName ?? actionDef.Name, actionDef.Bit, knownBit); Console.WriteLine("\"{0}\" ({1}){2}", actionDef.DisplayName ?? actionDef.Name, actionDef.Bit, knownBit);
} }
return gitNamespace; return gitNamespace;
} }
private Guid GitSecurityNamespace = Guid.Parse("2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87"); private Guid GitSecurityNamespace = Guid.Parse("2e9eb7ed-3c0a-47d4-87c1-0ffdd275fd87");
} }
} }

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

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="HtmlAgilityPack" version="1.4.9" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" /> <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.2" targetFramework="net452" /> <package id="Microsoft.AspNet.WebApi.Core" version="5.2.2" targetFramework="net452" />
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.13.9" targetFramework="net452" /> <package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.13.9" targetFramework="net452" />