#Region " Options "
Option Explicit On
Option Strict On
#End Region
#Region " Imports "
Imports System
Imports System.Text
Imports System.Windows.forms
'Microsoft.TeamFoundation.VersionControl.Client.dll
Imports Microsoft.TeamFoundation.VersionControl.Client
Imports System.Collections
Imports System.IO
#End Region
Namespace Org.Mvps.Leon.Tfs.Policies
'''
''' Check in policy to disallow direct check-ins, only merge and branch
'''
'''
''' Needs to be installed by adding a new string value to:
''' HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\8.0\TeamFoundation\SourceControl\Checkin Policies
''' pointing to this compiled DLL called the same name as the compiled DLL (without the DLL extension)
'''
_
Public Class MergeOnlyPolicy
Inherits PolicyBase
#Region " Class Data "
Private Shared _strType As String = "Branch and Merge Only"
Private Shared _strDescription As String = "This policy will only allow check-ins of type merge and branch."
Private Shared _strInstallationInstructions As String = "No installation instructions"
Private Shared _strTypeDescription As String = "Disallows direct check-ins, only merge and branch"
Private Shared _strDisposedMessage As String = _strType + " Object has been disposed."
Private Shared _strHelp As String = "No help available at this time"
Private Shared _strMessage As String = "Direct check-ins are disallowed. Please branch or merge from another branch."
#End Region
#Region " Properties "
'''
''' A description of the policy
'''
'''
'''
'''
Public Overrides ReadOnly Property Description() As String
Get
Return _strDescription
End Get
End Property
'''
''' The name of the policy
'''
'''
'''
'''
Public Overrides ReadOnly Property Type() As String
Get
Return _strType
End Get
End Property
'''
''' The description displayed in the policy picker window
'''
'''
'''
'''
Public Overrides ReadOnly Property TypeDescription() As String
Get
Return _strTypeDescription
End Get
End Property
Public Overrides ReadOnly Property CanEdit() As Boolean
Get
Return MyBase.CanEdit
End Get
End Property
'''
''' Returns a string os installation instructions
'''
'''
'''
'''
Public Overrides Property InstallationInstructions() As String
Get
Return _strInstallationInstructions
End Get
Set(ByVal value As String)
_strInstallationInstructions = value
End Set
End Property
#End Region
#Region " Methods "
Public Overrides Function Edit(ByVal policyEditArgs As Microsoft.TeamFoundation.VersionControl.Client.IPolicyEditArgs) As Boolean
' Must return true, because of a TFS bug
Return True
End Function
'''
''' Checks to make sure pending changes are either branches or merges
'''
''' Array of PolicyFailures
'''
Public Overrides Function Evaluate() As Microsoft.TeamFoundation.VersionControl.Client.PolicyFailure()
' Throw an exception if this object has been disposed of
If Me.Disposed = True Then
Throw New ObjectDisposedException(_strType, _strDisposedMessage)
End If
' Create an arraylist of policy failures
Dim policyFailures As New ArrayList()
' Define a flag to tell if any changes are not branch / merge operations
Dim blnNotMerge As Boolean = False
' Loop through all pending changes
For Each objChange As PendingChange In Me.PendingCheckin.PendingChanges.AllPendingChanges
' If the change is not a branch / merge (and e.g. edit)
' then set our flag to true
If objChange.IsBranch = False AndAlso objChange.IsMerge = False Then
blnNotMerge = True
End If
Next
' If the flag has been set, add a new policy failure to the return collection
If blnNotMerge = True Then
policyFailures.Add(New PolicyFailure("Only merging and branching changes are allowed into this branch", Me))
End If
' Return the failures collection
Return CType(policyFailures.ToArray(GetType(PolicyFailure)), PolicyFailure())
End Function
Public Overrides Sub DisplayHelp(ByVal failure As PolicyFailure)
MessageBox.Show(_strHelp)
End Sub
Public Overrides Sub Activate(ByVal failure As PolicyFailure)
MessageBox.Show(_strMessage)
End Sub
Protected Overrides Sub OnPolicyStateChanged(ByVal failures() As PolicyFailure)
MyBase.OnPolicyStateChanged(failures)
End Sub
Public Overrides Sub Initialize(ByVal pendingCheckin As IPendingCheckin)
MyBase.Initialize(pendingCheckin)
AddHandler pendingCheckin.PendingChanges.CheckedPendingChangesChanged, AddressOf pendingCheckin_CheckedPendingChangesChanged
End Sub
Public Overrides Sub Dispose()
RemoveHandler PendingCheckin.PendingChanges.CheckedPendingChangesChanged, AddressOf pendingCheckin_CheckedPendingChangesChanged
MyBase.Dispose()
End Sub
Private Sub pendingCheckin_CheckedPendingChangesChanged(ByVal sender As Object, ByVal e As EventArgs)
OnPolicyStateChanged(Evaluate())
End Sub
#End Region
End Class
End Namespace