1use std::path::Path;
5
6use anyhow::Result;
7use chrono::{DateTime, Utc};
8use serde::{Deserialize, Serialize};
9use uuid::Uuid;
10
11#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
14#[serde(rename_all = "snake_case")]
15pub enum ScanScheduleKind {
16 Webhook,
17 Poll,
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
21pub enum ScanScheduleProvider {
22 #[serde(rename = "github")]
23 GitHub,
24 #[serde(rename = "gitlab")]
25 GitLab,
26 #[serde(rename = "bitbucket")]
27 Bitbucket,
28 #[serde(rename = "any")]
29 Any,
30}
31
32impl ScanScheduleProvider {
33 #[must_use]
34 pub const fn display_name(&self) -> &'static str {
35 match self {
36 Self::GitHub => "GitHub",
37 Self::GitLab => "GitLab",
38 Self::Bitbucket => "Bitbucket",
39 Self::Any => "Any / Poll",
40 }
41 }
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
47pub struct ScanSchedule {
48 pub id: Uuid,
49 pub label: String,
50 pub repo_url: String,
51 pub branch: String,
52 pub kind: ScanScheduleKind,
53 pub provider: ScanScheduleProvider,
54 #[serde(skip_serializing_if = "Option::is_none")]
55 pub webhook_secret: Option<String>,
56 #[serde(skip_serializing_if = "Option::is_none")]
57 pub interval_secs: Option<u64>,
58 pub last_scan_sha: Option<String>,
59 pub last_scan_at: Option<DateTime<Utc>>,
60 pub last_run_id: Option<String>,
61 pub enabled: bool,
62}
63
64impl ScanSchedule {
65 #[must_use]
66 pub fn new_webhook(
67 repo_url: String,
68 branch: String,
69 provider: ScanScheduleProvider,
70 label: String,
71 ) -> Self {
72 Self {
73 id: Uuid::new_v4(),
74 label,
75 repo_url,
76 branch,
77 kind: ScanScheduleKind::Webhook,
78 provider,
79 webhook_secret: Some(generate_secret()),
80 interval_secs: None,
81 last_scan_sha: None,
82 last_scan_at: None,
83 last_run_id: None,
84 enabled: true,
85 }
86 }
87
88 #[must_use]
89 pub fn new_poll(repo_url: String, branch: String, interval_secs: u64, label: String) -> Self {
90 Self {
91 id: Uuid::new_v4(),
92 label,
93 repo_url,
94 branch,
95 kind: ScanScheduleKind::Poll,
96 provider: ScanScheduleProvider::Any,
97 webhook_secret: None,
98 interval_secs: Some(interval_secs),
99 last_scan_sha: None,
100 last_scan_at: None,
101 last_run_id: None,
102 enabled: true,
103 }
104 }
105}
106
107fn generate_secret() -> String {
108 format!("{}-{}", Uuid::new_v4().simple(), Uuid::new_v4().simple())
109}
110
111#[derive(Debug, Default, Serialize, Deserialize)]
114pub struct ScheduleStore {
115 pub schedules: Vec<ScanSchedule>,
116}
117
118impl ScheduleStore {
119 #[must_use]
120 pub fn load(path: &Path) -> Self {
121 std::fs::read_to_string(path)
122 .ok()
123 .and_then(|s| serde_json::from_str(&s).ok())
124 .unwrap_or_default()
125 }
126
127 pub fn save(&self, path: &Path) -> Result<()> {
130 let json = serde_json::to_string_pretty(self)?;
131 std::fs::write(path, json)?;
132 Ok(())
133 }
134
135 #[must_use]
136 pub fn find_matching<'a>(&'a self, repo_url: &str, branch: &str) -> Vec<&'a ScanSchedule> {
137 self.schedules
138 .iter()
139 .filter(|s| s.enabled && urls_match(&s.repo_url, repo_url) && s.branch == branch)
140 .collect()
141 }
142
143 pub fn by_id_mut(&mut self, id: Uuid) -> Option<&mut ScanSchedule> {
144 self.schedules.iter_mut().find(|s| s.id == id)
145 }
146
147 pub fn remove(&mut self, id: Uuid) {
148 self.schedules.retain(|s| s.id != id);
149 }
150}
151
152fn urls_match(a: &str, b: &str) -> bool {
153 normalize_url(a) == normalize_url(b)
154}
155
156fn normalize_url(url: &str) -> String {
157 url.trim_end_matches('/')
158 .trim_end_matches(".git")
159 .to_lowercase()
160}