const PREFIX: &str = "https://github.com/";
const MAX_OWNER: usize = 39;
const MAX_REPOSITORY: usize = 100;
const NUMBER_LIMIT: u32 = 1_000_000_000;
const LENIENT_SUFFIXES: [&str; 2] = ["/files", "/commits"];
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PullRequestLink {
repo: String,
number: u32,
}
impl PullRequestLink {
pub fn parse(url: &str) -> Option<Self> {
let parts: Vec<_> = url.strip_prefix(PREFIX)?.split('/').collect();
let [owner, repo, "pull", number] = parts.as_slice() else {
return None;
};
if !valid_owner(owner) || !valid_repository(repo) || !valid_number(number) {
return None;
}
Some(Self {
repo: format!("{owner}/{repo}"),
number: number.parse().ok()?,
})
}
pub fn parse_lenient(url: &str) -> Option<Self> {
let end = url.find(['?', '#']).unwrap_or(url.len());
let path = &url[..end];
let path = path.strip_suffix('/').unwrap_or(path);
let path = LENIENT_SUFFIXES
.iter()
.find_map(|suffix| path.strip_suffix(suffix))
.unwrap_or(path);
Self::parse(path)
}
pub fn from_key(key: &str) -> Option<Self> {
let (repo, number) = key.split_once('#')?;
let (owner, name) = repo.split_once('/')?;
if !valid_owner(owner) || !valid_repository(name) || !valid_number(number) {
return None;
}
Some(Self {
repo: repo.to_owned(),
number: number.parse().ok()?,
})
}
pub fn repo(&self) -> &str {
&self.repo
}
pub fn number(&self) -> u32 {
self.number
}
pub fn key(&self) -> String {
format!("{}#{}", self.repo, self.number)
}
pub fn url(&self) -> String {
format!("{PREFIX}{}/pull/{}", self.repo, self.number)
}
}
fn valid_owner(owner: &str) -> bool {
!owner.is_empty()
&& owner.len() <= MAX_OWNER
&& owner.starts_with(|c: char| c.is_ascii_alphanumeric())
&& owner.ends_with(|c: char| c.is_ascii_alphanumeric())
&& !owner.contains("--")
&& owner
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'-')
}
fn valid_repository(repo: &str) -> bool {
!repo.is_empty()
&& repo.len() <= MAX_REPOSITORY
&& !matches!(repo, "." | "..")
&& repo
.bytes()
.all(|b| b.is_ascii_alphanumeric() || matches!(b, b'.' | b'_' | b'-'))
}
fn valid_number(number: &str) -> bool {
!number.is_empty()
&& number.bytes().all(|b| b.is_ascii_digit())
&& number
.parse::<u32>()
.is_ok_and(|n| (1..NUMBER_LIMIT).contains(&n))
}