TicketImporter.TfsProject.toWorkItem C# (CSharp) Method

toWorkItem() private method

private toWorkItem ( Ticket toImport ) : Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem
toImport Ticket
return Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem
        private WorkItem toWorkItem(Ticket toImport)
        {
            var tfs_impersonated = tfsUsers.ImpersonateDefaultCreator();
            if (tfsUsers.CanAddTicket(toImport.CreatedBy))
            {
                tfs_impersonated = tfsUsers.Impersonate(toImport.CreatedBy);
            }

            var workItemStore = (WorkItemStore) tfs_impersonated.GetService(typeof (WorkItemStore));
            var workItemTypes = workItemStore.Projects[project].WorkItemTypes;

            var workItemType = workItemTypes[toImport.TicketType];
            var workItem = new WorkItem(workItemType);

            foreach (var fieldName in tfsFieldMap.Fields.EditableFields.
                Where(fieldName => string.IsNullOrEmpty(fields[fieldName].DefaultValue) == false))
            {
                assignToField(workItem, fieldName, fields[fieldName].DefaultValue);
            }

            workItem.Title = toImport.Summary;
            var description = toImport.Description;

            // TFS's limit on HTML / PlainText fields is 32k.
            if (description.Length > max_Description_length)
            {
                var attachment = string.Format("{0} (Description).txt", toImport.ID);
                description = "<p><b>Description stored as Attachment</b></p>";
                description += "<ul><li>Description exceeds 32K.A limit imposed by TFS.</li>";
                description += ("<li>See attachment \"" + attachment + "\"</li></ul>");
            }

            workItem.Description = description;
            assignToField(workItem, "Repro Steps", description);

            assignToField(workItem, "Team", assignedTeam);
            tfsUsers.AssignUser(toImport.AssignedTo, workItem);

            assignToField(workItem, "Story Points", toImport.StoryPoints);
            assignToField(workItem, "Effort", toImport.StoryPoints);
            workItem.AreaPath = (string.IsNullOrWhiteSpace(assignedAreaPath) ? project : assignedAreaPath);
            assignToField(workItem, "External Reference", toImport.ID);
            assignToField(workItem, tfsPriorityMap.PriorityField, tfsPriorityMap[toImport.Priority]);

            if (toImport.HasUrl)
            {
                try
                {
                    var hl = new Hyperlink(toImport.Url)
                    {
                        Comment = string.Format("{0} [{1}]", externalReferenceTag, toImport.ID)
                    };
                    workItem.Links.Add(hl);
                }
                catch
                {
                    /*Do nothing..*/
                }
            }

            var c = new StringBuilder();
            foreach (var comment in toImport.Comments)
            {
                var body = String.Format("<i>{0}</i></br>Created by {1} on the {2}.<br>",
                    comment.Body.Replace(Environment.NewLine, "<br>"),
                    comment.Author.DisplayName,
                    comment.CreatedOn.ToShortDateString());
                if (comment.UpdatedLater)
                {
                    body = String.Format("{0}<br>(Last updated on the {1}).<br>", body,
                        comment.Updated.ToShortDateString());
                }
                c.Append(body);
            }

            if (c.Length > 0)
            {
                c.Append("<br>");
            }
            c.Append(string.Format("<u><b>Additional {0} information</b></u><br>", externalReferenceTag));

            var rows = new List<Tuple<string, string>>
            {
                new Tuple<string, string>("Ticket",
                    string.Format("<a href=\"{0}\">{1}</a>", toImport.Url, toImport.ID + " - " + toImport.Summary)),
                new Tuple<string, string>("Created by ", toImport.CreatedBy.DisplayName),
                new Tuple<string, string>("Created on ", toImport.CreatedOn.ToString(CultureInfo.InvariantCulture))
            };
            if (toImport.TicketState == Ticket.State.Done)
            {
                rows.Add(new Tuple<string, string>("Closed on ", toImport.ClosedOn.ToString(CultureInfo.InvariantCulture)));
            }
            if (string.IsNullOrWhiteSpace(toImport.Project) == false)
            {
                rows.Add(new Tuple<string, string>("Belonged To", toImport.Project));
            }

            c.Append("<table style=\"width:100%\">");
            foreach (var row in rows)
            {
                c.Append(string.Format("<tr><td><b>{0}</b></td><td>{1}</td></tr>", row.Item1, row.Item2));
            }
            c.Append("</table>");
            workItem.History = c.ToString();

            return workItem;
        }