public Message ToSlackMessage(WorkItemCommentNotification notification, BotElement bot, string channel) { string header = notification.ToMessage(bot, s => s).First(); var fields = new[] { new AttachmentField(bot.Text.Comment, notification.Comment, false) }; return(SlackHelper.CreateSlackMessage(header, fields, bot, channel, bot.GetSetting("standardColor"))); }
protected override IEnumerable <INotification> CreateNotifications(TeamFoundationRequestContext requestContext, WorkItemChangedEvent ev, int maxLines) { var notifications = new List <INotification>(); var identityService = requestContext.GetService <TeamFoundationIdentityService>(); var identity = identityService.ReadIdentity(requestContext, IdentitySearchFactor.Identifier, ev.ChangerSid); if (ev.CoreFields == null) { throw new TfsNotificationRelayException("ev.CoreFields is null"); } if (ev.CoreFields.StringFields == null) { throw new TfsNotificationRelayException("ev.CoreFields.StringFields is null"); } if (ev.CoreFields.IntegerFields == null) { throw new TfsNotificationRelayException("ev.CoreFields.IntegerFields is null"); } var typeField = ev.CoreFields.StringFields.SingleOrDefault(f => f.ReferenceName == "System.WorkItemType"); if (typeField == null) { throw new TfsNotificationRelayException("missing System.WorkItemType"); } string type = typeField.NewValue; var idField = ev.CoreFields.IntegerFields.Single(f => f.ReferenceName == "System.Id"); if (idField == null) { throw new TfsNotificationRelayException("missing System.Id"); } int id = idField.NewValue; if (ev.TextFields != null) { var comment = ev.TextFields.FirstOrDefault(f => f.ReferenceName == "System.History" && !String.IsNullOrEmpty(f.Value)); if (comment != null) { var commentNotification = new WorkItemCommentNotification() { TeamProjectCollection = requestContext.ServiceHost.Name, UniqueName = identity.UniqueName, DisplayName = identity.DisplayName, WiUrl = ev.DisplayUrl, WiType = type, WiId = id, WiTitle = ev.WorkItemTitle, ProjectName = ev.PortfolioProject, AreaPath = ev.AreaPath, CommentHtml = comment.Value, Comment = TextHelper.HtmlToText(comment.Value) }; notifications.Add(commentNotification); } } var changeNotification = new WorkItemChangedNotification() { TeamProjectCollection = requestContext.ServiceHost.Name, IsNew = ev.ChangeType == ChangeTypes.New, UniqueName = identity.UniqueName, DisplayName = identity.DisplayName, WiUrl = ev.DisplayUrl, WiType = type, WiId = id, WiTitle = ev.WorkItemTitle, ProjectName = ev.PortfolioProject, AreaPath = ev.AreaPath, IsStateChanged = ev.ChangedFields != null && ev.ChangedFields.StringFields != null && ev.ChangedFields.StringFields.Any(f => f.ReferenceName == "System.State"), IsAssignmentChanged = ev.ChangedFields != null && ev.ChangedFields.StringFields != null && ev.ChangedFields.StringFields.Any(f => f.ReferenceName == "System.AssignedTo"), State = ev.CoreFields.StringFields.GetFieldValue("System.State", f => f.NewValue), AssignedTo = ev.CoreFields.StringFields.GetFieldValue("System.AssignedTo", f => f.NewValue), CoreFields = ev.CoreFields, ChangedFields = ev.ChangedFields }; notifications.Add(changeNotification); return(notifications); }
protected override IEnumerable <INotification> CreateNotifications(TeamFoundationRequestContext requestContext, WorkItemChangedEvent ev, int maxLines) { var notifications = new List <INotification>(); var identityService = requestContext.GetService <TeamFoundationIdentityService>(); var identity = identityService.ReadIdentity(requestContext, IdentitySearchFactor.Identifier, ev.ChangerSid); if (ev.CoreFields == null) { throw new TfsNotificationRelayException("ev.CoreFields is null"); } if (ev.CoreFields.StringFields == null) { throw new TfsNotificationRelayException("ev.CoreFields.StringFields is null"); } if (ev.CoreFields.IntegerFields == null) { throw new TfsNotificationRelayException("ev.CoreFields.IntegerFields is null"); } var typeField = ev.CoreFields.StringFields.SingleOrDefault(f => f.ReferenceName == "System.WorkItemType"); if (typeField == null) { throw new TfsNotificationRelayException("missing System.WorkItemType"); } string type = typeField.NewValue; var idField = ev.CoreFields.IntegerFields.Single(f => f.ReferenceName == "System.Id"); if (idField == null) { throw new TfsNotificationRelayException("missing System.Id"); } int id = idField.NewValue; var assignedToString = ev.CoreFields.StringFields.GetFieldValue("System.AssignedTo", f => f.NewValue); string assignedToUniqueName = null; string assignedTo = null; if (!string.IsNullOrEmpty(assignedToString)) { UserField assignedToField = null; if (UserField.TryParse(assignedToString, out assignedToField) && assignedToField.Identifier != Guid.Empty) { assignedTo = assignedToField.DisplayName; var assignedToIdentity = identityService.ReadIdentities(requestContext, new[] { assignedToField.Identifier }).First(); assignedToUniqueName = assignedToIdentity.UniqueName; } } var teamNames = GetUserTeamsByProjectUri(requestContext, ev.ProjectNodeId, identity.Descriptor).ToList(); var comment = ev.TextFields?.FirstOrDefault(f => f.ReferenceName == "System.History" && !string.IsNullOrEmpty(f.Value)); if (comment != null) { var commentNotification = new WorkItemCommentNotification() { TeamProjectCollection = requestContext.ServiceHost.Name, UniqueName = identity.UniqueName, DisplayName = identity.DisplayName, WiUrl = ev.DisplayUrl, WiType = type, WiId = id, WiTitle = ev.WorkItemTitle, ProjectName = ev.PortfolioProject, AreaPath = ev.AreaPath, AssignedTo = assignedTo, AssignedToUniqueName = assignedToUniqueName, CommentHtml = comment.Value, Comment = TextHelper.HtmlToText(comment.Value), TeamNames = teamNames }; notifications.Add(commentNotification); } var changeNotification = new WorkItemChangedNotification() { TeamProjectCollection = requestContext.ServiceHost.Name, IsNew = ev.ChangeType == ChangeTypes.New, UniqueName = identity.UniqueName, DisplayName = identity.DisplayName, WiUrl = ev.DisplayUrl, WiType = type, WiId = id, WiTitle = ev.WorkItemTitle, ProjectName = ev.PortfolioProject, AreaPath = ev.AreaPath, IsStateChanged = ev.ChangedFields?.StringFields?.Any(f => f.ReferenceName == "System.State") ?? false, IsAssignmentChanged = ev.ChangedFields?.StringFields?.Any(f => f.ReferenceName == "System.AssignedTo") ?? false, State = ev.CoreFields.StringFields.GetFieldValue("System.State", f => f.NewValue), AssignedTo = assignedTo, AssignedToUniqueName = assignedToUniqueName, CoreFields = ev.CoreFields, ChangedFields = ev.ChangedFields, TeamNames = teamNames }; notifications.Add(changeNotification); return(notifications); }