从 TFS 2018 获取 Code Review 评论历史

Get Code Review comments history from TFS 2018

我正在使用 TFS 2018 On Premise。

有什么方法可以使用 api 或使用 TFS 查询来获取评论列表(如 Visual studio 中所示)。

This case提供了解决方案,大家可以查看:

您应该能够使用 Microsoft.TeamFoundation.Discussion.Client 命名空间中的功能获得代码审查评论。

具体来说,可以通过 DiscussionThread class. And you should be able to query discussions using IDiscussionManager.

访问评论

代码片段如下:

using Microsoft.TeamFoundation.Discussion.Client;
using System;
using System.Collections.Generic;

namespace GetCodeReviewComments
{
   public class ExecuteQuery
    {
        public List<CodeReviewComment> GetCodeReviewComments(int workItemId)
        {
            List<CodeReviewComment> comments = new List<CodeReviewComment>();

            Uri uri = new Uri("http://tfs2018:8080/tfs/defaultcollection");
            TeamFoundationDiscussionService service = new TeamFoundationDiscussionService();
            service.Initialize(new Microsoft.TeamFoundation.Client.TfsTeamProjectCollection(uri));
            IDiscussionManager discussionManager = service.CreateDiscussionManager();

            IAsyncResult result = discussionManager.BeginQueryByCodeReviewRequest(workItemId, QueryStoreOptions.ServerAndLocal, new AsyncCallback(CallCompletedCallback), null);
            var output = discussionManager.EndQueryByCodeReviewRequest(result);

            foreach (DiscussionThread thread in output)
            {
                if (thread.RootComment != null)
                {
                    CodeReviewComment comment = new CodeReviewComment();
                    comment.Author = thread.RootComment.Author.DisplayName;
                    comment.Comment = thread.RootComment.Content;
                    comment.PublishDate = thread.RootComment.PublishedDate.ToShortDateString();
                    comment.ItemName = thread.ItemPath;
                    comments.Add(comment);
                    Console.WriteLine(comment.Comment);
                }
            }

            return comments;
        }

        static void CallCompletedCallback(IAsyncResult result)
        {
            // Handle error conditions here
        }

        public class CodeReviewComment
        {
            public string Author { get; set; }
            public string Comment { get; set; }
            public string PublishDate { get; set; }
            public string ItemName { get; set; }
        }
    }
}