Skip to content
Clone
b90f48e
134 lines 3.4 KB Rust
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RefUpdate {
pub name: String,
pub old_oid: String,
pub new_oid: String,
}
impl RefUpdate {
#[must_use]
pub fn is_delete(&self) -> bool {
self.new_oid.is_empty() || self.new_oid.chars().all(|c| c == '0')
}
#[must_use]
pub fn ref_kind(&self) -> Option<&'static str> {
if self.name.starts_with("refs/tags/") {
Some("tag")
} else if self.name.starts_with("refs/heads/") {
Some("branch")
} else {
None
}
}
#[must_use]
pub fn short_name(&self) -> &str {
self.name
.strip_prefix("refs/heads/")
.or_else(|| self.name.strip_prefix("refs/tags/"))
.unwrap_or(self.name.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PullRequestAction {
Opened,
Synchronize,
Reopened,
Merged,
Closed,
}
impl PullRequestAction {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Opened => "opened",
Self::Synchronize => "synchronize",
Self::Reopened => "reopened",
Self::Merged => "merged",
Self::Closed => "closed",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum CiEvent {
Push(PushEvent),
PullRequest(PullRequestEvent),
Trigger(TriggerEvent),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PushEvent {
pub id: Uuid,
pub repo_id: Uuid,
pub space_id: Uuid,
pub actor_id: Option<Uuid>,
pub refs: Vec<RefUpdate>,
/// Producing trace context, carried across the queue to webhook consumers.
#[serde(default)]
pub trace: Option<String>,
#[serde(default)]
pub request_id: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct PullRequestEvent {
pub id: Uuid,
pub repo_id: Uuid,
pub space_id: Uuid,
pub actor_id: Option<Uuid>,
pub number: i64,
pub action: PullRequestAction,
pub head_sha: String,
pub head_ref: String,
pub base_sha: String,
pub base_ref: String,
#[serde(default)]
pub head_repo_id: Option<Uuid>,
#[serde(default)]
pub trace: Option<String>,
#[serde(default)]
pub request_id: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct TriggerEvent {
pub id: Uuid,
pub repo_id: Uuid,
pub space_id: Uuid,
pub actor_id: Option<Uuid>,
pub event: String,
pub payload: serde_json::Value,
#[serde(default)]
pub trace: Option<String>,
#[serde(default)]
pub request_id: Option<String>,
}
impl CiEvent {
#[must_use]
pub fn trace(&self) -> Option<&str> {
match self {
Self::Push(event) => event.trace.as_deref(),
Self::PullRequest(event) => event.trace.as_deref(),
Self::Trigger(event) => event.trace.as_deref(),
}
}
#[must_use]
pub fn request_id(&self) -> Option<&str> {
match self {
Self::Push(event) => event.request_id.as_deref(),
Self::PullRequest(event) => event.request_id.as_deref(),
Self::Trigger(event) => event.request_id.as_deref(),
}
}
}