2

We have some xml files in the our project and whenever we check-in these xml files into TFS, We have make sure before checking-in that we have added those xml files to proprietary application.

Now the new employees more often forget to add files into proprietary application before check-in and this is getting serious...

We want kinda confirmation dialog (a reminder) asking the developers if they have added the xml files into the app. If yes then check-in otherwise keep it checkedout...

Please suggest if such thing is possible and any relevant code or links will be really appreciated.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Dexterslab
  • 129
  • 1
  • 2
  • 14

3 Answers3

2

It's not appropriate to raise UI in a custom check-in policy - the lifecycle of a check-in policy is very short, and they will be evaluated frequently and not necessarily in a UI context or on the UI thread.

Can you determine programmatically whether the appropriate XML files are being checked in? If so, you could create a custom check-in policy that fails if the XML files are not pended for add.

Gated Check-in may be the best solution to this problem: does the build fail if these XML files do not exist - or would unit tests fail if these files are missing? If so, this is a perfect candidate for Gated Check-in, which will prevent these check-ins from occurring.

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
  • Thanks a lot for your answer... I am afraid it neither crashes the build nor the unit test fails if these XML files are missing... – Dexterslab May 26 '11 at 18:56
  • In that case, I would consider leveraging work items for this - you are, after all, trying to ensure that your developers have done some amount of work (in this case, checking in XML files) – Edward Thomson May 26 '11 at 19:03
  • hmmmm, yes but this is not like work i would like to track in the form of work item.. Its like whenever the developer checks-in the xml file.. the developer must get kinda reminder that not to forget to add these xml specific xml files in the proprietary application (custom app nothing to do with TFS). So to make it more clear these XML gets added in 2 different applications. (1) .net project, this where TFS is used (2) custom proprietary application – Dexterslab May 26 '11 at 19:08
  • Yes, I understand -- my preference would be to find a way to query the proprietary application to know if those XML files were added or not, then write a check in policy that does that query and fails if the proprietary application does not have them. Short of that ability, things are going to be a bit of a hack. I can understand the hesitation around work items -- another idea is to require the user to add some other data to the check-in to "prove" that they've solved this requirement, for example putting the phrase "__XML ADDED__" in their comment, and then checking for that. – Edward Thomson May 26 '11 at 19:18
  • I like your idea "XML ADDED".. Can i have a required DropDownList kind of control instead of the comment text box? .. I appreciate your time..Thank! – Dexterslab May 26 '11 at 19:28
  • Unfortunately, no, you can't replace the comment text field with a different control. – Edward Thomson May 26 '11 at 20:05
  • 1
    Wouldn't you be able to use the Activate method to show UI in the check-in policy? That would trigger a warning on the check-in screen and then show UI when the user interacts with the message. – jessehouwing Jan 31 '14 at 11:05
  • That's very clever @jessehouwing. I don't remember the lifecycle offhand, so I want to make sure I understand: you're suggesting failing the policy, then when the user opens the policy for details, in the `Activate` method you can prompt the user and if their answer is satisfactory, you can pass the check-in policy evaluation in the future? – Edward Thomson Jan 31 '14 at 16:19
  • Added a sample policy as a separate answer. – jessehouwing Jan 31 '14 at 17:10
  • @EdwardThomson I tried it and it works, see my other answer and my small github project. – jessehouwing Feb 17 '14 at 15:27
2

I would create a custom build template that checks for these xml files. Make it a gated check-in and you've got your solution.

LeWoody
  • 3,593
  • 4
  • 25
  • 31
1

The Evaluate method is supposed to be quick and should not show UI, but there is an event on the policy that triggers when the user interacts with the policy called Activate, this is a good moment to show UI and communicate with the policy. You could do something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JesseHouwing.CheckinPolicies
{
    using System.Windows.Forms;

    using Microsoft.TeamFoundation.Client.Reporting;
    using Microsoft.TeamFoundation.VersionControl.Client;

    [Serializable]
    public class ConfirmPolicy :PolicyBase
    {
        private AffectedTeamProjectsEventHandler _affectedTeamProjectsEventHandler;
        private EventHandler _checkedPendingChangesEventHandler;

        public ConfirmPolicy()
        {
        }

        public void StatusChanged()
        {
            _userconfirmed = false;
            OnPolicyStateChanged(Evaluate());
        }

        public override void Initialize(IPendingCheckin pendingCheckin)
        {
            _affectedTeamProjectsEventHandler = (sender, e) => StatusChanged();
            _checkedPendingChangesEventHandler = (sender, e) => StatusChanged();

            base.Initialize(pendingCheckin);
            _userconfirmed = false;                
            pendingCheckin.PendingChanges.AffectedTeamProjectsChanged += _affectedTeamProjectsEventHandler;
            pendingCheckin.PendingChanges.CheckedPendingChangesChanged += _checkedPendingChangesEventHandler;
        }

        protected override void OnPolicyStateChanged(PolicyFailure[] failures)
        {
            _userconfirmed = false;
            base.OnPolicyStateChanged(Evaluate());
        }

        public override void Activate(PolicyFailure failure)
        {
            if (MessageBox.Show("Confirm the policy?", "Policy Check", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                _userconfirmed = true;
                base.OnPolicyStateChanged(Evaluate());
            }
        }

        public override PolicyFailure[] Evaluate()
        {
            if (_userconfirmed == true)
            {
                return new PolicyFailure[0];
            }
            else
            {
                return new PolicyFailure[]{new PolicyFailure("User must confirm", this)};
            }
        }

        public override string Description
        {
            get { throw new NotImplementedException(); }
        }

        public override bool Edit(IPolicyEditArgs policyEditArgs)
        {
            return true;
        }

        public override string Type
        {
            get
            {
                return "User Confirm";
            }
        }

        public override string TypeDescription
        {
            get
            {
                return "User Confirm";
            }
        }

        public override void Dispose()
        {
            this.PendingCheckin.PendingChanges.AffectedTeamProjectsChanged -= _affectedTeamProjectsEventHandler;
            this.PendingCheckin.PendingChanges.CheckedPendingChangesChanged -= _checkedPendingChangesEventHandler;

            base.Dispose();
        }
    }
}

I haven't tested this exact code yet, it might need some tweaking, but this is the general thing to do. Right now it triggers on a change in the files being checked in, but you can subscribe to any of the other events as well (work item changes) or trigger your own evaluation of the project each time Evaluate is called.

Or you can just trigger the confirm once per checkin cycle. it's all up to you. You could even do a "Click to Dismiss" and skip the Messagebox altogether. Just set _userConfirmed=true and fire the PolicyStateChanged event.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • I implemented this on one of my own policies, it's shared here: https://github.com/jessehouwing/tfstools/blob/master/JesseHouwing.CheckinPolicies/WarnMultipleBranchPolicy.cs – jessehouwing Feb 17 '14 at 15:20