arks_server/
push_policy.rs1use arks_oid::Oid;
12use arks_repo::Repository;
13use std::sync::{Arc, OnceLock, RwLock};
14
15#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct RefUpdate {
18 pub name: String,
19 pub old: Oid,
20 pub new: Oid,
21}
22
23impl RefUpdate {
24 pub fn is_create(&self) -> bool {
25 self.old.as_bytes().iter().all(|b| *b == 0)
26 }
27
28 pub fn is_delete(&self) -> bool {
29 self.new.as_bytes().iter().all(|b| *b == 0)
30 }
31}
32
33#[derive(Debug, Clone, Default, PartialEq, Eq)]
35pub struct Pusher {
36 pub transport: &'static str,
38 pub verified_user: Option<String>,
40 pub authorization: Option<String>,
42}
43
44impl Pusher {
45 pub fn local() -> Self {
46 Self {
47 transport: "local",
48 ..Self::default()
49 }
50 }
51
52 pub fn ssh() -> Self {
53 Self {
54 transport: "ssh",
55 ..Self::default()
56 }
57 }
58}
59
60pub trait PushPolicy: Send + Sync {
62 fn check(
64 &self,
65 repo: &Repository,
66 pusher: &Pusher,
67 updates: &[RefUpdate],
68 ) -> Vec<Option<String>>;
69}
70
71fn slot() -> &'static RwLock<Option<Arc<dyn PushPolicy>>> {
72 static SLOT: OnceLock<RwLock<Option<Arc<dyn PushPolicy>>>> = OnceLock::new();
73 SLOT.get_or_init(|| RwLock::new(None))
74}
75
76pub fn install_push_policy(policy: Option<Arc<dyn PushPolicy>>) {
78 if let Ok(mut g) = slot().write() {
79 *g = policy;
80 }
81}
82
83pub fn installed_push_policy() -> Option<Arc<dyn PushPolicy>> {
85 slot().read().ok().and_then(|g| g.clone())
86}
87
88pub(crate) fn verdicts(
90 policy: Option<&dyn PushPolicy>,
91 repo: &Repository,
92 pusher: &Pusher,
93 updates: &[RefUpdate],
94) -> Vec<Option<String>> {
95 let Some(policy) = policy else {
96 return vec![None; updates.len()];
97 };
98 let out = policy.check(repo, pusher, updates);
99 if out.len() != updates.len() {
100 return vec![Some("push policy error".into()); updates.len()];
101 }
102 out.into_iter()
103 .map(|v| v.map(|r| r.replace(['\n', '\r'], " ")))
104 .collect()
105}