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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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}");
            });
        }