Files
coa-server-manager/crates/coa-core/src/report.rs
T

178 lines
6.2 KiB
Rust

//! Problem-report destinations for the Manager, bot systems and upstream custom races.
use std::path::Path;
use serde::Serialize;
pub const MANAGER_REPO: &str = "Corfirean/coa-server-manager";
/// One place a report can be filed. `id` is what the page sends back; `repo` is `owner/name` on GitHub.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct Target {
pub id: &'static str,
pub repo: String,
/// The report form asks for the Manager's labels only where we own the repository.
pub ours: bool,
}
/// `https://github.com/owner/name/tree/main/x` -> `owner/name`.
fn repo_of(url: &str) -> Option<String> {
let rest = url.strip_prefix("https://github.com/")?;
let mut parts = rest.split('/');
let (owner, name) = (parts.next()?, parts.next()?);
let ok = |s: &str| {
!s.is_empty()
&& s.chars()
.all(|c| c.is_ascii_alphanumeric() || "._-".contains(c))
};
(ok(owner) && ok(name)).then(|| format!("{owner}/{name}"))
}
fn catalog_repo(id: &str) -> Option<String> {
crate::modules::catalog()
.into_iter()
.find(|e| e.id == id)
.and_then(|e| repo_of(&e.repo))
}
/// Report destinations in display order. Custom-race reports go to the upstream asset maintainer.
pub fn targets() -> Vec<Target> {
let mut v = vec![Target {
id: "manager",
repo: MANAGER_REPO.into(),
ours: true,
}];
for (id, module) in [("companions", "companions"), ("squid", "playerbots"), ("content-scaling", "content-scaling")] {
if let Some(repo) = catalog_repo(module) {
v.push(Target {
id,
repo: repo.clone(),
ours: repo.starts_with("Corfirean/"),
});
}
}
v.push(Target {
id: "custom-races",
repo: "ilusixn/azerothcore-wotlk-coa".into(),
ours: false,
});
v
}
pub fn target(id: &str) -> Option<Target> {
targets().into_iter().find(|t| t.id == id)
}
/// True for the "new issue" page of one of the targets: exactly that page, optionally with a query.
pub fn is_new_issue_url(url: &str) -> bool {
targets().iter().any(|t| {
let base = format!("https://github.com/{}/issues/new", t.repo);
url == base || url.starts_with(&format!("{base}?"))
})
}
/// What the report form fills in about the bot systems of this server.
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct Bots {
/// The target the form should start with: the bot system that is switched on, else the Manager.
pub suggested: &'static str,
/// The release of SQUID Playerbots, when it is the one switched on.
pub squid_version: Option<String>,
}
pub fn bots(root: &Path) -> Bots {
let list = crate::modules::list(root);
let on = |id: &str| {
list.iter()
.find(|m| m.id == id)
.filter(|m| m.installed && m.enabled)
};
let squid = on("playerbots");
Bots {
suggested: if squid.is_some() {
"squid"
} else if on("companions").is_some() {
"companions"
} else {
"manager"
},
squid_version: squid
.and_then(|m| m.version.clone())
.filter(|v| !v.is_empty()),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn report_targets_route_to_their_maintainers() {
let t = targets();
let repo = |id: &str| t.iter().find(|x| x.id == id).map(|x| x.repo.clone());
assert_eq!(
repo("manager").as_deref(),
Some("Corfirean/coa-server-manager")
);
assert_eq!(
repo("companions").as_deref(),
Some("Corfirean/mod-coa-playerbots")
);
assert_eq!(repo("squid").as_deref(), Some("Zyth45/mod-playerbots"));
assert!(t.iter().find(|x| x.id == "squid").is_some_and(|x| !x.ours));
assert_eq!(repo("custom-races").as_deref(), Some("ilusixn/azerothcore-wotlk-coa"));
assert!(t.iter().find(|x| x.id == "custom-races").is_some_and(|x| !x.ours));
assert_eq!(repo("content-scaling").as_deref(), Some("Corfirean/mod-coa-content-scaling"));
assert!(t.iter().find(|x| x.id == "content-scaling").is_some_and(|x| x.ours));
}
#[test]
fn repository_names_are_read_from_module_links() {
assert_eq!(
repo_of("https://github.com/Corfirean/azerothcore-wotlk-coa/tree/coa-bots/modules/x")
.as_deref(),
Some("Corfirean/azerothcore-wotlk-coa")
);
assert_eq!(
repo_of("https://github.com/Zyth45/mod-playerbots").as_deref(),
Some("Zyth45/mod-playerbots")
);
assert_eq!(repo_of("https://example.com/a/b"), None);
assert_eq!(repo_of("https://github.com/a"), None);
}
#[test]
fn only_the_new_issue_pages_of_the_targets_are_allowed() {
assert!(is_new_issue_url("https://github.com/Corfirean/mod-coa-content-scaling/issues/new?title=a&body=b"));
assert!(is_new_issue_url("https://github.com/ilusixn/azerothcore-wotlk-coa/issues/new?title=a&body=b"));
assert!(!is_new_issue_url("https://github.com/ilusixn/azerothcore-wotlk-coa/issues/newer"));
assert!(is_new_issue_url(
"https://github.com/Zyth45/mod-playerbots/issues/new"
));
assert!(is_new_issue_url(
"https://github.com/Corfirean/mod-coa-playerbots/issues/new?title=a&body=b"
));
assert!(!is_new_issue_url(
"https://github.com/Zyth45/mod-playerbots/issues/newer"
));
assert!(!is_new_issue_url(
"https://github.com/Zyth45/other/issues/new"
));
assert!(!is_new_issue_url(
"https://evil.example/Corfirean/coa-server-manager/issues/new"
));
}
#[test]
fn the_suggested_target_follows_the_bot_system_that_is_on() {
let d = tempfile::tempdir().unwrap();
let root = d.path().join("srv");
crate::layout::testkit::fake_repack(&root);
let conf = root.join("Core/configs/modules");
std::fs::write(conf.join("mod_coa_playerbots.conf"), "CoaBots.Enable = 1\n").unwrap();
assert_eq!(bots(&root).suggested, "companions");
std::fs::write(conf.join("mod_coa_playerbots.conf"), "CoaBots.Enable = 0\n").unwrap();
assert_eq!(bots(&root).suggested, "manager");
}
}