public void GetFieldOrNull_ShouldReturnField_WhenFieldIsInCoreFieldsOrChangedFields()
 {
     var changedEvent = new WorkItemChangedEvent()
         {
             CoreFields = new FieldGroup()
                 {
                     StringFields = new[] {new Field {Name = "coreString"}},
                     IntegerFields = new[] {new Field {Name = "coreInt"}},
                 },
             ChangedFields = new FieldGroup()
                 {
                     StringFields = new[] {new Field {Name = "changedString"}},
                     IntegerFields = new[] {new Field {Name = "changedInt"}},
                 }
         };
     Assert.Null(changedEvent.GetFieldOrNull("notAField"));
     Assert.NotNull(changedEvent.GetFieldOrNull("coreString"));
     Assert.NotNull(changedEvent.GetFieldOrNull("coreInt"));
     Assert.NotNull(changedEvent.GetFieldOrNull("changedString"));
     Assert.NotNull(changedEvent.GetFieldOrNull("changedInt"));
 }
        public void HandleWorkItemChanged_ShouldSendNotification_WhenTaskStateIsChanged()
        {
            var notifier = Substitute.For<IHipChatNotifier>();
            var configProvider = CreateFakeConfigurationProvider();
            var notificationHandler = new NotificationHandler(notifier, configProvider);
            var changedEvent = new WorkItemChangedEvent
                {
                    PortfolioProject = "TestProject",
                    ChangeType = "Change",
                    CoreFields = { StringFields = new[] {new Field() {Name = "Work Item Type", NewValue = "Task"}} },
                    ChangedFields = { StringFields = new[] {new Field() {Name = "State", NewValue = "In Progress"}} }
                };

            notificationHandler.HandleWorkItemChanged(changedEvent);

            notifier.ReceivedWithAnyArgs().SendTaskStateChangedNotification(null, 0);
        }
        public void HandleWorkItemChanged_ShouldNotSendNotification_WhenWorkItemTypeIsNotTask()
        {
            var notifier = Substitute.For<IHipChatNotifier>();
            var configProvider = CreateFakeConfigurationProvider();
            var notificationHandler = new NotificationHandler(notifier, configProvider);
            var changedEvent = new WorkItemChangedEvent() { PortfolioProject = "TestProject", ChangeType = "foo"};

            notificationHandler.HandleWorkItemChanged(changedEvent);

            notifier.DidNotReceiveWithAnyArgs().SendTaskChangedRemainingNotification(null, 0);
            notifier.DidNotReceiveWithAnyArgs().SendTaskOwnerChangedNotification(null, 0);
            notifier.DidNotReceiveWithAnyArgs().SendTaskStateChangedNotification(null, 0);
            notifier.DidNotReceiveWithAnyArgs().SendTaskHistoryCommentNotification(null, 0);
        }
        public void HandleWorkItemChanged_ShouldNotSendNotification_WhenTaskHistoryChangedIsAutomaticChangesetComment()
        {
            var notifier = Substitute.For<IHipChatNotifier>();
            var configProvider = CreateFakeConfigurationProvider();
            var notificationHandler = new NotificationHandler(notifier, configProvider);
            var changedEvent = new WorkItemChangedEvent
            {
                PortfolioProject = "TestProject",
                ChangeType = "Change",
                CoreFields =
                {
                    StringFields = new[]
                                {
                                    new Field() {Name = "Work Item Type", NewValue = "Task"},
                                    new Field() {Name = "State", NewValue = "In Progress"}
                                }
                },
                TextFields = new[] { new TextField() { Name = "History", Value = "Associated with changeset 123456" } }
            };

            notificationHandler.HandleWorkItemChanged(changedEvent);

            notifier.DidNotReceiveWithAnyArgs().SendTaskHistoryCommentNotification(null, 0);
        }
        public void HandleWorkItemChanged_ShouldNotSendNotification_WhenChangeTypeIsNotChange()
        {
            var notifier = Substitute.For<IHipChatNotifier>();
            var configProvider = CreateFakeConfigurationProvider();
            var notificationHandler = new NotificationHandler(notifier, configProvider);
            var changedEvent = new WorkItemChangedEvent()
                {
                    PortfolioProject = "TestProject",
                    ChangeType = "foo",
                    CoreFields = { StringFields = new[] { new Field() { Name = "Work Item Type", NewValue = "Product Backlog Item" } } },
                    ChangedFields = { StringFields = new[] { new Field() { Name = "State", NewValue = "In Progress" } } }
                };

            notificationHandler.HandleWorkItemChanged(changedEvent);

            notifier.DidNotReceiveWithAnyArgs().SendTaskChangedRemainingNotification(null, 0);
            notifier.DidNotReceiveWithAnyArgs().SendTaskOwnerChangedNotification(null, 0);
            notifier.DidNotReceiveWithAnyArgs().SendTaskStateChangedNotification(null, 0);
            notifier.DidNotReceiveWithAnyArgs().SendTaskHistoryCommentNotification(null, 0);
        }
        public void HandleWorkItemChanged(WorkItemChangedEvent changedEvent)
        {
            var teamProjectMapping = FindTeamProjectMapping(changedEvent.PortfolioProject);

            if (teamProjectMapping == null)
            {
                _logger.Trace("Ignoring project {0}", changedEvent.PortfolioProject);
                return;
            }

            var workItemTypeField = changedEvent.GetFieldOrNull("Work Item Type");
            if (workItemTypeField == null)
            {
                _logger.Warn("Could not find field 'Work Item Type'");
                return;
            }

            if (changedEvent.ChangeType == "Change")
            {
                if (workItemTypeField.NewValue == "Task")
                {
                    if (!IsNotificationSubscribedTo(teamProjectMapping, Notification.TaskWorkChange))
                    {
                        return;
                    }

                    var stateChangedField = changedEvent.GetChangedFieldOrNull("State");
                    if (stateChangedField != null)
                    {
                        _hipChatNotifier.SendTaskStateChangedNotification(
                            changedEvent, teamProjectMapping.HipChatRoomId);
                    }

                    var stateField = changedEvent.GetFieldOrNull("State");
                    var remainingWorkChangedField = changedEvent.GetChangedFieldOrNull("Remaining Work");
                    if (remainingWorkChangedField != null
                        && stateField.NewValue == "In Progress")
                    {
                        _hipChatNotifier.SendTaskChangedRemainingNotification(
                            changedEvent, teamProjectMapping.HipChatRoomId);
                    }

                    var assignedToField = changedEvent.GetChangedFieldOrNull("Assigned To");
                    if (assignedToField != null
                        && assignedToField.NewValue != assignedToField.OldValue
                        && stateField.NewValue == "In Progress")
                    {
                        _hipChatNotifier.SendTaskOwnerChangedNotification(
                            changedEvent, teamProjectMapping.HipChatRoomId);
                    }

                    var historyField = changedEvent.GetTextFieldOrNull("History");
                    if (historyField != null)
                    {
                        if (!historyField.Value.StartsWith("Associated with changeset") &&
                            !historyField.Value.Contains(
                                "The Fixed In field was updated as part of associating work items with the build."))
                        {
                            _hipChatNotifier.SendTaskHistoryCommentNotification(changedEvent,
                                                                                teamProjectMapping.HipChatRoomId);
                        }
                    }
                }
                else
                {
                    _logger.Info(string.Format("Ignoring unhandled field type: {0}", workItemTypeField.NewValue));
                    return;
                }
            }
            else if (changedEvent.ChangeType == "New")
            {
                _logger.Trace("Ignoring new work item event");
                return;
            }
        }
        public void SendTaskChangedRemainingNotification(WorkItemChangedEvent changedEvent, int roomId)
        {
            var title = changedEvent.GetFieldOrNull("Title").NewValue;
            var remaining = changedEvent.GetFieldOrNull("Remaining Work");
            var message = string.Format("<a href='{0}'>{1}</a> hours changed: {2} &#8658; {3}",
                                        changedEvent.DisplayUrl,
                                        title,
                                        remaining.OldValue,
                                        remaining.NewValue);
            var userName = GetAssignedToOrNull(changedEvent) ?? GetChangedByOrNull(changedEvent);
            if (userName != null)
            {
                message = string.Format("{0} - {1}", userName, message);
            }

            _hipChatClient.From = GetFromValue("Task");
            _hipChatClient.RoomId = roomId;
            _hipChatClient.SendMessage(message, HipChatClient.BackgroundColor.yellow);
        }
 private static string GetChangedByOrNull(WorkItemChangedEvent workItemChangedEvent)
 {
     var changedByField = workItemChangedEvent.GetFieldOrNull("Changed By");
     if (changedByField != null && !string.IsNullOrEmpty(changedByField.NewValue))
     {
         return changedByField.NewValue;
     }
     return null;
 }
 private static string GetAssignedToOrNull(WorkItemChangedEvent workItemChangedEvent)
 {
     var assignedToField = workItemChangedEvent.GetFieldOrNull("Assigned To");
     if (assignedToField != null && !string.IsNullOrEmpty(assignedToField.NewValue))
     {
         return assignedToField.NewValue;
     }
     return null;
 }
