From 5132a144f9edbde5997add10a28ef2e7ec10533a Mon Sep 17 00:00:00 2001 From: Rasmus Wulff Jensen Date: Fri, 9 Jun 2023 09:25:32 +0200 Subject: [PATCH] - Added Automation Trigger CardMovedAwayFromListTrigger - Nuget Bump to 1.6.4 --- Changelog.md | 5 + .../Triggers/CardMovedAwayFromListTrigger.cs | 108 ++++++++++++++++++ .../CardMovedAwayFromListTriggerConstraint.cs | 18 +++ TrelloDotNet/TrelloDotNet/TrelloDotNet.csproj | 2 +- 4 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 TrelloDotNet/TrelloDotNet/AutomationEngine/Model/Triggers/CardMovedAwayFromListTrigger.cs create mode 100644 TrelloDotNet/TrelloDotNet/AutomationEngine/Model/Triggers/CardMovedAwayFromListTriggerConstraint.cs diff --git a/Changelog.md b/Changelog.md index 569fd7c..8702a4e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,11 @@ # Changelog: *Below is the version history of [TrelloDotNet](https://github.com/rwjdk/TrelloDotNet) (An wrapper of the Trello API)* +## 1.6.4 (9th of June 2023) +- Added Automation Trigger [`CardMovedAwayFromListTrigger`](https://github.com/rwjdk/TrelloDotNet/wiki/CardMovedAwayFromListTrigger) + +
+ ## 1.6.3 (7th of June 2023) - Added Automation Action [`AddLabelsToCardAction`](https://github.com/rwjdk/TrelloDotNet/wiki/AddLabelsToCardAction) - Added Automation Action [`AddMembersToCardAction`](https://github.com/rwjdk/TrelloDotNet/wiki/AddMembersToCardAction) diff --git a/TrelloDotNet/TrelloDotNet/AutomationEngine/Model/Triggers/CardMovedAwayFromListTrigger.cs b/TrelloDotNet/TrelloDotNet/AutomationEngine/Model/Triggers/CardMovedAwayFromListTrigger.cs new file mode 100644 index 0000000..9dd6a44 --- /dev/null +++ b/TrelloDotNet/TrelloDotNet/AutomationEngine/Model/Triggers/CardMovedAwayFromListTrigger.cs @@ -0,0 +1,108 @@ +using System; +using System.Linq; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using TrelloDotNet.AutomationEngine.Interface; +using TrelloDotNet.Model.Webhook; + +namespace TrelloDotNet.AutomationEngine.Model.Triggers +{ + /// + /// Trigger of an event that is a Card is Moved away from a List + /// + public class CardMovedAwayFromListTrigger : IAutomationTrigger + { + /// + /// The constraints of the Trigger + /// + public CardMovedAwayFromListTriggerConstraint Constraint { get; } + + /// + /// The Ids of the Lists the trigger should evaluate. Tip: These can be List-names instead of Ids if you set 'TreatListNameAsId' to True + /// + public string[] ListIds { get; } + + /// + /// Set this to 'True' if you supplied names of Lists instead of the Ids. While this is more convenient, it will sometimes be slightly slower and less resilient to the renaming of things. + /// + public bool TreatListNameAsId { get; set; } + + /// + /// Defines the criteria on how to match Names (only used if TreatListNameAsId = 'True'). Default is Equal Match + /// + public StringMatchCriteria ListNameMatchCriteria { get; set; } + + /// + /// Constructor + /// + /// The constraints of the Trigger + /// + public CardMovedAwayFromListTrigger(CardMovedAwayFromListTriggerConstraint constraint, params string[] listIds) + { + Constraint = constraint; + ListIds = listIds; + ListNameMatchCriteria = StringMatchCriteria.Equal; + } + + /// + /// If the Trigger is met + /// + /// + /// While this is built to support async execution, It is best practice to keep a Trigger as light as possible only checking values against the webhook Action and not make any API Calls. The reason for this is that Triggers are called quite often, so it is better to make simple triggers and supplement with Conditions + /// + /// The Webhook Action that led to the check of the trigger. This object also have the TrelloClient and information about the event at your disposal + /// If trigger is met or not + public async Task IsTriggerMetAsync(WebhookAction webhookAction) + { + await Task.CompletedTask; + var partToCheck = TreatListNameAsId ? webhookAction.Data?.ListBefore?.Name : webhookAction.Data?.ListBefore?.Id; + if (partToCheck == null) + { + return false; + } + var correctType = webhookAction.Type == WebhookActionTypes.UpdateCard; + + if (!TreatListNameAsId) + { + ListNameMatchCriteria = StringMatchCriteria.Equal; //Force exact match no matter what user defined as it does not make sense to partly match auto-generated Ids + } + + switch (Constraint) + { + case CardMovedAwayFromListTriggerConstraint.AnyOfTheseListsAreMovedAwayFrom: + switch (ListNameMatchCriteria) + { + case StringMatchCriteria.StartsWith: + return correctType && ListIds.Any(x => partToCheck.StartsWith(x)); + case StringMatchCriteria.EndsWith: + return correctType && ListIds.Any(x => partToCheck.EndsWith(x)); + case StringMatchCriteria.Contains: + return correctType && ListIds.Any(x => partToCheck.Contains(x)); + case StringMatchCriteria.RegEx: + return correctType && ListIds.Any(x => Regex.IsMatch(partToCheck, x)); + case StringMatchCriteria.Equal: + default: + return correctType && ListIds.Contains(partToCheck); + } + + case CardMovedAwayFromListTriggerConstraint.AnyButTheseListsAreMovedAwayFrom: + switch (ListNameMatchCriteria) + { + case StringMatchCriteria.StartsWith: + return correctType && ListIds.Any(x => !partToCheck.StartsWith(x)); + case StringMatchCriteria.EndsWith: + return correctType && ListIds.Any(x => !partToCheck.EndsWith(x)); + case StringMatchCriteria.Contains: + return correctType && ListIds.Any(x => !partToCheck.Contains(x)); + case StringMatchCriteria.RegEx: + return correctType && ListIds.Any(x => !Regex.IsMatch(partToCheck, x)); + case StringMatchCriteria.Equal: + default: + return correctType && !ListIds.Contains(partToCheck); + } + default: + throw new ArgumentOutOfRangeException(); + } + } + } +} \ No newline at end of file diff --git a/TrelloDotNet/TrelloDotNet/AutomationEngine/Model/Triggers/CardMovedAwayFromListTriggerConstraint.cs b/TrelloDotNet/TrelloDotNet/AutomationEngine/Model/Triggers/CardMovedAwayFromListTriggerConstraint.cs new file mode 100644 index 0000000..152b240 --- /dev/null +++ b/TrelloDotNet/TrelloDotNet/AutomationEngine/Model/Triggers/CardMovedAwayFromListTriggerConstraint.cs @@ -0,0 +1,18 @@ +namespace TrelloDotNet.AutomationEngine.Model.Triggers +{ + /// + /// The Constraints on a CardMoved Away From List Trigger + /// + public enum CardMovedAwayFromListTriggerConstraint + { + /// + /// That any of the provided Lists are being moved away from + /// + AnyOfTheseListsAreMovedAwayFrom = 1, + /// + /// That any but the provided lists are being moved away from (Example, any but the 'Done' Column) + /// + AnyButTheseListsAreMovedAwayFrom = 2 + + } +} \ No newline at end of file diff --git a/TrelloDotNet/TrelloDotNet/TrelloDotNet.csproj b/TrelloDotNet/TrelloDotNet/TrelloDotNet.csproj index 876baed..9de7c58 100644 --- a/TrelloDotNet/TrelloDotNet/TrelloDotNet.csproj +++ b/TrelloDotNet/TrelloDotNet/TrelloDotNet.csproj @@ -13,7 +13,7 @@ https://github.com/rwjdk/TrelloDotNet trello.png trello;api;rest;trello api - 1.6.3 + 1.6.4 RWJDK MIT Please see: https://github.com/rwjdk/TrelloDotNet/blob/main/Changelog.md