176 lines
6.3 KiB
Rust
176 lines
6.3 KiB
Rust
//! What a system/org webhook delivery from a managed Gitea asks for.
|
|
|
|
use super::gitea::GiteaRepo;
|
|
|
|
/// The action a delivery maps to.
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub enum SourceAction {
|
|
/// A repository was created: make it a target, scan it unless it is empty.
|
|
Created(GiteaRepo),
|
|
/// A repository was deleted: archive its target.
|
|
Deleted(GiteaRepo),
|
|
/// A push to the default branch: scan the target, creating it first if
|
|
/// the `created` delivery was lost.
|
|
Push(GiteaRepo),
|
|
/// Nothing to do, and why.
|
|
Ignore(&'static str),
|
|
}
|
|
|
|
impl SourceAction {
|
|
/// The repository the action is about; `None` for [`Self::Ignore`].
|
|
pub fn repo(&self) -> Option<&GiteaRepo> {
|
|
match self {
|
|
Self::Created(repo) | Self::Deleted(repo) | Self::Push(repo) => Some(repo),
|
|
Self::Ignore(_) => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A branch deletion pushes this as the new head.
|
|
const ZERO_SHA: &str = "0000000000000000000000000000000000000000";
|
|
|
|
/// Route a delivery: `event` is the `X-Gitea-Event` header, `org` the source's
|
|
/// organisation. Repositories of any other owner are ignored, so a system
|
|
/// webhook (which sees the whole instance) is safe to use.
|
|
pub fn route_event(event: &str, payload: &serde_json::Value, org: &str) -> SourceAction {
|
|
let Some(repo) = payload
|
|
.get("repository")
|
|
.cloned()
|
|
.and_then(|r| serde_json::from_value::<GiteaRepo>(r).ok())
|
|
else {
|
|
return match event {
|
|
"repository" | "push" => SourceAction::Ignore("payload without a repository"),
|
|
_ => SourceAction::Ignore("event not handled"),
|
|
};
|
|
};
|
|
if !matches!(event, "repository" | "push") {
|
|
return SourceAction::Ignore("event not handled");
|
|
}
|
|
if !repo.in_org(org) {
|
|
return SourceAction::Ignore("repository outside the configured organisation");
|
|
}
|
|
match event {
|
|
"repository" => match payload.get("action").and_then(|a| a.as_str()) {
|
|
Some("created") => SourceAction::Created(repo),
|
|
Some("deleted") => SourceAction::Deleted(repo),
|
|
_ => SourceAction::Ignore("repository action not handled"),
|
|
},
|
|
_ => {
|
|
let git_ref = payload.get("ref").and_then(|r| r.as_str()).unwrap_or("");
|
|
let after = payload.get("after").and_then(|a| a.as_str()).unwrap_or("");
|
|
if after == ZERO_SHA {
|
|
SourceAction::Ignore("branch deleted")
|
|
} else if git_ref != format!("refs/heads/{}", repo.branch()) {
|
|
SourceAction::Ignore("push to another branch than the default")
|
|
} else {
|
|
SourceAction::Push(repo)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[allow(clippy::expect_used, clippy::unwrap_used)]
|
|
mod tests {
|
|
use serde_json::json;
|
|
|
|
use super::*;
|
|
|
|
fn repository(owner: &str) -> serde_json::Value {
|
|
json!({
|
|
"id": 42, "name": "fw", "full_name": format!("{owner}/fw"),
|
|
"owner": { "login": owner }, "default_branch": "main", "empty": true,
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn an_action_names_its_repository_unless_ignored() {
|
|
let p = json!({ "action": "created", "repository": repository("kunde") });
|
|
assert_eq!(
|
|
route_event("repository", &p, "kunde").repo().map(|r| r.id),
|
|
Some(42)
|
|
);
|
|
assert!(SourceAction::Ignore("x").repo().is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn created_and_deleted() {
|
|
let p = json!({ "action": "created", "repository": repository("kunde") });
|
|
match route_event("repository", &p, "kunde") {
|
|
SourceAction::Created(r) => {
|
|
assert_eq!(
|
|
(r.id, r.full_name.as_str(), r.empty),
|
|
(42, "kunde/fw", true)
|
|
);
|
|
}
|
|
other => panic!("{other:?}"),
|
|
}
|
|
let p = json!({ "action": "deleted", "repository": repository("kunde") });
|
|
assert!(matches!(
|
|
route_event("repository", &p, "kunde"),
|
|
SourceAction::Deleted(r) if r.id == 42
|
|
));
|
|
let p = json!({ "action": "renamed", "repository": repository("kunde") });
|
|
assert!(matches!(
|
|
route_event("repository", &p, "kunde"),
|
|
SourceAction::Ignore(_)
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn push_to_the_default_branch_only() {
|
|
let push = |git_ref: &str, after: &str| json!({ "ref": git_ref, "after": after, "repository": repository("kunde") });
|
|
assert!(matches!(
|
|
route_event("push", &push("refs/heads/main", "abc"), "kunde"),
|
|
SourceAction::Push(r) if r.id == 42
|
|
));
|
|
assert_eq!(
|
|
route_event("push", &push("refs/heads/feature", "abc"), "kunde"),
|
|
SourceAction::Ignore("push to another branch than the default")
|
|
);
|
|
assert_eq!(
|
|
route_event("push", &push("refs/tags/v1", "abc"), "kunde"),
|
|
SourceAction::Ignore("push to another branch than the default")
|
|
);
|
|
assert_eq!(
|
|
route_event("push", &push("refs/heads/main", ZERO_SHA), "kunde"),
|
|
SourceAction::Ignore("branch deleted")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn other_orgs_are_ignored_case_insensitively_matched() {
|
|
let p = json!({ "action": "created", "repository": repository("andere") });
|
|
assert_eq!(
|
|
route_event("repository", &p, "kunde"),
|
|
SourceAction::Ignore("repository outside the configured organisation")
|
|
);
|
|
let p = json!({ "action": "created", "repository": repository("Kunde") });
|
|
assert!(matches!(
|
|
route_event("repository", &p, "kunde"),
|
|
SourceAction::Created(_)
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn unknown_events_and_bad_payloads_are_ignored() {
|
|
let p = json!({ "action": "opened", "repository": repository("kunde") });
|
|
assert_eq!(
|
|
route_event("pull_request", &p, "kunde"),
|
|
SourceAction::Ignore("event not handled")
|
|
);
|
|
assert_eq!(
|
|
route_event("issues", &json!({}), "kunde"),
|
|
SourceAction::Ignore("event not handled")
|
|
);
|
|
assert_eq!(
|
|
route_event("push", &json!({ "ref": "refs/heads/main" }), "kunde"),
|
|
SourceAction::Ignore("payload without a repository")
|
|
);
|
|
assert_eq!(
|
|
route_event("", &json!({ "repository": repository("kunde") }), "kunde"),
|
|
SourceAction::Ignore("event not handled")
|
|
);
|
|
}
|
|
}
|