Wiki

Case Status Kiln
Register Log In

Wiki

 
"How To" Guides»Event-Handling Approach»OnSourceControlCommit Event Ha…
  • RSS Feed

Last modified on 5/13/2009 11:55 AM by User.

Tags:

OnSourceControlCommit Event Handling Example

Event-Handling Approach > OnSourceControlCommit

Introduction

This article will demonstrate using event handling in FogCreek.Plugins.InterfaceEvents.CPluginCVSCommitEvents to perform an action (commiting a new BugEvent to a case) upon check-in of a file into source control with a case number specified. For further information and examples on editing cases via a plugin, see How To Edit Cases.

Functionality

This plugin will handle the OnSourceControlCommit event to add a BugEvent to a case upon each checkin to source control which references that case in its commit message. Note: Version 1 of the plugin architecture does not send normal case modified notifications when a plugin calls CBug.Commit() so in this example, the bugevents created by the plugin do not prompt FogBugz to email the case assignee.

An example case:


After checking in the file "Local Menus.txt" with commit message "bugzid:14":


C# Code

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

/* FogBugz namespaces-- make sure you add the neccesary assembly references to
* the following DLL files contained in C:\Program Files\FogBugz\Website\bin\
* FogBugz.dll, FogCreek.Plugins.dll, FogCreek.Plugins.InterfaceEvents.dll */
using FogCreek.FogBugz.Plugins;
using FogCreek.FogBugz.Plugins.Interfaces;
using FogCreek.FogBugz;
using FogCreek.FogBugz.UI;

using FogCreek.Plugins.InterfaceEvents;

namespace CVS_Checkin_BugEvent_Example
{
/* Class Declaration: Inherit from Plugin and handle events in
* FogCreek.Plugins.InterfaceEvents.CPluginCVSCommitEvents
* Note that the plugin must implement the same interface as
* the event handling library, IPluginCVSCommit */
public class CVS_Checkin_BugEvent_Example : Plugin, IPluginCVSCommit
{
CPluginCVSCommitEvents eventCVSCommit;
/* Constructor: We'll initialize the inherited Plugin class, which
* takes the passed instance of CPluginApi and sets its "api" member
* variable. Instantiate the CPluginCVSCommitEvents class */
public CVS_Checkin_BugEvent_Example(CPluginApi api): base(api)
{
eventCVSCommit = new CPluginCVSCommitEvents();
/* If the constructor does much, it's nice to put the event-
* handlers in a separate method. */
EventHandlers();
}

void EventHandlers()
{
eventCVSCommit.OnSourceControlCommit += new EventHandler<CPluginCVSCommitEvents.CVSEventArgs>(eventCVSCommit_OnSourceControlCommit);
}

void eventCVSCommit_OnSourceControlCommit(object sender, CPluginCVSCommitEvents.CVSEventArgs e)
{
CBug bug = api.Bug.GetBug(e.CVS.ixBug);
/* the logged-in user is used for the commit when no ixPerson is specified.
* CVS commits don't have a user, so the default "FogBugz" user is used.
* Built-in users: -1 FogBugz
* 0 CLOSED
* 2 Administrator */
bug.Commit(string.Format("Revision {1} of file {0} committed to source control.",
e.CVS.sFile,
e.CVS.sNew));
}

/* dispatch to the InterfaceEvents library object when the interface is called */
#region IPluginCVSCommit Members

public void CVSCommitAfter(CCVS cvs)
{
eventCVSCommit.CVSCommitAfter(cvs);
}

public bool CVSCommitBefore(CCVS cvs)
{
return eventCVSCommit.CVSCommitBefore(cvs);
}

public void CVSCommitRollback(CCVS cvs)
{
eventCVSCommit.CVSCommitRollback(cvs);
}

#endregion
}
}

Compile and Install It On Your Own

Download the source file:   CVS_Checkin_BugEvent_Example.cs

Then follow these instructions to create a functioning plugin assembly: Compiling and Installing a FogBugz Plugin