Incorporating TFS check in policy for proper file name

246 Views Asked by At

The context: Some developers are not maintaining proper naming conventions before check in their code. Hence, those artifacts are not been picked up by the build process, hence the dependencies are broken and causes failure after deployment on the target servers. For instance a file that contains a db function GetMaxId() shall have the naming convention like GetMaxId.Function.Ora.Sql

The requirement: Is it possible to implement any check in policy for this so that developer can notified about the issue before check in. In that process we can save loads of time and effort.

2

There are 2 best solutions below

2
On BEST ANSWER

Yes this is possible using a TFVC Checkin policy which will run inside Visual Studio. This is a custom dll which needs to be deployed to each client. The Check-in policy is just a piece of code that will run when the user opens the checkin pending changes windows.

You will need a policy assembly for each version of Visual Studio you need to support. The right set of framework libraries etc can be found here.

You'll need to override the Evaluate method to implement your check. You can access the list of selected pending changes through this.PendingCheckin.PendingChanges.... Here's an example of a policy that looks at the files selected to be checked in:

public override PolicyFailure[] Evaluate()
{
    if (PendingCheckin.PendingChanges.AffectedTeamProjectPaths.Length > 1)
    {
        return new[]{new PolicyFailure("Checking into multiple projects at the same time", this)};
    }
    var branches = this.PendingCheckin.PendingChanges.Workspace.VersionControlServer
        .QueryRootBranchObjects(RecursionType.Full);
    var groupedChanges = PendingCheckin.PendingChanges.CheckedPendingChanges.GroupBy(
       change => branches.SingleOrDefault(branch => change.ServerItem.StartsWith(branch.Properties.RootItem.Item)));
    if (groupedChanges.Count() > 1)
    {
       return new[]{new PolicyFailure("Checking into multiple branches at the same time", this)};
    }
    return new PolicyFailure[0];
}

You'll need to do your own parsing of the files you want to match to the file name or rely on another library to do that for you. The Checkin policy framework just gives you access to the path of the files.

You're likely going to want some caching if you're going to parse files.

A sample policy can be found here.

4
On

No such an existing check in policy, however you can apply the Check-in Notes to notify the developer about the issue before check in.

Navigate to Team Explorer-Home --> Settings --> Source Control --> Add a check-in note,

  • Set the Title e.g.: Please make sure maintain proper naming conventions before check in
  • Enable Required on check-ins

Thus, the system will prevent the developer checking in changes without entering notes, and the title will prompt developer maintaining proper naming conventions before check in. Once verified then enter notes and check in the changes...

enter image description here