通过REST API在Azure DevOps中创建工作项的副本?

3
1个回答

3

我们不能直接复制工作项,因为它包含了一些系统字段需要跳过。另外,您的流程可能会在创建步骤中阻止某些字段。以下是通过REST API克隆工作项的小例子,使用https://www.nuget.org/packages/Microsoft.TeamFoundationServer.Client

class Program
{
    static string[] systemFields = { "System.IterationId", "System.ExternalLinkCount", "System.HyperLinkCount", "System.AttachedFileCount", "System.NodeName",
    "System.RevisedDate", "System.ChangedDate", "System.Id", "System.AreaId", "System.AuthorizedAs", "System.State", "System.AuthorizedDate", "System.Watermark",
        "System.Rev", "System.ChangedBy", "System.Reason", "System.WorkItemType", "System.CreatedDate", "System.CreatedBy", "System.History", "System.RelatedLinkCount",
    "System.BoardColumn", "System.BoardColumnDone", "System.BoardLane", "System.CommentCount", "System.TeamProject"}; //system fields to skip

    static string[] customFields = { "Microsoft.VSTS.Common.ActivatedDate", "Microsoft.VSTS.Common.ActivatedBy", "Microsoft.VSTS.Common.ResolvedDate", 
        "Microsoft.VSTS.Common.ResolvedBy", "Microsoft.VSTS.Common.ResolvedReason", "Microsoft.VSTS.Common.ClosedDate", "Microsoft.VSTS.Common.ClosedBy",
    "Microsoft.VSTS.Common.StateChangeDate"}; //unneeded fields to skip

    const string ChildRefStr = "System.LinkTypes.Hierarchy-Forward"; //should be only one parent


    static void Main(string[] args)
    {
        string pat = "<pat>"; //https://learn.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate
        string orgUrl = "https://dev.azure.com/<org>";
        string newProjectName = "";
        int wiIdToClone = 0; 


        VssConnection connection = new VssConnection(new Uri(orgUrl), new VssBasicCredential(string.Empty, pat));
        var witClient = connection.GetClient<WorkItemTrackingHttpClient>();

        CloneWorkItem(witClient, wiIdToClone, newProjectName, true);            
    }

    private static void CloneWorkItem(WorkItemTrackingHttpClient witClient, int wiIdToClone, string NewTeamProject = "", bool CopyLink = false)
    {
        WorkItem wiToClone = (CopyLink) ? witClient.GetWorkItemAsync(wiIdToClone, expand: WorkItemExpand.Relations).Result
            : witClient.GetWorkItemAsync(wiIdToClone).Result;

        string teamProjectName = (NewTeamProject != "") ? NewTeamProject : wiToClone.Fields["System.TeamProject"].ToString();
        string wiType = wiToClone.Fields["System.WorkItemType"].ToString();

        JsonPatchDocument patchDocument = new JsonPatchDocument();

        foreach (var key in wiToClone.Fields.Keys) //copy fields
            if (!systemFields.Contains(key) && !customFields.Contains(key))
                if (NewTeamProject == "" ||
                    (NewTeamProject != "" && key != "System.AreaPath" && key != "System.IterationPath")) //do not copy area and iteration into another project
                    patchDocument.Add(new JsonPatchOperation()
                    {
                        Operation = Operation.Add,
                        Path = "/fields/" + key,
                        Value = wiToClone.Fields[key]
                    });

        if (CopyLink) //copy links
            foreach (var link in wiToClone.Relations)
            {
                if (link.Rel != ChildRefStr)
                {
                    patchDocument.Add(new JsonPatchOperation()
                    {
                        Operation = Operation.Add,
                        Path = "/relations/-",
                        Value = new
                        {
                            rel = link.Rel,
                            url = link.Url
                        }
                    });
                }
            }

        WorkItem clonedWi = witClient.CreateWorkItemAsync(patchDocument, teamProjectName, wiType).Result;

        Console.WriteLine("New work item: " + clonedWi.Id);
    }
}

完整项目链接:https://github.com/ashamrai/AzureDevOpsExtensions/tree/master/CustomNetTasks/CloneWorkItem


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接