Skip to main content

stormchaser_model/
event_rules.rs

1//! Rule-based event processing and webhook configuration models.
2
3use chrono::{DateTime, Utc};
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6use std::collections::HashMap;
7use uuid::Uuid;
8
9/// Configuration for an external webhook integration.
10#[derive(Debug, Serialize, Deserialize, Clone, sqlx::FromRow)]
11pub struct WebhookConfig {
12    /// Unique identifier for the webhook configuration.
13    pub id: Uuid,
14    /// Descriptive name for the webhook.
15    pub name: String,
16    /// Optional detailed description.
17    pub description: Option<String>,
18    /// System generating the webhook (e.g., 'github', 'generic').
19    pub source_type: String, // 'github', 'generic', etc.
20    /// Secret token used to validate incoming requests.
21    pub secret_token: Option<String>,
22    /// Whether the webhook is actively receiving events.
23    pub is_active: bool,
24    /// Optional custom CA certificate for mTLS.
25    pub ca_cert: Option<String>,
26    /// Optional client certificate for mTLS.
27    pub client_cert: Option<String>,
28    /// Optional client private key for mTLS.
29    pub client_key: Option<String>,
30    /// Timestamp when the webhook was created.
31    pub created_at: DateTime<Utc>,
32    /// Timestamp when the webhook was last updated.
33    pub updated_at: DateTime<Utc>,
34}
35
36/// Defines a rule for triggering workflows based on incoming events.
37#[derive(Debug, Serialize, Deserialize, Clone, sqlx::FromRow)]
38pub struct EventRule {
39    /// Unique identifier for the event rule.
40    pub id: Uuid,
41    /// Name of the event rule.
42    pub name: String,
43    /// Optional description of the rule's behavior.
44    pub description: Option<String>,
45    /// Optional ID of the webhook this rule applies to.
46    pub webhook_id: Option<Uuid>,
47    /// Pattern matching the type of event to handle.
48    pub event_type_pattern: String,
49    /// CEL expression used to filter matching events.
50    pub condition_expr: Option<String>,
51    /// Name of the workflow to trigger when the rule matches.
52    pub workflow_name: String,
53    /// Git repository URL containing the workflow.
54    pub repo_url: String,
55    /// Path to the workflow file within the repository.
56    pub workflow_path: String,
57    /// Git reference (branch, tag, or commit) to execute.
58    pub git_ref: String,
59    /// JSON mapping of workflow inputs to CEL expressions evaluated against the event.
60    pub input_mappings: Value, // Map of name -> CEL expr
61    /// Whether this rule is actively being evaluated.
62    pub is_active: bool,
63    /// Timestamp when the rule was created.
64    pub created_at: DateTime<Utc>,
65    /// Timestamp when the rule was last updated.
66    pub updated_at: DateTime<Utc>,
67}
68
69impl EventRule {
70    /// Returns the parsed input mappings as a hash map of string keys to CEL expressions.
71    pub fn get_input_mappings(&self) -> HashMap<String, String> {
72        serde_json::from_value(self.input_mappings.clone()).unwrap_or_default()
73    }
74}