Пример #1
0
        public override async Task StartAsync(CancellationToken cancellationToken)
        {
            // create a subscription for PullRequestUpdated
            var projectHttpClient = connection.GetClient <ProjectHttpClient>();
            var teamProject       = await projectHttpClient.GetProject(azureDevopsConfig.ProjectName);

            var receiverEndpoint = this.kestrelConfig.Endpoints.ContainsKey("Https") ? this.kestrelConfig.Endpoints["Https"] : this.kestrelConfig.Endpoints["Http"];

            var serverSecret = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(JWTOptions.serverSecret));

            var subscriptionParameters = new Subscription()
            {
                ConsumerId       = "webHooks",
                ConsumerActionId = "httpRequest",
                ConsumerInputs   = new Dictionary <string, string>
                {
                    { "url", $"{receiverEndpoint.Url.Substring(0, receiverEndpoint.Url.LastIndexOf(":"))}/EventReceiver" },
                    { "httpHeaders", $"Authorization:Bearer {GenerateToken(serverSecret)}" }
                },
                EventType       = "git.pullrequest.updated",
                PublisherId     = "tfs",
                PublisherInputs = new Dictionary <string, string>
                {
                    { "projectId", teamProject.Id.ToString() }
                },
            };

            var subscription = await subscriptionClient.CreateSubscriptionAsync(subscriptionParameters);

            subscriptions.Add(subscription);

            await base.StartAsync(cancellationToken);
        }
        public Subscription CreateDeleteRMWebHooksSubscription()
        {
            // 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>();

            // Get the list of publishers
            IList <VisualStudio.Services.ServiceHooks.WebApi.Publisher> publishers = serviceHooksClient.GetPublishersAsync().Result;

            // Find the Release Management publisher and get its ServiceInstanceType
            VisualStudio.Services.ServiceHooks.WebApi.Publisher rmPublisher = publishers.First(p => p.Id == "rm");
            Guid rmServiceInstance = Guid.Parse(rmPublisher.ServiceInstanceType);

            // Get a new client using the RM ServiceInstanceType
            ServiceHooksPublisherHttpClient rmServiceHooksClient = connection.GetClient <ServiceHooksPublisherHttpClient>(rmServiceInstance);

            Subscription subscriptionParameters = new Subscription()
            {
                ConsumerId       = "webHooks",
                ConsumerActionId = "httpRequest",
                ConsumerInputs   = new Dictionary <string, string>
                {
                    { "url", "https://requestb.in/12h6lw21" }
                },
                EventType       = "ms.vss-release.release-created-event",
                PublisherId     = "rm",
                PublisherInputs = new Dictionary <string, string>
                {
                    { "projectId", project.Id.ToString() }
                },
            };
            Subscription newSubscription = rmServiceHooksClient.CreateSubscriptionAsync(subscriptionParameters).Result;
            Guid         subscriptionId  = newSubscription.Id;

            LogSubscription(newSubscription);

            // Delete the subscription
            rmServiceHooksClient.DeleteSubscriptionAsync(subscriptionId).SyncResult();

            // Try to get the subscription (should result in an exception)
            try
            {
                newSubscription = rmServiceHooksClient.GetSubscriptionAsync(subscriptionId).Result;
            }
            catch (Exception e)
            {
                Context.Log("Unable to get the deleted subscription:" + e.Message);
            }

            return(newSubscription);
        }
        public Subscription CreateDeleteWebHooksSubscription()
        {
            // 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;
            Guid         subscriptionId  = newSubscription.Id;

            LogSubscription(newSubscription);

            // Delete the subscription
            serviceHooksClient.DeleteSubscriptionAsync(subscriptionId).SyncResult();

            // Try to get the subscription (should result in an exception)
            try
            {
                newSubscription = serviceHooksClient.GetSubscriptionAsync(subscriptionId).Result;
            } catch (Exception e)
            {
                Context.Log("Unable to get the deleted subscription:" + e.Message);
            }

            return(newSubscription);
        }
        public Subscription CreateDeleteAccountWideWebHooksSubscription()
        {
            // 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;
            Guid         subscriptionId  = newSubscription.Id;

            LogSubscription(newSubscription);

            // Delete the subscription
            serviceHooksClient.DeleteSubscriptionAsync(subscriptionId).SyncResult();

            // Try to get the subscription (should result in an exception)
            try
            {
                newSubscription = serviceHooksClient.GetSubscriptionAsync(subscriptionId).Result;
            }
            catch (Exception e)
            {
                Context.Log("Unable to get the deleted subscription:" + e.Message);
            }

            return(newSubscription);
        }