public bool AddTicket(Ticket toAdd)
{
var addedOk = false;
var ticketSummary = toAdd.ID + " - " + toAdd.Summary;
onDetailedProcessing(ticketSummary);
if (previouslyImported.ContainsKey(toAdd.ID) == false)
{
var workItem = toWorkItem(toAdd);
var rejectedAttachments = new List<string>();
foreach (var attachment in toAdd.Attachments)
{
if (attachment.Downloaded)
{
var toAttach =
new Microsoft.TeamFoundation.WorkItemTracking.Client.Attachment(attachment.Source);
workItem.Attachments.Add(toAttach);
}
}
if (toAdd.Description.Length > max_Description_length)
{
var attachment =
Path.Combine(Path.GetTempPath(), string.Format("{0} (Description).txt", toAdd.ID));
File.WriteAllText(attachment, toAdd.Description);
var toAttach =
new Microsoft.TeamFoundation.WorkItemTracking.Client.Attachment(attachment);
workItem.Attachments.Add(toAttach);
}
var attempts = workItem.AttachedFileCount + 1;
do
{
try
{
workItem.Save(SaveFlags.MergeAll);
addedOk = true;
}
catch (FileAttachmentException attachmentException)
{
var rejectedFile = attachmentException.SourceAttachment.Name;
rejectedAttachments.Add(string.Format("{0} ({1})", rejectedFile, attachmentException.Message));
workItem.Attachments.Clear();
foreach (var attachment in toAdd.Attachments)
{
if (string.CompareOrdinal(rejectedFile, attachment.FileName) != 0 && attachment.Downloaded)
{
var toAttach =
new Microsoft.TeamFoundation.WorkItemTracking.Client.Attachment(attachment.Source);
workItem.Attachments.Add(toAttach);
}
}
attempts--;
}
catch (Exception generalException)
{
importSummary.Errors.Add(string.Format("Failed to add work item '{0}'.", ticketSummary));
importSummary.Errors.Add(generalException.Message);
break;
}
} while (addedOk == false && attempts > 0);
if (addedOk)
{
newlyImported[toAdd] = workItem;
if (toAdd.Description.Length > max_Description_length)
{
importSummary.Warnings.Add(
string.Format("Description for {0} - \"{1}\" stored as attachment. Exceeded 32k.", workItem.Id, workItem.Title));
}
if (rejectedAttachments.Count > 0)
{
importSummary.Warnings.Add(
string.Format("Failed to attach the following item(s) to {0} - \"{1}\"", workItem.Id, workItem.Title));
foreach (var file in rejectedAttachments)
{
importSummary.Warnings.Add(string.Format(" - {0}", file));
}
failedAttachments = true;
}
}
}
else
{
addedOk = true;
}
return addedOk;
}