use gitpoulpi_config::{ApiFamily, Integration, IntegrationKind};
use crate::PullRequestForm;
mod github;
mod gitlab;
#[cfg(test)]
mod tests;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RemoteRef {
pub web_base: String,
pub project: String,
}
#[cfg(feature = "hosting")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CheckoutSource {
HeadRef { remote_ref: String, local: String },
Branch {
fork_url: Option<String>,
branch: String,
local: String,
},
}
pub struct FamilySpec {
pub family: ApiFamily,
pub tag: &'static str,
pub pr_sigil: char,
pub parse_remote: fn(&str) -> Option<RemoteRef>,
pub owns_remote: fn(&Integration, &RemoteRef) -> bool,
pub pr_web_url: fn(web_root: &str, project: &str, number: u64) -> Option<String>,
pub pr_edit_url: fn(pr_web_url: &str) -> String,
pub new_pr_url:
fn(web_root: &str, project: &str, source: &str, target: Option<&str>) -> Option<String>,
pub new_pr_form_url: fn(web_root: &str, form: &PullRequestForm<'_>) -> Option<String>,
pub issue_web_url: fn(web_root: &str, project: &str, reference: &str) -> Option<String>,
pub new_issue_url: fn(
web_root: &str,
project: &str,
title: &str,
body: &str,
labels: &[String],
) -> Option<String>,
pub branch_url: fn(web_root: &str, project: &str, branch: &str) -> Option<String>,
pub commit_url: fn(web_root: &str, project: &str, sha: &str) -> Option<String>,
#[cfg(feature = "hosting")]
pub checkout: fn(&crate::model::PullRequestSummary) -> CheckoutSource,
pub token_page: fn(&Integration) -> Option<String>,
pub well_known: fn(host: &str) -> Option<IntegrationKind>,
}
impl FamilySpec {
pub const fn none(family: ApiFamily, tag: &'static str, pr_sigil: char) -> Self {
Self {
family,
tag,
pr_sigil,
parse_remote: default_parse_remote,
owns_remote: default_owns_remote,
pr_web_url: |_, _, _| None,
pr_edit_url: |url| url.to_string(),
new_pr_url: |_, _, _, _| None,
new_pr_form_url: |_, _| None,
issue_web_url: |_, _, _| None,
new_issue_url: |_, _, _, _, _| None,
branch_url: |_, _, _| None,
commit_url: |_, _, _| None,
#[cfg(feature = "hosting")]
checkout: default_checkout,
token_page: token_page_from_help,
well_known: |_| None,
}
}
}
pub fn spec_for(family: ApiFamily) -> &'static FamilySpec {
match family {
ApiFamily::Github => &github::SPEC,
ApiFamily::Gitlab => &gitlab::SPEC,
#[cfg(feature = "azure-devops")]
ApiFamily::AzureDevops => &crate::azure_devops::spec::SPEC,
#[cfg(feature = "bitbucket")]
ApiFamily::Bitbucket => &crate::bitbucket::spec::SPEC,
#[cfg(feature = "jira")]
ApiFamily::Jira => &crate::jira::spec::SPEC,
#[cfg(feature = "trello")]
ApiFamily::Trello => &crate::trello::spec::SPEC,
#[cfg(feature = "gitea")]
ApiFamily::Gitea => &crate::gitea::spec::SPEC,
#[cfg(feature = "linear")]
ApiFamily::Linear => &crate::linear::spec::SPEC,
#[cfg(feature = "youtrack")]
ApiFamily::YouTrack => &crate::youtrack::spec::SPEC,
#[cfg(feature = "redmine")]
ApiFamily::Redmine => &crate::redmine::spec::SPEC,
#[cfg(feature = "gerrit")]
ApiFamily::Gerrit => &crate::gerrit::spec::SPEC,
#[cfg(feature = "local-issues")]
ApiFamily::LocalMarkdown => &crate::local_markdown::spec::SPEC,
#[allow(unreachable_patterns)]
other => inert(other),
}
}
#[allow(dead_code)]
fn inert(family: ApiFamily) -> &'static FamilySpec {
static AZ: FamilySpec = FamilySpec::none(ApiFamily::AzureDevops, "AZ", '!');
static BB: FamilySpec = FamilySpec::none(ApiFamily::Bitbucket, "BB", '#');
static JI: FamilySpec = FamilySpec::none(ApiFamily::Jira, "JI", '#');
static TR: FamilySpec = FamilySpec::none(ApiFamily::Trello, "TR", '#');
static GT: FamilySpec = FamilySpec::none(ApiFamily::Gitea, "GT", '#');
static LN: FamilySpec = FamilySpec::none(ApiFamily::Linear, "LN", '#');
static YT: FamilySpec = FamilySpec::none(ApiFamily::YouTrack, "YT", '#');
static RM: FamilySpec = FamilySpec::none(ApiFamily::Redmine, "RM", '#');
static GR: FamilySpec = FamilySpec::none(ApiFamily::Gerrit, "GR", '#');
static MD: FamilySpec = FamilySpec::none(ApiFamily::LocalMarkdown, "MD", '#');
match family {
ApiFamily::LocalMarkdown => &MD,
ApiFamily::AzureDevops => &AZ,
ApiFamily::Jira => &JI,
ApiFamily::Trello => &TR,
ApiFamily::Gitea => >,
ApiFamily::Linear => &LN,
ApiFamily::YouTrack => &YT,
ApiFamily::Redmine => &RM,
ApiFamily::Gerrit => &GR,
_ => &BB,
}
}
pub fn default_parse_remote(remote_url: &str) -> Option<RemoteRef> {
let (web_base, project) = crate::remote_web_base(remote_url)?;
Some(RemoteRef { web_base, project })
}
pub fn default_owns_remote(integration: &Integration, remote: &RemoteRef) -> bool {
integration_hostname(integration) == hostname(&remote.web_base).trim_start_matches("www.")
}
#[cfg(feature = "hosting")]
fn default_checkout(pr: &crate::model::PullRequestSummary) -> CheckoutSource {
CheckoutSource::Branch {
fork_url: None,
branch: pr.source_branch.clone(),
local: format!("pr/{}", pr.number),
}
}
pub(crate) fn hostname(web_base: &str) -> String {
let rest = web_base
.split_once("://")
.map(|(_, r)| r)
.unwrap_or(web_base);
let authority = rest.split('/').next().unwrap_or("");
let authority = authority
.rsplit_once('@')
.map(|(_, h)| h)
.unwrap_or(authority);
let host = match authority.rsplit_once(':') {
Some((h, port)) if port.chars().all(|c| c.is_ascii_digit()) => h,
_ => authority,
};
host.trim().to_ascii_lowercase()
}
pub(crate) fn integration_hostname(integration: &Integration) -> String {
let url = if integration.url.trim().is_empty() {
integration.kind.public_url().unwrap_or("")
} else {
integration.url.as_str()
};
let host = hostname(url);
host.strip_prefix("api.")
.filter(|_| integration.kind.public_url().is_some())
.map(str::to_string)
.unwrap_or(host)
.trim_start_matches("www.")
.to_string()
}
pub fn web_base(integration: &Integration) -> Option<String> {
let url = integration.url.trim().trim_end_matches('/');
if !url.is_empty() {
return Some(url.to_string());
}
integration.kind.public_url().map(str::to_string)
}
pub(crate) fn token_page_from_help(integration: &Integration) -> Option<String> {
let base = web_base(integration)?;
integration.token_link_url(&base)
}