heliosdb-nano 3.30.0

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! Webhook routes for Git provider integration
//!
//! Exposes endpoints for GitHub, GitLab, and generic webhooks to automate
//! PR/MR preview environments.
//!
//! ## Endpoints
//!
//! - `POST /api/webhooks/github` - GitHub webhook
//! - `POST /api/webhooks/gitlab` - GitLab webhook
//! - `POST /api/webhooks/generic` - Provider-agnostic webhook

use axum::{
    extract::{State, Json},
    http::{HeaderMap, StatusCode},
    response::IntoResponse,
    routing::post,
    Router,
};
use serde::{Deserialize, Serialize};

use crate::api::server::AppState;
use crate::git_integration::webhooks::{
    WebhookHandler, WebhookConfig, WebhookResult, WebhookEvent,
    GitProvider, WebhookEventType, StorageWebhookHandler,
};

/// Webhook response
#[derive(Debug, Serialize)]
pub struct WebhookResponse {
    pub success: bool,
    pub message: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub branch_id: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub action: Option<String>,
}

impl From<WebhookResult> for WebhookResponse {
    fn from(result: WebhookResult) -> Self {
        Self {
            success: result.success,
            message: result.message,
            branch_id: result.branch_id,
            action: result.action,
        }
    }
}

/// Error response
#[derive(Debug, Serialize)]
pub struct WebhookError {
    pub error: String,
    pub code: String,
}

impl WebhookError {
    fn new(error: impl Into<String>, code: impl Into<String>) -> Self {
        Self {
            error: error.into(),
            code: code.into(),
        }
    }
}

/// Create webhook routes
pub fn routes() -> Router<AppState> {
    Router::new()
        .route("/github", post(handle_github_webhook))
        .route("/gitlab", post(handle_gitlab_webhook))
        .route("/generic", post(handle_generic_webhook))
        .route("/health", axum::routing::get(health_check))
}

/// Health check for webhook endpoints
async fn health_check() -> impl IntoResponse {
    Json(serde_json::json!({
        "status": "ok",
        "service": "webhooks"
    }))
}

/// Handle GitHub webhook
///
/// Expects:
/// - Header `X-GitHub-Event`: Event type (e.g., "pull_request")
/// - Header `X-Hub-Signature-256`: HMAC signature (optional, for validation)
/// - Body: JSON payload
async fn handle_github_webhook(
    State(state): State<AppState>,
    headers: HeaderMap,
    body: String,
) -> impl IntoResponse {
    // Get event type header
    let event_type = match headers.get("X-GitHub-Event") {
        Some(v) => v.to_str().unwrap_or("unknown"),
        None => {
            return (
                StatusCode::BAD_REQUEST,
                Json(WebhookError::new(
                    "Missing X-GitHub-Event header",
                    "MISSING_EVENT_TYPE",
                )),
            ).into_response();
        }
    };

    // Get webhook config from state (if available)
    let config = get_webhook_config(&state);
    let handler = WebhookHandler::new(config);

    // Validate signature if configured
    if let Some(signature) = headers.get("X-Hub-Signature-256") {
        let sig_str = signature.to_str().unwrap_or("");
        match handler.validate_github_signature(body.as_bytes(), sig_str) {
            Ok(false) => {
                return (
                    StatusCode::UNAUTHORIZED,
                    Json(WebhookError::new(
                        "Invalid webhook signature",
                        "INVALID_SIGNATURE",
                    )),
                ).into_response();
            }
            Err(e) => {
                return (
                    StatusCode::UNAUTHORIZED,
                    Json(WebhookError::new(
                        format!("Signature validation error: {}", e),
                        "SIGNATURE_ERROR",
                    )),
                ).into_response();
            }
            Ok(true) => {}
        }
    }

    // Parse the webhook payload
    let event = match handler.parse_github(&body, event_type) {
        Ok(e) => e,
        Err(e) => {
            return (
                StatusCode::BAD_REQUEST,
                Json(WebhookError::new(
                    format!("Failed to parse webhook: {}", e),
                    "PARSE_ERROR",
                )),
            ).into_response();
        }
    };

    // Handle the event - prefer storage-aware handler if available
    let result = handle_event_with_storage(&state, &event);
    match result {
        Ok(result) => {
            let status = if result.success {
                StatusCode::OK
            } else {
                StatusCode::INTERNAL_SERVER_ERROR
            };
            (status, Json(WebhookResponse::from(result))).into_response()
        }
        Err(e) => {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(WebhookError::new(
                    format!("Handler error: {}", e),
                    "HANDLER_ERROR",
                )),
            ).into_response()
        }
    }
}

/// Handle GitLab webhook
///
/// Expects:
/// - Header `X-Gitlab-Event`: Event type
/// - Header `X-Gitlab-Token`: Secret token (optional, for validation)
/// - Body: JSON payload
async fn handle_gitlab_webhook(
    State(state): State<AppState>,
    headers: HeaderMap,
    body: String,
) -> impl IntoResponse {
    // Get webhook config from state
    let config = get_webhook_config(&state);
    let handler = WebhookHandler::new(config);

    // Validate token if provided
    if let Some(token) = headers.get("X-Gitlab-Token") {
        let token_str = token.to_str().unwrap_or("");
        match handler.validate_gitlab_token(token_str) {
            Ok(false) => {
                return (
                    StatusCode::UNAUTHORIZED,
                    Json(WebhookError::new(
                        "Invalid GitLab token",
                        "INVALID_TOKEN",
                    )),
                ).into_response();
            }
            Err(e) => {
                return (
                    StatusCode::UNAUTHORIZED,
                    Json(WebhookError::new(
                        format!("Token validation error: {}", e),
                        "TOKEN_ERROR",
                    )),
                ).into_response();
            }
            Ok(true) => {}
        }
    }

    // Parse the webhook payload
    let event = match handler.parse_gitlab(&body) {
        Ok(e) => e,
        Err(e) => {
            return (
                StatusCode::BAD_REQUEST,
                Json(WebhookError::new(
                    format!("Failed to parse webhook: {}", e),
                    "PARSE_ERROR",
                )),
            ).into_response();
        }
    };

    // Handle the event - prefer storage-aware handler if available
    let result = handle_event_with_storage(&state, &event);
    match result {
        Ok(result) => {
            let status = if result.success {
                StatusCode::OK
            } else {
                StatusCode::INTERNAL_SERVER_ERROR
            };
            (status, Json(WebhookResponse::from(result))).into_response()
        }
        Err(e) => {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(WebhookError::new(
                    format!("Handler error: {}", e),
                    "HANDLER_ERROR",
                )),
            ).into_response()
        }
    }
}

/// Generic webhook payload
#[derive(Debug, Deserialize)]
pub struct GenericWebhookPayload {
    /// Event type (pr_opened, pr_closed, pr_merged, etc.)
    pub event_type: String,
    /// Source branch name
    pub source_branch: String,
    /// Target branch name (for PRs)
    #[serde(default)]
    pub target_branch: Option<String>,
    /// PR/MR number
    #[serde(default)]
    pub pr_number: Option<u64>,
    /// Commit SHA
    #[serde(default)]
    pub commit_sha: Option<String>,
    /// Provider name (github, gitlab, bitbucket, custom)
    #[serde(default = "default_provider")]
    pub provider: String,
    /// Repository identifier
    #[serde(default)]
    pub repository: Option<String>,
}

fn default_provider() -> String {
    "generic".to_string()
}

