nightporter 0.1.1

Handles your PRs while you're away: an agentic night shift for GitHub and GitLab
//! Thin JSON-over-HTTP helper shared by the forge clients, plus token lookup.

use crate::config::TokenSource;
use crate::forge::ForgeKind;
use anyhow::{Context, Result, bail};
use reqwest::{Method, StatusCode, header};
use secrecy::{ExposeSecret, SecretString};
use serde::de::DeserializeOwned;
use serde_json::Value;
use std::fmt::Write as _;

#[derive(Clone)]
pub struct Api {
    http: reqwest::Client,
    pub base: String,
    token: SecretString,
    /// `Authorization: Bearer` (GitHub) vs `PRIVATE-TOKEN` (GitLab).
    gitlab: bool,
}

pub enum Got<T> {
    Fresh(T, header::HeaderMap),
    NotModified,
}

impl Api {
    pub fn new(base: &str, token: SecretString, gitlab: bool) -> Result<Self> {
        let http = reqwest::Client::builder()
            .user_agent(concat!("nightporter/", env!("CARGO_PKG_VERSION")))
            .timeout(std::time::Duration::from_secs(30))
            // Follow redirects only to the same scheme, host and port: GitLab's PRIVATE-TOKEN
            // header is not stripped on cross-host redirects the way `Authorization` is, and an
            // https→http hop would send it in the clear.
            .redirect(reqwest::redirect::Policy::custom(|a| {
                let same = a.previous().first().is_some_and(|p| same_origin(p, a.url()));
                if same && a.previous().len() < 5 { a.follow() } else { a.stop() }
            }))
            .build()?;
        Ok(Self { http, base: base.trim_end_matches('/').into(), token, gitlab })
    }

    fn is_ours(&self, url: &str) -> bool {
        match (reqwest::Url::parse(&self.base), reqwest::Url::parse(url)) {
            (Ok(b), Ok(u)) => same_origin(&b, &u),
            _ => false,
        }
    }

    fn req(&self, m: Method, path: &str) -> reqwest::RequestBuilder {
        let url = if path.starts_with("http") { path.to_string() } else { format!("{}{path}", self.base) };
        let r = self.http.request(m, url);
        if self.gitlab {
            r.header("PRIVATE-TOKEN", self.token.expose_secret())
        } else {
            r.bearer_auth(self.token.expose_secret())
                .header(header::ACCEPT, "application/vnd.github+json")
                .header("X-GitHub-Api-Version", "2022-11-28")
        }
    }

    pub async fn get<T: DeserializeOwned>(&self, path: &str) -> Result<T> {
        match self.get_cond(path, None).await? {
            Got::Fresh(v, _) => Ok(v),
            Got::NotModified => bail!("unexpected 304 for {path}"),
        }
    }

    /// GET every page (follows `Link: <…>; rel="next"`, which GitHub and GitLab both send), up to
    /// `max_pages`. Errors on any page fail the whole call so callers don't advance cursors.
    pub async fn get_all<T: DeserializeOwned>(&self, path: &str, max_pages: usize) -> Result<Vec<T>> {
        let mut out = Vec::new();
        let mut next = Some(path.to_string());
        for _ in 0..max_pages {
            let Some(p) = next.take() else { break };
            match self.get_cond::<Vec<T>>(&p, None).await? {
                Got::Fresh(v, h) => {
                    out.extend(v);
                    // Only follow `next` links back to our own API: they carry the token.
                    next = next_link(&h).filter(|u| self.is_ours(u));
                }
                Got::NotModified => break,
            }
        }
        Ok(out)
    }

    /// GET with `If-Modified-Since`. 304s don't count against GitHub's rate limit.
    pub async fn get_cond<T: DeserializeOwned>(&self, path: &str, since: Option<&str>) -> Result<Got<T>> {
        let mut r = self.req(Method::GET, path);
        if let Some(s) = since {
            r = r.header(header::IF_MODIFIED_SINCE, s);
        }
        let resp = r.send().await.with_context(|| format!("GET {path}"))?;
        if resp.status() == StatusCode::NOT_MODIFIED {
            return Ok(Got::NotModified);
        }
        let headers = resp.headers().clone();
        let v = check(resp, path).await?.json().await.with_context(|| format!("decode {path}"))?;
        Ok(Got::Fresh(v, headers))
    }

    pub async fn send(&self, m: Method, path: &str, body: &Value) -> Result<Value> {
        let resp = self.req(m.clone(), path).json(body).send().await.with_context(|| format!("{m} {path}"))?;
        let resp = check(resp, path).await?;
        let text = resp.text().await?;
        Ok(if text.is_empty() { Value::Null } else { serde_json::from_str(&text)? })
    }
}

