TFS (Team Foundation Server) API是微软提供的用于与Team Foundation Server或Azure DevOps Server交互的编程接口。代码评审(Code Review)是软件开发过程中的一个重要环节,团队成员可以通过它审查代码变更并提出改进建议。
是的,可以通过TFS API向代码评审添加注释。这属于TFS/Azure DevOps的代码评审功能的一部分。
使用Azure DevOps Services REST API中的Pull Request Threads
和Comments
端点可以实现添加评论:
POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories/{repositoryId}/pullRequests/{pullRequestId}/threads?api-version=6.0
请求体示例:
{
"comments": [
{
"parentCommentId": 0,
"content": "这里可能需要添加异常处理",
"commentType": 1
}
],
"status": 1
}
使用Microsoft.TeamFoundationServer.Client库:
using Microsoft.TeamFoundation.SourceControl.WebApi;
using Microsoft.VisualStudio.Services.Common;
// 连接TFS/Azure DevOps
VssConnection connection = new VssConnection(new Uri("http://yourtfsserver:8080/tfs"), new VssCredentials());
GitHttpClient gitClient = connection.GetClient<GitHttpClient>();
// 创建评论
GitPullRequestComment comment = new GitPullRequestComment()
{
Content = "建议使用更高效的算法",
CommentType = CommentType.Text
};
// 创建评论线程
GitPullRequestCommentThread thread = new GitPullRequestCommentThread()
{
Comments = new List<GitPullRequestComment> { comment },
Status = CommentThreadStatus.Active
};
// 添加到拉取请求
await gitClient.CreateThreadAsync(thread, repositoryId, pullRequestId);
原因:API调用者没有足够的权限 解决:确保服务账号有"Contribute to pull requests"权限
原因:没有正确指定评论的代码位置
解决:在创建评论时指定正确的position
参数
{
"comments": [
{
"content": "这个变量命名不规范",
"commentType": 1
}
],
"status": 1,
"threadContext": {
"filePath": "src/controllers/userController.js",
"rightFileStart": {
"line": 42,
"offset": 1
},
"rightFileEnd": {
"line": 42,
"offset": 10
}
}
}
原因:没有正确设置评论类型或内容格式
解决:确保使用正确的commentType
(1为文本,2为代码变更建议)