自动通知团队成员执行 Visual Source Safe 操作时的邮件
一个使用 VB 编写的 Visual Source Safe 插件,它可以在您对 VSS 执行操作(添加、签入、签出...)时自动向您的团队成员发送电子邮件
引言
我和我的五个朋友正在做一个项目。我们使用 Visual Source Safe 管理我们的代码。上周,在我们项目发布的前一天,发生了一件不幸的事情。我发现我前一天签入 VSS 的代码丢失了。当我询问我的朋友时,我们发现其中一个人在不知情的情况下,用他正在处理的代码覆盖了 VSS 中的那段代码。
然后我们认为,如果将来我们可以通过某种方式自动通知彼此有关我们在 VSS 中添加/签入代码的信息,那就更好了。然后我读到了一篇 MSDN 中的文章,该文章使用 VC++ 描述了如何在对 VSS 执行任何操作时拦截 VSS 事件。此外,我还了解了 Outlook Redemption 库,该库可以帮助我们以编程方式发送电子邮件,绕过安全警报对话框。在所有这些帮助下,我用 Visual Basic 开发了这个简单的 VSS 插件。
安装此插件后(就像这个插件一样简单),无论何时您对 VSS 执行任何操作,都会向您的所有朋友发送电子邮件。电子邮件将在您从 VSS 注销时发送,并将包含您在该 VSS 会话期间执行的操作摘要。电子邮件将使用您在 Outlook 中配置的默认电子邮件帐户发送。此外,在所有这些操作过程中都不会显示安全检查对话框。
先决条件
安装
- 将 *VSSVBAddin.dll* 复制到 *%SystemRoot%\System32* 文件夹中。
- 从命令提示符中,使用以下命令注册 *VSSVBAddin.dll*
RegSvr32 VSSVBAddin.dll
- 在 *VSS\Win32* 文件夹中创建一个名为 *Ssaddin.ini* 的文件,其中注册了 *Ssapi.dll* 文件(换句话说,就是您安装 Visual SourceSafe 的文件夹)。此文件夹也应该是以下注册表项指向的文件夹
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\SourceSafe\SCCServerPath
- 打开 *Ssaddin.ini* 文件并向其中添加以下行
VSSVBAddin.AddinClass = 1
- 在 Visual Source Safe 数据库 *srcsafe.ini* 所在的文件夹中创建一个名为 *maillist.txt* 的文件。
- 打开文件并在每行上写入所有团队成员的电子邮件 ID。例如:<
pradeep@xyz.com sudeep@pqr.com ....
Using the Code
Visual Source Safe 提供了一种通过使用插件来捕获和控制其各种事件(例如 BeforeAdd、BeforeCheckIn、....
)的方法。此代码实现了这些事件,并且每当对 VSS 执行操作时,它都会将与这些操作相关的详细信息添加到全局字符串变量 str
中。
'Event fired after addition of any item.
Sub VSSHandler_AfterAdd(ByVal Item As IVSSItem, ByVal LocalSpec As _
String, ByVal Comment As String)
'Check if any comment is added. then include that.
If Comment <> "" Then
str = str & vbCrLf & "Added: " & _
Item.Parent.Spec & "/" & Item.Name & _
" at : " & Now & ", Commented : " & Comment
Else
str = str & vbCrLf & "Added: " & _
Item.Parent.Spec & "/" & _
Item.Name & " at : " & Now
End If
End Sub
稍后,当用户退出 VSS 时,会调用 Class_Terminate
。在这里,我们从与 VSS 数据库本身的 *srcsafe.ini* 文件位于同一路径的 *maillist.txt* 文件中读取所有收件人的邮件 ID。然后,我们使用 Redemption Library(它使用您在 Outlook 上配置的默认电子邮件帐户)向所有这些收件人发送邮件。
Private Sub Class_Terminate()
On Error GoTo Mailing_Error
'Check if any operation is done. else this string will be empty
If str <> "" Then
Dim SafeItem, oItem
'Creates a mail item to be mailed.
Set Application = CreateObject("Outlook.Application")
Set Namespace = Application.GetNamespace("MAPI")
Namespace.Logon
Set SafeItem = CreateObject("Redemption.SafeMailItem")
Set oItem = Application.CreateItem(0)
SafeItem.Item = oItem
'Reads maillist.txt file for recipient address.
Dim nFileNum As Integer, sNextLine As String
'Get free file number
nFileNum = FreeFile
'Opens the file
Open Left$(VSSHandler.VSSDatabase.SrcSafeIni, _
Len(VSSHandler.VSSDatabase.SrcSafeIni) - 11) _
& "maillist.txt" For Input As nFileNum
Do While Not EOF(nFileNum)
'Read recipient mail id
Line Input #nFileNum, sNextLine
'Add email recipient
SafeItem.Recipients.Add sNextLine
Loop
' Close the file
Close nFileNum
'Resolves email ids
SafeItem.Recipients.ResolveAll
'Create the subject of mail
SafeItem.Subject = "VSS Notification - " & _
VSSHandler.VSSDatabase.SrcSafeIni
'Create the body of email
str = "This is an auto generated message." & vbCrLf & _
"User :" & VSSHandler.VSSDatabase.Username & _
", performed the following operations on VSS Database " _
& vbCrLf & str
SafeItem.Body = str
'Send the email
SafeItem.Send
'Used in case of non Exchange Server mailids e.g. pop-smtp... _
to immediately deliver mail
Set Utils = CreateObject("Redemption.MAPIUtils")
Utils.DeliverNow
End If
Exit Sub
'Handle any error here.
Mailing_Error:
MsgBox "Error " & Err.Number & " while sending mail" & vbCrLf & Err.Description
End Sub
相关链接
- 示例:VSSAddin.exe 通知 SourceSafe 事件的管理员
- Outlook Redemption 库
- Visual SourceSafe 自动化
- Microsoft.VisualStudio.SourceSafe.Interop 命名空间
- Visual SourceSafe 6.0 自动化
历史
- 2006 年 1 月 20 日:首次发布