Skip to main content

stormchaser_model/
event_rules.rs

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