async fn check(resp: reqwest::Response, path: &str) -> Result<reqwest::Response> {
    let s = resp.status();
    if s.is_success() {
        return Ok(resp);
    }
    let body = resp.text().await.unwrap_or_default();
    let body: String = body.chars().take(300).collect();
    bail!("{path}: HTTP {s}: {body}")
}

fn same_origin(a: &reqwest::Url, b: &reqwest::Url) -> bool {
    a.scheme() == b.scheme() && a.host_str() == b.host_str() && a.port_or_known_default() == b.port_or_known_default()
}

/// The `rel="next"` URL from a `Link` header.
pub fn next_link(h: &header::HeaderMap) -> Option<String> {
    let link = h.get(header::LINK)?.to_str().ok()?;
    link.split(',').find(|p| p.contains("rel=\"next\"")).and_then(|p| Some(p.split_once('<')?.1.split_once('>')?.0.to_string()))
}

/// Get a token: from `gh`/`glab` (they refresh it themselves) or from the OS keychain.
/// Never read from config files or environment of the agent.
pub async fn token(forge: ForgeKind, host: &str, source: TokenSource) -> Result<SecretString> {
    if let Some(t) = std::env::var("NIGHTPORTER_TEST_TOKEN").ok().filter(|_| cfg!(debug_assertions)) {
        return Ok(t.into());
    }
    match source {
        TokenSource::Keyring => {
            let e = keyring::Entry::new("nightporter", &format!("{}:{host}", forge.as_str()))?;
            Ok(e.get_password()
                .with_context(|| format!("no token in keychain for {host}; run `nightporter token set {} {host}`", forge.as_str()))?
                .into())
        }
        TokenSource::Cli => {
            let (bin, args): (&str, Vec<&str>) = match forge {
                ForgeKind::GitHub => ("gh", vec!["auth", "token", "--hostname", host]),
                ForgeKind::GitLab => ("glab", vec!["config", "get", "token", "--host", host]),
            };
            let out =
                tokio::process::Command::new(bin).args(&args).output().await.with_context(|| format!("run `{bin}` (is it installed?)"))?;
            let t = String::from_utf8(out.stdout)?.trim().to_string();
            if !out.status.success() || t.is_empty() {
                bail!("`{bin} {}` gave no token; log in with `{bin} auth login --hostname {host}`", args.join(" "));
            }
            Ok(t.into())
        }
    }
}

pub fn store_token(forge: ForgeKind, host: &str, token: &str) -> Result<()> {
    keyring::Entry::new("nightporter", &format!("{}:{host}", forge.as_str()))?.set_password(token)?;
    Ok(())
}

/// Percent-encode a path segment (GitLab project ids are `group%2Fname`).
pub fn enc(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for b in s.bytes() {
        match b {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => out.push(b as char),
            _ => {
                let _ = write!(out, "%{b:02X}");
            }
        }
    }
    out
}

#[cfg(test)]
mod tests {
    #[test]
    fn redirects_and_next_links_stay_on_origin() {
        let u = |s: &str| reqwest::Url::parse(s).unwrap();
        assert!(super::same_origin(&u("https://gitlab.example.com/api/v4/x"), &u("https://gitlab.example.com/api/v4/y?page=2")));
        assert!(super::same_origin(&u("https://h.example/a"), &u("https://h.example:443/b")));
        assert!(!super::same_origin(&u("https://h.example/a"), &u("http://h.example/a")), "no https→http");
        assert!(!super::same_origin(&u("https://h.example/a"), &u("https://h.example:8443/a")), "no port change");
        assert!(!super::same_origin(&u("https://h.example/a"), &u("https://evil.example/a")));
    }

    #[test]
    fn link_next() {
        let mut h = reqwest::header::HeaderMap::new();
        h.insert(
            reqwest::header::LINK,
            r#"<https://api.github.com/x?page=2>; rel="next", <https://api.github.com/x?page=9>; rel="last""#.parse().unwrap(),
        );
        assert_eq!(super::next_link(&h).as_deref(), Some("https://api.github.com/x?page=2"));
        h.insert(reqwest::header::LINK, r#"<https://x?page=1>; rel="prev""#.parse().unwrap());
        assert_eq!(super::next_link(&h), None);
    }

    #[test]
    fn enc() {
        assert_eq!(super::enc("group/sub proj"), "group%2Fsub%20proj");
    }
}