public bool UpdateWorkItemLinks()

in src/VstsRestAPI/WorkItemAndTracking/ImportWorkItems.cs [323:452]


        public bool UpdateWorkItemLinks(string workItemTemplateJson)
        {
            try
            {
                ImportWorkItemModel.WorkItems fetchedPBIs = JsonConvert.DeserializeObject<ImportWorkItemModel.WorkItems>(workItemTemplateJson);
                string wiToUpdate = "";
                WIMapData findIDforUpdate;
                if (fetchedPBIs.count > 0)
                {

                    foreach (ImportWorkItemModel.Value newWI in fetchedPBIs.value)
                    {
                        //continue next iteration if there is no relation
                        if (newWI.relations == null)
                        {
                            continue;
                        }

                        int relCount = newWI.relations.Length;
                        string oldWIID = newWI.id.ToString();

                        findIDforUpdate = wiData.Find(t => t.OldID == oldWIID);
                        if (findIDforUpdate != null)
                        {
                            wiToUpdate = findIDforUpdate.NewID;
                            foreach (ImportWorkItemModel.Relations rel in newWI.relations)
                            {
                                if (relTypes.Contains(rel.rel.Trim()))
                                {
                                    oldWIID = rel.url.Substring(rel.url.LastIndexOf("/") + 1);
                                    WIMapData findIDforlink = wiData.Find(t => t.OldID == oldWIID);

                                    if (findIDforlink != null)
                                    {
                                        string newWIID = findIDforlink.NewID;
                                        Object[] patchWorkItem = new Object[1];
                                        // change some values on a few fields
                                        patchWorkItem[0] = new
                                        {
                                            op = "add",
                                            path = "/relations/-",
                                            value = new
                                            {
                                                rel = rel.rel,
                                                url = _configuration.UriString + "/_apis/wit/workitems/" + newWIID,
                                                attributes = new
                                                {
                                                    comment = "Making a new link for the dependency"
                                                }
                                            }
                                        };
                                        if (UpdateLink("Product Backlog Item", wiToUpdate, patchWorkItem))
                                        {
                                        }
                                    }
                                }
                                if (rel.rel == "Hyperlink")
                                {
                                    Object[] patchWorkItem = new Object[1];
                                    patchWorkItem[0] = new
                                    {
                                        op = "add",
                                        path = "/relations/-",
                                        value = new
                                        {
                                            rel = "Hyperlink",
                                            url = rel.url
                                        }
                                    };
                                    bool isHyperLinkCreated = UpdateLink(string.Empty, wiToUpdate, patchWorkItem);
                                }
                                if (rel.rel == "AttachedFile")
                                {
                                    Object[] patchWorkItem = new Object[1];
                                    string filPath = string.Format(attachmentFolder + @"\{0}{1}", rel.attributes["id"], rel.attributes["name"]);
                                    string fileName = rel.attributes["name"];
                                    string attchmentURl = UploadAttchment(filPath, fileName);
                                    if (!string.IsNullOrEmpty(attchmentURl))
                                    {
                                        patchWorkItem[0] = new
                                        {
                                            op = "add",
                                            path = "/relations/-",
                                            value = new
                                            {
                                                rel = "AttachedFile",
                                                url = attchmentURl
                                            }
                                        };
                                        bool isAttachmemntCreated = UpdateLink(string.Empty, wiToUpdate, patchWorkItem);
                                    }
                                }
                                if (rel.rel == "ArtifactLink")
                                {
                                    rel.url = rel.url.Replace("$projectId$", projectId).Replace("$RepositoryId$", repositoryId);
                                    foreach (var pullReqest in pullRequests)
                                    {
                                        string key = string.Format("${0}$", pullReqest.Key);
                                        rel.url = rel.url.Replace(key, pullReqest.Value);
                                    }
                                    Object[] patchWorkItem = new Object[1];
                                    patchWorkItem[0] = new
                                    {
                                        op = "add",
                                        path = "/relations/-",
                                        value = new
                                        {
                                            rel = "ArtifactLink",
                                            url = rel.url,
                                            attributes = new
                                            {
                                                name = rel.attributes["name"]
                                            }
                                        }

                                    };
                                    bool isArtifactLinkCreated = UpdateLink(string.Empty, wiToUpdate, patchWorkItem);
                                }
                            }
                        }
                    }
                    return true;
                }
            }
            catch (Exception ex)
            {
                logger.Debug(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + ex.Message + "\t" + "\n" + ex.StackTrace + "\n");
            }
            return false;
        }