【问题标题】:How to create/update work item with parent-child relation in C# using Azure Devops API in Azure Devops如何使用 Azure Devops 中的 Azure Devops API 在 C# 中创建/更新具有父子关系的工作项
【发布时间】:2019-01-30 11:09:56
【问题描述】:

我正在尝试使用 API 在 Azure devops 中创建/更新工作项。如果它没有任何关系,我可以创建/更新项目。但是,如果我指定关系,例如父子然后我得到以下错误:

TF401349:发生意外错误,请验证您的请求并重试。

我正在使用 JsonPatchDocument 来创建/更新工作项。示例如下:

class Example
{
    JsonPatchOperation AddRelationship(JsonPatchDocument doc, string rel, WorkItem linkedItem, bool isNew, int index)
    {
        //update link
        if (!isNew)
        {
            return new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path = "/relations/" + index,
                Value = new { rel, url = linkedItem.Url, attributes = new { comment = "comment while update" } }
            };
        }
        else
            return new JsonPatchOperation()
            {
                Operation = Operation.Add,
                Path = "/relations/-",
                Value = new { rel, url = linkedItem.Url, attributes = new { comment = "Comment while creating item" } }
            };
    }

    void Save()
    {
        // some code
        doc.Add(AddRelationship(doc, "System.LinkTypes.Hierarchy-Forward", item, isNew, index++));

        var workItem = isNew
                     ? witClient.CreateWorkItemAsync(doc, Vsts.Project, issueType, bypassRules: true, validateOnly: Mode == ProcessingMode.ReadOnly).Result
                     : witClient.UpdateWorkItemAsync(doc, existingWorkItemId.Value, bypassRules: true, validateOnly: Mode == ProcessingMode.ReadOnly).Result;

    }
}

}

谢谢。

【问题讨论】:

标签: azure-devops azure-devops-rest-api


【解决方案1】:

我在您的示例中看不到“rel”的定义。像这样的:

patchDocument.Add(new JsonPatchOperation()
{
    Operation = Operation.Add,
    Path = "/relations/-",
    Value = new {
        rel = "System.LinkTypes.Hierarchy-Forward",
        url = RelUrl,
        attributes = new
        {
            comment = "Comment for the link"
        }
    }
});

也许你的代码必须是这样的:

JsonPatchOperation AddRelationship(JsonPatchDocument doc, string relname, WorkItem linkedItem, bool isNew, int index)
{
    //update link
    if (!isNew)
    {
        return new JsonPatchOperation()
        {
            Operation = Operation.Replace,
            Path = "/relations/" + index + "/attributes/comment",
            Value = "comment while update" 
        };
    }
    else
        return new JsonPatchOperation()
        {
            Operation = Operation.Add,
            Path = "/relations/-",
            Value = new { rel = relname, url = linkedItem.Url, attributes = new { comment = "Comment while creating item" } }
        };
}

【讨论】:

  • 我也检查了替换和添加操作。由于工作项只有一个子项,我尝试提供 Path = "/relations/1" 并且它给出了超出范围的异常。但是当我提供正确的索引,即“/relations/0”时,我得到了上述异常。
  • 根据这个链接Update a link我们可以通过“/relations/[index]/attributes/comment”改变属性。如果您想将链接设置到另一个工作项:删除旧链接并添加新链接。
猜你喜欢
  • 2021-11-21
  • 2020-07-26
  • 2021-06-14
  • 2021-03-19
  • 1970-01-01
  • 1970-01-01
  • 2019-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多