3

I'm trying to write my own checking policy. I want to review if any .cs file contains some code. So my question is, if its possible to get the content of every file from the changeset in the overridden Initialize-Method and/or Evaluate-Method (from PolicyBase).

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
rhe1980
  • 1,557
  • 1
  • 15
  • 36
  • Why would you want to create your own Checkin policy where TFS based on it's build capabilities can handle this for you.. unless I am not understanding your question.. – MethodMan Jan 11 '12 at 15:14
  • I want to check if any code-file contains a copyright-statement. And I didn't found any available policy in tfs. – rhe1980 Jan 12 '12 at 07:16

1 Answers1

4

You can't get the contents from the files directly, you'll need to open them yourselves. For each checked In your Evaluate method, you should look at the PendingCheckin.PendingChanges.CheckedPendingChanges (to ensure that you only limit yourself to the pending changes that will be checked in.) Each PendingChange has a LocalItem that you can open and scan.

For example:

public override PolicyFailure[] Evaluate()
{
    List<PolicyFailure> failures = new List<PolicyFailure>();

    foreach(PendingChange pc in PendingCheckin.PendingChanges.CheckedPendingChanges)
    {
        if(pc.LocalItem == null)
        {
            continue;
        }

        /* Open the file */
        using(FileStream fs = new FileStream(pc.LocalItem, ...))
        {
            if(/* File contains your prohibited code */)
            {
                failures.Add(new PolicyFailure(/* Explain the problem */));
            }

            fs.Close();
        }
    }

    return failures.ToArray();
}
Edward Thomson
  • 74,857
  • 14
  • 158
  • 187