• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rwjdk / TrelloDotNet / 6040892211

31 Aug 2023 06:06PM UTC coverage: 69.675% (+0.1%) from 69.534%
6040892211

push

github

Rasmus Wulff Jensen
Automation Engine can now make automations with multiple triggers

836 of 1521 branches covered (0.0%)

Branch coverage included in aggregate %.

24 of 24 new or added lines in 2 files covered. (100.0%)

2105 of 2700 relevant lines covered (77.96%)

62.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.68
/TrelloDotNet/TrelloDotNet/AutomationEngine/AutomationController.cs
1
using System;
2
using System.Linq;
3
using System.Threading;
4
using System.Threading.Tasks;
5
using TrelloDotNet.AutomationEngine.Interface;
6
using TrelloDotNet.AutomationEngine.Model;
7
using TrelloDotNet.Model.Webhook;
8

9
namespace TrelloDotNet.AutomationEngine
10
{
11
    /// <summary>
12
    /// The Automation Controller that can be used to automate receiving of Webhooks events against a set of configured automations
13
    /// </summary>
14
    public class AutomationController
15
    {
16
        private readonly Automation[] _automations;
17
        private readonly WebhookDataReceiver _receiver;
18

19
        /// <summary>
20
        /// Constructor
21
        /// </summary>
22
        /// <param name="configuration">Configuration of the Automation Engine</param>
23
        public AutomationController(Configuration configuration)
15✔
24
        {
25
            if (configuration == null)
15✔
26
            {
27
                throw new ArgumentNullException(nameof(configuration), "You did not provide a configuration");
1✔
28
            }
29

30
            var trelloClient = configuration.TrelloClient;
14✔
31
            _automations = configuration.Automations;
14✔
32
            _receiver = new WebhookDataReceiver(trelloClient);
14✔
33
        }
14✔
34

35
        /// <summary>
36
        /// Process raw JSON from a Trello Webhook against the provided set of Automations in the configuration
37
        /// </summary>
38
        /// <param name="request">The processing request (with the JSON)</param>
39
        /// <param name="cancellationToken">Cancellation Token</param>
40
        /// <returns></returns>
41
        public async Task<ProcessingResult> ProcessJsonFromWebhookAsync(ProcessingRequest request, CancellationToken cancellationToken = default)
42
        {
43
            cancellationToken.ThrowIfCancellationRequested();
14✔
44
            WebhookNotification data = _receiver.ConvertJsonToWebhookNotification(request.JsonFromWebhook);
14✔
45
            var webhookAction = data.Action;
14✔
46
            var result = new ProcessingResult();
14✔
47

48
            foreach (var automation in _automations)
75✔
49
            {
50
                IAutomationTrigger triggerBeingChecked = null;
25✔
51
                try
52
                {
53
                    var triggerMet = false;
25✔
54
                    cancellationToken.ThrowIfCancellationRequested();
25✔
55
                    foreach (var trigger in automation.Triggers)
87✔
56
                    {
57
                        triggerBeingChecked = trigger;
29✔
58
                        triggerMet = await trigger.IsTriggerMetAsync(webhookAction);
29✔
59
                        if(triggerMet)
28✔
60
                        {
61
                            break;
62
                        }
63
                    }
64
                    if (!triggerMet)
24✔
65
                    {
66
                        result.AddToLog($"Automation '{automation.Name}' was not processed as trigger was not met");
4✔
67
                        result.AutomationsSkipped++;
4✔
68
                        continue; //Wrong Trigger
4✔
69
                    }
70
                }
20✔
71
                catch (Exception e)
1✔
72
                {
73
                    if (triggerBeingChecked != null)
1!
74
                    {
75
                        throw new AutomationException($"Error checking Trigger of type '{triggerBeingChecked.GetType()}' in automation '{automation.Name}'{AddErrorContext(data)}", e);
1✔
76
                    }
77
                    throw new AutomationException($"Error checking Trigger in automation '{automation.Name}'{AddErrorContext(data)}", e);
×
78
                }
79

80
                var conditionsMet = true;
20✔
81
                if (automation.Conditions != null)
20✔
82
                {
83
                    foreach (var x in automation.Conditions)
65✔
84
                    {
85
                        try
86
                        {
87
                            cancellationToken.ThrowIfCancellationRequested();
15✔
88
                            if (!await x.IsConditionMetAsync(webhookAction))
15✔
89
                            {
90
                                conditionsMet = false;
4✔
91
                                break;
4✔
92
                            }
93
                        }
10✔
94
                        catch (Exception e)
1✔
95
                        {
96
                            throw new AutomationException($"Error checking Condition of type '{x.GetType()}' in automation '{automation.Name}'{AddErrorContext(data)}", e);
1✔
97
                        }
98
                    }
10✔
99
                }
100

101
                if (conditionsMet)
19✔
102
                {
103
                    result.AutomationsProcessed++;
15✔
104
                    result.AddToLog($"Automation '{automation.Name}' trigger and condition was met - Executing {automation.Actions.Count} Action");
15✔
105
                    foreach (var actionAction in automation.Actions.Where(x => x != null))
80✔
106
                    {
107
                        try
108
                        {
109
                            cancellationToken.ThrowIfCancellationRequested();
17✔
110
                            await actionAction.PerformActionAsync(webhookAction, result);
17✔
111
                        }
16✔
112
                        catch(StopProcessingFurtherActionException)
×
113
                        {
114
                            return result;
×
115
                        }
116
                        catch (Exception e)
1✔
117
                        {
118
                            throw new AutomationException($"Error performing Action of type '{actionAction.GetType()}' in automation '{automation.Name}'{AddErrorContext(data)}", e);
1✔
119
                        }
120

121
                    }
16✔
122
                }
123
                else
124
                {
125
                    result.AddToLog($"Automation '{automation.Name}' was not processed as not all conditions where met was not met");
4✔
126
                    result.AutomationsSkipped++;
4✔
127
                }
128
            }
18✔
129
            return result;
11✔
130
        }
11✔
131

132
        private string AddErrorContext(WebhookNotification webhookNotification)
133
        {
134
            try
135
            {
136
                return webhookNotification.Action.SummarizeEvent();
3✔
137
            }
138
            catch
×
139
            {
140
                return string.Empty;
×
141
            }
142
        }
3✔
143
    }
144
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc