apiforge 0.4.0

Production-grade API release automation CLI. From merged code to healthy pods in production — one command.
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
use std::collections::HashMap;
use std::path::Path;

use chrono::Utc;
use serde::{Deserialize, Serialize};
use tracing::{debug, error, info, warn};
use uuid::Uuid;

pub mod retry;

#[cfg(test)]
const MAX_AUDIT_RECORDS: usize = 50;
#[cfg(not(test))]
const MAX_AUDIT_RECORDS: usize = 10_000;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReleaseRecord {
    pub id: String,
    pub version: String,
    pub bump_type: String,
    pub timestamp: String,
    pub status: ReleaseStatus,
    pub steps: Vec<StepRecord>,
    pub duration_ms: u64,
    pub dry_run: bool,
    pub metadata: HashMap<String, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum ReleaseStatus {
    Success,
    Failed,
    RolledBack,
}

impl std::fmt::Display for ReleaseStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ReleaseStatus::Success => write!(f, "success"),
            ReleaseStatus::Failed => write!(f, "failed"),
            ReleaseStatus::RolledBack => write!(f, "rolled_back"),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepRecord {
    pub name: String,
    pub status: StepStatus,
    pub duration_ms: u64,
    pub message: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum StepStatus {
    Success,
    Failed,
    Skipped,
}

pub struct AuditStore {
    db: sled::Db,
    retry_config: retry::AuditRetryConfig,
}

impl AuditStore {
    pub fn open(path: &Path) -> crate::error::Result<Self> {
        Self::open_with_config(path, retry::AuditRetryConfig::default())
    }

    pub fn open_with_config(
        path: &Path,
        retry_config: retry::AuditRetryConfig,
    ) -> crate::error::Result<Self> {
        // Ensure audit path exists before opening sled.
        if !path.exists() {
            std::fs::create_dir_all(path).map_err(|e| {
                crate::error::ApiForgeError::Audit(format!(
                    "Failed to create audit directory {:?}: {}",
                    path, e
                ))
            })?;
        }

        let db = retry::with_sled_retry(&retry_config, "Open audit database", || sled::open(path))?;

        debug!("Audit store opened at {:?}", path);
        Ok(Self { db, retry_config })
    }

    pub fn record(&self, record: &ReleaseRecord) -> crate::error::Result<()> {
        let key = format!("{}_{}", record.timestamp, record.id);
        let value = serde_json::to_vec(record).map_err(|e| {
            crate::error::ApiForgeError::Audit(format!("Failed to serialize record: {}", e))
        })?;

        retry::with_sled_retry(&self.retry_config, "Write audit record", || {
            self.db.insert(key.as_bytes(), value.clone())
        })?;

        // Flush after each write to ensure data persistence
        retry::with_sled_retry(&self.retry_config, "Flush audit database", || {
            self.db.flush()
        })?;

        let pruned = self.prune_excess_records(MAX_AUDIT_RECORDS)?;
        if pruned > 0 {
            info!(
                "Audit record retention enforced: pruned {} old record(s), retained latest {}",
                pruned, MAX_AUDIT_RECORDS
            );
        }

        debug!("Audit record written: {}", record.id);
        Ok(())
    }

    pub fn list(&self, limit: usize) -> crate::error::Result<Vec<ReleaseRecord>> {
        let mut records: Vec<ReleaseRecord> = Vec::new();

        let entries: Vec<(sled::IVec, sled::IVec)> =
            retry::with_sled_retry(&self.retry_config, "Iterate audit database", || {
                self.db.iter().rev().collect::<Result<Vec<_>, _>>()
            })?;

        for (key, value) in entries {
            if records.len() >= limit {
                break;
            }
            let record: ReleaseRecord = serde_json::from_slice(&value).map_err(|e| {
                crate::error::ApiForgeError::Audit(format!(
                    "Failed to deserialize record for key {:?}: {}",
                    key, e
                ))
            })?;
            records.push(record);
        }
        Ok(records)
    }

    /// Flush the database to disk explicitly
    pub fn flush(&self) -> crate::error::Result<()> {
        retry::with_sled_retry(&self.retry_config, "Flush audit database", || {
            self.db.flush()
        })?;
        Ok(())
    }

    /// Get the approximate size of the database on disk
    pub fn size_on_disk(&self) -> crate::error::Result<u64> {
        let size = retry::with_sled_retry(&self.retry_config, "Get database size", || {
            self.db.size_on_disk()
        })?;
        Ok(size)
    }

    /// Compact the database to reclaim space
    ///
    /// This operation removes stale data and rewrites the database
    /// to reclaim disk space from deleted/updated entries.
    /// Should be called periodically (e.g., weekly) to prevent
    /// unbounded growth.
    pub fn compact(&self) -> crate::error::Result<()> {
        info!("Starting audit database compaction...");

        let size_before = self.size_on_disk()?;
        debug!("Database size before compaction: {} bytes", size_before);

        let pruned = self.prune_excess_records(MAX_AUDIT_RECORDS)?;
        retry::with_sled_retry(&self.retry_config, "Compact audit database", || {
            self.db.flush()
        })?;

        let size_after = self.size_on_disk()?;
        let saved = size_before.saturating_sub(size_after);

        if saved > 0 {
            let reduction_percent = if size_before > 0 {
                saved
                    .checked_mul(100)
                    .and_then(|product| product.checked_div(size_before))
                    .unwrap_or_default()
            } else {
                0
            };
            info!(
                "Compaction completed: freed {} bytes ({} MB) ({}% reduction)",
                saved,
                saved / 1_048_576,
                reduction_percent
            );
        } else {
            info!(
                "Compaction completed: no space to reclaim (size: {} bytes)",
                size_after
            );
        }

        if pruned > 0 {
            info!(
                "Compaction also pruned {} old record(s) to enforce retention limit ({})",
                pruned, MAX_AUDIT_RECORDS
            );
        }

        Ok(())
    }

    /// Compact the database if it exceeds the given size threshold
    ///
    /// Returns true if compaction was performed
    pub fn compact_if_needed(&self, threshold_bytes: u64) -> crate::error::Result<bool> {
        let size = self.size_on_disk()?;
        if size > threshold_bytes {
            warn!(
                "Audit database size ({} bytes) exceeds threshold ({} bytes), compacting...",
                size, threshold_bytes
            );
            self.compact()?;
            Ok(true)
        } else {
            debug!(
                "Audit database size ({} bytes) below threshold ({} bytes), skipping compaction",
                size, threshold_bytes
            );
            Ok(false)
        }
    }

    fn prune_excess_records(&self, max_records: usize) -> crate::error::Result<usize> {
        let total = self.db.len();
        if total <= max_records {
            return Ok(0);
        }

        let to_delete = total - max_records;
        let keys_to_delete: Vec<Vec<u8>> =
            retry::with_sled_retry(&self.retry_config, "Collect records for pruning", || {
                let mut keys = Vec::with_capacity(to_delete);
                for entry in self.db.iter().take(to_delete) {
                    let (key, _) = entry?;
                    keys.push(key.to_vec());
                }
                Ok::<_, sled::Error>(keys)
            })?;

        let mut deleted = 0usize;
        for key in keys_to_delete {
            retry::with_sled_retry(&self.retry_config, "Prune old record", || {
                self.db.remove(&key)
            })?;
            deleted += 1;
        }

        if deleted > 0 {
            retry::with_sled_retry(&self.retry_config, "Flush pruned records", || {
                self.db.flush()
            })?;
        }

        Ok(deleted)
    }

    /// Get the number of records in the database
    pub fn len(&self) -> crate::error::Result<usize> {
        // sled::Db::len() returns usize directly, not Result
        let count = self.db.len();
        Ok(count)
    }

    /// Check if the database is empty
    pub fn is_empty(&self) -> crate::error::Result<bool> {
        Ok(self.len()? == 0)
    }

    /// Delete old records older than the given retention period
    ///
    /// Returns the number of records deleted
    pub fn prune_old_records(&self, retention_days: u32) -> crate::error::Result<usize> {
        if retention_days == 0 {
            return Ok(0);
        }

        let cutoff = chrono::Utc::now() - chrono::Duration::days(retention_days as i64);
        let cutoff_str = cutoff.to_rfc3339();

        let mut deleted = 0usize;

        let keys_to_delete: Vec<Vec<u8>> =
            retry::with_sled_retry(&self.retry_config, "Scan for old records", || {
                let mut keys = Vec::new();
                for entry in self.db.iter() {
                    let (key, _) = entry?;
                    let key_str = String::from_utf8_lossy(&key);
                    // Keys are formatted as "{timestamp}_{uuid}"
                    if let Some(timestamp) = key_str.split('_').next() {
                        if *timestamp < *cutoff_str {
                            keys.push(key.to_vec());
                        }
                    }
                }
                Ok::<_, sled::Error>(keys)
            })
            .map_err(|e| {
                crate::error::ApiForgeError::Audit(format!("Failed to scan records: {}", e))
            })?;

        for key in keys_to_delete {
            retry::with_sled_retry(&self.retry_config, "Delete old record", || {
                self.db.remove(&key)
            })?;
            deleted += 1;
        }

        if deleted > 0 {
            info!(
                "Pruned {} records older than {} days",
                deleted, retention_days
            );
            self.flush()?;
        }

        Ok(deleted)
    }
}

impl Drop for AuditStore {
    fn drop(&mut self) {
        // Ensure all data is written to disk when the store is dropped
        // Note: We can't use retry logic here since we're in Drop
        if let Err(e) = self.db.flush() {
            error!("Failed to flush audit database on drop: {}", e);
        }
    }
}

impl AuditStore {
    pub fn new_record(version: &str, bump_type: &str, dry_run: bool) -> ReleaseRecord {
        ReleaseRecord {
            id: Uuid::new_v4().to_string(),
            version: version.to_string(),
            bump_type: bump_type.to_string(),
            timestamp: Utc::now().to_rfc3339(),
            status: ReleaseStatus::Success,
            steps: Vec::new(),
            duration_ms: 0,
            dry_run,
            metadata: HashMap::new(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Duration as ChronoDuration;
    use std::time::Duration;
    use tempfile::TempDir;

    fn create_test_store() -> (AuditStore, TempDir) {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("test_audit");
        let store = AuditStore::open(&db_path).unwrap();
        (store, temp_dir)
    }

    #[test]
    fn test_audit_store_open_close() {
        let (store, _temp) = create_test_store();
        // Just verify it opens without error
        drop(store);
    }

    #[test]
    fn test_audit_store_record_and_list() {
        let (store, _temp) = create_test_store();

        let record = AuditStore::new_record("1.0.0", "minor", false);
        store.record(&record).unwrap();

        let records = store.list(10).unwrap();
        assert_eq!(records.len(), 1);
        assert_eq!(records[0].version, "1.0.0");
    }

    #[test]
    fn test_audit_store_list_limit() {
        let (store, _temp) = create_test_store();

        // Insert 5 records
        for i in 0..5 {
            let record = AuditStore::new_record(&format!("1.0.{}", i), "patch", false);
            store.record(&record).unwrap();
        }

        // List only 3
        let records = store.list(3).unwrap();
        assert_eq!(records.len(), 3);
    }

    #[test]
    fn test_audit_store_is_empty() {
        let (store, _temp) = create_test_store();

        assert!(store.is_empty().unwrap());

        let record = AuditStore::new_record("1.0.0", "minor", false);
        store.record(&record).unwrap();

        assert!(!store.is_empty().unwrap());
    }

    #[test]
    fn test_audit_store_len() {
        let (store, _temp) = create_test_store();

        assert_eq!(store.len().unwrap(), 0);

        for i in 0..3 {
            let record = AuditStore::new_record(&format!("1.0.{}", i), "patch", false);
            store.record(&record).unwrap();
        }

        assert_eq!(store.len().unwrap(), 3);
    }

    #[test]
    fn test_audit_store_size_on_disk() {
        let (store, _temp) = create_test_store();

        // Insert some data to ensure database has content
        let record = AuditStore::new_record("1.0.0", "minor", false);
        store.record(&record).unwrap();
        store.flush().unwrap();

        // size_on_disk returns a u64, should not panic
        let _size = store.size_on_disk().unwrap();
        // sled database size is valid (u64 is always >= 0)
    }

    #[test]
    fn test_audit_store_compact() {
        let (store, _temp) = create_test_store();

        // Insert some records
        for i in 0..10 {
            let record = AuditStore::new_record(&format!("1.0.{}", i), "patch", false);
            store.record(&record).unwrap();
        }

        // Compact should succeed
        store.compact().unwrap();

        // Records should still be there
        let records = store.list(20).unwrap();
        assert_eq!(records.len(), 10);
    }

    #[test]
    fn test_audit_store_compact_if_needed() {
        let (store, _temp) = create_test_store();

        // Insert some records
        for i in 0..5 {
            let record = AuditStore::new_record(&format!("1.0.{}", i), "patch", false);
            store.record(&record).unwrap();
        }

        // With a very high threshold, compaction should not happen
        let compacted = store.compact_if_needed(u64::MAX).unwrap();
        assert!(!compacted);

        // With a very low threshold, compaction should happen
        let compacted = store.compact_if_needed(1).unwrap();
        assert!(compacted);
    }

    #[test]
    fn test_audit_store_prune_old_records() {
        let (store, _temp) = create_test_store();

        // Insert a record with a very old timestamp
        let mut old_record = AuditStore::new_record("0.1.0", "minor", false);
        old_record.timestamp = "2020-01-01T00:00:00+00:00".to_string();
        store.record(&old_record).unwrap();

        // Insert a recent record
        let new_record = AuditStore::new_record("1.0.0", "minor", false);
        store.record(&new_record).unwrap();

        assert_eq!(store.len().unwrap(), 2);

        // Prune records older than 365 days
        let deleted = store.prune_old_records(365).unwrap();
        assert_eq!(deleted, 1);
        assert_eq!(store.len().unwrap(), 1);
    }

    #[test]
    fn test_audit_store_prune_zero_days() {
        let (store, _temp) = create_test_store();

        let record = AuditStore::new_record("1.0.0", "minor", false);
        store.record(&record).unwrap();

        // Pruning with 0 days should delete nothing
        let deleted = store.prune_old_records(0).unwrap();
        assert_eq!(deleted, 0);
        assert_eq!(store.len().unwrap(), 1);
    }

    #[test]
    fn test_audit_store_auto_prunes_excess_records() {
        let (store, _temp) = create_test_store();
        let base_ts = chrono::DateTime::parse_from_rfc3339("2024-01-01T00:00:00Z")
            .unwrap()
            .with_timezone(&Utc);
        let total_records = MAX_AUDIT_RECORDS + 5;

        for i in 0..total_records {
            let mut record = AuditStore::new_record(&format!("1.0.{}", i), "patch", false);
            record.id = format!("id-{:04}", i);
            record.timestamp = (base_ts + ChronoDuration::seconds(i as i64)).to_rfc3339();
            store.record(&record).unwrap();
        }

        assert_eq!(store.len().unwrap(), MAX_AUDIT_RECORDS);
        let records = store.list(MAX_AUDIT_RECORDS + 10).unwrap();
        assert_eq!(records.first().unwrap().version, "1.0.54");
        assert_eq!(records.last().unwrap().version, "1.0.5");
    }

    #[test]
    fn test_audit_store_with_retry_config() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("test_audit");

        let retry_config = retry::AuditRetryConfig {
            max_retries: 5,
            initial_delay: Duration::from_millis(10),
            max_delay: Duration::from_millis(100),
            backoff_multiplier: 1.5,
        };

        let store = AuditStore::open_with_config(&db_path, retry_config).unwrap();

        let record = AuditStore::new_record("1.0.0", "minor", false);
        store.record(&record).unwrap();

        let records = store.list(10).unwrap();
        assert_eq!(records.len(), 1);
    }

    #[test]
    fn test_release_status_display() {
        assert_eq!(format!("{}", ReleaseStatus::Success), "success");
        assert_eq!(format!("{}", ReleaseStatus::Failed), "failed");
        assert_eq!(format!("{}", ReleaseStatus::RolledBack), "rolled_back");
    }

    #[test]
    fn test_release_record_serialization() {
        let record = ReleaseRecord {
            id: "test-id".to_string(),
            version: "1.0.0".to_string(),
            bump_type: "minor".to_string(),
            timestamp: "2024-01-01T00:00:00Z".to_string(),
            status: ReleaseStatus::Success,
            steps: vec![StepRecord {
                name: "test-step".to_string(),
                status: StepStatus::Success,
                duration_ms: 100,
                message: None,
            }],
            duration_ms: 1000,
            dry_run: false,
            metadata: {
                let mut m = HashMap::new();
                m.insert("key".to_string(), "value".to_string());
                m
            },
        };

        let json = serde_json::to_string(&record).unwrap();
        let deserialized: ReleaseRecord = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.version, "1.0.0");
        assert_eq!(deserialized.steps.len(), 1);
    }
}