feat: add SQL/SQLite, AppSec, Capstone incident-tracker, Gitea Actions CI and spaced repetition
Test and Lint / quality (push) Successful in 1m50s
Test and Lint / quality (push) Successful in 1m50s
This commit is contained in:
1 parent
015eae08c3
commit
9130af822e
19 files changed
+722
-7
No files matched your search
@@ -0,0 +1,165 @@
|
||||
#![allow(unused_variables, dead_code)]
|
||||
|
||||
use clap::{Parser, Subcommand};
|
||||
use rusqlite::{params, Connection};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::BTreeMap;
|
||||
use std::path::Path;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum TrackerError {
|
||||
#[error("invalid incident input: {0}")]
|
||||
InvalidInput(String),
|
||||
#[error("incident {0} not found")]
|
||||
NotFound(i64),
|
||||
#[error("database error: {0}")]
|
||||
Db(#[from] rusqlite::Error),
|
||||
#[error("cli error: {0}")]
|
||||
Cli(String),
|
||||
#[error("json error: {0}")]
|
||||
Json(#[from] serde_json::Error),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct Incident {
|
||||
pub id: i64,
|
||||
pub title: String,
|
||||
pub severity: String,
|
||||
pub status: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[command(name = "incident-tracker")]
|
||||
pub struct Cli {
|
||||
#[command(subcommand)]
|
||||
pub command: Command,
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
pub enum Command {
|
||||
Add {
|
||||
title: String,
|
||||
#[arg(long, default_value = "medium")]
|
||||
severity: String,
|
||||
},
|
||||
List {
|
||||
#[arg(long)]
|
||||
severity: Option<String>,
|
||||
},
|
||||
Resolve {
|
||||
id: i64,
|
||||
},
|
||||
Stats,
|
||||
}
|
||||
|
||||
pub struct IncidentStore {
|
||||
conn: Connection,
|
||||
}
|
||||
|
||||
impl IncidentStore {
|
||||
pub fn open(path: &Path) -> Result<Self, TrackerError> {
|
||||
Ok(Self {
|
||||
conn: Connection::open(path)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn init_schema(&self) -> Result<(), TrackerError> {
|
||||
todo!("CREATE TABLE IF NOT EXISTS incidents (...)")
|
||||
}
|
||||
|
||||
pub fn add(&self, title: &str, severity: &str) -> Result<Incident, TrackerError> {
|
||||
let _ = params![title, severity];
|
||||
todo!("Zwaliduj title i severity (low|medium|high|critical), zapisz i zwróć Incident")
|
||||
}
|
||||
|
||||
pub fn list(&self, severity: Option<&str>) -> Result<Vec<Incident>, TrackerError> {
|
||||
todo!("Zwróć incydenty (opcjonalnie przefiltrowane po severity)")
|
||||
}
|
||||
|
||||
pub fn resolve(&self, id: i64) -> Result<Incident, TrackerError> {
|
||||
todo!("Ustaw status='resolved' lub zwróć TrackerError::NotFound(id)")
|
||||
}
|
||||
|
||||
pub fn stats(&self) -> Result<BTreeMap<String, usize>, TrackerError> {
|
||||
todo!("Zwróć mapę z kluczami 'open' i 'resolved'")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run_cli(db_path: &Path, args: &[&str]) -> Result<String, TrackerError> {
|
||||
todo!("Sparsuj Cli::try_parse_from(args), wykonaj komendę na IncidentStore i zwróć JSON")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
fn temp_db(tag: &str) -> std::path::PathBuf {
|
||||
let nanos = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos();
|
||||
std::env::temp_dir().join(format!("rust-incident-{tag}-{nanos}.db"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incident_store_sqlite_lifecycle() {
|
||||
let db = temp_db("store");
|
||||
let store = IncidentStore::open(&db).unwrap();
|
||||
store.init_schema().unwrap();
|
||||
|
||||
assert!(matches!(
|
||||
store.add(" ", "high"),
|
||||
Err(TrackerError::InvalidInput(_))
|
||||
));
|
||||
assert!(matches!(
|
||||
store.add("DB down", "urgent"),
|
||||
Err(TrackerError::InvalidInput(_))
|
||||
));
|
||||
|
||||
let inc1 = store.add("K8s node NotReady", "critical").unwrap();
|
||||
let _inc2 = store.add("High disk usage", "medium").unwrap();
|
||||
assert_eq!(inc1.status, "open");
|
||||
assert_eq!(store.list(Some("critical")).unwrap().len(), 1);
|
||||
|
||||
let resolved = store.resolve(inc1.id).unwrap();
|
||||
assert_eq!(resolved.status, "resolved");
|
||||
let stats = store.stats().unwrap();
|
||||
assert_eq!(stats.get("open"), Some(&1));
|
||||
assert_eq!(stats.get("resolved"), Some(&1));
|
||||
assert!(matches!(
|
||||
store.resolve(9999),
|
||||
Err(TrackerError::NotFound(9999))
|
||||
));
|
||||
|
||||
drop(store);
|
||||
let _ = fs::remove_file(&db);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn incident_cli_end_to_end() {
|
||||
let db = temp_db("cli");
|
||||
let add_json = run_cli(
|
||||
&db,
|
||||
&[
|
||||
"incident-tracker",
|
||||
"add",
|
||||
"API 502 spike",
|
||||
"--severity",
|
||||
"high",
|
||||
],
|
||||
)
|
||||
.unwrap();
|
||||
let created: Incident = serde_json::from_str(&add_json).unwrap();
|
||||
assert_eq!(created.severity, "high");
|
||||
|
||||
let stats_json = run_cli(&db, &["incident-tracker", "stats"]).unwrap();
|
||||
let stats: BTreeMap<String, usize> = serde_json::from_str(&stats_json).unwrap();
|
||||
assert_eq!(stats.get("open"), Some(&1));
|
||||
assert_eq!(stats.get("resolved"), Some(&0));
|
||||
|
||||
let _ = fs::remove_file(&db);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user