mod checks;
mod issues;
mod pulls;
use super::token;
use crate::config;
use crate::engine::cooldown as engine_cooldown;
use crate::engine::cooldown;
use crate::security;
use anyhow::Context;
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64;
pub(crate) use checks::{
REQUIRED_CHECK_RULESET_NAME, RulesetCreateKind, classify_ruleset_create_error,
};
pub use pulls::{CommitMeta, PrReviewBatchData};
use reqwest::{Client, Response, StatusCode, header};
use serde::de::DeserializeOwned;
use std::any;
use std::cmp::Reverse;
use std::collections::HashMap;
use std::error::Error as _;
use std::fmt;
use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt::Write as _;
use std::sync::LazyLock;
use std::sync::{
Arc, Mutex, OnceLock,
atomic::{AtomicU64, Ordering},
};
use std::time as std_time;
use std::time::SystemTime;
use std::time::{Duration, Instant};
use tokio::runtime::Handle;
use tokio::time;
use urlencoding;
#[cfg(not(test))]
const GITHUB_API: &str = "https://api.github.com";
#[cfg(test)]
const GITHUB_API: &str = "http://127.0.0.1:9";
const REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
const READ_TIMEOUT: Duration = Duration::from_secs(if cfg!(test) { 1 } else { 10 });
const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
const TCP_KEEPALIVE: Duration = Duration::from_secs(30);
const POOL_IDLE_TIMEOUT: Duration = Duration::from_secs(60);
const HTTP2_KEEP_ALIVE_INTERVAL: Duration = Duration::from_secs(15);
const HTTP2_KEEP_ALIVE_TIMEOUT: Duration = Duration::from_secs(5);
#[derive(Debug, serde::Deserialize)]
struct BlockedByPayload {
#[serde(default)]
id: i64,
number: i64,
#[serde(default)]
state: String,
#[serde(default)]
state_reason: Option<String>,
#[serde(default)]
repository: Option<BlockedByRepoPayload>,
}
#[derive(Debug, serde::Deserialize)]
struct BlockedByRepoPayload {
full_name: String,
}
pub(crate) fn is_full_commit_sha(sha: &str) -> bool {
let len = sha.len();
(len == 40 || len == 64) && sha.bytes().all(|b| b.is_ascii_hexdigit())
}
fn graphql_timeout(query: &str) -> Duration {
if query.trim_start().starts_with("query") {
READ_TIMEOUT
} else {
REQUEST_TIMEOUT
}
}
fn github_retry_attempts() -> u32 {
#[cfg(test)]
{
let n = TEST_RETRY_ATTEMPTS.load(Ordering::Relaxed);
if n > 0 {
return u32::try_from(n).unwrap_or(3);
}
}
config::get("gh.5xx.retry_attempts")
.ok()
.and_then(|v| v.parse::<u32>().ok())
.unwrap_or(3)
}
fn repo_from_url(url: &str) -> Option<String> {
let rest = url.split_once("/repos/")?.1;
let mut parts = rest.split(['/', '?']);
let owner = parts.next().filter(|s| !s.is_empty())?;
let name = parts.next().filter(|s| !s.is_empty())?;
Some(format!("{owner}/{name}"))
}
pub(crate) fn redact_posted_body(body: &str) -> String {
if !security::has_leaks(body) {
return body.to_string();
}
let leaks = security::scan(body);
let rules: Vec<&str> = leaks.iter().map(|leak| leak.rule).collect();
let warning = format!(
"\n\n> ⚠️ **Security Notice**: {} potential secret(s) detected and redacted: {}",
leaks.len(),
rules.join(", ")
);
format!("{}{warning}", security::redact(body))
}
fn merge_pr_body(head_sha: &str) -> serde_json::Value {
serde_json::json!({
"merge_method": "squash",
"sha": head_sha,
})
}
fn parse_positive_issue_number(number: &str) -> anyhow::Result<i64> {
let parsed = number
.parse::<i64>()
.with_context(|| format!("invalid issue number: {number}"))?;
if parsed <= 0 {
anyhow::bail!("invalid issue number: {number}");
}
Ok(parsed)
}
struct RateLimit {
remaining: Option<u32>,
reset_at: Option<u64>,
backoff_until: Option<Instant>,
backoff_delay: Duration,
backoff_base: Duration,
backoff_max: Duration,
throttle_threshold: u32,
}
impl RateLimit {
fn new() -> Self {
let base = config::get("gh.backoff.base_seconds")
.ok()
.and_then(|v| v.parse::<u64>().ok())
.unwrap_or(30);
let max = config::get("gh.backoff.max_seconds")
.ok()
.and_then(|v| v.parse::<u64>().ok())
.unwrap_or(900);
let threshold = config::get("gh.rate_limit.throttle_threshold")
.ok()
.and_then(|v| v.parse::<u32>().ok())
.unwrap_or(200);
Self {
remaining: None,
reset_at: None,
backoff_until: None,
backoff_delay: Duration::ZERO,
backoff_base: Duration::from_secs(base),
backoff_max: Duration::from_secs(max),
throttle_threshold: threshold,
}
}
fn update_from_headers(&mut self, headers: &header::HeaderMap) {
if let Some(v) = headers.get("x-ratelimit-remaining") {
self.remaining = v.to_str().ok().and_then(|s| s.parse().ok());
}
if let Some(v) = headers.get("x-ratelimit-reset") {
self.reset_at = v.to_str().ok().and_then(|s| s.parse().ok());
}
}
fn record_success(&mut self) {
if self.backoff_delay > Duration::ZERO {
tracing::info!("GitHub backoff cleared after successful API call");
}
self.backoff_delay = Duration::ZERO;
self.backoff_until = None;
}
fn record_rate_limit(&mut self) {
METRIC_RATE_LIMIT_HITS.fetch_add(1, Ordering::Relaxed);
if let Some(reset_epoch) = self.reset_at {
match SystemTime::now().duration_since(std_time::UNIX_EPOCH) {
Ok(since_epoch) => {
let now_epoch = since_epoch.as_secs();
if reset_epoch > now_epoch {
let wait_secs = reset_epoch - now_epoch + 1;
self.backoff_delay = Duration::from_secs(wait_secs);
self.backoff_until = Some(Instant::now() + self.backoff_delay);
tracing::warn!(
wait_secs,
"GitHub rate limit hit, waiting until reset time"
);
return;
}
}
Err(e) => {
tracing::error!(
error = %e,
"System clock is before UNIX epoch; ignoring reset_at and falling back to exponential backoff"
);
}
}
}
self.backoff_delay = if self.backoff_delay.is_zero() {
self.backoff_base
} else {
(self.backoff_delay * 2).min(self.backoff_max)
};
self.backoff_until = Some(Instant::now() + self.backoff_delay);
tracing::warn!(
delay_secs = self.backoff_delay.as_secs(),
"GitHub rate limit hit, backing off (no reset header)"
);
}
fn backoff_remaining(&self) -> Option<Duration> {
if let Some(until) = self.backoff_until {
let now = Instant::now();
if now < until {
return Some(until - now);
}
}
None
}
fn proactive_wait_duration(&self) -> Option<Duration> {
if self.backoff_remaining().is_some() {
return None;
}
if let Some(remaining) = self.remaining
&& remaining < self.throttle_threshold
&& let Some(reset_epoch) = self.reset_at
{
let now_epoch = SystemTime::now()
.duration_since(std_time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
if now_epoch < reset_epoch {
return Some(Duration::from_secs(reset_epoch - now_epoch + 1));
}
}
None
}
fn is_active(&self) -> Option<Duration> {
self.backoff_remaining()
.or_else(|| self.proactive_wait_duration())
}
}
static REST_RATE_LIMIT: LazyLock<Mutex<RateLimit>> = LazyLock::new(|| Mutex::new(RateLimit::new()));
#[cfg(test)]
static TEST_RETRY_ATTEMPTS: AtomicU64 = AtomicU64::new(0);
static GRAPHQL_RATE_LIMIT: LazyLock<Mutex<RateLimit>> =
LazyLock::new(|| Mutex::new(RateLimit::new()));
static METRIC_RATE_LIMIT_HITS: AtomicU64 = AtomicU64::new(0);
static METRIC_PROACTIVE_THROTTLES: AtomicU64 = AtomicU64::new(0);
static METRIC_5XX_HITS: AtomicU64 = AtomicU64::new(0);
fn check_rate_limit_backoff(cell: &Mutex<RateLimit>, api: &str) -> anyhow::Result<()> {
let Some(remaining) = cell.lock().ok().and_then(|rl| rl.is_active()) else {
return Ok(());
};
METRIC_PROACTIVE_THROTTLES.fetch_add(1, Ordering::Relaxed);
anyhow::bail!(
"GitHub {} API rate-limited, skipping call for {}s",
api,
remaining.as_secs()
);
}
static CALL_COUNTS: LazyLock<Mutex<HashMap<(String, String), u64>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));
fn count_api_call(url: &str) {
let path = url.split('?').next().unwrap_or(url);
let key = match path.split_once("/repos/") {
Some((_, rest)) => {
let mut parts = rest.split('/');
let project = format!(
"{}/{}",
parts.next().unwrap_or(""),
parts.next().unwrap_or("")
);
(project, parts.next().unwrap_or("repo").to_string())
}
None if path.ends_with("/graphql") => ("-".to_string(), "graphql".to_string()),
None => ("-".to_string(), "other".to_string()),
};
if let Ok(mut counts) = CALL_COUNTS.lock() {
*counts.entry(key).or_insert(0) += 1;
}
}
pub fn drain_call_counts() -> Vec<(String, String, u64)> {
let mut rows: Vec<_> = CALL_COUNTS
.lock()
.map(|mut c| c.drain().map(|((p, k), n)| (p, k, n)).collect())
.unwrap_or_default();
rows.sort_by_key(|r| Reverse(r.2));
rows
}
struct CachedPage {
etag: String,
body: String,
next: Option<String>,
}
static PAGE_CACHE: LazyLock<Mutex<HashMap<String, CachedPage>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));
const PAGE_CACHE_MAX: usize = 256;
fn page_cache_key<T>(url: &str, query: Option<&[(&str, &str)]>) -> Option<String> {
let mut key = String::from(any::type_name::<T>());
key.push(' ');
key.push_str(url);
if let Some(q) = query {
for (i, (k, v)) in q.iter().enumerate() {
key.push(if i == 0 { '?' } else { '&' });
key.push_str(k);
key.push('=');
key.push_str(v);
}
}
if key.contains("since=") {
return None;
}
Some(key)
}
fn page_cache_etag(key: &str) -> Option<String> {
PAGE_CACHE.lock().ok()?.get(key).map(|c| c.etag.clone())
}
fn page_cache_hit(key: &str) -> Option<(String, Option<String>)> {
let cache = PAGE_CACHE.lock().ok()?;
let entry = cache.get(key)?;
Some((entry.body.clone(), entry.next.clone()))
}
fn page_cache_store(key: String, etag: String, body: String, next: Option<String>) {
let Ok(mut cache) = PAGE_CACHE.lock() else {
return;
};
if cache.len() >= PAGE_CACHE_MAX && !cache.contains_key(&key) {
cache.clear();
}
cache.insert(key, CachedPage { etag, body, next });
}
fn maybe_record_body_rate_limit(cell: &Mutex<RateLimit>, status: StatusCode, body: &str) {
if is_forbidden_rate_limit(status, body)
&& let Ok(mut rl) = cell.lock()
{
rl.record_rate_limit();
}
}
fn is_forbidden_rate_limit(status: StatusCode, body: &str) -> bool {
if status != StatusCode::FORBIDDEN {
return false;
}
let lower = body.to_lowercase();
lower.contains("rate limit")
|| lower.contains("abuse detection")
|| lower.contains("secondary rate")
}
fn record_response_for_limiter(cell: &Mutex<RateLimit>, resp: &Response) {
if let Ok(mut rl) = cell.lock() {
rl.update_from_headers(resp.headers());
if resp.status() == StatusCode::TOO_MANY_REQUESTS {
rl.record_rate_limit();
} else if resp.status().is_success() || resp.status() == StatusCode::NOT_MODIFIED {
rl.record_success();
}
}
let status = resp.status();
if status.is_server_error() {
METRIC_5XX_HITS.fetch_add(1, Ordering::Relaxed);
if Handle::try_current().is_ok() {
tokio::spawn(async move {
cooldown::record_github_5xx().await;
});
}
}
}
#[derive(Clone)]
pub struct GhHttp {
client: Client,
token_resolver: Arc<token::TokenResolver>,
app: Option<Arc<super::app::AppAuth>>,
owner_only: bool,
cached_username: OnceLock<String>,
#[cfg(test)]
api_base: Option<String>,
}
impl Debug for GhHttp {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("GhHttp")
.field("owner_only", &self.owner_only)
.finish_non_exhaustive()
}
}
fn transport_error_detail(e: &reqwest::Error) -> String {
let mut out = e.to_string();
let mut source = e.source();
while let Some(s) = source {
let _ = write!(out, ": {s}");
source = s.source();
}
let _ = write!(
out,
" (is_timeout={}, is_connect={})",
e.is_timeout(),
e.is_connect()
);
out
}
impl GhHttp {
pub fn new() -> anyhow::Result<Self> {
let client = Client::builder()
.user_agent("orch/0.1 (reqwest)")
.pool_max_idle_per_host(4)
.timeout(REQUEST_TIMEOUT)
.connect_timeout(CONNECT_TIMEOUT)
.tcp_keepalive(TCP_KEEPALIVE)
.pool_idle_timeout(POOL_IDLE_TIMEOUT)
.http2_keep_alive_interval(HTTP2_KEEP_ALIVE_INTERVAL)
.http2_keep_alive_timeout(HTTP2_KEEP_ALIVE_TIMEOUT)
.http2_keep_alive_while_idle(true)
.build()
.map_err(|e| anyhow::anyhow!("failed to build HTTP client (TLS init): {e}"))?;
Ok(Self {
client,
token_resolver: token::shared(),
app: None,
owner_only: false,
cached_username: OnceLock::new(),
#[cfg(test)]
api_base: None,
})
}
pub fn owner() -> anyhow::Result<Self> {
let mut gh = Self::new()?;
gh.owner_only = true;
Ok(gh)
}
#[cfg(test)]
pub(crate) fn for_test(api_base: String, app: Option<Arc<super::app::AppAuth>>) -> Self {
Self {
client: Client::new(),
token_resolver: Arc::new(token::TokenResolver::default_env()),
app,
owner_only: false,
cached_username: OnceLock::new(),
api_base: Some(api_base),
}
}
#[cfg_attr(
not(test),
expect(
clippy::unused_self,
reason = "only the test build reads the api base override"
)
)]
fn api_root(&self) -> &str {
#[cfg(test)]
if let Some(base) = &self.api_base {
return base;
}
GITHUB_API
}
pub fn is_rate_limited() -> Option<Duration> {
REST_RATE_LIMIT.lock().ok().and_then(|rl| rl.is_active())
}
fn check_backoff(url: &str) -> anyhow::Result<()> {
count_api_call(url);
check_rate_limit_backoff(&REST_RATE_LIMIT, "REST")
}
fn check_graphql_backoff(url: &str) -> anyhow::Result<()> {
count_api_call(url);
check_rate_limit_backoff(&GRAPHQL_RATE_LIMIT, "GraphQL")
}
fn record_response(resp: &Response) {
record_response_for_limiter(&REST_RATE_LIMIT, resp);
}
fn record_graphql_response(resp: &Response) {
record_response_for_limiter(&GRAPHQL_RATE_LIMIT, resp);
}
fn maybe_record_rate_limit_from_body(status: StatusCode, body: &str) {
maybe_record_body_rate_limit(&REST_RATE_LIMIT, status, body);
}
fn maybe_record_graphql_rate_limit_from_body(status: StatusCode, body: &str) {
maybe_record_body_rate_limit(&GRAPHQL_RATE_LIMIT, status, body);
}
fn record_success() {
if let Ok(mut rl) = REST_RATE_LIMIT.lock() {
rl.record_success();
}
}
fn app(&self) -> Option<Arc<super::app::AppAuth>> {
if self.owner_only {
return None;
}
self.app.clone()
}
async fn auth_header(&self, url: &str) -> anyhow::Result<String> {
if let (Some(app), Some(repo)) = (self.app(), repo_from_url(url)) {
match app.token_for_repo(&repo).await {
Ok(Some(token)) => return Ok(format!("Bearer {token}")),
Ok(None) => {}
Err(e) => {
tracing::warn!(error = %e, repo, "GitHub App token unavailable, using the owner token");
}
}
}
match self.token_resolver.get_token().await? {
Some(token) if !token.is_empty() => Ok(format!("Bearer {token}")),
_ => anyhow::bail!("No GitHub token available — run `gh auth login` or set GH_TOKEN"),
}
}
async fn send_with_retries<F>(
&self,
mut make_req: F,
is_graphql: bool,
) -> anyhow::Result<Response>
where
F: FnMut() -> anyhow::Result<reqwest::RequestBuilder>,
{
if engine_cooldown::is_agent_in_cooldown("github:5xx") {
if let Some(until) = engine_cooldown::cooldown_until("github:5xx") {
let now = chrono::Utc::now().timestamp();
let remaining = if until > now { until - now } else { 0 };
anyhow::bail!("GitHub API transient 5xx circuit-breaker active for {remaining}s");
}
anyhow::bail!("GitHub API transient 5xx circuit-breaker active");
}
let attempts = github_retry_attempts();
let base_secs = config::get("gh.5xx.base_backoff_seconds")
.ok()
.and_then(|v| v.parse::<u64>().ok())
.unwrap_or(1);
let max_secs = config::get("gh.5xx.max_backoff_seconds")
.ok()
.and_then(|v| v.parse::<u64>().ok())
.unwrap_or(30);
let circuit_cooldown_secs = config::get("gh.5xx.circuit_breaker_cooldown_seconds")
.ok()
.and_then(|v| v.parse::<u64>().ok())
.unwrap_or(300);
let mut last_err: Option<anyhow::Error> = None;
let mut auth_override: Option<String> = None;
let mut auth_retried = false;
for attempt in 0..attempts {
let mut req = make_req()?;
if let Some(auth) = &auth_override {
req = req.header(header::AUTHORIZATION, auth.clone());
}
let resp_result = req.send().await;
match resp_result {
Ok(resp) => {
if is_graphql {
Self::record_graphql_response(&resp);
} else {
Self::record_response(&resp);
}
if resp.status() == StatusCode::UNAUTHORIZED {
let resp_url = resp.url().to_string();
let status = resp.status();
let body = resp.text().await.unwrap_or_default();
if body.contains("Bad credentials") && !auth_retried {
tracing::warn!(
"GitHub API 401 Bad credentials — clearing cached token and re-resolving"
);
self.token_resolver.clear_cache().await;
if let Some(app) = self.app() {
app.invalidate();
}
match self.auth_header(&resp_url).await {
Ok(new_auth) => {
auth_retried = true;
auth_override = Some(new_auth);
}
Err(e) => {
last_err = Some(
e.context("GitHub token re-resolution failed after 401"),
);
break;
}
}
continue;
}
if auth_retried {
self.token_resolver.clear_cache().await;
anyhow::bail!(
"GitHub authentication failed ({status}): token re-resolved after cache clear but still rejected"
);
}
anyhow::bail!("GitHub API request failed ({status}): {body}");
}
if resp.status().is_server_error() {
let status = resp.status();
let headers = resp.headers().clone();
let body = resp.text().await.unwrap_or_default();
let req_id = headers
.get("x-github-request-id")
.and_then(|v| v.to_str().ok())
.unwrap_or("<none>");
let snippet: String = body.chars().take(400).collect();
tracing::warn!(
status = %status,
request_id = %req_id,
attempt,
"GitHub server error (5xx) — attempt {attempt}: {snippet}"
);
last_err = Some(anyhow::anyhow!(
"GitHub API server error ({status}) request_id={req_id}: {snippet}"
));
if attempt + 1 < attempts {
let exp = 1u64.checked_shl(attempt.min(63)).unwrap_or(u64::MAX);
let mut backoff = base_secs.saturating_mul(exp);
if backoff > max_secs {
backoff = max_secs;
}
let jitter_ms = u64::from(attempt) * 100;
let sleep_dur = Duration::from_secs(backoff)
+ Duration::from_millis(jitter_ms.min(1000));
time::sleep(sleep_dur).await;
continue;
}
tracing::warn!(
"GitHub API 5xx persisted after {} attempts — setting circuit-breaker for {}s",
attempts,
circuit_cooldown_secs
);
engine_cooldown::set_agent_cooldown("github:5xx", circuit_cooldown_secs)
.await;
break;
}
return Ok(resp);
}
Err(e) => {
let detail = transport_error_detail(&e);
tracing::warn!(err = %detail, attempt, "HTTP request failed to send (transport error, GitHub not reached), will retry if attempts remain");
last_err = Some(anyhow::anyhow!(
"HTTP request failed to send (transport error): {detail}"
));
if attempt + 1 < attempts {
let exp = 1u64.checked_shl(attempt.min(63)).unwrap_or(u64::MAX);
let backoff = base_secs.saturating_mul(exp);
let sleep_dur = Duration::from_secs(backoff)
+ Duration::from_millis((u64::from(attempt) * 100).min(1000));
time::sleep(sleep_dur).await;
continue;
}
tracing::warn!(
"HTTP request failed to send after {} attempts (transport error, GitHub not reached) — not tripping the github:5xx circuit-breaker",
attempts
);
break;
}
}
}
Err(last_err.unwrap_or_else(|| anyhow::anyhow!("unexpected send_with_retries failure")))
}
async fn get_json<T: DeserializeOwned>(&self, url: &str) -> anyhow::Result<T> {
Self::check_backoff(url)?;
let auth = self.auth_header(url).await?;
let make_req = || {
Ok(self
.client
.get(url)
.timeout(READ_TIMEOUT)
.header(header::AUTHORIZATION, auth.clone())
.header(header::ACCEPT, "application/vnd.github+json")
.header("X-GitHub-Api-Version", "2022-11-28"))
};
let resp = self.send_with_retries(make_req, false).await?;
let status = resp.status();
if !status.is_success() {
let body = resp.text().await.unwrap_or_default();
Self::maybe_record_rate_limit_from_body(status, &body);
anyhow::bail!("GitHub API GET {url} failed ({status}): {body}");
}
let body = resp.text().await?;
serde_json::from_str(&body)
.with_context(|| format!("failed to parse GitHub API response from GET {url}: {body}"))
}
async fn get_bytes(&self, url: &str) -> anyhow::Result<Vec<u8>> {
Self::check_backoff(url)?;
let auth = self.auth_header(url).await?;
let make_req = || {
Ok(self
.client
.get(url)
.timeout(READ_TIMEOUT)
.header(header::AUTHORIZATION, auth.clone())
.header(header::ACCEPT, "application/vnd.github+json")
.header("X-GitHub-Api-Version", "2022-11-28"))
};
let resp = self.send_with_retries(make_req, false).await?;
let status = resp.status();
if !status.is_success() {
let body = resp.text().await.unwrap_or_default();
Self::maybe_record_rate_limit_from_body(status, &body);
anyhow::bail!("GitHub API GET {url} failed ({status}): {body}");
}
Ok(resp.bytes().await?.to_vec())
}
async fn get_with_query<T: DeserializeOwned>(
&self,
url: &str,
query: &[(&str, &str)],
) -> anyhow::Result<T> {
Self::check_backoff(url)?;
let auth = self.auth_header(url).await?;
let make_req = || {
Ok(self
.client
.get(url)
.timeout(READ_TIMEOUT)
.query(query)
.header(header::AUTHORIZATION, auth.clone())
.header(header::ACCEPT, "application/vnd.github+json")
.header("X-GitHub-Api-Version", "2022-11-28"))
};
let resp = self.send_with_retries(make_req, false).await?;
let status = resp.status();
if !status.is_success() {
let body = resp.text().await.unwrap_or_default();
Self::maybe_record_rate_limit_from_body(status, &body);
anyhow::bail!("GitHub API GET {url} failed ({status}): {body}");
}
let body = resp.text().await?;
serde_json::from_str(&body)
.with_context(|| format!("failed to parse GitHub API response from GET {url}: {body}"))
}
async fn post_json_raw(&self, url: &str, body: &serde_json::Value) -> anyhow::Result<String> {
Self::check_backoff(url)?;
let auth = self.auth_header(url).await?;
let make_req = || {
Ok(self
.client
.post(url)
.json(body)
.header(header::AUTHORIZATION, auth.clone())
.header(header::ACCEPT, "application/vnd.github+json")
.header("X-GitHub-Api-Version", "2022-11-28"))
};
let resp = self.send_with_retries(make_req, false).await?;
let status = resp.status();
let text_res = resp.text().await;
if !status.is_success() {
let text = text_res.unwrap_or_default();
Self::maybe_record_rate_limit_from_body(status, &text);
anyhow::bail!("GitHub API POST {url} failed ({status}): {text}");
}
let text = text_res?;
Ok(text)
}
async fn post_json<T: DeserializeOwned>(
&self,
url: &str,
body: &serde_json::Value,
) -> anyhow::Result<T> {
let text = self.post_json_raw(url, body).await?;
Ok(serde_json::from_str(&text)?)
}
async fn patch_json_raw(&self, url: &str, body: &serde_json::Value) -> anyhow::Result<String> {
Self::check_backoff(url)?;
let auth = self.auth_header(url).await?;
let make_req = || {
Ok(self
.client
.patch(url)
.json(body)
.header(header::AUTHORIZATION, auth.clone())
.header(header::ACCEPT, "application/vnd.github+json")
.header("X-GitHub-Api-Version", "2022-11-28"))
};
let resp = self.send_with_retries(make_req, false).await?;
let status = resp.status();
let text_res = resp.text().await;
if !status.is_success() {
let text = text_res.unwrap_or_default();
Self::maybe_record_rate_limit_from_body(status, &text);
anyhow::bail!("GitHub API PATCH {url} failed ({status}): {text}");
}
let text = text_res?;
Ok(text)
}
pub async fn put_file(
&self,
repo: &str,
path: &str,
branch: &str,
message: &str,
content: &str,
) -> anyhow::Result<()> {
let encoded_path = path
.split('/')
.map(|segment| urlencoding::encode(segment).into_owned())
.collect::<Vec<_>>()
.join("/");
let url = format!("{}/repos/{repo}/contents/{encoded_path}", self.api_root());
let current = self
.get_json::<serde_json::Value>(&format!("{url}?ref={}", urlencoding::encode(branch)))
.await
.ok()
.and_then(|v| v.get("sha").and_then(|s| s.as_str()).map(str::to_string));
let mut body = serde_json::json!({
"message": message,
"content": BASE64.encode(content),
"branch": branch,
});
if let Some(sha) = current {
body["sha"] = serde_json::Value::String(sha);
}
self.put_json_raw(&url, &body).await.map(|_| ())
}
async fn put_json_raw(&self, url: &str, body: &serde_json::Value) -> anyhow::Result<String> {
Self::check_backoff(url)?;
let auth = self.auth_header(url).await?;
let make_req = || {
Ok(self
.client
.put(url)
.json(body)
.header(header::AUTHORIZATION, auth.clone())
.header(header::ACCEPT, "application/vnd.github+json")
.header("X-GitHub-Api-Version", "2022-11-28"))
};
let resp = self.send_with_retries(make_req, false).await?;
let status = resp.status();
let text_res = resp.text().await;
if !status.is_success() {
let text = text_res.unwrap_or_default();
Self::maybe_record_rate_limit_from_body(status, &text);
anyhow::bail!("GitHub API PUT {url} failed ({status}): {text}");
}
let text = text_res?;
Ok(text)
}
async fn delete(&self, url: &str) -> anyhow::Result<StatusCode> {
Self::check_backoff(url)?;
let auth = self.auth_header(url).await?;
let make_req = || {
Ok(self
.client
.delete(url)
.header(header::AUTHORIZATION, auth.clone())
.header(header::ACCEPT, "application/vnd.github+json")
.header("X-GitHub-Api-Version", "2022-11-28"))
};
let resp = self.send_with_retries(make_req, false).await?;
let status = resp.status();
if !status.is_success() && status != StatusCode::NOT_FOUND {
let body = resp.text().await.unwrap_or_default();
Self::maybe_record_rate_limit_from_body(status, &body);
anyhow::bail!("GitHub API DELETE {url} failed ({status}): {body}");
}
Ok(status)
}
async fn get_all_pages<T: DeserializeOwned>(
&self,
url: &str,
query: &[(&str, &str)],
) -> anyhow::Result<Vec<T>> {
const MAX_JSON_DECODE_RETRIES: u32 = 2;
const JSON_DECODE_RETRY_BACKOFF_MS: u64 = 200;
Self::check_backoff(url)?;
let mut all: Vec<T> = Vec::new();
let mut next_url: Option<String> = None;
let mut is_first = true;
loop {
let (request_url, request_query) = if is_first {
is_first = false;
(url, Some(query))
} else {
let Some(u) = next_url.as_ref() else { break };
(u.as_str(), None)
};
let cache_key = page_cache_key::<T>(request_url, request_query);
let mut cached_etag = cache_key.as_deref().and_then(page_cache_etag);
let mut decode_attempt = 0;
let (page, next) = loop {
let auth = self.auth_header(request_url).await?;
let mut req = self
.client
.get(request_url)
.timeout(READ_TIMEOUT)
.header(header::AUTHORIZATION, auth)
.header(header::ACCEPT, "application/vnd.github+json")
.header("X-GitHub-Api-Version", "2022-11-28");
if let Some(q) = request_query {
req = req.query(q);
}
if let Some(etag) = &cached_etag {
req = req.header(header::IF_NONE_MATCH, etag.clone());
}
let make_req = || {
req.try_clone()
.ok_or_else(|| anyhow::anyhow!("request clone failed"))
};
let resp = self.send_with_retries(make_req, false).await?;
let status = resp.status();
let next_from_headers = parse_link_next(resp.headers());
let etag = resp
.headers()
.get(header::ETAG)
.and_then(|v| v.to_str().ok())
.map(ToString::to_string);
let content_type = resp
.headers()
.get(header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
.map_or_else(|| "<missing>".to_string(), ToString::to_string);
if status == StatusCode::NOT_MODIFIED {
if let Some((body, next)) = cache_key.as_deref().and_then(page_cache_hit) {
break (serde_json::from_str::<Vec<T>>(&body)?, next);
}
cached_etag = None;
continue;
}
if !status.is_success() {
let body = resp.text().await.unwrap_or_default();
Self::maybe_record_rate_limit_from_body(status, &body);
anyhow::bail!("GitHub API GET (paginated) failed ({status}): {body}");
}
let text = resp.text().await?;
match serde_json::from_str::<Vec<T>>(&text) {
Ok(page) => {
if let (Some(key), Some(etag)) = (cache_key.clone(), etag) {
page_cache_store(key, etag, text, next_from_headers.clone());
}
break (page, next_from_headers);
}
Err(err) => {
let snippet: String = text.chars().take(200).collect();
let decode_error = anyhow::anyhow!(
"GitHub API GET (paginated) JSON decode failed ({status}, content-type={content_type}): {snippet}"
)
.context(err);
if decode_attempt < MAX_JSON_DECODE_RETRIES {
decode_attempt += 1;
tracing::warn!(
attempt = decode_attempt,
max_attempts = MAX_JSON_DECODE_RETRIES,
status = %status,
content_type,
err = %decode_error,
"json decode failed; retrying paginated GET"
);
time::sleep(Duration::from_millis(
JSON_DECODE_RETRY_BACKOFF_MS * u64::from(decode_attempt),
))
.await;
continue;
}
return Err(decode_error);
}
}
};
next_url = next;
all.extend(page);
if next_url.is_none() {
break;
}
}
Ok(all)
}
async fn get_all_pages_from_wrapper<T: DeserializeOwned>(
&self,
url: &str,
array_field: &str,
) -> anyhow::Result<Vec<T>> {
Self::check_backoff(url)?;
let mut all: Vec<T> = Vec::new();
let mut current_url: String = url.to_string();
loop {
let auth = self.auth_header(¤t_url).await?;
let make_req = || {
self.client
.get(¤t_url)
.timeout(READ_TIMEOUT)
.header(header::AUTHORIZATION, auth.clone())
.header(header::ACCEPT, "application/vnd.github+json")
.header("X-GitHub-Api-Version", "2022-11-28")
.try_clone()
.ok_or_else(|| anyhow::anyhow!("request clone failed"))
};
let resp = self.send_with_retries(make_req, false).await?;
let status = resp.status();
let next = parse_link_next(resp.headers());
if !status.is_success() {
let body = resp.text().await.unwrap_or_default();
Self::maybe_record_rate_limit_from_body(status, &body);
anyhow::bail!(
"GitHub API GET (wrapped paginated) {current_url} failed ({status}): {body}"
);
}
let body = resp.text().await?;
let wrapper: serde_json::Value = serde_json::from_str(&body).with_context(|| {
format!("failed to parse wrapper response from {current_url}: {body}")
})?;
let items = wrapper
.get(array_field)
.and_then(|v| v.as_array())
.cloned()
.unwrap_or_default();
for item in items {
match serde_json::from_value::<T>(item) {
Ok(v) => all.push(v),
Err(e) => {
tracing::warn!(
field = array_field,
err = %e,
"skipping item that failed to deserialize in wrapped pagination"
);
}
}
}
match next {
Some(next_url) => current_url = next_url,
None => break,
}
}
Ok(all)
}
async fn graphql_request(
&self,
query: &str,
extra_headers: &[(&str, &str)],
variables: Option<serde_json::Value>,
) -> anyhow::Result<serde_json::Value> {
self.graphql_request_to_url(
&format!("{GITHUB_API}/graphql"),
query,
extra_headers,
variables,
)
.await
}
async fn graphql_request_to_url(
&self,
url: &str,
query: &str,
extra_headers: &[(&str, &str)],
variables: Option<serde_json::Value>,
) -> anyhow::Result<serde_json::Value> {
Self::check_graphql_backoff(url)?;
let body = if let Some(vars) = variables {
serde_json::json!({ "query": query, "variables": vars })
} else {
serde_json::json!({ "query": query })
};
let auth = self.auth_header(url).await?;
let mut req = self
.client
.post(url)
.timeout(graphql_timeout(query))
.json(&body)
.header(header::AUTHORIZATION, auth)
.header(header::ACCEPT, "application/vnd.github+json")
.header("X-GitHub-Api-Version", "2022-11-28");
for (k, v) in extra_headers {
req = req.header(*k, *v);
}
let make_req = || {
req.try_clone()
.ok_or_else(|| anyhow::anyhow!("graphql request clone failed"))
};
let resp = self.send_with_retries(make_req, true).await?;
let status = resp.status();
let text_res = resp.text().await;
if !status.is_success() {
let text = text_res.unwrap_or_default();
Self::maybe_record_graphql_rate_limit_from_body(status, &text);
anyhow::bail!("GitHub GraphQL failed ({status}): {text}");
}
let text = text_res?;
let json: serde_json::Value = serde_json::from_str(&text)?;
if let Some(errors) = json.get("errors").and_then(|e| e.as_array())
&& !errors.is_empty()
{
let messages: Vec<&str> = errors
.iter()
.filter_map(|e| e.get("message").and_then(|m| m.as_str()))
.collect();
if json.get("data").is_none() || json["data"].is_null() {
anyhow::bail!("GitHub GraphQL errors: {}", messages.join("; "));
}
tracing::warn!(errors = ?messages, "GitHub GraphQL returned partial errors");
}
Ok(json)
}
pub async fn graphql_with_vars(
&self,
query: &str,
variables: serde_json::Value,
) -> anyhow::Result<serde_json::Value> {
self.graphql_request(query, &[], Some(variables)).await
}
pub async fn graphql_with_headers(
&self,
query: &str,
headers: &[&str],
) -> anyhow::Result<serde_json::Value> {
let pairs: Vec<(&str, &str)> = headers.iter().filter_map(|h| h.split_once(':')).collect();
self.graphql_request(query, &pairs, None).await
}
pub async fn graphql_with_headers_and_vars(
&self,
query: &str,
headers: &[&str],
variables: serde_json::Value,
) -> anyhow::Result<serde_json::Value> {
let pairs: Vec<(&str, &str)> = headers.iter().filter_map(|h| h.split_once(':')).collect();
self.graphql_request(query, &pairs, Some(variables)).await
}
}
pub fn has_status(text: &str, status: u16) -> bool {
text.contains(&format!("failed ({status}")) || text.contains(&format!("HTTP {status}"))
}
fn sanitize_github_issue_numbers(issue_numbers: &[u64]) -> (Vec<u64>, Vec<u64>) {
let mut valid = Vec::with_capacity(issue_numbers.len());
let mut invalid = Vec::new();
for &issue_number in issue_numbers {
if issue_number == 0 {
invalid.push(issue_number);
} else {
valid.push(issue_number);
}
}
(valid, invalid)
}
#[derive(Debug, Clone)]
pub struct RateLimitMetrics {
pub hits: u64,
pub skipped_calls: u64,
pub rest_remaining: Option<u32>,
pub rest_reset_at: Option<u64>,
pub graphql_remaining: Option<u32>,
pub five_xx_hits: u64,
}
pub fn rate_limit_metrics() -> RateLimitMetrics {
let (rest_remaining, rest_reset_at) = REST_RATE_LIMIT
.lock()
.ok()
.map_or((None, None), |rl| (rl.remaining, rl.reset_at));
let graphql_remaining = GRAPHQL_RATE_LIMIT.lock().ok().and_then(|rl| rl.remaining);
RateLimitMetrics {
hits: METRIC_RATE_LIMIT_HITS.load(Ordering::Relaxed),
skipped_calls: METRIC_PROACTIVE_THROTTLES.load(Ordering::Relaxed),
rest_remaining,
rest_reset_at,
graphql_remaining,
five_xx_hits: METRIC_5XX_HITS.load(Ordering::Relaxed),
}
}
fn parse_link_next(headers: &header::HeaderMap) -> Option<String> {
let link = headers.get("link")?.to_str().ok()?;
for part in link.split(',') {
let part = part.trim();
if part.contains("rel=\"next\"") {
let start = part.find('<')? + 1;
let end = part.find('>')?;
if start >= end {
return None;
}
return Some(part[start..end].to_string());
}
}
None
}
pub fn status_label_color(label: &str) -> &'static str {
match label {
"status:new" => "0e8a16",
"status:routed" => "1d76db",
"status:in_progress" => "fbca04",
"status:done" => "6f42c1",
"status:blocked" => "d73a4a",
"status:in_review" => "0075ca",
"status:approved" => "2ea44f",
"status:needs_review" => "e4e669",
"status:waiting" => "5319e7",
_ => "c5def5",
}
}
#[cfg(test)]
mod tests;