如何使用Azure Devops API在Azure Devops中使用C#创建/更新具有父子关系的工作项

3

我正在尝试使用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;

    }
}

}

谢谢。


4
增加您的示例。没有源代码很难找到错误))也许这个链接对您有用:Azure DevOps Rest API添加和编辑工作项链接 - Shamrai Aleksander
@ShamraiAleksander 请看我用过的示例...谢谢。 - user2663330
1个回答

4
我看不到你的示例中 "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" } }
        };
}

我已经检查了Replace和Add操作。由于工作项只有一个子项,我尝试提供路径 =“/ relations / 1”,但它会抛出范围异常。但是,当我提供正确的索引即“/ relations / 0”时,我得到了上面的异常。 - user2663330
根据此链接更新链接,我们可以通过"/relations/[index]/attributes/comment"更改属性。如果您想将链接设置为另一个工作项:请删除旧链接并添加新链接。 - Shamrai Aleksander

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