74 lines
2.3 KiB
Rust
74 lines
2.3 KiB
Rust
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<TriggerPush>,
|
|
}
|
|
|
|
/// 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<String>,
|
|
|
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
|
pub(crate) branches: Vec<String>,
|
|
|
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
|
pub(crate) skip_branches: Vec<String>,
|
|
}
|
|
|
|
impl RemoteConfig {
|
|
pub(crate) fn from_str<S: AsRef<str>>(
|
|
contents: S,
|
|
) -> Result<RemoteConfig, Box<dyn std::error::Error>> {
|
|
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<B: AsRef<str>>(
|
|
&self,
|
|
branch: B,
|
|
) -> Result<Vec<&TriggerPush>, Box<dyn std::error::Error>> {
|
|
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::<Result<_, _>>()?;
|
|
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::<Result<_, _>>()?;
|
|
if !push.branches.is_empty() && !match_regex.iter().any(|r| r.is_match(branch)) {
|
|
continue;
|
|
}
|
|
|
|
ret.push(push);
|
|
}
|
|
Ok(ret)
|
|
}
|
|
}
|