private async Task <WorkItem> MigrateWorkItemHistoryAsync(MigrationContext context, WorkItem sourceItem, CancellationToken cancellationToken) { WorkItem item = null; var entries = await context.SourceService.GetWorkItemHistoryAsync(sourceItem.Id.Value, cancellationToken).ConfigureAwait(false); //Now migrate the remaining history foreach (var entry in entries) { cancellationToken.ThrowIfCancellationRequested(); Logger.StartActivity($"Migrating history from '{entry.RevisedBy.Name}' on '{entry.RevisedDate}'"); using (var logger = Logger.BeginScope("MigrateHistory")) { var doc = await CreatePatchDocumentAsync(context, entry, cancellationToken).ConfigureAwait(false); //Create or update the item if (item == null) { var teamProject = await context.GetTargetProjectAsync(cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); var workItemType = doc.FirstOrDefault(x => x.Path.Contains(WorkItemFields.WorkItemType)).Value.ToString() ?? sourceItem.GetWorkItemType(); item = await context.TargetService.CreateWorkItemUnrestrictedAsync(teamProject, doc, workItemType, cancellationToken).ConfigureAwait(false); } else { item = await context.TargetService.UpdateWorkItemUnrestrictedAsync(item, doc, cancellationToken).ConfigureAwait(false); } }; } ; return(item); }
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); } }