3

我已经从这篇 MSDN 文章中创建了签入策略作为示例(代码只是复制/粘贴)。

这很好用,当我尝试办理登机手续时出现,但它显示为警告。所以我可以通过再次按签入来忽略它。如何更改 URL 中列出的代码,使其返回错误而不是警告。我看不到 PolicyFailure 上的任何属性来执行此操作。

本质上,我希望它看起来像此屏幕截图中的错误: 在此处输入图像描述

图片来源

编辑:这是我正在使用的确切代码。现在它从原始来源略微修改,但没有以我不会想到的任何大规模方式进行。不幸的是,我无法发布屏幕截图,但我会尝试描述我所做的一切。

所以我有一个来自下面代码的 DLL,我已将它添加到 C:\TFS\CheckInComments.dll 的文件夹中。我在 Checkin Policies 下添加了一个带有 DLL 路径的注册表项,字符串值名称与我的 DLL 相同(减去 .dll)。在源代码控制下的项目设置中,我添加了此签入策略。

一切似乎都很好,如果我尝试办理登机手续,它会显示一条警告,上面写着“请提供一些关于您办理登机手续的评论”,这是我所期望的,我希望它停止如果不符合任何政策,请签入,但是我仍然希望用户能够在必要时选择覆盖。目前,即使有警告,如果我单击“签入”按钮,它也会成功签入代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace CheckInComments
{
    [Serializable]
    public class CheckInComments : PolicyBase
    {
        public override string Description
       {
            get
            { 
                return "Remind users to add meaningful comments to their checkins";

            }
        }

        public override string InstallationInstructions
        { 
            get { return "To install this policy, read InstallInstructions.txt"; } 
        }

        public override string Type
        {
            get { return "Check for Comments Policy"; }
        }


        public override string TypeDescription
        {
            get
            {
                return "This policy will prompt the user to decide whether or not they should be allowed to check in";
            }
        }

        public override bool Edit(IPolicyEditArgs args)
        {

            return true;
        }


        public override PolicyFailure[] Evaluate()
        {
            string proposedComment = PendingCheckin.PendingChanges.Comment;
            if (String.IsNullOrEmpty(proposedComment))
            {
                PolicyFailure failure = new PolicyFailure("Please provide some comments about your check-in", this);
                failure.Activate();

                return new PolicyFailure[1]
                {
                    failure
                };
            }
            else
            {
                return new PolicyFailure[0];
            }
        }

        public override void Activate(PolicyFailure failure)
        {
            MessageBox.Show("Please provide comments for your check-in.", "How to fix your policy failure");
        }

        public override void DisplayHelp(PolicyFailure failure)
        {
            MessageBox.Show("This policy helps you to remember to add comments to your check-ins", "Prompt Policy Help");
        }
    }
}
4

2 回答 2

1

签入策略将始终返回警告,如果您的用户有权忽略它们,那么他们可以。

用户始终可以覆盖该策略。您可以查询 TFS 仓库以生成用户违反政策的报告以及他们提供的违规原因。或者在有人忽略这些礼貌警告时设置警报。

无法从策略本身强制执行此操作。仅来自服务器端插件,如 Neno 在您引用的帖子中所述。也可以为 2012 或 2010 创建这样的服务器端插件。此处解释了该过程

于 2015-02-27T17:32:30.690 回答
0

我刚刚通过在我的项目上打开代码分析解决了这个问题 - 右键单击​​您的项目,单击属性,转到代码分析,选择配置下拉菜单并选择“所有配置”,选择“在构建时启用代码分析” .

进行构建并确保没有错误/警告。

这将使您通过任何需要在构建时进行代码分析的策略。

于 2019-03-06T16:05:33.400 回答