Skip to main content

cac_webhook/
config.rs

1use std::path::PathBuf;
2
3#[derive(Debug, Clone)]
4pub struct WebhookConfig {
5    pub bind_addr: String,
6    pub webhook_secret: String,
7    pub policies_dir: PathBuf,
8    pub work_dir: PathBuf,
9    pub signing_key: Option<String>,
10    pub github_token: Option<String>,
11    pub codeberg_token: Option<String>,
12    pub auto_fix_pr: bool,
13    pub status_context: String,
14    pub public_url: Option<String>,
15}
16
17impl WebhookConfig {
18    pub fn from_env(policies_dir: PathBuf) -> Self {
19        Self {
20            bind_addr: std::env::var("CAC_BIND_ADDR").unwrap_or_else(|_| "0.0.0.0:8080".into()),
21            webhook_secret: std::env::var("CAC_WEBHOOK_SECRET")
22                .unwrap_or_else(|_| "change-me".into()),
23            policies_dir,
24            work_dir: std::env::var("CAC_WORK_DIR")
25                .map(PathBuf::from)
26                .unwrap_or_else(|_| std::env::temp_dir().join("cac-webhook")),
27            signing_key: std::env::var("CAC_LEDGER_SIGNING_KEY").ok(),
28            github_token: std::env::var("CAC_GITHUB_TOKEN").ok(),
29            codeberg_token: std::env::var("CAC_CODEBERG_TOKEN").ok(),
30            auto_fix_pr: std::env::var("CAC_AUTO_FIX_PR")
31                .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
32                .unwrap_or(false),
33            status_context: std::env::var("CAC_STATUS_CONTEXT")
34                .unwrap_or_else(|_| "compliance-as-code/scan".into()),
35            public_url: std::env::var("CAC_PUBLIC_URL").ok(),
36        }
37    }
38}