Skip to content

Commit

Permalink
- Added Automation Trigger CardMovedAwayFromListTrigger
Browse files Browse the repository at this point in the history
- Nuget Bump to 1.6.4
  • Loading branch information
rwjdk committed Jun 9, 2023
1 parent b12af43 commit 5132a14
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -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)

<hr>

## 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Trigger of an event that is a Card is Moved away from a List
/// </summary>
public class CardMovedAwayFromListTrigger : IAutomationTrigger
{
/// <summary>
/// The constraints of the Trigger
/// </summary>
public CardMovedAwayFromListTriggerConstraint Constraint { get; }

/// <summary>
/// The Ids of the Lists the trigger should evaluate. Tip: These can be List-names instead of Ids if you set 'TreatListNameAsId' to True
/// </summary>
public string[] ListIds { get; }

/// <summary>
/// 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.
/// </summary>
public bool TreatListNameAsId { get; set; }

/// <summary>
/// Defines the criteria on how to match Names (only used if TreatListNameAsId = 'True'). Default is Equal Match
/// </summary>
public StringMatchCriteria ListNameMatchCriteria { get; set; }

/// <summary>
/// Constructor
/// </summary>
/// <param name="constraint">The constraints of the Trigger</param>
/// <param name="listIds"></param>
public CardMovedAwayFromListTrigger(CardMovedAwayFromListTriggerConstraint constraint, params string[] listIds)
{
Constraint = constraint;
ListIds = listIds;
ListNameMatchCriteria = StringMatchCriteria.Equal;
}

/// <summary>
/// If the Trigger is met
/// </summary>
/// <remarks>
/// 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
/// </remarks>
/// <param name="webhookAction">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</param>
/// <returns>If trigger is met or not</returns>
public async Task<bool> 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();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace TrelloDotNet.AutomationEngine.Model.Triggers
{
/// <summary>
/// The Constraints on a CardMoved Away From List Trigger
/// </summary>
public enum CardMovedAwayFromListTriggerConstraint
{
/// <summary>
/// That any of the provided Lists are being moved away from
/// </summary>
AnyOfTheseListsAreMovedAwayFrom = 1,
/// <summary>
/// That any but the provided lists are being moved away from (Example, any but the 'Done' Column)
/// </summary>
AnyButTheseListsAreMovedAwayFrom = 2

}
}
2 changes: 1 addition & 1 deletion TrelloDotNet/TrelloDotNet/TrelloDotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageProjectUrl>https://github.com/rwjdk/TrelloDotNet</PackageProjectUrl>
<PackageIcon>trello.png</PackageIcon>
<PackageTags>trello;api;rest;trello api</PackageTags>
<Version>1.6.3</Version>
<Version>1.6.4</Version>
<Company>RWJDK</Company>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReleaseNotes>Please see: https://github.com/rwjdk/TrelloDotNet/blob/main/Changelog.md</PackageReleaseNotes>
Expand Down

0 comments on commit 5132a14

Please sign in to comment.