Azure DevOps 评论中的 Ping (@) 用户

Ping (@) user in Azure DevOps comment

我有下面的方法可以在 Azure DevOps 中发布对工作项的评论。

我如何@评论中的用户?

下面的两个例子只是发布了一个 @firstName lastName 的字符串,并没有标记用户。

pMessage = "@User you need to take a look at this workitem"

pMessage = "@firstName lastName <mail> you need to take a look at this workitem"

public async Task PingUser(List<int> pId, string pMessage, VssConnection pConnection)
{
    WorkItemTrackingHttpClient client = pConnection.GetClient<WorkItemTrackingHttpClient>();

    foreach (var id in pId)
    {
        await client.UpdateWorkItemAsync(
            new JsonPatchDocument()
            {new JsonPatchOperation(){
                Operation = Operation.Add,
                Value = pMessage,
                Path = "/fields/System.History",
            }}, id);
    }
}

如果你执行Get操作你可以看到你需要的格式:

var wi = workitemClient.GetWorkItemAsync("project", id).Result;

wi.Fields["System.History"]值为:

所以格式是:

<a href="#" data-vss-mention="version:2.0,userid"></a>

userid 替换为用户 ID,要获取它,您可以使用 User Entitlements - List Rest API.