use serde::{Deserialize, Serialize};
pub(crate) const GITHUB: &str = "github.com";
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub(crate) struct RepoId {
pub(crate) host: String,
pub(crate) owner: String,
pub(crate) name: String,
}
impl RepoId {
pub(crate) fn from_url(url: &str) -> Option<Self> {
let url = url.trim().trim_end_matches('/');
let url = url.strip_suffix(".git").unwrap_or(url);
let (host, path) = if let Some((_, rest)) = url.split_once("://") {
let (authority, path) = rest.split_once('/')?;
let host = without_user(authority);
(host.split_once(':').map_or(host, |(host, _)| host), path)
} else {
let (authority, path) = url.split_once(':')?;
if authority.contains('/') {
return None;
}
(without_user(authority), path)
};
let mut segments = path.rsplit('/').filter(|segment| !segment.is_empty());
let name = segments.next()?;
let owner = segments.next()?;
if host.is_empty() {
return None;
}
Some(Self {
host: host.to_ascii_lowercase(),
owner: owner.to_owned(),
name: name.to_owned(),
})
}
}
fn without_user(authority: &str) -> &str {
authority
.rsplit_once('@')
.map_or(authority, |(_, host)| host)
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub(crate) struct BranchId {
pub(crate) repo: RepoId,
pub(crate) branch: String,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub(crate) struct PrId {
pub(crate) repo: RepoId,
pub(crate) number: u32,
}
impl std::fmt::Display for PrId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}/{}#{}", self.repo.owner, self.repo.name, self.number)
}
}
impl PrId {
pub(crate) fn url(&self) -> String {
let RepoId { host, owner, name } = &self.repo;
format!("https://{host}/{owner}/{name}/pull/{}", self.number)
}
pub(crate) fn parse(text: &str, repo: Option<&RepoId>) -> Result<Self, &'static str> {
const FORMS: &str = "Give the pull request's URL, owner/name#number, or #number.";
const NOT_A_PR_URL: &str = "That URL is not a GitHub pull request's; rho reads GitHub \
pull requests only, by a URL such as \
https://github.com/owner/name/pull/12.";
let text = text.trim();
let number = |text: &str| text.parse::<u32>().ok().filter(|&number| number > 0);
if let Some((_, rest)) = text.split_once("://") {
let mut parts = rest.split('/');
let (Some(host), Some(owner), Some(name), Some("pull"), Some(n)) = (
parts.next(),
parts.next(),
parts.next(),
parts.next(),
parts.next(),
) else {
return Err(NOT_A_PR_URL);
};
let number = number(n.split(['#', '?']).next().unwrap_or(n)).ok_or(NOT_A_PR_URL)?;
let repo = RepoId {
host: without_user(host).to_ascii_lowercase(),
owner: owner.to_owned(),
name: name.to_owned(),
};
return Ok(Self { repo, number });
}
if let Some((repo_name, n)) = text.split_once('#').filter(|(name, _)| !name.is_empty()) {
let (owner, name) = repo_name.split_once('/').ok_or(FORMS)?;
let host = repo.map_or("github.com", |repo| repo.host.as_str());
let repo = RepoId {
host: host.to_owned(),
owner: owner.to_owned(),
name: name.to_owned(),
};
return Ok(Self {
repo,
number: number(n).ok_or(FORMS)?,
});
}
let number = number(text.trim_start_matches('#')).ok_or(FORMS)?;
let repo =
repo.ok_or("This session's folder has no repository on a forge: give the URL.")?;
Ok(Self {
repo: repo.clone(),
number,
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum PrState {
Open,
Draft,
Merged,
Closed,
}
impl PrState {
pub(crate) fn word(self) -> &'static str {
match self {
Self::Open => "open",
Self::Draft => "draft",
Self::Merged => "merged",
Self::Closed => "closed",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Rollup {
None,
Pending,
Passing,
Failing,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct ChangeKey {
pub(crate) updated_at: String,
pub(crate) head_oid: String,
pub(crate) rollup: Rollup,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct PrSummary {
pub(crate) id: PrId,
pub(crate) title: String,
pub(crate) url: String,
pub(crate) state: PrState,
pub(crate) change: ChangeKey,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Review {
None,
Required,
ChangesRequested,
Approved,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum CheckStatus {
Failed,
Running,
Pending,
Cancelled,
Passed,
Neutral,
Skipped,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct CheckRun {
pub(crate) name: String,
pub(crate) workflow: Option<String>,
pub(crate) status: CheckStatus,
pub(crate) url: Option<String>,
pub(crate) required: Option<bool>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct PrDetail {
pub(crate) summary: PrSummary,
pub(crate) author: String,
pub(crate) head: String,
pub(crate) base: String,
pub(crate) body: String,
pub(crate) additions: u32,
pub(crate) deletions: u32,
pub(crate) changed_files: u32,
pub(crate) review: Review,
pub(crate) checks: Vec<CheckRun>,
pub(crate) checks_total: u32,
pub(crate) queued_workflows: u32,
pub(crate) unread_suites: u32,
pub(crate) posts: Vec<Post>,
pub(crate) older_posts: u32,
pub(crate) threads: Vec<Thread>,
pub(crate) older_threads: u32,
pub(crate) mergeable: Mergeable,
pub(crate) merge_methods: Vec<MergeMethod>,
pub(crate) viewer: Option<String>,
pub(crate) merged_by: Option<String>,
pub(crate) merge_commit: Option<String>,
}
impl PrDetail {
pub(crate) fn unread_checks(&self) -> u32 {
let read = u32::try_from(self.checks.len()).unwrap_or(u32::MAX);
self.checks_total.saturating_sub(read)
}
pub(crate) fn checks_to_come(&self) -> bool {
self.unread_checks() > 0 || self.queued_workflows > 0 || self.unread_suites > 0
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Mergeable {
Yes,
Conflicting,
Unknown,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum MergeMethod {
Merge,
Squash,
Rebase,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Post {
pub(crate) id: String,
pub(crate) author: String,
pub(crate) author_association: Option<String>,
pub(crate) avatar_url: Option<String>,
pub(crate) at: i64,
pub(crate) body: String,
pub(crate) verdict: Option<Verdict>,
pub(crate) url: String,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Verdict {
Approved,
ChangesRequested,
Commented,
Dismissed,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Thread {
pub(crate) id: String,
pub(crate) path: String,
pub(crate) line: Option<u32>,
pub(crate) state: ThreadState,
pub(crate) comments: Vec<Post>,
pub(crate) more_comments: u32,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ThreadState {
Open,
Outdated,
Resolved,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(crate) struct FetchPlan {
pub(crate) branches: Vec<BranchId>,
pub(crate) links: Vec<PrId>,
pub(crate) details: Vec<PrId>,
}
impl FetchPlan {
pub(crate) fn is_empty(&self) -> bool {
self.branches.is_empty() && self.links.is_empty() && self.details.is_empty()
}
}
#[derive(Debug, Default, PartialEq, Eq)]
pub(crate) struct Batch {
pub(crate) budget: Option<Budget>,
pub(crate) default_branches: Vec<(RepoId, Option<String>)>,
pub(crate) branches: Vec<(BranchId, Found)>,
pub(crate) links: Vec<(PrId, Found)>,
pub(crate) details: Vec<(PrId, Option<PrDetail>)>,
pub(crate) missing: Vec<(RepoId, String)>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum Found {
Pr(PrSummary),
None,
OnlyForks(usize),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct Budget {
pub(crate) remaining: u32,
pub(crate) resets_at: i64,
pub(crate) cost: Option<u32>,
}
#[cfg(test)]
#[path = "model_tests.rs"]
mod tests;