Esempio n. 1
0
        private XmlDocument CreateWorkItemUpdateOperation(MigrationAction action)
        {
            XmlDocument desc     = action.RecordDetails.DetailsDocument;
            XmlElement  rootNode = desc.DocumentElement;

            Debug.Assert(null != rootNode,
                         "Wit IMigrationAction.MigrationActionDescription is invalid.");
            Debug.Assert(null != rootNode.Attributes["WorkItemType"],
                         "WorkItemType is missing in MigrationActionDescription.");
            Debug.Assert(null != rootNode.Attributes["Author"],
                         "Author is missing in MigrationActionDescription.");
            Debug.Assert(null != rootNode.Attributes["ChangeDate"],
                         "ChangeDate is missing in MigrationActionDescription.");
            string workItemType   = rootNode.Attributes["WorkItemType"].Value;
            string author         = rootNode.Attributes["Author"].Value;
            string changeDate     = rootNode.Attributes["ChangeDate"].Value;
            string targetRevision = rootNode.Attributes["TargetRevision"].Value;

            TfsUpdateDocument tfsUpdateDocument = InitializeUpdateDocument();

            tfsUpdateDocument.CreateWorkItemUpdateDoc(TargetWorkItem.Id.ToString(), targetRevision);
            tfsUpdateDocument.AddFields(action, workItemType, author, changeDate, false);

            tfsUpdateDocument.InsertConversionHistoryCommentToHistory(
                workItemType,
                GenerateMigrationHistoryComment(action));

            // insert source item Id to field TfsMigrationTool.ReflectedWorkItemId if it is in WITD
            tfsUpdateDocument.InsertConversionHistoryField(
                workItemType,
                action.SourceWorkItemId);

            return(tfsUpdateDocument.UpdateDocument);
        }
        protected override TfsUpdateDocument SubmitAttachmentChanges(IMigrationAction action, ConflictManager conflictMgrService)
        {
            /*
             * retrieve change details
             */
            XmlDocument desc     = action.MigrationActionDescription;
            XmlElement  rootNode = desc.DocumentElement;

            Debug.Assert(null != rootNode);
            XmlNode attachmentNode   = rootNode.FirstChild;
            string  originalName     = attachmentNode.Attributes["Name"].Value;
            string  utcCreationDate  = attachmentNode.Attributes["UtcCreationDate"].Value;
            string  utcLastWriteDate = attachmentNode.Attributes["UtcLastWriteDate"].Value;
            string  length           = attachmentNode.Attributes["Length"].Value;
            string  comment          = attachmentNode.FirstChild.InnerText;
            int     targetWorkItemId = FindTargetWorkItemId(action, conflictMgrService);
            string  targetRevision   = rootNode.Attributes["TargetRevision"].Value;

            /*
             * create operation document
             */
            TfsUpdateDocument tfsUpdateDocument = InitializeUpdateDocument();

            tfsUpdateDocument.CreateWorkItemUpdateDoc(targetWorkItemId.ToString(), targetRevision);

            /*
             * insert Connector specific comment
             */
            WorkItem item = WorkItemStore.GetWorkItem(targetWorkItemId);

            Debug.Assert(null != item, "target work item does not exist");
            tfsUpdateDocument.InsertConversionHistoryCommentToHistory(item.Type.Name, GenerateMigrationHistoryComment(action));

            int[] fileId = FindAttachmentFileId(targetWorkItemId, originalName,
                                                utcCreationDate, utcLastWriteDate, length, comment);

            /*
             * delete attachment
             */
            if (action.Action == WellKnownChangeActionId.DelAttachment)
            {
                if (fileId.Length == 0)
                {
                    action.State = ActionState.Skipped;
                    return(null);
                }
                else
                {
                    tfsUpdateDocument.RemoveAttachment(fileId[0]);
                    return(tfsUpdateDocument);
                }
            }

            /*
             * add attachment
             */
            try
            {
                string sourceStoreCountString = attachmentNode.Attributes["CountInSourceSideStore"].Value;
                int    sourceStoreCount;
                if (int.TryParse(sourceStoreCountString, out sourceStoreCount))
                {
                    if (sourceStoreCount <= fileId.Length)
                    {
                        action.State = ActionState.Skipped;
                        return(null);
                    }
                }
            }
            catch (Exception e)
            {
                TraceManager.TraceVerbose(e.ToString());
                // for backward compatibility, just proceed
            }

            if (AttachmentIsOversized(length))
            {
                MigrationConflict conflict = new FileAttachmentOversizedConflictType().CreateConflict(
                    originalName, length, MaxAttachmentSize, targetWorkItemId.ToString(), Core.ServerName, Core.Config.Project, action);

                List <MigrationAction>   actions;
                ConflictResolutionResult resolveRslt = conflictMgrService.TryResolveNewConflict(conflictMgrService.SourceId, conflict, out actions);

                if (!resolveRslt.Resolved)
                {
                    return(null);
                }

                if (resolveRslt.ResolutionType == ConflictResolutionType.SuppressedConflictedChangeAction)
                {
                    action.State = ActionState.Skipped;
                    return(null);
                }

                if (resolveRslt.ResolutionType == ConflictResolutionType.Other)
                {
                    // conflict resolved, just proceed
                }
                else
                {
                    throw new NotImplementedException();
                }
            }

            Guid fileGuid = Guid.NewGuid();

            tfsUpdateDocument.AddAttachment(originalName, XmlConvert.ToString(fileGuid),
                                            utcCreationDate, utcLastWriteDate, length, comment);

            // get areaNodeUri - required by Dev10 OM
            Project project        = item.Project;
            string  projectNodeUri = (project.Uri).ToString();
            string  areaNodeUri    = string.Empty;

            if (project.Id == item.AreaId)
            {
                areaNodeUri = Core.AreaNodeUri;
            }
            else
            {
                // Loop through the area root nodes looking for the one we're on
                foreach (Node node in project.AreaRootNodes)
                {
                    // It could be one of the root nodes
                    if (node.Id == item.AreaId)
                    {
                        areaNodeUri = node.Uri.ToString();
                        break;
                    }

                    // Now check if it is a child of the current area root node
                    try
                    {
                        Node node2 = node.FindNodeInSubTree(item.AreaId);
                        areaNodeUri = node2.Uri.ToString();
                        break;
                    }
                    catch (DeniedOrNotExistException)
                    {
                        // Ignore if not found, go onto the next area root node
                        continue;
                    }
                }
            }

            //Now upload the file since that has to be done before the Xml batch is executed.
            Debug.Assert(!string.IsNullOrEmpty(LocalWorkDir));
            string filePath = Path.Combine(LocalWorkDir, fileGuid.ToString());

            action.SourceItem.Download(filePath);
            using (var strm = File.OpenRead(filePath))
            {
                var f = new FileAttachment();
                f.AreaNodeUri  = areaNodeUri;
                f.ProjectUri   = projectNodeUri;
                f.FileNameGUID = fileGuid;
                f.LocalFile    = strm; // attachment.GetFileContents();

                WorkItemServer.UploadFile(f);
            }

            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }

            return(tfsUpdateDocument);
        }