/// Handle generic webhook
///
/// Provider-agnostic webhook endpoint that accepts a standardized payload format.
/// This allows integration with any CI/CD system or custom tooling.
async fn handle_generic_webhook(
    State(state): State<AppState>,
    headers: HeaderMap,
    Json(payload): Json<GenericWebhookPayload>,
) -> impl IntoResponse {
    // Get webhook config from state
    let config = get_webhook_config(&state);

    // Validate secret if provided in Authorization header
    if let Some(auth) = headers.get("Authorization") {
        if let Some(ref secret) = config.generic_secret {
            let auth_str = auth.to_str().unwrap_or("");
            let expected = format!("Bearer {}", secret);
            if auth_str != expected {
                return (
                    StatusCode::UNAUTHORIZED,
                    Json(WebhookError::new(
                        "Invalid authorization",
                        "INVALID_AUTH",
                    )),
                ).into_response();
            }
        }
    }

    // Parse event type
    let event_type = match payload.event_type.as_str() {
        "pr_opened" | "pull_request_opened" | "mr_opened" => WebhookEventType::PrOpened,
        "pr_updated" | "pull_request_updated" | "mr_updated" => WebhookEventType::PrUpdated,
        "pr_merged" | "pull_request_merged" | "mr_merged" => WebhookEventType::PrMerged,
        "pr_closed" | "pull_request_closed" | "mr_closed" => WebhookEventType::PrClosed,
        "push" => WebhookEventType::Push,
        "branch_created" => WebhookEventType::BranchCreated,
        "branch_deleted" => WebhookEventType::BranchDeleted,
        _ => {
            return (
                StatusCode::BAD_REQUEST,
                Json(WebhookError::new(
                    format!("Unknown event type: {}", payload.event_type),
                    "UNKNOWN_EVENT_TYPE",
                )),
            ).into_response();
        }
    };

    // Parse provider
    let provider = match payload.provider.to_lowercase().as_str() {
        "github" => GitProvider::GitHub,
        "gitlab" => GitProvider::GitLab,
        "bitbucket" => GitProvider::Bitbucket,
        _ => GitProvider::Generic,
    };

    // Build event
    let event = WebhookEvent {
        event_type,
        source_branch: payload.source_branch,
        target_branch: payload.target_branch,
        pr_number: payload.pr_number,
        commit_sha: payload.commit_sha,
        provider,
        repository: payload.repository,
        timestamp: Some(
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs(),
        ),
        raw_payload: None,
    };

    // Handle the event - prefer storage-aware handler if available
    let result = handle_event_with_storage(&state, &event);
    match result {
        Ok(result) => {
            let status = if result.success {
                StatusCode::OK
            } else {
                StatusCode::INTERNAL_SERVER_ERROR
            };
            (status, Json(WebhookResponse::from(result))).into_response()
        }
        Err(e) => {
            (
                StatusCode::INTERNAL_SERVER_ERROR,
                Json(WebhookError::new(
                    format!("Handler error: {}", e),
                    "HANDLER_ERROR",
                )),
            ).into_response()
        }
    }
}

/// Handle webhook event with storage integration if available
///
/// If the database has branching enabled, uses StorageWebhookHandler
/// to actually create/merge/drop branches. Otherwise falls back to
/// the basic WebhookHandler that just logs events.
fn handle_event_with_storage(
    state: &AppState,
    event: &WebhookEvent,
) -> Result<WebhookResult, crate::Error> {
    let config = get_webhook_config(state);

    // Try to use storage-aware handler if branching is enabled
    if let Some(branch_manager) = state.db.storage.branch_manager() {
        let storage_handler = StorageWebhookHandler::new(config, branch_manager.as_ref());
        storage_handler.handle(event)
    } else {
        // Fall back to basic handler (just logging)
        let basic_handler = WebhookHandler::new(config);
        basic_handler.handle(event)
    }
}

/// Get webhook configuration from app state
///
/// Reads webhook secrets from environment variables:
/// - HELIOS_GITHUB_WEBHOOK_SECRET: GitHub webhook signature secret
/// - HELIOS_GITLAB_WEBHOOK_TOKEN: GitLab webhook token
/// - HELIOS_WEBHOOK_SECRET: Generic webhook secret
fn get_webhook_config(_state: &AppState) -> WebhookConfig {
    WebhookConfig {
        github_secret: std::env::var("HELIOS_GITHUB_WEBHOOK_SECRET").ok(),
        gitlab_token: std::env::var("HELIOS_GITLAB_WEBHOOK_TOKEN").ok(),
        generic_secret: std::env::var("HELIOS_WEBHOOK_SECRET").ok(),
        allowed_ips: Vec::new(),
        rate_limit: 60, // requests per minute
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_generic_payload_parsing() {
        let json = r#"{
            "event_type": "pr_opened",
            "source_branch": "feature/test",
            "target_branch": "main",
            "pr_number": 123
        }"#;

        let payload: GenericWebhookPayload = serde_json::from_str(json).unwrap();
        assert_eq!(payload.event_type, "pr_opened");
        assert_eq!(payload.source_branch, "feature/test");
        assert_eq!(payload.target_branch, Some("main".to_string()));
        assert_eq!(payload.pr_number, Some(123));
        assert_eq!(payload.provider, "generic");
    }

    #[test]
    fn test_webhook_response_from_result() {
        let result = WebhookResult::success("Test success")
            .with_branch(42)
            .with_action("test");

        let response: WebhookResponse = result.into();
        assert!(response.success);
        assert_eq!(response.message, "Test success");
        assert_eq!(response.branch_id, Some(42));
        assert_eq!(response.action, Some("test".to_string()));
    }
}