Exemple #10
0
 public void SendTaskStateChangedNotification(WorkItemChangedEvent changedEvent, int roomId)
 {
     var title = changedEvent.GetFieldOrNull("Title").NewValue;
     var state = changedEvent.GetFieldOrNull("State").NewValue;
     var message = string.Format("<a href='{0}'>{1}</a> is <i>{2}</i>",
         changedEvent.DisplayUrl,
         title,
         state);
     var userName = GetAssignedToOrNull(changedEvent) ?? GetChangedByOrNull(changedEvent);
     if (userName != null)
     {
         message = string.Format("{0} - {1}", userName, message);
     }
     _hipChatClient.From = GetFromValue("Task");
     _hipChatClient.RoomId = roomId;
     _hipChatClient.SendMessage(message, HipChatClient.BackgroundColor.yellow);
 }
Exemple #11
0
 public void SendTaskOwnerChangedNotification(WorkItemChangedEvent changedEvent, int roomId)
 {
     var title = changedEvent.GetFieldOrNull("Title").NewValue;
     var assignedTo = changedEvent.GetFieldOrNull("Assigned To").NewValue;
     var message = string.Format("<a href='{0}'>{1}</a> assigned to <i>{2}</i>",
         changedEvent.DisplayUrl,
         title,
         assignedTo);
     _hipChatClient.From = GetFromValue("Task");
     _hipChatClient.RoomId = roomId;
     _hipChatClient.SendMessage(message, HipChatClient.BackgroundColor.yellow);
 }
Exemple #12
0
 public void SendTaskHistoryCommentNotification(WorkItemChangedEvent changedEvent, int roomId)
 {
     var shortTitle = Shorten(changedEvent.GetFieldOrNull("Title").NewValue, 25);
     var userName = GetChangedByOrNull(changedEvent) ?? GetAssignedToOrNull(changedEvent);
     var comment = Shorten(changedEvent.GetTextFieldOrNull("History").Value, 250);
     var message = string.Format("<a href='{0}'>{1}</a> comment: <i>{2}</i>",
                                 changedEvent.DisplayUrl,
                                 shortTitle,
                                 comment);
     if (userName != null)
     {
         message = string.Format("{0} - {1}", userName, message);
     }
     _hipChatClient.From = GetFromValue("Task");
     _hipChatClient.RoomId = roomId;
     _hipChatClient.SendMessage(message, HipChatClient.BackgroundColor.purple);
 }