bebot/src/config.rs

107 lines
3.3 KiB
Rust

// bebot
// Copyright (C) 2023 Brian Tarricone <brian@tarricone.org>
//
// 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 <http://www.gnu.org/licenses/>.
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<Vec<Regex>>,
},
TagPush,
Issues {
actions: Option<Vec<IssueAction>>,
},
MergeRequest {
actions: Option<Vec<MergeRequestAction>>,
},
Pipeline {
statuses: Option<Vec<PipelineStatus>>,
},
}
#[derive(Deserialize)]
pub struct RepoConfig {
pub token: String,
#[serde(default)]
#[serde(deserialize_with = "crate::matrix::deser_optional_room_or_alias_id")]
pub room: Option<OwnedRoomOrAliasId>,
pub publish_events: Option<Vec<PublishEvent>>,
}
#[derive(Deserialize)]
pub struct GitlabWebhookConfig {
pub url_prefix: Option<String>,
#[serde(default)]
#[serde(deserialize_with = "crate::matrix::deser_optional_room_or_alias_id")]
pub default_room: Option<OwnedRoomOrAliasId>,
pub default_publish_events: Option<Vec<PublishEvent>>,
pub repo_configs: HashMap<String, RepoConfig>, // 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<OwnedRoomOrAliasId>,
}
#[derive(Deserialize)]
pub struct MailArchiveConfig {
#[serde(default)]
pub default_rooms: Vec<OwnedRoomOrAliasId>,
pub update_interval: u64, // seconds
pub state_dir: PathBuf,
pub lists: Vec<MailListConfig>,
}
#[derive(Deserialize)]
pub struct Config {
pub bind_address: Option<String>,
pub bind_port: Option<u16>,
#[serde(deserialize_with = "crate::matrix::deser_user_id")]
pub user_id: OwnedUserId,
pub password: String,
pub gitlab_webhook: Option<GitlabWebhookConfig>,
pub mail_archive: Option<MailArchiveConfig>,
}
fn load_blocking(path: &String) -> anyhow::Result<Config> {
let f = File::open(path)?;
let r = BufReader::new(f);
let config: Config = serde_yaml::from_reader(r)?;
Ok(config)
}
pub async fn load<S: AsRef<str>>(path: S) -> anyhow::Result<Config> {
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)
}