Ejemplo n.º 1
0
        private async Task MigrateWorkItemAttachmentAsync(MigrationContext context, IEnumerable <WorkItemRelation> relations, WorkItem targetItem, CancellationToken cancellationToken)
        {
            var doc = new JsonPatchDocument();

            var attachmentFiles = relations.Where(r => r.IsAttachment());

            foreach (var attachmentFile in attachmentFiles)
            {
                var guid = attachmentFile.GetRelatedGuid();

                if (guid == Guid.Empty)
                {
                    continue;
                }

                var fileName  = attachmentFile.Attributes["name"].ToString();
                var attributs = attachmentFile.Attributes.Where(x => x.Key != "id").ToDictionary(x => x.Key, x => x.Value);

                var readOnlyAttachmentContent = await context.SourceService.TryCatchAsync(c => c.GetAttachmentContentAsync(guid, cancellationToken: cancellationToken)).ConfigureAwait(false);

                using (MemoryStream attachmentContent = new MemoryStream())
                {
                    readOnlyAttachmentContent.CopyTo(attachmentContent);
                    attachmentContent.Position = 0;

                    var attachmentReference = await context.TargetService.TryCatchAsync(c => c.CreateAttachmentAsync(attachmentContent, fileName: fileName, cancellationToken: cancellationToken)).ConfigureAwait(false);

                    Logger.Debug($"Adding Attachment file {fileName}");
                    doc.AddLink(new WorkItemRelation {
                        Attributes = attributs, Rel = WorkItemRelations.Attachment, Url = attachmentReference.Url
                    });
                }
            }

            //If we have made any changes then update the target
            if (doc.Any())
            {
                await context.TargetService.UpdateWorkItemUnrestrictedAsync(targetItem, doc, cancellationToken).ConfigureAwait(false);
            }
        }
        private async Task MigrateWorkItemGitCommitLinkAsync(MigrationContext context, IEnumerable <WorkItemRelation> relations, WorkItem targetItem, CancellationToken cancellationToken)
        {
            var   doc = new JsonPatchDocument();
            var   targetRepositories = Settings.Repositories.ToDictionary(x => x.Source, x => x.Target);
            Regex regexSplitGitUrl   = new Regex(@"^vstfs:\/\/\/git\/commit\/(?<projectId>[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12})\/(?<repositoryId>[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12})\/(?<commitId>[a-f0-9]{40})$", RegexOptions.IgnoreCase);

            var sourceGitCommitRelations = relations.Where(r => r.IsGitCommit());

            foreach (var sourceGitCommit in sourceGitCommitRelations)
            {
                var gitCommitUrl = Uri.UnescapeDataString(sourceGitCommit.Url);
                var match        = regexSplitGitUrl.Match(gitCommitUrl);

                if (match.Success && targetRepositories.TryGetValue(match.Groups["repositoryId"].Value, out string targetRepositoryId))
                {
                    var targetProject = await context.GetTargetProjectAsync(cancellationToken);

                    var targetgitCommitUrl           = regexSplitGitUrl.Replace(gitCommitUrl, m => $"Vstfs:///Git/Commit/{targetProject.Id}/{targetRepositoryId}/{m.Groups["commitId"].Value}");
                    WorkItemRelation targetGitCommit = new WorkItemRelation
                    {
                        Rel        = sourceGitCommit.Rel,
                        Attributes = sourceGitCommit.Attributes,
                        Url        = targetgitCommitUrl
                    };

                    Logger.Debug($"Adding Git commit link {targetgitCommitUrl}");
                    doc.AddLink(targetGitCommit);
                }
            }

            //If we have made any changes then update the target
            if (doc.Any())
            {
                await context.TargetService.UpdateWorkItemUnrestrictedAsync(targetItem, doc, cancellationToken).ConfigureAwait(false);
            }
        }
Ejemplo n.º 3
0
        private async Task MigrateWorkItemRelationsAsync(MigrationContext context, IEnumerable <WorkItemRelation> relations, WorkItem targetItem, CancellationToken cancellationToken)
        {
            var doc = new JsonPatchDocument();

            foreach (var relation in relations)
            {
                var isChild   = relation.IsChild();
                var isParent  = !isChild && relation.IsParent();
                var isRelated = !isChild && !isParent && relation.IsRelated();

                //Not a relation we care about
                if (!isChild && !isParent && !isRelated)
                {
                    continue;
                }

                //Get the related item information
                var relatedId = relation.GetRelatedId();
                if (relatedId <= 0)
                {
                    continue;
                }

                var related = context.GetMigratedWorkItem(relatedId);

                if (isChild)
                {
                    //If the child has been migrated then add a child link to the related item
                    //else if the target isn't closed (or the override setting is set) then add the related item to migration list
                    if (related != null)
                    {
                        if (related.TargetId != 0)
                        {
                            Logger.Debug($"Adding Child link to {related.TargetId}");
                            doc.AddLink(WorkItemRelations.Child, related.TargetUrl);
                        }
                        else
                        {
                            Logger.Warning($"Skipping Child link to {related.SourceId} because it failed to migrate");
                        }
                    }
                    else if (!targetItem.IsClosed() || Settings.IncludeChildLinksOnClosed)
                    {
                        Logger.Debug($"Adding Child link {relatedId} to migration list");
                        AddToMigrationQueue(context.MigrationQueue, new MigratedWorkItem()
                        {
                            SourceId = relatedId
                        });
                    }
                    ;
                }
                else if (isParent)
                {
                    //If the parent has already been migrated then add a parent link to the related item
                    if (related != null)
                    {
                        if (related.TargetId != 0)
                        {
                            Logger.Debug($"Adding Parent link to {related.TargetId}");
                            doc.AddLink(WorkItemRelations.Parent, related.TargetUrl);
                        }
                        else
                        {
                            Logger.Warning($"Skipping Parent link to {related.SourceId} because it failed to migrate");
                        }
                    }
                    else if (!targetItem.IsClosed() || Settings.IncludeParentLinksOnClosed)
                    {
                        Logger.Debug($"Adding Parent link {relatedId} to migration list");
                        AddToMigrationQueue(context.MigrationQueue, new MigratedWorkItem()
                        {
                            SourceId = relatedId
                        });
                    }
                    ;
                }
                else if (isRelated)
                {
                    //If the related item has already been migrated then add a related link to it
                    //else if the target is not closed (or the override setting is set) then add the related item to the migration list
                    if (related != null)
                    {
                        if (related.TargetId != 0)
                        {
                            Logger.Debug($"Adding Related link to {related.TargetId}");
                            doc.AddLink(WorkItemRelations.Related, related.TargetUrl);
                        }
                        else
                        {
                            Logger.Warning($"Skipping Related link to {related.SourceId} because it failed to migrate");
                        }
                    }
                    else if (!targetItem.IsClosed() || Settings.IncludeRelatedLinksOnClosed)
                    {
                        Logger.Debug($"Adding Related link {relatedId} to migration list");
                        AddToMigrationQueue(context.MigrationQueue, new MigratedWorkItem()
                        {
                            SourceId = relatedId
                        });
                    }
                    ;
                }
                ;  //else ignore

                cancellationToken.ThrowIfCancellationRequested();
            }
            ;

            //If we have made any changes then update the target
            if (doc.Any())
            {
                await context.TargetService.UpdateWorkItemUnrestrictedAsync(targetItem, doc, cancellationToken).ConfigureAwait(false);
            }
        }