-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to run multiple processing strategies #83
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.IO; | ||
using log4net; | ||
using Mail2Bug.Email; | ||
using Mail2Bug.Helpers; | ||
using Mail2Bug.WorkItemManagement; | ||
|
||
namespace Mail2Bug.MessageProcessingStrategies | ||
{ | ||
/// <summary> | ||
/// Allows Mail2Bug administrators to create their own processing strategies to be run in conjunction with (or without) the default | ||
/// SimpleBugStrategy. This opens up the door for plugins to write messages to storage or auto response bots while maintaining | ||
/// current functionality. | ||
/// </summary> | ||
public class MultiStrategy : IMessageProcessingStrategy | ||
{ | ||
private List<IMessageProcessingStrategy> _strategies; | ||
public MultiStrategy(List<IMessageProcessingStrategy> strategies) { | ||
_strategies = strategies; | ||
} | ||
public void ProcessInboxMessage(IIncomingEmailMessage message) | ||
{ | ||
foreach (IMessageProcessingStrategy strategy in _strategies) { | ||
strategy.ProcessInboxMessage(message); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,18 +19,28 @@ public class SimpleBugStrategy : IMessageProcessingStrategy, IDisposable | |
private readonly AckEmailHandler _ackEmailHandler; | ||
private readonly MessageToWorkItemMapper _messageToWorkItemMapper; | ||
|
||
public SimpleBugStrategy(Config.InstanceConfig config, IWorkItemManager workItemManager) | ||
public SimpleBugStrategy(Config.InstanceConfig config) | ||
{ | ||
_config = config; | ||
_workItemManager = workItemManager; | ||
_workItemManager = InitWorkItemManager(); | ||
_ackEmailHandler = new AckEmailHandler(config); | ||
_messageToWorkItemMapper = | ||
new MessageToWorkItemMapper( | ||
_config.EmailSettings.AppendOnlyEmailTitleRegex, | ||
_config.EmailSettings.AppendOnlyEmailBodyRegex, | ||
_workItemManager.WorkItemsCache); | ||
} | ||
|
||
public SimpleBugStrategy(Config.InstanceConfig config, IWorkItemManager WitManager) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the same variable name as the original - |
||
{ | ||
_config = config; | ||
_workItemManager = WitManager; | ||
_ackEmailHandler = new AckEmailHandler(config); | ||
_messageToWorkItemMapper = | ||
new MessageToWorkItemMapper( | ||
_config.EmailSettings.AppendOnlyEmailTitleRegex, | ||
_config.EmailSettings.AppendOnlyEmailBodyRegex, | ||
_workItemManager.WorkItemsCache); | ||
} | ||
public void ProcessInboxMessage(IIncomingEmailMessage message) | ||
{ | ||
var workItemId = _messageToWorkItemMapper.GetWorkItemId(message); | ||
|
@@ -187,6 +197,22 @@ private static TempFileCollection SaveAttachments(IIncomingEmailMessage message) | |
return attachmentFiles; | ||
} | ||
|
||
private IWorkItemManager InitWorkItemManager() { | ||
IWorkItemManager workItemManager; | ||
if (_config.TfsServerConfig.SimulationMode) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any specific reason we need to duplicate this code here? We should rather keep this in Mail2BugEngine itself so that these classes are agnostic of which IWorkItemManager is being used. |
||
{ | ||
Logger.InfoFormat("Working in simulation mode. Using WorkItemManagerMock"); | ||
workItemManager = new WorkItemManagerMock(_config.WorkItemSettings.ConversationIndexFieldName); | ||
} | ||
else | ||
{ | ||
Logger.InfoFormat("Working in standard mode, using TFSWorkItemManager"); | ||
workItemManager = new TFSWorkItemManager(_config); | ||
} | ||
|
||
return workItemManager; | ||
} | ||
|
||
private static readonly ILog Logger = LogManager.GetLogger(typeof(SimpleBugStrategy)); | ||
|
||
public void Dispose() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Mail2Bug.MessageProcessingStrategies; | ||
using Mail2Bug.Email; | ||
|
||
namespace Mail2BugUnitTests.Mocks | ||
{ | ||
public class ProcessingStrategyMock : IMessageProcessingStrategy | ||
{ | ||
public bool status { get; set; } | ||
public ProcessingStrategyMock() { | ||
status = false; | ||
} | ||
public void ProcessInboxMessage(IIncomingEmailMessage message) { | ||
status = true; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Mail2Bug.MessageProcessingStrategies; | ||
using Mail2BugUnitTests.Mocks; | ||
using Mail2BugUnitTests.Mocks.Email; | ||
|
||
namespace Mail2BugUnitTests | ||
{ | ||
[TestClass] | ||
public class MultiStrategyUnitTest | ||
{ | ||
[TestMethod] | ||
public void MultipleStrategiesCalled() | ||
{ | ||
List<IMessageProcessingStrategy> mocks = new List<IMessageProcessingStrategy>(); | ||
IncomingEmailMessageMock messageMock = new IncomingEmailMessageMock(); | ||
|
||
mocks.Add(new ProcessingStrategyMock()); | ||
mocks.Add(new ProcessingStrategyMock()); | ||
MultiStrategy m = new MultiStrategy(mocks); | ||
|
||
m.ProcessInboxMessage(messageMock); | ||
|
||
foreach (ProcessingStrategyMock strategy in mocks) { | ||
Assert.IsTrue(strategy.status, "Process message not successfully called on child strategies."); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than creating a strategy to encapsulate others, can we update Mail2BugEngine's behavior to support a list of strategies instead of one. This will keep it simpler and will still support the same set of use cases. The default implementation can have only SimpleBugStrategy in the list.