private WitBatchRequest GenerateWitBatchRequestFromWorkItem(WorkItem sourceWorkItem) { Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("Content-Type", "application/json-patch+json"); JsonPatchDocument jsonPatchDocument = CreateJsonPatchDocumentFromWorkItemFields(sourceWorkItem); JsonPatchOperation insertIdAddOperation = GetInsertBatchIdAddOperation(); jsonPatchDocument.Add(insertIdAddOperation); // add hyperlink to source WorkItem string sourceWorkItemApiEndpoint = ClientHelpers.GetWorkItemApiEndpoint(this.migrationContext.Config.SourceConnection.Account, sourceWorkItem.Id.Value); JsonPatchOperation addHyperlinkAddOperation = MigrationHelpers.GetHyperlinkAddOperation(sourceWorkItemApiEndpoint, sourceWorkItem.Rev.ToString()); jsonPatchDocument.Add(addHyperlinkAddOperation); string json = JsonConvert.SerializeObject(jsonPatchDocument); string workItemType = jsonPatchDocument.Find(a => a.Path.Contains(FieldNames.WorkItemType)).Value as string; var witBatchRequest = new WitBatchRequest(); witBatchRequest.Method = "PATCH"; witBatchRequest.Headers = headers; witBatchRequest.Uri = $"/{this.migrationContext.Config.TargetConnection.Project}/_apis/wit/workItems/${workItemType}?{this.QueryString}"; witBatchRequest.Body = json; return(witBatchRequest); }
public void GetSourceWorkItemApiEndpoint_AccountWithoutSlashReturnsCorrectValue() { string account = "accountWithoutSlash"; int workItemId = 777; string expected = "accountWithoutSlash/_apis/wit/workItems/777"; string actual = ClientHelpers.GetWorkItemApiEndpoint(account, workItemId); Assert.AreEqual(expected, actual); }
public static JsonPatchOperation GetWorkItemLinkAddOperation(IMigrationContext migrationContext, WorkItemLink workItemLink) { string workItemEndpoint = ClientHelpers.GetWorkItemApiEndpoint(migrationContext.Config.TargetConnection.Account, workItemLink.Id); JsonPatchOperation jsonPatchOperation = new JsonPatchOperation(); jsonPatchOperation.Operation = Operation.Add; jsonPatchOperation.Path = $"/{Constants.Relations}/-"; jsonPatchOperation.Value = new { rel = workItemLink.ReferenceName, url = workItemEndpoint, attributes = new { comment = workItemLink.Comment } }; return(jsonPatchOperation); }
public async Task Preprocess(IMigrationContext migrationContext, IBatchMigrationContext batchContext, IList <WorkItem> sourceWorkItems, IList <WorkItem> targetWorkItems) { var linkedWorkItemArtifactUrls = new HashSet <string>(); foreach (WorkItem sourceWorkItem in sourceWorkItems) { var relations = GetWorkItemLinkRelations(migrationContext, sourceWorkItem.Relations); var linkedIds = relations.Select(r => ClientHelpers.GetWorkItemIdFromApiEndpoint(r.Url)); var uris = linkedIds.Where(id => !migrationContext.SourceToTargetIds.ContainsKey(id)).Select(id => ClientHelpers.GetWorkItemApiEndpoint(migrationContext.Config.SourceConnection.Account, id)); linkedWorkItemArtifactUrls.AddRange(uris); } await linkedWorkItemArtifactUrls.Batch(Constants.BatchSize).ForEachAsync(migrationContext.Config.Parallelism, async(workItemArtifactUris, batchId) => { Logger.LogTrace(LogDestination.File, $"Finding linked work items on target for batch {batchId}"); var results = await ClientHelpers.QueryArtifactUriToGetIdsFromUris(migrationContext.TargetClient.WorkItemTrackingHttpClient, workItemArtifactUris); foreach (var result in results.ArtifactUrisQueryResult) { if (result.Value != null) { if (result.Value.Count() == 1) { var sourceId = ClientHelpers.GetWorkItemIdFromApiEndpoint(result.Key); var targetId = result.Value.First().Id; migrationContext.SourceToTargetIds[sourceId] = targetId; } } } Logger.LogTrace(LogDestination.File, $"Finished finding linked work items on target for batch {batchId}"); }); }