public NotificationSubscription CreateSubscriptionForTeam() { NotificationSubscription newSubscription; WebApiTeamRef team = ClientSampleHelpers.FindAnyTeam(this.Context, null); NotificationHttpClient notificationClient = Context.Connection.GetClient <NotificationHttpClient>(); // Query the available event types and find the first that can be used in a custom subscription List <NotificationEventType> eventTypes = notificationClient.ListEventTypesAsync().Result; NotificationEventType eventType = eventTypes.Find(e => { return(e.CustomSubscriptionsAllowed); }); NotificationSubscriptionCreateParameters createParams = new NotificationSubscriptionCreateParameters() { Description = "A subscription for our team", Filter = new ExpressionFilter(eventType.Id), Channel = new UserSubscriptionChannel(), Subscriber = new IdentityRef() { Id = team.Id.ToString() } }; newSubscription = notificationClient.CreateSubscriptionAsync(createParams).Result; LogSubscription(newSubscription); return(newSubscription); }
public NotificationSubscription FollowWorkItem() { NotificationSubscription newFollowSubscription; // Get a work item to follow. For this sample, just create a temporary work item. WorkItem newWorkItem; using (new ClientSampleHttpLoggerOutputSuppression()) { WorkItemsSample witSample = new WorkItemsSample(); witSample.Context = this.Context; newWorkItem = witSample.CreateWorkItem(); // Save the new work item so we can unfollow it later this.Context.SetValue <WorkItem>("$followedWorkItem", newWorkItem); } NotificationSubscriptionCreateParameters createParams = new NotificationSubscriptionCreateParameters() { Filter = new ArtifactFilter(null) { ArtifactType = "WorkItem", ArtifactId = newWorkItem.Id.ToString() } }; VssConnection connection = Context.Connection; NotificationHttpClient notificationClient = Context.Connection.GetClient <NotificationHttpClient>(); newFollowSubscription = notificationClient.CreateSubscriptionAsync(createParams).Result; LogSubscription(newFollowSubscription); return(newFollowSubscription); }
public NotificationSubscription UnfollowWorkItem() { NotificationSubscription newFollowSubscription; // Step 1: Get a work item to follow. For this sample, just create a temporary work item. WorkItem newWorkItem; using (new ClientSampleHttpLoggerOutputSuppression()) { WorkItemsSample witSample = new WorkItemsSample(); witSample.Context = this.Context; newWorkItem = witSample.CreateWorkItem(); } string workItemArtifactUri = "vstfs:///WorkItemTracking/WorkItem/" + newWorkItem.Id; // Step 2: Follow this workitem by creating a subscription NotificationSubscriptionCreateParameters createParams = new NotificationSubscriptionCreateParameters() { Filter = new ArtifactFilter(workItemArtifactUri), Channel = new UserSubscriptionChannel() }; VssConnection connection = Context.Connection; NotificationHttpClient notificationClient = Context.Connection.GetClient <NotificationHttpClient>(); newFollowSubscription = notificationClient.CreateSubscriptionAsync(createParams).Result; LogSubscription(newFollowSubscription); // Step 3: Query for the follow subscription SubscriptionQuery query = new SubscriptionQuery() { Conditions = new[] { new SubscriptionQueryCondition() { Filter = new ArtifactFilter(workItemArtifactUri) } } }; NotificationSubscription followSubscription = notificationClient.QuerySubscriptionsAsync(query).Result.FirstOrDefault(); // Step 4: Now, unfollow the above workitem, by deleting the subscription if (followSubscription != null) { notificationClient.DeleteSubscriptionAsync(followSubscription.Id).SyncResult(); } // Step 5: Cleanup the temporary work item using (new ClientSampleHttpLoggerOutputSuppression()) { WorkItemTrackingHttpClient witClient = connection.GetClient <WorkItemTrackingHttpClient>(); witClient.DeleteWorkItemAsync(newWorkItem.Id.Value, destroy: true); } return(followSubscription); }
/// <summary> /// Creates a subscription /// </summary> /// <param name="newSubscription">Subscription to create</param> /// <returns>The newly created subscription</returns> public async Task <NotificationSubscription> CreateSubscriptionAsync(NotificationSubscriptionCreateParameters newSubscription) { var client = await GetClientAsync <NotificationHttpClient>(); logger.LogInformation("CreateSubscriptionAsync Description = {0}", newSubscription.Description); var result = await client.CreateSubscriptionAsync(newSubscription); return(result); }
private async Task EnsureScheduledBuildFailSubscriptionExists(BuildDefinition pipeline, WebApiTeam team, bool persistChanges) { const string BuildFailureNotificationTag = "#AutomaticBuildFailureNotification"; var subscriptions = await service.GetSubscriptionsAsync(team.Id); var hasSubscription = subscriptions.Any(sub => sub.Description.Contains(BuildFailureNotificationTag)); logger.LogInformation("Team Is Subscribed TeamId = {0} PipelineId = {1} HasSubscription = {2}", team.Id, pipeline.Id, hasSubscription); if (!hasSubscription) { var filterModel = new ExpressionFilterModel { Clauses = new ExpressionFilterClause[] { new ExpressionFilterClause { Index = 1, LogicalOperator = "", FieldName = "Status", Operator = "=", Value = "Failed" }, new ExpressionFilterClause { Index = 2, LogicalOperator = "And", FieldName = "Definition name", Operator = "=", Value = $"\\{pipeline.Project.Name}\\{pipeline.Name}" }, new ExpressionFilterClause { Index = 3, LogicalOperator = "And", FieldName = "Build reason", Operator = "=", Value = "Scheduled" } } }; var filter = new ExpressionFilter("ms.vss-build.build-completed-event", filterModel); var identity = new IdentityRef { Id = team.Id.ToString(), Url = team.IdentityUrl }; var newSubscription = new NotificationSubscriptionCreateParameters { Channel = new UserSubscriptionChannel { UseCustomAddress = false }, Description = $"A build fails {BuildFailureNotificationTag}", Filter = filter, Scope = new SubscriptionScope { Type = "none", Id = pipeline.Project.Id }, Subscriber = identity, }; logger.LogInformation("Creating Subscription PipelineId = {0}, TeamId = {1}", pipeline.Id, team.Id); if (persistChanges) { var subscription = await service.CreateSubscriptionAsync(newSubscription); } } }
public NotificationSubscription CreateSubscriptionForUser() { NotificationHttpClient notificationClient = Context.Connection.GetClient <NotificationHttpClient>(); // Query the available event types and find the first that can be used in a custom subscription List <NotificationEventType> eventTypes = notificationClient.ListEventTypesAsync().Result; NotificationEventType eventType = eventTypes.Find(e => { return(e.CustomSubscriptionsAllowed); }); NotificationSubscriptionCreateParameters createParams = new NotificationSubscriptionCreateParameters() { Description = "My first subscription!", Filter = new ExpressionFilter(eventType.Id), Channel = new UserSubscriptionChannel() }; NotificationSubscription newSubscription = notificationClient.CreateSubscriptionAsync(createParams).Result; LogSubscription(newSubscription); return(newSubscription); }
public NotificationSubscription FollowWorkItem() { NotificationSubscription newFollowSubscription; // Get a work item to follow. For this sample, just create a temporary work item. WorkItem newWorkItem; using (new ClientSampleHttpLoggerOutputSuppression()) { WorkItemsSample witSample = new WorkItemsSample(); witSample.Context = this.Context; newWorkItem = witSample.CreateWorkItem(); } string workItemArtifactUri = "vstfs:///WorkItemTracking/WorkItem/" + newWorkItem.Id; NotificationSubscriptionCreateParameters createParams = new NotificationSubscriptionCreateParameters() { Filter = new ArtifactFilter(workItemArtifactUri), Channel = new UserSubscriptionChannel() }; VssConnection connection = Context.Connection; NotificationHttpClient notificationClient = Context.Connection.GetClient <NotificationHttpClient>(); newFollowSubscription = notificationClient.CreateSubscriptionAsync(createParams).Result; LogSubscription(newFollowSubscription); // Cleanup the temporary work item using (new ClientSampleHttpLoggerOutputSuppression()) { WorkItemTrackingHttpClient witClient = connection.GetClient <WorkItemTrackingHttpClient>(); witClient.DeleteWorkItemAsync(newWorkItem.Id.Value, destroy: true); } return(newFollowSubscription); }
public NotificationSubscription CreateUpdateDeleteSubscription() { // Get the client VssConnection connection = Context.Connection; NotificationHttpClient notificationClient = connection.GetClient <NotificationHttpClient>(); // // Part 1: create a subscription to get notified about certain pull request events // // Create parameters for the new subscription NotificationSubscriptionCreateParameters createParams = new NotificationSubscriptionCreateParameters() { Description = "Someone is waiting on one of my pull requests", Filter = new ExpressionFilter("ms.vss-code.git-pullrequest-event") { FilterModel = new ExpressionFilterModel() { Clauses = new ExpressionFilterClause[] { new ExpressionFilterClause() { FieldName = "Vote", Operator = "Changes to", Value = "Waiting for author", LogicalOperator = "And" }, new ExpressionFilterClause() { FieldName = "Created by", Operator = "=", Value = "[Me]", LogicalOperator = "And" } } } }, Channel = new UserSubscriptionChannel() }; // Scope to only events from one project ProjectHttpClient projectClient = this.Context.Connection.GetClient <ProjectHttpClient>(); Guid projectId; String projectName; if (!this.Context.TryGetValue <String>("projectName", out projectName)) { // Get the ID of the first project projectId = projectClient.GetProjects().Result.First().Id; } else { // Get the ID of the specified project projectId = projectClient.GetProject(projectName).Result.Id; } createParams.Scope = new SubscriptionScope() { Id = projectId }; NotificationSubscription newSubscription = notificationClient.CreateSubscriptionAsync(createParams).Result; String subscriptionId = newSubscription.Id; Context.Log("New subscription created! ID: {0}", subscriptionId); // // Part 2: disable and delete the subscription // // Disable the new subscription NotificationSubscriptionUpdateParameters updateParams = new NotificationSubscriptionUpdateParameters() { Status = SubscriptionStatus.Disabled }; newSubscription = notificationClient.UpdateSubscriptionAsync(updateParams, subscriptionId).Result; Context.Log("Is subscription disabled? {0}", newSubscription.Status < 0); // Delete the subscription notificationClient.DeleteSubscriptionAsync(subscriptionId).SyncResult(); // Try to get the subscription (should result in an exception) try { newSubscription = notificationClient.GetSubscriptionAsync(subscriptionId, SubscriptionQueryFlags.IncludeFilterDetails).Result; } catch (Exception e) { Context.Log("Unable to get the deleted subscription:" + e.Message); } // Try again (the default query flags says to return deleted subscriptions so this should work) newSubscription = notificationClient.GetSubscriptionAsync(subscriptionId).Result; return(newSubscription); }
private async Task EnsureScheduledBuildFailSubscriptionExists(BuildDefinition pipeline, WebApiTeam team, bool persistChanges) { const string BuildFailureNotificationTag = "#AutomaticBuildFailureNotification"; var subscriptions = await service.GetSubscriptionsAsync(team.Id); var subscription = subscriptions.FirstOrDefault(sub => sub.Description.Contains(BuildFailureNotificationTag)); logger.LogInformation("Team Is Subscribed TeamName = {0} PipelineId = {1}", team.Name, pipeline.Id); string definitionName = $"\\{pipeline.Project.Name}\\{pipeline.Name}"; if (subscription == default) { var filterModel = new ExpressionFilterModel { Clauses = new ExpressionFilterClause[] { new ExpressionFilterClause { Index = 1, LogicalOperator = "", FieldName = "Status", Operator = "=", Value = "Failed" }, new ExpressionFilterClause { Index = 2, LogicalOperator = "And", FieldName = "Definition name", Operator = "=", Value = definitionName }, new ExpressionFilterClause { Index = 3, LogicalOperator = "And", FieldName = "Build reason", Operator = "=", Value = "Scheduled" } } }; var filter = new ExpressionFilter("ms.vss-build.build-completed-event", filterModel); var identity = new IdentityRef { Id = team.Id.ToString(), Url = team.IdentityUrl }; var newSubscription = new NotificationSubscriptionCreateParameters { Channel = new UserSubscriptionChannel { UseCustomAddress = false }, Description = $"A build fails {BuildFailureNotificationTag}", Filter = filter, Scope = new SubscriptionScope { Type = "none", Id = pipeline.Project.Id }, Subscriber = identity, }; logger.LogInformation("Creating Subscription PipelineId = {0}, TeamId = {1}", pipeline.Id, team.Id); if (persistChanges) { subscription = await service.CreateSubscriptionAsync(newSubscription); } } else { var filter = subscription.Filter as ExpressionFilter; if (filter == null) { logger.LogWarning("Subscription expression is not correct for of team {0}", team.Name); return; } var definitionClause = filter.FilterModel.Clauses.FirstOrDefault(c => c.FieldName == "Definition name"); if (definitionClause == null) { logger.LogWarning("Subscription doesn't have correct expression filters for of team {0}", team.Name); return; } if (definitionClause.Value != definitionName) { definitionClause.Value = definitionName; if (persistChanges) { var updateParameters = new NotificationSubscriptionUpdateParameters() { Channel = subscription.Channel, Description = subscription.Description, Filter = subscription.Filter, Scope = subscription.Scope, }; logger.LogInformation("Updating Subscription expression for team {0} with correct definition name {1}", team.Name, definitionName); subscription = await service.UpdatedSubscriptionAsync(updateParameters, subscription.Id.ToString()); } } } }