0

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.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Nilanjan Saha
  • 451
  • 4
  • 6
  • You didn't specify a language, but this kind of thing is typically handled by static analysis toolkits during the build process. – Daniel Mann Jul 26 '18 at 17:51
  • @Daniel: I have used the word tfs that means the target environment is visual studio. I am not sure how the language is going to play a vital role here? The target solution could be through tfs configuration or creation custom library in terms of .dll file. How the language is important here? – Nilanjan Saha Jul 27 '18 at 04:15

2 Answers2

0

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

Andy Li-MSFT
  • 28,712
  • 2
  • 33
  • 55
  • Thanks Andy for your response. Much appreciated!! – Nilanjan Saha Jul 27 '18 at 09:07
  • @NilanjanSaha You are welcome. Yeah no such an existing check-in policy. So if you want to apply policy, then you need to write your own check-in policy just as jessehouwing mentioned above. But in my opinion, the solution I provided should be enough for your requirement to notify the developers... – Andy Li-MSFT Jul 27 '18 at 09:13
  • There are two ways to see the thing. If a developer accidentally forgets to put the right file name on any of an artifact then a notification can help there. But on the other side if the developer is unaware of the right naming convention of the file name based on the artifacts then there shall be some validation routine in tfs itself to tackle this kind of situation. – Nilanjan Saha Jul 27 '18 at 09:38
  • @NilanjanSaha Got it, thus you have to write your own check-in policy, just reference the sample which jessehouwing provided... – Andy Li-MSFT Jul 30 '18 at 01:57
0

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.

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
  • @jessehouwing Just for some confirmation Can we do this kind of validation for Git version Control? Like the question mentioned https://social.msdn.microsoft.com/Forums/en-US/58661ca5-4076-41ff-9d61-c34c958d6536/how-to-avoid-the-unwanted-check-in-to-the-git-branch-in-tfs-2018?forum=Offtopic – Jayendran Jul 31 '18 at 10:03
  • For Git you can use a pre-commit or pre-push hook or configure a pull-request workflow in which you can configure a custom pull-request service: https://learn.microsoft.com/en-us/vsts/git/how-to/pr-status-policy?view=vsts – jessehouwing Jul 31 '18 at 12:25