Add Not
filter to FilterType
To invert the result of filters.
This commit is contained in:
parent
4d39488c32
commit
9c423b8dc8
2 changed files with 19 additions and 2 deletions
19
src/main.rs
19
src/main.rs
|
@ -679,7 +679,7 @@ mod tests {
|
||||||
let mut hooks = BTreeMap::new();
|
let mut hooks = BTreeMap::new();
|
||||||
|
|
||||||
hooks.insert(
|
hooks.insert(
|
||||||
"test_hook".to_string(),
|
"test_hook0".to_string(),
|
||||||
Hook {
|
Hook {
|
||||||
command:
|
command:
|
||||||
"/usr/bin/echo {{ /repository/full_name }} --foo {{ /pull_request/base/ref }}"
|
"/usr/bin/echo {{ /repository/full_name }} --foo {{ /pull_request/base/ref }}"
|
||||||
|
@ -695,7 +695,7 @@ mod tests {
|
||||||
);
|
);
|
||||||
|
|
||||||
hooks.insert(
|
hooks.insert(
|
||||||
"test_hook".to_string(),
|
"test_hook2".to_string(),
|
||||||
Hook {
|
Hook {
|
||||||
command: "/usr/bin/echo {{ /repository/full_name }} {{ /pull_request/base/ref }}"
|
command: "/usr/bin/echo {{ /repository/full_name }} {{ /pull_request/base/ref }}"
|
||||||
.to_string(),
|
.to_string(),
|
||||||
|
@ -709,6 +709,21 @@ mod tests {
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
hooks.insert(
|
||||||
|
"test_hook3".to_string(),
|
||||||
|
Hook {
|
||||||
|
command: "/usr/bin/echo {{ /repository/full_name }} {{ /pull_request/base/ref }}"
|
||||||
|
.to_string(),
|
||||||
|
signature: "X-Gitea-Signature".to_string(),
|
||||||
|
ip_filter: None,
|
||||||
|
secrets: vec!["valid".to_string()],
|
||||||
|
filter: FilterType::Not(Box::new(FilterType::JsonFilter(JsonFilter {
|
||||||
|
pointer: "/foobar".to_string(),
|
||||||
|
regex: "bar".to_string(),
|
||||||
|
}))),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
let config = Config {
|
let config = Config {
|
||||||
metrics: None,
|
metrics: None,
|
||||||
hooks: hooks,
|
hooks: hooks,
|
||||||
|
|
|
@ -93,6 +93,7 @@ impl JsonFilter {
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
#[serde(deny_unknown_fields, rename_all = "lowercase")]
|
#[serde(deny_unknown_fields, rename_all = "lowercase")]
|
||||||
pub enum FilterType {
|
pub enum FilterType {
|
||||||
|
Not(Box<FilterType>),
|
||||||
And(Vec<FilterType>),
|
And(Vec<FilterType>),
|
||||||
Or(Vec<FilterType>),
|
Or(Vec<FilterType>),
|
||||||
#[serde(rename = "json")]
|
#[serde(rename = "json")]
|
||||||
|
@ -102,6 +103,7 @@ pub enum FilterType {
|
||||||
impl FilterType {
|
impl FilterType {
|
||||||
pub fn evaluate(&self, data: &serde_json::Value) -> Result<bool, WebhookeyError> {
|
pub fn evaluate(&self, data: &serde_json::Value) -> Result<bool, WebhookeyError> {
|
||||||
match self {
|
match self {
|
||||||
|
FilterType::Not(filter) => Ok(!filter.evaluate(data)?),
|
||||||
FilterType::And(filters) => {
|
FilterType::And(filters) => {
|
||||||
let (mut results, mut errors) = (Vec::new(), Vec::new());
|
let (mut results, mut errors) = (Vec::new(), Vec::new());
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue