use gitpoulpi_config::{ApiFamily, Integration, IntegrationCategory};
use gitpoulpi_core::domain::rewrite::{HostKind, HostProtection};
use crate::client::HostingClient;
use crate::resolve::{resolve_for_repo, ResolveRemote};
use crate::spec::{spec_for, web_base};
use crate::Error;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ProtectedBranches {
pub patterns: Vec<String>,
pub note: Option<String>,
}
impl ProtectedBranches {
pub fn add(&mut self, name: &str) {
let name = name.trim();
let short = name.strip_prefix("refs/heads/").unwrap_or(name);
if !short.is_empty() && !self.patterns.iter().any(|p| p == short) {
self.patterns.push(short.to_string());
}
}
pub fn note(&mut self, text: &str) {
match &mut self.note {
Some(existing) => {
existing.push_str("; ");
existing.push_str(text);
}
None => self.note = Some(text.to_string()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ProtectionLookup {
Known(ProtectedBranches),
Unknown(String),
}
pub fn unknown_reason(error: &Error) -> String {
match error {
Error::Unauthorized { .. } => {
"the host rejected the token, so its branch protection could not be read".to_string()
}
Error::Forbidden { .. } => {
"the token lacks the scope to read the host's branch protection".to_string()
}
Error::Api { status: 404, .. } => "the token cannot see this repository's branch protection (it needs admin or maintainer access)"
.to_string(),
Error::Unsupported(_) | Error::NotImplemented(_) => {
"this host's client cannot read branch protection".to_string()
}
other => format!("the host's branch protection could not be read ({other})"),
}
}
pub async fn lookup(client: &dyn HostingClient, project: &str) -> ProtectionLookup {
match client.protected_branches(project).await {
Ok(found) => ProtectionLookup::Known(found),
Err(error) => ProtectionLookup::Unknown(unknown_reason(&error)),
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Located {
pub kind: HostKind,
pub integration: Option<Integration>,
pub web_base: String,
pub project: String,
pub remote: String,
pub linked: bool,
}
impl Located {
pub fn protection(&self, lookup: Option<ProtectionLookup>) -> HostProtection {
let mut out = HostProtection::new(self.kind, self.project.clone(), self.web_base.clone());
match lookup {
None => {}
Some(ProtectionLookup::Known(found)) => {
out.asked = true;
out.patterns = found.patterns;
out.unknown = found.note;
}
Some(ProtectionLookup::Unknown(why)) => {
out.asked = true;
out.unknown = Some(why);
}
}
out
}
}
fn host_kind(family: ApiFamily) -> HostKind {
match family {
ApiFamily::Github => HostKind::GitHub,
ApiFamily::Gitlab => HostKind::GitLab,
ApiFamily::AzureDevops => HostKind::AzureDevOps,
ApiFamily::Bitbucket => HostKind::Bitbucket,
_ => HostKind::Other,
}
}
fn family_of(kind: HostKind) -> Option<ApiFamily> {
match kind {
HostKind::GitHub => Some(ApiFamily::Github),
HostKind::GitLab => Some(ApiFamily::Gitlab),
HostKind::AzureDevOps => Some(ApiFamily::AzureDevops),
HostKind::Bitbucket => Some(ApiFamily::Bitbucket),
HostKind::Other => None,
}
}
fn is_hosting(integration: &Integration) -> bool {
integration.enabled && integration.kind.category() == IntegrationCategory::Hosting
}
pub fn locate(
remotes: &[ResolveRemote],
integrations: &[Integration],
link: Option<(&str, &str)>,
pinned: Option<&str>,
remote_hint: Option<&str>,
) -> Option<Located> {
if let Some((id, project)) = link {
if let Some(integration) = integrations.iter().find(|i| i.id == id && is_hosting(i)) {
let project = if project.is_empty() {
resolve_for_repo(remotes, std::slice::from_ref(integration), Some(id))
.map(|r| r.project_path)
.unwrap_or_default()
} else {
project.to_string()
};
if let (Some(base), false) = (web_base(integration), project.is_empty()) {
return Some(Located {
kind: host_kind(integration.kind.family()),
integration: Some(integration.clone()),
web_base: base,
project,
remote: remote_hint.unwrap_or_default().to_string(),
linked: true,
});
}
}
}
let ordered: Vec<ResolveRemote> = match remote_hint {
Some(hint) => remotes
.iter()
.filter(|r| r.name == hint)
.chain(remotes.iter().filter(|r| r.name != hint))
.cloned()
.collect(),
None => remotes.to_vec(),
};
if let Some(found) = resolve_for_repo(&ordered, integrations, pinned) {
if let Some(base) = web_base(found.integration) {
return Some(Located {
kind: host_kind(found.integration.kind.family()),
integration: Some(found.integration.clone()),
web_base: base,
project: found.project_path,
remote: found.remote,
linked: pinned.is_some_and(|p| p == found.integration.id),
});
}
}
for remote in &ordered {
let kind = HostKind::of_url(&remote.url);
let Some(family) = family_of(kind) else {
continue;
};
if let Some(parsed) = (spec_for(family).parse_remote)(&remote.url) {
return Some(Located {
kind,
integration: None,
web_base: parsed.web_base,
project: parsed.project,
remote: remote.name.clone(),
linked: false,
});
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use gitpoulpi_config::IntegrationKind;
fn remote(name: &str, url: &str) -> ResolveRemote {
ResolveRemote::new(name, url)
}
#[test]
fn an_unlinked_repository_is_located_from_its_remote_url_and_never_asked() {
let found = locate(
&[remote("origin", "git@github.com:octo/thing.git")],
&[],
None,
None,
None,
)
.unwrap();
assert_eq!(found.kind, HostKind::GitHub);
assert_eq!(found.web_base, "https://github.com");
assert_eq!(found.project, "octo/thing");
assert!(!found.linked, "no link: the host is not to be queried");
let how = found.protection(None).howto(&[]);
assert!(how.ways[0].links[0].starts_with("https://github.com/octo/thing/settings"));
}
#[test]
fn a_saved_link_decides_the_project_and_makes_the_repository_linked() {
let gitlab = Integration::new(
"corp",
IntegrationKind::GitlabSelfManaged,
"https://git.corp.example",
);
let found = locate(
&[remote("origin", "https://elsewhere.example/x/y.git")],
&[gitlab],
Some(("corp", "team/sub/proj")),
None,
None,
)
.unwrap();
assert!(found.linked);
assert_eq!(found.kind, HostKind::GitLab);
assert_eq!(found.project, "team/sub/proj");
assert_eq!(found.web_base, "https://git.corp.example");
}
#[test]
fn a_failed_read_is_unknown_with_the_reason_never_an_error() {
let forbidden = Error::Forbidden {
what: "x".into(),
detail: "no".into(),
};
assert!(unknown_reason(&forbidden).contains("lacks the scope"));
let missing = Error::Api {
what: "x".into(),
status: 404,
detail: "Not Found".into(),
};
assert!(unknown_reason(&missing).contains("admin or maintainer"));
let located = locate(
&[remote("origin", "https://github.com/o/r")],
&[],
None,
None,
None,
)
.unwrap();
let protection = located.protection(Some(ProtectionLookup::Unknown("because".into())));
assert!(protection.asked && protection.patterns.is_empty());
assert_eq!(protection.unknown.as_deref(), Some("because"));
}
#[test]
fn names_are_shortened_and_deduplicated() {
let mut list = ProtectedBranches::default();
list.add("refs/heads/main");
list.add("main");
list.add(" ");
list.add("release/*");
assert_eq!(list.patterns, ["main", "release/*"]);
}
}