3

I've created the check-in policy from this MSDN article as an example (code is just copy / pasted).

This works fine, it appears when I try and do a check-in, however it appears as an warning. So I can ignore it by just pressing Check In again. How can I change the code, as listed in the URL, so that it will return an Error not a warning. I can't see any properties on PolicyFailure to do this.

Essentially I want it to look like the error in this screenshot: enter image description here

Image Source

EDIT: Here is the exact code that I'm using. Now it is slightly modified from the original source, but not in any massive way I wouldn't have thought. Unfortunately I can't post screenshots, but I'll try and describe everything I've done.

So I have a DLL from the code below, I've added it into a folder at C:\TFS\CheckInComments.dll. I added a registry key under Checkin Policies with the path to the DLL, the string value name is the same as my DLL (minus .dll). In my project settings under source control I've added this Check-In Policy.

It all seems to work fine, if I try and do a check-in it will display a warning saying "Please provide some comments about your check-in" which is what I expect, what I'd like is for it to stop the check-in if any policies are not met, however I would still like the user to be able to select Override if necessary. At the moment, even though there is a warning, if I was to click the Check In button then it would successfully check-in the code.

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");
        }
    }
}
MattR
  • 641
  • 3
  • 17
  • 38

2 Answers2

1

A check-in policy will always return a warning and if your user has permission to ignore them, then they can.

Users can always override the policy. You can query the TFS warehouse to generate a report of users violating the policies and their reasons for the violation if they provided any. Or setup an alert whenever someone ignores these polite warnings.

There is no way to enforce this from the policy itself. Only from a server side plugin, as described by Neno in the post you quoted. Such a server side plugin can be created for 2012 or 2010 as well. The process is explained here.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • Hi jessehouwing, I don't necessarily mind if a user selects to override a policy, but I would like it to initially display "Check-in validation failed. A policy warning override reason and/or a check-in note is required." if it fails. Similar to how it does when you add the in-built policy "Work Items - Require associated work items." Is the only way to get this functionality to block all overrides? – MattR Mar 02 '15 at 09:17
  • To have the error appear at the top, you need a server side plugin, but hitting Check-in twice should not by-pass the policy, that looks like a bug in the implementation. – jessehouwing Mar 02 '15 at 09:49
0

I just got past that issue by turning on Code Analysis on my project - right click on your project, click properties, go to Code Analysis, select the Configuration drop down and pick "All Configurations", select the "Enable Code Analysis on Build".

Do a build and make sure you have no errors / warnings.

This will get you past any policies requiring code analysis on build.

JakeJ
  • 2,361
  • 5
  • 23
  • 35