【问题标题】:Using TFS API, how can I find the comments which were made on a Code Review?使用 TFS API,我如何找到代码审查中的评论?
【发布时间】:2013-04-10 09:44:50
【问题描述】:

我正在尝试找出一种方法来查找有关 TFS2012 中代码审查请求/响应项的详细信息。

我可以通过以下方式查询所有代码审查请求/响应项:

const string TfsUri = "http://mytfsserver:8080/tfs/Default ProjectCollection";

var tfs = new TfsTeamProjectCollection(new Uri(TfsUri));
var store = tfs.GetService<WorkItemStore>();

var versionStore = tfs.GetService<Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer>();

var queryText = "SELECT [System.Id], 
                 FROM WorkItems 
                 WHERE [System.WorkItemType] = 'Code Review Request' 
                 or [System.WorkItemType] = 'Code Review Response'";
var query = new Query(store, queryText);

var result = query.RunQuery().OfType<WorkItem>();

这给了我WorkItem 类型的列表。当我遍历result.FirstOrDefault().Fields 属性时,它确实为我提供了一些与代码审查相关的ShelveSet 的有用信息,即“关联上下文”。使用这些信息,我可以查询 ShelveSet:

var versionStore = tfs.GetService<VersionControlServer>();
var shelveset = versionStore.QueryShelvesets("someCodeReviewId_xxxx","someUserName");

这给了我一个ShelveSet 项目,但这就是我卡住的地方。

我查看了Microsoft.TeamFoundation.CodeReview.ComponentsMicrosoft.TeamFoundation.CodeReview.Controls 库提供的Microsoft.TeamFoundation.CodeReview 命名空间,但这也对我没有进一步的帮助。

我的问题是:如何通过 TFS API 找到代码审查期间在 ShelveSet 上创建的实际 cmets(通用 cmets 和文件 cmets)?

【问题讨论】:

    标签: c# api object-model tfs-code-review


    【解决方案1】:

    我们有一个从 TFS 中提取代码审查 cmets 的新要求,下面是我们实施的一个简短示例。必须通过另一种方法查询 workItemId。您甚至可以在 Visual Studio 中或通过 UI 中的 TFS 查询来查找它。这是可用内容和我们正在使用的内容的一小部分。我找到了this link to be helpful after digging through MSDN

     public List<CodeReviewComment> GetCodeReviewComments(int workItemId)
     {
            List<CodeReviewComment> comments = new List<CodeReviewComment>();
    
            Uri uri = new Uri(URL_TO_TFS_COLLECTION);
            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);
                }
            }
    
            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; }
        }
    

    【讨论】:

    • 我在数以千计的代码审查请求中运行它。有什么办法可以加快这个速度吗?这一步需要时间: var output = DiscussionManager.EndQueryByCodeReviewRequest(result);
    • 这一步对我们来说总是运行得很快,因为它使用单个工作项 ID (workItemId) 作为输入参数。我们设置了一个 Web 应用程序,它列出了给定标准的所有变更集,并通过代码审查突出显示了这些变更集。然后,我们单击要深入了解的每个步骤,这些步骤会导致本示例中的步骤。
    【解决方案2】:

    我没有代码示例,但根据 this discussion,您应该能够使用 Microsoft.TeamFoundation.Discussion.Client 命名空间中的功能进行代码审查 cmets。

    具体来说,可以通过 DiscussionThread 类访问 cmets。您应该可以使用IDiscussionManager 查询讨论。

    【讨论】:

    • 这正是我想要的!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2020-11-12
    • 2017-06-16
    • 2017-10-19
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 2013-04-15
    相关资源
    最近更新 更多