// bebot // Copyright (C) 2023 Brian Tarricone // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . use std::{collections::HashMap, fs::File, io::BufReader, path::PathBuf}; use anyhow::Context; use matrix_sdk::ruma::{OwnedRoomOrAliasId, OwnedUserId}; use regex::Regex; use crate::gitlab_event::{IssueAction, MergeRequestAction, PipelineStatus}; #[derive(Deserialize)] #[serde(tag = "name", rename_all = "snake_case")] pub enum PublishEvent { Push { #[serde(default)] #[serde(with = "serde_regex")] branches: Option>, }, TagPush, Issues { actions: Option>, }, MergeRequest { actions: Option>, }, Pipeline { statuses: Option>, }, } #[derive(Deserialize)] pub struct RepoConfig { pub token: String, #[serde(default)] #[serde(deserialize_with = "crate::matrix::deser_optional_room_or_alias_id")] pub room: Option, pub publish_events: Option>, } #[derive(Deserialize)] pub struct GitlabWebhookConfig { pub url_prefix: Option, #[serde(default)] #[serde(deserialize_with = "crate::matrix::deser_optional_room_or_alias_id")] pub default_room: Option, pub default_publish_events: Option>, pub repo_configs: HashMap, // key is repo url without scheme; e.g. // gitlab.xfce.org/xfce/xfdesktop } #[derive(Clone, Deserialize)] pub struct MailListConfig { pub name: String, #[serde(default)] pub rooms: Vec, } #[derive(Deserialize)] pub struct MailArchiveConfig { #[serde(default)] pub default_rooms: Vec, pub update_interval: u64, // seconds pub state_dir: PathBuf, pub lists: Vec, } #[derive(Deserialize)] pub struct Config { pub bind_address: Option, pub bind_port: Option, #[serde(deserialize_with = "crate::matrix::deser_user_id")] pub user_id: OwnedUserId, pub password: String, pub gitlab_webhook: Option, pub mail_archive: Option, } fn load_blocking(path: &String) -> anyhow::Result { let f = File::open(path)?; let r = BufReader::new(f); let config: Config = serde_yaml::from_reader(r)?; Ok(config) } pub async fn load>(path: S) -> anyhow::Result { let p = String::from(path.as_ref()); let config = tokio::task::spawn_blocking(move || { load_blocking(&p).with_context(|| format!("Failed to load config from {}", p)) }) .await??; Ok(config) }