use regex::Regex; use serde::Deserialize; use serde::Serialize; /// The webhook_bridge.toml file that lives inside repos that have their CI triggered by webhook_bridge. #[derive(Serialize, Deserialize, Clone, Debug)] #[serde(deny_unknown_fields)] pub(crate) struct RemoteConfig { pub(crate) version: String, #[serde(default, skip_serializing_if = "Vec::is_empty")] pub(crate) push: Vec, } /// A config for a job that is triggered by a push to a git repo. #[derive(Serialize, Deserialize, Clone, Debug)] #[serde(deny_unknown_fields)] pub(crate) struct TriggerPush { pub(crate) name: String, pub(crate) source: String, #[serde(default, skip_serializing_if = "Option::is_none")] pub(crate) clone_uri: Option, #[serde(default, skip_serializing_if = "Vec::is_empty")] pub(crate) branches: Vec, #[serde(default, skip_serializing_if = "Vec::is_empty")] pub(crate) skip_branches: Vec, } impl RemoteConfig { pub(crate) fn from_str>( contents: S, ) -> Result> { let parsed_remote_config: RemoteConfig = toml::from_str(contents.as_ref())?; assert!( parsed_remote_config.version == "0.0.1", "We only support version 0.0.1 currently." ); Ok(parsed_remote_config) } pub(crate) fn get_push_triggers_for_branch>( &self, branch: B, ) -> Result, Box> { let branch = branch.as_ref(); let mut ret = Vec::new(); for push in &self.push { let skip_regex: Vec<_> = push .skip_branches .iter() .map(|s| Regex::new(s.as_str())) .collect::>()?; if skip_regex.iter().any(|r| r.is_match(branch)) { continue; } let match_regex: Vec<_> = push .branches .iter() .map(|s| Regex::new(s.as_str())) .collect::>()?; if !push.branches.is_empty() && !match_regex.iter().any(|r| r.is_match(branch)) { continue; } ret.push(push); } Ok(ret) } }