public void RetryLinkImportRevision(WiRevision rev, WorkItem wi)
        {
            try
            {
                if (!wi.IsOpen || !wi.IsPartialOpen)
                {
                    wi.PartialOpen();
                }

                var authorField = rev.Fields.Where((field) =>
                {
                    return(field.ReferenceName == WiFieldReference.CreatedBy);
                });
                UpdateWIFields(authorField, wi);

                bool applied = ApplyLinks(rev, wi);

                if (!applied)
                {
                    Logger.Log(LogLevel.Warning, $"RETRY LINKS FAILED: '{rev.ToString()}' - not all changes were saved.");
                }
                else
                {
                    SaveWorkItem(rev, wi);

                    Logger.Log(LogLevel.Debug, $"RETRY LINKS: Imported revision.");
                }

                wi.Close();
            }
            catch (AbortMigrationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                Logger.Log(ex, $"RETRY LINKS: Failed to import revisions for '{wi.Id}'.");
            }
        }
Exemple #2
0
        private void SaveWorkItem(WiRevision rev, WorkItem newWorkItem)
        {
            if (!newWorkItem.IsValid())
            {
                Logger.Log(LogLevel.Warning, $"{rev.ToString()} - Invalid revision");

                var reasons = newWorkItem.Validate();
                foreach (Microsoft.TeamFoundation.WorkItemTracking.Client.Field reason in reasons)
                {
                    Logger.Log(LogLevel.Info, $"Field: {reason.Name}, Status: {reason.Status}, Value: {reason.Value}");
                }
            }
            try
            {
                newWorkItem.Save(SaveFlags.MergeAll);
            }
            catch (FileAttachmentException faex)
            {
                Logger.Log(LogLevel.Error, $"[{faex.GetType().ToString()}] {faex.Message}. Attachment {faex.SourceAttachment.Name}({faex.SourceAttachment.Id}) in {rev.ToString()} will be skipped");
                newWorkItem.Attachments.Remove(faex.SourceAttachment);
                SaveWorkItem(rev, newWorkItem);
            }
        }
 private void SaveWorkItem(WiRevision rev, WorkItem newWorkItem)
 {
     if (!newWorkItem.IsValid())
     {
         var reasons = newWorkItem.Validate();
         foreach (Microsoft.TeamFoundation.WorkItemTracking.Client.Field reason in reasons)
         {
             Logger.Log(LogLevel.Info, $"Field: '{reason.Name}', Status: '{reason.Status}', Value: '{reason.Value}'");
         }
     }
     try
     {
         newWorkItem.Save(SaveFlags.MergeAll);
     }
     catch (FileAttachmentException faex)
     {
         Logger.Log(faex,
                    $"[{faex.GetType().ToString()}] {faex.Message}. Attachment {faex.SourceAttachment.Name}({faex.SourceAttachment.Id}) in {rev.ToString()} will be skipped.");
         newWorkItem.Attachments.Remove(faex.SourceAttachment);
         SaveWorkItem(rev, newWorkItem);
     }
     catch (WorkItemLinkValidationException wilve)
     {
         Logger.Log(wilve, $"[{wilve.GetType()}] {wilve.Message}. Link Source: {wilve.LinkInfo.SourceId}, Target: {wilve.LinkInfo.TargetId} in {rev} will be skipped.");
         var exceedsLinkLimit = RemoveLinksFromWiThatExceedsLimit(newWorkItem);
         if (exceedsLinkLimit)
         {
             SaveWorkItem(rev, newWorkItem);
         }
     }
 }
        public bool ImportRevision(WiRevision rev, WorkItem wi)
        {
            var incomplete = false;

            try
            {
                if (rev.Index == 0)
                {
                    EnsureClassificationFields(rev);
                }

                EnsureDateFields(rev, wi);
                EnsureAuthorFields(rev);
                EnsureAssigneeField(rev, wi);
                EnsureFieldsOnStateChange(rev, wi);

                var attachmentMap = new Dictionary <string, Attachment>();
                if (rev.Attachments.Any() && !ApplyAttachments(rev, wi, attachmentMap))
                {
                    incomplete = true;
                }

                if (rev.Fields.Any() && !UpdateWIFields(rev.Fields, wi))
                {
                    incomplete = true;
                }

                if (rev.Links.Any() && !ApplyLinks(rev, wi))
                {
                    incomplete = true;
                }

                if (incomplete)
                {
                    Logger.Log(LogLevel.Warning, $"'{rev.ToString()}' - not all changes were saved.");
                }

                if (rev.Attachments.All(a => a.Change != ReferenceChangeType.Added) && rev.AttachmentReferences)
                {
                    Logger.Log(LogLevel.Debug, $"Correcting description on '{rev.ToString()}'.");
                    CorrectDescription(wi, _context.GetItem(rev.ParentOriginId), rev);
                }
                if (!string.IsNullOrEmpty(wi.History))
                {
                    Logger.Log(LogLevel.Debug, $"Correcting comments on '{rev.ToString()}'.");
                    CorrectComment(wi, _context.GetItem(rev.ParentOriginId), rev);
                }

                SaveWorkItem(rev, wi);

                foreach (var wiAtt in rev.Attachments)
                {
                    if (attachmentMap.TryGetValue(wiAtt.AttOriginId, out Attachment tfsAtt) && tfsAtt.IsSaved)
                    {
                        _context.Journal.MarkAttachmentAsProcessed(wiAtt.AttOriginId, tfsAtt.Id);
                    }
                }

                if (rev.Attachments.Any(a => a.Change == ReferenceChangeType.Added) && rev.AttachmentReferences)
                {
                    Logger.Log(LogLevel.Debug, $"Correcting description on separate revision on '{rev.ToString()}'.");

                    try
                    {
                        if (CorrectDescription(wi, _context.GetItem(rev.ParentOriginId), rev))
                        {
                            SaveWorkItem(rev, wi);
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(ex, $"Failed to correct description for '{wi.Id}', rev '{rev.ToString()}'.");
                    }
                }

                _context.Journal.MarkRevProcessed(rev.ParentOriginId, wi.Id, rev.Index);

                Logger.Log(LogLevel.Debug, $"Imported revision.");

                wi.Close();

                return(true);
            }
            catch (AbortMigrationException)
            {
                throw;
            }
            catch (Exception ex)
            {
                Logger.Log(ex, $"Failed to import revisions for '{wi.Id}'.");
                return(false);
            }
        }
Exemple #5
0
        public bool ImportRevision(WiRevision rev, WorkItem wi)
        {
            try
            {
                bool incomplete = false;

                if (rev.Index == 0)
                {
                    EnsureClasificationFields(rev);
                }

                EnsureDateFields(rev);
                EnsureAuthorFields(rev);

                var attachmentMap = new Dictionary <string, Attachment>();
                if (rev.Attachments.Any() && !ApplyAttachments(rev, wi, attachmentMap))
                {
                    incomplete = true;
                }

                if (rev.Fields.Any() && !UpdateWIFields(rev.Fields, wi))
                {
                    incomplete = true;
                }

                if (rev.Links.Any() && !ApplyLinks(rev, wi))
                {
                    incomplete = true;
                }

                if (incomplete)
                {
                    Logger.Log(LogLevel.Warning, $"{rev.ToString()} - not all changes were implemented");
                }

                if (!rev.Attachments.Any(a => a.Change == ReferenceChangeType.Added) && rev.AttachmentReferences)
                {
                    Logger.Log(LogLevel.Info, $"Correcting description on {rev.ToString()}");
                    CorrectDescription(wi, _context.GetItem(rev.ParentOriginId), rev);
                }

                SaveWorkItem(rev, wi);

                if (rev.Attachments.Any(a => a.Change == ReferenceChangeType.Added) && rev.AttachmentReferences)
                {
                    Logger.Log(LogLevel.Info, $"Correcting description on separate revision on {rev.ToString()}");

                    try
                    {
                        if (CorrectDescription(wi, _context.GetItem(rev.ParentOriginId), rev))
                        {
                            SaveWorkItem(rev, wi);
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.Log(ex);
                    }
                }

                _context.Journal.MarkRevProcessed(rev.ParentOriginId, wi.Id, rev.Index);
                foreach (var wiAtt in rev.Attachments)
                {
                    if (attachmentMap.TryGetValue(wiAtt.AttOriginId, out Attachment tfsAtt) && tfsAtt.IsSaved)
                    {
                        _context.Journal.MarkAttachmentAsProcessed(wiAtt.AttOriginId, tfsAtt.Id);
                    }
                }


                Logger.Log(LogLevel.Info, $"Imported {rev.ToString()}");

                wi.Close();

                return(true);
            }
            catch (AbortMigrationException ame)
            {
                throw new AbortMigrationException(ame.Reason)
                      {
                          Revision = rev
                      };
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                return(false);
            }
        }