Example #1
0
        /// <param name="workItemId">The ID of the work item to modify </param>
        /// <param name="comment">Comment to add to description</param>
        /// <param name="commentIsHtml"></param>
        /// <param name="values">List of fields to change</param>
        /// <param name="attachments"></param>
        public void ModifyWorkItem(int workItemId, string comment, bool commentIsHtml, Dictionary <string, string> values, MessageAttachmentCollection attachments)
        {
            if (workItemId <= 0)
            {
                return;
            }

            var workItem = _tfsStore.GetWorkItem(workItemId);

            workItem.Open();

            if (commentIsHtml)
            {
                workItem.History = EmailBodyProcessingUtils.UpdateEmbeddedImageLinks(comment, attachments.Attachments);
            }
            else
            {
                workItem.History = comment.Replace("\n", "<br>");
            }

            foreach (var key in values.Keys)
            {
                TryApplyFieldValue(workItem, key, values[key]);
            }

            if (attachments != null)
            {
                string GetAttachmentKey(string fileName, long length)
                {
                    return($"{fileName}|{length}");
                }

                var existingAttachments = new HashSet <string>(workItem.Attachments
                                                               .OfType <Attachment>()
                                                               .Select(attachment => GetAttachmentKey(attachment.Name, attachment.Length)));

                foreach (var attachment in attachments.Attachments)
                {
                    var    localFileInfo = new FileInfo(attachment.FilePath);
                    string key           = GetAttachmentKey(localFileInfo.Name, localFileInfo.Length);

                    // If there's already an attachment with the same file name and size, don't bother re-uploading it
                    // TODO: this may break embedded images for html replies since we update the img tag above to point to a local path we don't upload
                    // However, we haven't confirmed this breaks and replying with an identical image is unlikely, so we ignore this for now
                    if (!existingAttachments.Contains(key))
                    {
                        workItem.Attachments.Add(new Attachment(attachment.FilePath));
                    }
                }
            }

            ValidateAndSaveWorkItem(workItem);

            workItem.Save();
        }
Example #2
0
        private void InitWorkItemFields(IIncomingEmailMessage message, Dictionary <string, string> workItemUpdates, MessageAttachmentCollection attachments)
        {
            var resolver = new SpecialValueResolver(message, _workItemManager.GetNameResolver());

            workItemUpdates["Title"] = resolver.Subject;
            var rawConversationIndex = message.ConversationId;

            workItemUpdates[_config.WorkItemSettings.ConversationIndexFieldName] =
                rawConversationIndex.Substring(0, Math.Min(rawConversationIndex.Length, TfsTextFieldMaxLength));

            bool enableImgUpdating = _config.WorkItemSettings.EnableExperimentalHtmlFeatures;

            foreach (var defaultFieldValue in _config.WorkItemSettings.DefaultFieldValues)
            {
                var result = resolver.Resolve(defaultFieldValue.Value);
                if (enableImgUpdating && message.IsHtmlBody && defaultFieldValue.Value == SpecialValueResolver.RawMessageBodyKeyword)
                {
                    result = EmailBodyProcessingUtils.UpdateEmbeddedImageLinks(result, attachments.Attachments);
                }

                workItemUpdates[defaultFieldValue.Field] = result;
            }
        }
        public void TestUpdateEmbeddedImageLinks_Basic()
        {
            string original = @"<html>
<body>
<img src=""cid:123"" >
</body>
</html>";

            IReadOnlyCollection <MessageAttachmentInfo> attachmentInfo = new List <MessageAttachmentInfo>
            {
                new MessageAttachmentInfo(@"x:\image.png", "123"),
            };

            // Note: it's acceptable to not preserve whitespace / insert empty HTML tags because we're
            // manipulating HTML, not plain text. As long as the rendered page isn't impacted, all is well
            string expected = @"<html><head></head><body>
<img src=""file:///x:/image.png"">

</body></html>";

            string actual = EmailBodyProcessingUtils.UpdateEmbeddedImageLinks(original, attachmentInfo);

            Assert.AreEqual(Normalize(expected), Normalize(actual));
        }