0

I am running a simple plugin, yet when I build the file and add a new case, the displayed counter increases by 2 each time, not one. When I create a new case via an email account with pop3 access, the counter increases by 3. In the BugCommitAfter method, I am testing if the method is in the correct project, and only update the count if it is. Is there any reason this method is called several times? Thanks

using System;
using FogCreek.FogBugz;
using FogCreek.FogBugz.Plugins;
using FogCreek.FogBugz.Plugins.Api;
using FogCreek.FogBugz.Plugins.Entity;
using FogCreek.FogBugz.Plugins.Interfaces;
using FogCreek.FogBugz.UI;

namespace FogCreek.Plugins.HelloWorld
{  
    public class HelloWorld : Plugin, IPluginPageDisplay, IPluginBugCommit, IPluginExtrasMenu    
    {
        public string hello;
        public HelloWorld(CPluginApi api)            
            : base(api) 
        {
        }

    private int GetCommitCount()        
    {            
        int cCommits = 0;            
        return Int32.TryParse(api.Database.GetKeyValue("cCommits"), out cCommits) ? cCommits : 0;       
    }        

    private void IncrementCommitCount()        
    {            
        int cCommits = GetCommitCount();            
        api.Database.SetKeyValue("cCommits", (cCommits + 1).ToString());        
    }        

    public string PageDisplay()        
    {           
        return string.Format("Commits: {0}", GetCommitCount()) + api.Database.GetKeyValue("Hello");        
    }        

    #region IPluginBugCommit Members        
    public void BugCommitAfter(CBug bug, BugAction nBugAction, CBugEvent bugevent, bool fPublic)        
    {
        if (bug.ixProject.ToString().Equals("1"))
        {
            api.Database.SetKeyValue("Hello", bug.sTitle);
            IncrementCommitCount();
        }
    }        

    public void BugCommitBefore(CBug bug, BugAction nBugAction, CBugEvent bugevent, bool fPublic)
    {
    }        
    public void BugCommitRollback(CBug bug, BugAction nBugAction, bool fPublic){}        
    #endregion        

    public PermissionLevel PageVisibility()        
    {            
        return PermissionLevel.Normal;        
    }


    public CNavMenuLink[] ExtrasMenuLinks() 
    { 
        return new CNavMenuLink[] { new CNavMenuLink("Say Hello", api.Url.PluginPageUrl()) };
    }
}

}

flag

1 Answer

2

The BugCommit functions get called for each bug "event" that is added to the bug; I expect it's being called multiple times when you create a bug, because it is being Opened (one event) and Assigned (another event)

You can check which event is occurring by looking at the EventType property on the bugevent that is passed to BugCommitBefore or BugCommitAfter, e.g.

public void BugCommitBefore(CBug bug, BugAction nBugAction, CBugEvent bugevent, 
                            bool fPublic)
{
   switch(bugevent.EventType)
   {
      case BugEventType.Opened:
      case BugEventType.Received:
         // Handle bug creation
      case BugEventType.Edited:
         // Handle a bug being edited
      case ...
   }
}

If you want to get the ixProject value for a project, given it's name, you can use code like this:

static public int GetIxProjectFromName(string sName)
{
   CProjectQuery queryProject = api.Project.NewProjectQuery();

   queryProject.AddSelect("ixProject");
   queryProject.AddWhere("sProject LIKE @sName");
   queryProject.SetParamString("sName", sName);

   int[] rgIds = queryProject.ListIds();

   return (rgIds.Length > 0) ? rgIds[0] : -1;
}
link|flag
Yup, I knew something like this was going on, but couldn't figure out how to access it, thanks! On a similar topic, do you know of any way to gain access to the ID of a project without just hardcoding it? Something like if (bug.ixProject.ToString().Equals(findProjectByName("MyProject")))? I have been looking into querying an entity, but am pretty much new to FogBugz, and C# for that matter. – mbauer14 Jul 16 2010 at 19:59
@mbauer14: I've updated my answer to address your question – Daniel LeCheminant Jul 16 2010 at 20:24

Your Answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.