You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过API在Azure DevOps工作项评论中@提及团队?

如何通过API在Azure DevOps工作项评论中@提及团队?

嗨,我来帮你搞定这个问题!在Azure DevOps里通过API@提及团队和提及用户的方式确实不一样,我给你一步步讲清楚怎么做:

1. 明确团队提及的正确格式

和用户提及不同,团队需要用**@[团队显示名称]<团队唯一ID>**的格式来构造,这里的团队ID是Azure DevOps内部分配的GUID,不是显示名称——直接写显示名是不会触发@提醒的,这是关键!

2. 获取团队的唯一ID

你需要先调用Azure DevOps的Teams API拿到目标团队的ID:

  • 请求地址:GET https://dev.azure.com/{你的组织名}/{你的项目名}/_apis/teams?api-version=7.1-preview.3
  • 用PAT令牌做身份验证,返回的结果里每个团队对象的id字段就是我们需要的GUID。

3. 构造评论并调用Work Item API更新

拿到团队ID后,就可以构造包含团队提及的评论内容,然后调用Work Items的更新API把评论添加到工作项中。这里给你一个C#的示例代码:

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;

class AzureDevOpsCommentHelper
{
    static async System.Threading.Tasks.Task Main(string[] args)
    {
        // 替换成你的实际信息
        string orgName = "你的组织名称";
        string projectName = "你的项目名称";
        string workItemId = "目标工作项的ID";
        string patToken = "你的PAT令牌(需要Work Items读写权限)";
        string targetTeamName = "你的团队显示名";
        string targetTeamId = "从Teams API获取的团队GUID";

        // 构造带团队提及的评论
        string commentContent = $"@{targetTeamName}<{targetTeamId}> 麻烦关注一下这个工作项的最新进展~";

        // 构造JSON Patch文档,用于更新工作项的历史(评论)字段
        var patchOperations = new[]
        {
            new
            {
                op = "add",
                path = "/fields/System.History",
                value = commentContent
            }
        };

        string patchJson = JsonConvert.SerializeObject(patchOperations);

        using (HttpClient client = new HttpClient())
        {
            // 设置请求头
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                "Basic", 
                Convert.ToBase64String(Encoding.ASCII.GetBytes($":{patToken}"))
            );

            // 构建API请求地址
            string apiUrl = $"https://dev.azure.com/{orgName}/{projectName}/_apis/wit/workitems/{workItemId}?api-version=7.1-preview.3";

            // 发送Patch请求
            var requestContent = new StringContent(patchJson, Encoding.UTF8, "application/json-patch+json");
            var response = await client.PatchAsync(apiUrl, requestContent);

            if (response.IsSuccessStatusCode)
            {
                Console.WriteLine("评论已成功添加,团队@提及生效!");
            }
            else
            {
                string errorDetails = await response.Content.ReadAsStringAsync();
                Console.WriteLine($"请求失败:{response.StatusCode}\n错误详情:{errorDetails}");
            }
        }
    }
}

几个关键注意事项

  • 确保你的PAT令牌拥有**Work Items (Read & Write)**的权限,否则会被拒绝访问
  • 团队ID必须是正确的GUID,不能用显示名替代,否则@提及不会触发通知
  • 尽量使用较新的API版本(比如7.1-preview.3或更高),保证兼容性和功能支持

备注:内容来源于stack exchange,提问作者RSD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.13 17:23:04