示例#1
0
        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);
                }
            }
        }
        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());
                    }
                }
            }
        }