132 lines
4.8 KiB
Rust
132 lines
4.8 KiB
Rust
//! The recent webhook deliveries of a tenant, as the target page shows them.
|
|
//!
|
|
//! A webhook answers its sender before the work is done (the managed Gitea
|
|
//! source answers `202` and decides about entitlement in the background), so
|
|
//! the sender's delivery log cannot tell a refused delivery from a handled
|
|
//! one. Each verified delivery that asks for work is recorded here with what
|
|
//! became of it.
|
|
|
|
use chrono::{DateTime, Utc};
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use super::git_source::SourceRepo;
|
|
|
|
/// Deliveries shown per target.
|
|
pub const DELIVERIES_SHOWN: i64 = 20;
|
|
|
|
/// The longest `detail` stored, in characters: a refusal reason or an error
|
|
/// fits, a runaway error message does not bloat the collection.
|
|
pub const MAX_DETAIL_CHARS: usize = 300;
|
|
|
|
/// What became of a delivery.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum DeliveryResult {
|
|
/// The work was done or queued.
|
|
Handled,
|
|
/// Not done on purpose: no entitlement, a read-only tenant.
|
|
Refused,
|
|
/// Not done because something broke: the database, the git source.
|
|
Failed,
|
|
}
|
|
|
|
/// One webhook delivery.
|
|
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
|
pub struct WebhookDelivery {
|
|
#[serde(rename = "_id", default, skip_serializing_if = "Option::is_none")]
|
|
pub id: Option<bson::oid::ObjectId>,
|
|
/// When the delivery arrived.
|
|
#[serde(with = "super::serde_helpers::bson_datetime")]
|
|
pub received_at: DateTime<Utc>,
|
|
/// The receiver: `gitea-source`, `gitea`, `github` or `gitlab`.
|
|
pub source: String,
|
|
/// The sender's event name (`push`, `repository`, `pull_request`, ...).
|
|
pub event: String,
|
|
pub result: DeliveryResult,
|
|
/// What was done, or why not.
|
|
#[serde(default)]
|
|
pub detail: String,
|
|
/// How long the handling took (background work included).
|
|
#[serde(default)]
|
|
pub elapsed_ms: u64,
|
|
/// The target, when it is known.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub target_id: Option<String>,
|
|
/// The repository of a managed git source. A delivery refused before its
|
|
/// target exists has only this; the target shows it once it does.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub source_repo: Option<SourceRepo>,
|
|
}
|
|
|
|
impl WebhookDelivery {
|
|
/// A delivery received now; `detail` is cut to [`MAX_DETAIL_CHARS`].
|
|
pub fn new(source: &str, event: &str, result: DeliveryResult, detail: &str) -> Self {
|
|
Self {
|
|
id: None,
|
|
received_at: Utc::now(),
|
|
source: source.to_string(),
|
|
event: event.to_string(),
|
|
result,
|
|
detail: clip(detail, MAX_DETAIL_CHARS),
|
|
elapsed_ms: 0,
|
|
target_id: None,
|
|
source_repo: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// `text` cut to at most `max` characters (never inside a character), with
|
|
/// an ellipsis when it was cut.
|
|
fn clip(text: &str, max: usize) -> String {
|
|
match text.char_indices().nth(max) {
|
|
// `nth(max)` is the first character past the limit: keep max - 1 and
|
|
// the ellipsis, so the result is exactly `max` characters.
|
|
Some(_) => {
|
|
let keep: String = text.chars().take(max.saturating_sub(1)).collect();
|
|
format!("{keep}…")
|
|
}
|
|
None => text.to_string(),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[allow(clippy::expect_used, clippy::unwrap_used)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn a_short_detail_is_kept_as_is() {
|
|
let d = WebhookDelivery::new("gitea", "push", DeliveryResult::Handled, "scan queued");
|
|
assert_eq!(d.detail, "scan queued");
|
|
assert_eq!(d.elapsed_ms, 0);
|
|
assert!(d.target_id.is_none() && d.source_repo.is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn a_long_detail_is_clipped_on_a_character_boundary() {
|
|
let long = "ä".repeat(MAX_DETAIL_CHARS + 50);
|
|
let d = WebhookDelivery::new("gitea", "push", DeliveryResult::Failed, &long);
|
|
assert_eq!(d.detail.chars().count(), MAX_DETAIL_CHARS);
|
|
assert!(d.detail.ends_with('…'));
|
|
// exactly at the limit: nothing cut
|
|
let exact = "x".repeat(MAX_DETAIL_CHARS);
|
|
assert_eq!(clip(&exact, MAX_DETAIL_CHARS), exact);
|
|
}
|
|
|
|
#[test]
|
|
fn results_are_snake_case_and_round_trip() {
|
|
let mut d = WebhookDelivery::new("gitea-source", "push", DeliveryResult::Refused, "no");
|
|
d.source_repo = Some(SourceRepo {
|
|
base_url: "https://git.example".into(),
|
|
repo_id: 7,
|
|
full_name: "acme/fw".into(),
|
|
});
|
|
let json = serde_json::to_value(&d).unwrap();
|
|
assert_eq!(json["result"], "refused");
|
|
assert!(json.get("target_id").is_none());
|
|
let back: WebhookDelivery = serde_json::from_value(json).unwrap();
|
|
assert_eq!(back.source_repo, d.source_repo);
|
|
assert_eq!(back.result, DeliveryResult::Refused);
|
|
}
|
|
}
|