我已经从这篇 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");
}
}
}