Add HeaderFilter for filter based on the header

This extends filtering to filter also on the received http header.
This commit is contained in:
finga 2021-11-17 15:13:12 +01:00
parent 0d9c5f650f
commit 8c9d9e63f2
2 changed files with 52 additions and 12 deletions

View file

@ -199,7 +199,7 @@ impl Hooks {
let mut data: serde_json::Value =
serde_json::from_slice(&buffer).map_err(WebhookeyError::Serde)?;
match hook.filter.evaluate(&data) {
match hook.filter.evaluate(request, &data) {
Ok(true) => match hook.get_command(hook_name, request, &mut data) {
Ok(command) => {
info!("Filter for `{}` matched", &hook_name);
@ -483,7 +483,7 @@ async fn main() -> Result<()> {
#[cfg(test)]
mod tests {
use super::*;
use crate::webhooks::{AddrType, JsonFilter};
use crate::webhooks::{AddrType, HeaderFilter, JsonFilter};
use rocket::{
http::{ContentType, Header},
local::asynchronous::Client,
@ -850,8 +850,8 @@ hooks:
- json:
pointer: /ref
regex: refs/heads/master
- json:
pointer: /after
- header:
field: X-Gitea-Signature
regex: f6e5fe4fe37df76629112d55cc210718b6a55e7e"#,
)
.unwrap();
@ -889,8 +889,8 @@ hooks:
pointer: "/ref".to_string(),
regex: "refs/heads/master".to_string(),
}),
FilterType::JsonFilter(JsonFilter {
pointer: "/after".to_string(),
FilterType::HeaderFilter(HeaderFilter {
field: "X-Gitea-Signature".to_string(),
regex: "f6e5fe4fe37df76629112d55cc210718b6a55e7e".to_string(),
}),
]),