use std::collections::{HashMap, HashSet};
use std::fmt;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant, SystemTime};
use futures_util::future::BoxFuture;
use reqwest::header::{self, HeaderMap, HeaderValue};
use reqwest::{Method, StatusCode};
use serde::Deserialize;
use serde::de::DeserializeOwned;
use tracing::info;
use self::token::Tokens;
use self::workflows::{Next, Workflows};
use crate::api;
use crate::command;
use crate::docker::JitConfig;
use crate::sync::lock;
use crate::time;
mod requests;
mod token;
mod workflows;
use requests::Counter;
pub use requests::{Class, RateLimit, Report, Spend, Tally};
pub const API_URL: &str = "https://api.github.com";
const REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
const PER_PAGE: u32 = 100;
const MAX_PAGES: u32 = 10;
const JOB_REQUESTS: usize = 200;
const CONDITIONAL_BYTES: usize = 32 * 1024 * 1024;
const CONDITIONAL_FRESH: Duration = Duration::from_secs(600);
#[derive(Clone)]
pub struct Token(String);
impl fmt::Debug for Token {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Token(<redacted>)")
}
}
pub type TokenSource = Box<dyn Fn() -> Result<Token, Error> + Send + Sync>;
pub const TOKEN_DEADLINE: Duration = Duration::from_secs(15);
pub fn gh_token() -> Result<Token, Error> {
let output = command::output_within(
std::process::Command::new("gh").args(["auth", "token", "--hostname", "github.com"]),
TOKEN_DEADLINE,
&command::RUNNING,
)
.map_err(|error| Error::Token(format!("cannot run gh: {error}")))?;
if !output.status.success() {
return Err(Error::Token(format!(
"gh auth token failed: {}",
String::from_utf8_lossy(&output.stderr).trim()
)));
}
let token = String::from_utf8(output.stdout)
.map_err(|_| Error::Token("gh auth token printed a token that is not UTF-8".to_owned()))?;
Ok(Token(token.trim().to_owned()))
}
pub fn install_crypto_provider() {
let _ = rustls::crypto::ring::default_provider().install_default();
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("GitHub's rate limit is used up until {}s after the epoch", secs(*until))]
RateLimited {
until: SystemTime,
},
#[error("GitHub answered {status} to {method} {path}: {message}")]
Status {
method: Method,
path: String,
status: StatusCode,
message: String,
},
#[error("{method} {path}: {source}")]
Http {
method: Method,
path: String,
source: reqwest::Error,
},
#[error("{path}: unexpected response: {source}")]
Decode {
path: String,
source: serde_json::Error,
},
#[error("no GitHub token: {0}")]
Token(String),
}
impl Error {
pub fn is_gone(&self) -> bool {
matches!(
self,
Error::Status {
status: StatusCode::NOT_FOUND | StatusCode::GONE,
..
}
)
}
}
fn secs(time: SystemTime) -> u64 {
time.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct QueuedJob {
pub id: u64,
pub run_id: u64,
pub name: String,
pub labels: Vec<String>,
pub created_at: u64,
pub run_attempt: u32,
pub workflow: Option<String>,
pub branch: Option<String>,
pub url: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct JobDetails {
pub id: u64,
pub run_id: u64,
#[serde(default = "first_attempt")]
pub run_attempt: u32,
pub name: String,
pub workflow_name: Option<String>,
pub head_branch: Option<String>,
pub html_url: Option<String>,
#[serde(default)]
pub labels: Vec<String>,
pub created_at: Option<String>,
pub status: String,
pub conclusion: Option<String>,
pub started_at: Option<String>,
pub completed_at: Option<String>,
pub runner_name: Option<String>,
#[serde(default)]
pub steps: Vec<JobStep>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NamedRun {
pub repository: String,
pub run_id: u64,
pub workflow: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RunTrigger {
pub event: String,
pub head_branch: Option<String>,
pub head_repository: Option<String>,
pub head_sha: String,
pub by_user: bool,
pub pull_requests: Vec<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PullRequest {
pub by_user: bool,
pub head_repository: Option<String>,
pub head_branch: String,
pub base_branch: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RepositoryDetails {
pub default_branch: String,
pub visibility: api::Visibility,
}
fn first_attempt() -> u32 {
1
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct JobStep {
pub number: u32,
pub name: String,
pub status: String,
pub conclusion: Option<String>,
pub started_at: Option<String>,
pub completed_at: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct Runner {
pub id: u64,
pub name: String,
pub status: String,
pub busy: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Deleted {
Deleted,
NotFound,
Busy,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum JobActionRequest {
CancelRun {
run: u64,
},
RerunJob {
job: u64,
},
RerunFailed {
run: u64,
},
}
impl JobActionRequest {
fn path(self, repository: &str) -> String {
match self {
JobActionRequest::CancelRun { run } => {
format!("/repos/{repository}/actions/runs/{run}/cancel")
}
JobActionRequest::RerunJob { job } => {
format!("/repos/{repository}/actions/jobs/{job}/rerun")
}
JobActionRequest::RerunFailed { run } => {
format!("/repos/{repository}/actions/runs/{run}/rerun-failed-jobs")
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct RunNow {
pub attempt: u32,
pub completed: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ActionFailure {
NoToken,
RateLimited {
status: Option<u16>,
until: SystemTime,
},
Status {
status: u16,
},
Unreachable,
TimedOut,
Broken,
Withdrawn,
}
pub type Permit<'permit> = dyn Fn() -> BoxFuture<'permit, bool> + Send + Sync + 'permit;
impl ActionFailure {
fn of(error: &Error) -> ActionFailure {
match error {
Error::Token(_) => ActionFailure::NoToken,
Error::RateLimited { until } => ActionFailure::RateLimited {
status: None,
until: *until,
},
Error::Status { status, .. } => ActionFailure::Status {
status: status.as_u16(),
},
Error::Http { source, .. } if source.is_connect() => ActionFailure::Unreachable,
Error::Http { source, .. } if source.is_timeout() => ActionFailure::TimedOut,
Error::Http { .. } | Error::Decode { .. } => ActionFailure::Broken,
}
}
}
struct Withdrawn;
pub trait GithubApi: Send + Sync {
fn queued_jobs(
&self,
repository: &str,
) -> impl Future<Output = Result<Vec<QueuedJob>, Error>> + Send;
fn runners(&self, repository: &str) -> impl Future<Output = Result<Vec<Runner>, Error>> + Send;
fn jit_config(
&self,
repository: &str,
name: &str,
labels: &[String],
) -> impl Future<Output = Result<(Runner, JitConfig), Error>> + Send;
fn delete_runner(
&self,
repository: &str,
id: u64,
) -> impl Future<Output = Result<Deleted, Error>> + Send;
fn job(
&self,
repository: &str,
id: u64,
) -> impl Future<Output = Result<JobDetails, Error>> + Send;
fn run_trigger(
&self,
repository: &str,
run: u64,
) -> impl Future<Output = Result<RunTrigger, Error>> + Send;
fn pull_request(
&self,
repository: &str,
number: u64,
) -> impl Future<Output = Result<PullRequest, Error>> + Send;
fn repository(
&self,
repository: &str,
) -> impl Future<Output = Result<RepositoryDetails, Error>> + Send;
fn on_branch(
&self,
repository: &str,
branch: &str,
sha: &str,
) -> impl Future<Output = Result<bool, Error>> + Send;
fn cancel_run(
&self,
repository: &str,
run: u64,
) -> impl Future<Output = Result<(), Error>> + Send;
fn job_action(
&self,
repository: &str,
request: JobActionRequest,
permit: &Permit<'_>,
) -> impl Future<Output = Result<u16, ActionFailure>> + Send;
fn action_run(
&self,
repository: &str,
run: u64,
) -> impl Future<Output = Result<RunNow, ActionFailure>> + Send;
fn named_runs(&self) -> impl Future<Output = Vec<NamedRun>> + Send;
fn spend(&self) -> Spend;
}
pub struct Github {
http: reqwest::Client,
base: String,
tokens: Tokens,
etags: Mutex<Conditional>,
workflows: Mutex<Workflows>,
resolving: tokio::sync::Mutex<()>,
blocked_until: Mutex<Option<SystemTime>>,
requests: Mutex<Counter>,
}
impl Drop for Github {
fn drop(&mut self) {
let report = lock(&self.requests).take(Instant::now());
if !report.is_empty() {
info!("{}", report.line(SystemTime::now()));
}
}
}
impl fmt::Debug for Github {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Github")
.field("base", &self.base)
.finish_non_exhaustive()
}
}
#[derive(Deserialize)]
struct Runs {
workflow_runs: Vec<Run>,
}
#[derive(Deserialize)]
struct Run {
id: u64,
workflow_id: Option<u64>,
}
#[derive(Deserialize)]
struct RunWorkflow {
workflow_id: Option<u64>,
}
#[derive(Deserialize)]
struct WorkflowList {
workflows: Vec<WorkflowBody>,
}
#[derive(Deserialize)]
struct WorkflowBody {
id: u64,
name: Option<String>,
}
struct Unresolved;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum RunState {
Waiting,
Started,
}
#[derive(Deserialize)]
struct Jobs {
jobs: Vec<Job>,
}
#[derive(Deserialize)]
struct Job {
id: u64,
run_id: u64,
name: String,
status: String,
labels: Vec<String>,
created_at: Option<String>,
run_attempt: Option<u32>,
workflow_name: Option<String>,
head_branch: Option<String>,
html_url: Option<String>,
}
#[derive(Deserialize)]
struct RunBody {
workflow_id: Option<u64>,
event: String,
head_branch: Option<String>,
head_repository: Option<RepositoryName>,
head_sha: String,
actor: Option<Account>,
triggering_actor: Option<Account>,
#[serde(default)]
pull_requests: Vec<PullRequestNumber>,
}
#[derive(Deserialize)]
struct RunAttemptBody {
#[serde(default = "first_attempt")]
run_attempt: u32,
status: Option<String>,
}
#[derive(Deserialize)]
struct Account {
#[serde(rename = "type")]
kind: String,
}
impl Account {
fn is_user(account: Option<&Account>) -> bool {
account.is_some_and(|account| account.kind == "User")
}
}
#[derive(Deserialize)]
struct PullRequestNumber {
number: u64,
base: Option<PullRequestBase>,
}
#[derive(Deserialize)]
struct PullRequestBase {
repo: Option<RepositoryUrl>,
}
#[derive(Deserialize)]
struct RepositoryUrl {
url: String,
}
impl PullRequestNumber {
fn targets(&self, repository: &str) -> bool {
let Some(url) = self
.base
.as_ref()
.and_then(|base| base.repo.as_ref())
.map(|repo| repo.url.to_ascii_lowercase())
else {
return false;
};
url.ends_with(&format!("/repos/{}", repository.to_ascii_lowercase()))
}
}
#[derive(Deserialize)]
struct PullRequestBody {
user: Option<Account>,
head: PullRequestHead,
base: PullRequestBaseRef,
}
#[derive(Deserialize)]
struct PullRequestBaseRef {
#[serde(rename = "ref")]
branch: String,
}
#[derive(Deserialize)]
struct PullRequestHead {
#[serde(rename = "ref")]
branch: String,
repo: Option<RepositoryName>,
}
#[derive(Deserialize)]
struct RepositoryName {
full_name: String,
}
#[derive(Deserialize)]
struct CompareBody {
status: String,
}
#[derive(Deserialize)]
struct BranchBody {
commit: CommitBody,
}
#[derive(Deserialize)]
struct CommitBody {
sha: String,
}
#[derive(Deserialize)]
struct RepositoryBody {
default_branch: String,
visibility: api::Visibility,
}
#[derive(Deserialize)]
struct Runners {
runners: Vec<Runner>,
}
#[derive(Deserialize)]
struct JitResponse {
runner: Runner,
encoded_jit_config: String,
}
#[derive(Deserialize)]
struct ErrorBody {
message: String,
}
impl Github {
pub fn new(base: &str, token_source: TokenSource) -> Result<Github, Error> {
install_crypto_provider();
let mut headers = HeaderMap::new();
headers.insert(
header::ACCEPT,
HeaderValue::from_static("application/vnd.github+json"),
);
headers.insert(
"x-github-api-version",
HeaderValue::from_static("2022-11-28"),
);
let http = reqwest::Client::builder()
.user_agent(concat!("runnerd/", env!("CARGO_PKG_VERSION")))
.default_headers(headers)
.timeout(REQUEST_TIMEOUT)
.build()
.map_err(|source| Error::Http {
method: Method::GET,
path: base.to_owned(),
source,
})?;
Ok(Github {
http,
base: base.trim_end_matches('/').to_owned(),
tokens: Tokens::new(Arc::from(token_source)),
etags: Mutex::new(Conditional::new(CONDITIONAL_BYTES, CONDITIONAL_FRESH)),
workflows: Mutex::new(Workflows::default()),
resolving: tokio::sync::Mutex::new(()),
blocked_until: Mutex::new(None),
requests: Mutex::new(Counter::new(Instant::now(), requests::WINDOW)),
})
}
pub fn requests(&self) -> Report {
lock(&self.requests).peek(Instant::now())
}
fn count(&self, class: Class, status: StatusCode, headers: &HeaderMap) {
let due = {
let mut requests = lock(&self.requests);
requests.record(class, status, headers);
requests.due(Instant::now())
};
if let Some(report) = due {
info!("{}", report.line(SystemTime::now()));
}
}
pub fn blocked_until(&self) -> Option<SystemTime> {
let blocked = *lock(&self.blocked_until);
blocked.filter(|until| *until > SystemTime::now())
}
async fn workflow_name(
&self,
repository: &str,
run: u64,
) -> Result<Option<String>, Unresolved> {
match lock(&self.workflows).next(repository, run, Instant::now()) {
Next::Named(name) => return Ok(Some(name)),
Next::Unnamed => return Ok(None),
Next::Waiting => return Err(Unresolved),
Next::ReadRun | Next::ReadList(_) => {}
}
let _reading = self.resolving.lock().await;
for _ in 0..3 {
let next = lock(&self.workflows).next(repository, run, Instant::now());
match next {
Next::Named(name) => return Ok(Some(name)),
Next::Unnamed => return Ok(None),
Next::Waiting => return Err(Unresolved),
Next::ReadRun => {
let path = format!("/repos/{repository}/actions/runs/{run}");
let body: Result<RunWorkflow, Error> = self.get(&path).await;
lock(&self.etags).remove(&path);
let mut workflows = lock(&self.workflows);
match body {
Ok(body) => workflows.run(repository, run, body.workflow_id),
Err(error) if error.is_gone() => workflows.run(repository, run, None),
Err(_) => workflows.failed(repository, Instant::now()),
}
}
Next::ReadList(workflow) => {
let path = format!("/repos/{repository}/actions/workflows?per_page={PER_PAGE}");
let pages: Result<Vec<WorkflowList>, Error> = self.get_pages(&path).await;
let mut workflows = lock(&self.workflows);
match pages {
Ok(pages) => {
let names = pages
.into_iter()
.flat_map(|page| page.workflows)
.filter_map(|listed| Some((listed.id, listed.name?)))
.collect();
return Ok(workflows.listed(
repository,
names,
workflow,
Instant::now(),
));
}
Err(_) => workflows.failed(repository, Instant::now()),
}
}
}
}
Err(Unresolved)
}
async fn get<T: DeserializeOwned>(&self, path: &str) -> Result<T, Error> {
self.get_page(path).await.map(|page| page.body)
}
async fn get_page<T: DeserializeOwned>(&self, path: &str) -> Result<Page<T>, Error> {
let cached = lock(&self.etags).get(path, Instant::now());
let etag = cached.as_ref().map(|cached| &cached.etag);
let response = self.send(Method::GET, path, None, etag).await?;
let (bytes, next) = if response.status == StatusCode::NOT_MODIFIED {
match cached {
Some(cached) => (cached.body, cached.next),
None => {
return Err(Error::Status {
method: Method::GET,
path: path.to_owned(),
status: response.status,
message: "not modified, but nothing is cached".to_owned(),
});
}
}
} else {
let etag = response.etag.clone();
let response = expect_success(Method::GET, path, response)?;
let body: Arc<[u8]> = response.bytes.into();
if let Some(etag) = etag {
lock(&self.etags).insert(
path,
Cached {
etag,
body: body.clone(),
next: response.next,
},
Instant::now(),
);
}
(body, response.next)
};
Ok(Page {
body: decode(path, &bytes)?,
next,
})
}
async fn get_pages<T: DeserializeOwned>(&self, path: &str) -> Result<Vec<T>, Error> {
let mut pages = Vec::new();
for page in 1..=MAX_PAGES {
let path = if page == 1 {
path.to_owned()
} else {
format!("{path}&page={page}")
};
let answer: Page<T> = self.get_page(&path).await?;
pages.push(answer.body);
if !answer.next {
break;
}
}
Ok(pages)
}
async fn send(
&self,
method: Method,
path: &str,
body: Option<&serde_json::Value>,
etag: Option<&HeaderValue>,
) -> Result<Response, Error> {
let Ok(answered) = self
.exchange(&method, path, body, etag, || async {
Ok::<(), std::convert::Infallible>(())
})
.await?;
let Answered { response, limited } = answered;
let status = response.status();
if let Some(until) = limited
&& matches!(
status,
StatusCode::FORBIDDEN | StatusCode::TOO_MANY_REQUESTS
)
{
return Err(Error::RateLimited { until });
}
let etag = response.headers().get(header::ETAG).cloned();
let next = names_next_page(response.headers());
let bytes = response.bytes().await.map_err(|source| Error::Http {
method: method.clone(),
path: path.to_owned(),
source: source.without_url(),
})?;
Ok(Response {
status,
etag,
next,
bytes: bytes.to_vec(),
})
}
async fn exchange<W, Allowed: Future<Output = Result<(), W>>>(
&self,
method: &Method,
path: &str,
body: Option<&serde_json::Value>,
etag: Option<&HeaderValue>,
permit: impl Fn() -> Allowed,
) -> Result<Result<Answered, W>, Error> {
if let Some(until) = self.blocked_until() {
return Err(Error::RateLimited { until });
}
let class = Class::of(method, path);
let mut refused: Option<Token> = None;
loop {
let token = self.tokens.get(refused.as_ref()).await?;
if let Err(withdrawn) = permit().await {
return Ok(Err(withdrawn));
}
let mut request = self
.http
.request(method.clone(), format!("{}{path}", self.base))
.bearer_auth(&token.0);
if let Some(body) = body {
request = request
.header(header::CONTENT_TYPE, "application/json")
.body(body.to_string());
}
if let Some(etag) = etag {
request = request.header(header::IF_NONE_MATCH, etag.clone());
}
let response = request.send().await.map_err(|source| Error::Http {
method: method.clone(),
path: path.to_owned(),
source: source.without_url(),
})?;
let status = response.status();
self.count(class, status, response.headers());
if status == StatusCode::UNAUTHORIZED && refused.is_none() {
refused = Some(token);
continue;
}
let limited = rate_limited_until(status, response.headers(), SystemTime::now());
if let Some(until) = limited {
*lock(&self.blocked_until) = Some(until);
}
return Ok(Ok(Answered { response, limited }));
}
}
}
struct Answered {
response: reqwest::Response,
limited: Option<SystemTime>,
}
impl GithubApi for Github {
async fn queued_jobs(&self, repository: &str) -> Result<Vec<QueuedJob>, Error> {
let mut runs = Vec::new();
for (state, status) in [
(RunState::Waiting, "queued"),
(RunState::Started, "in_progress"),
] {
let pages: Vec<Runs> = self
.get_pages(&format!(
"/repos/{repository}/actions/runs?status={status}&per_page={PER_PAGE}"
))
.await?;
let listed = pages.into_iter().flat_map(|page| page.workflow_runs);
let mut workflows = lock(&self.workflows);
runs.extend(listed.map(|run| {
if let Some(workflow) = run.workflow_id {
workflows.run(repository, run.id, Some(workflow));
}
(state, run.id)
}));
}
runs.sort_unstable_by_key(|&(state, id)| (id, state));
runs.dedup_by_key(|&mut (_, id)| id);
runs.sort_unstable();
let listed: HashSet<u64> = runs.iter().map(|&(_, id)| id).collect();
let mut jobs = Vec::new();
let mut requests = 0;
for &(_, run) in &runs {
if requests >= JOB_REQUESTS {
break;
}
let pages: Vec<Jobs> = self.get_pages(&jobs_path(repository, run)).await?;
requests += pages.len();
let queued: Vec<Job> = pages
.into_iter()
.flat_map(|page| page.jobs)
.filter(|job| job.status == "queued")
.collect();
let Some(first) = queued.first() else {
continue;
};
let workflow = match self.workflow_name(repository, first.run_id).await {
Ok(Some(name)) => Some(name),
Ok(None) | Err(Unresolved) => first.workflow_name.clone(),
};
jobs.extend(queued.into_iter().map(|job| {
QueuedJob {
id: job.id,
run_id: job.run_id,
name: job.name,
labels: job.labels,
created_at: job
.created_at
.as_deref()
.and_then(time::parse_ms)
.unwrap_or(0),
run_attempt: job.run_attempt.unwrap_or(1),
workflow: workflow.clone(),
branch: job.head_branch,
url: job.html_url,
}
}));
}
let runs_prefix = format!("/repos/{repository}/actions/runs/");
lock(&self.etags).retain(|path| {
match path
.strip_prefix(&runs_prefix)
.and_then(|rest| rest.split_once("/jobs?"))
{
Some((run, _)) => run.parse().is_ok_and(|run: u64| listed.contains(&run)),
None => true,
}
});
jobs.sort_by_key(|job| job.id);
jobs.dedup_by_key(|job| job.id);
Ok(jobs)
}
async fn runners(&self, repository: &str) -> Result<Vec<Runner>, Error> {
let pages: Vec<Runners> = self
.get_pages(&format!(
"/repos/{repository}/actions/runners?per_page={PER_PAGE}"
))
.await?;
let mut seen = HashSet::new();
Ok(pages
.into_iter()
.flat_map(|page| page.runners)
.filter(|runner| seen.insert(runner.id))
.collect())
}
async fn jit_config(
&self,
repository: &str,
name: &str,
labels: &[String],
) -> Result<(Runner, JitConfig), Error> {
let body = serde_json::json!({
"name": name,
"runner_group_id": 1,
"labels": labels,
"work_folder": "_work",
});
let path = format!("/repos/{repository}/actions/runners/generate-jitconfig");
let response = self.send(Method::POST, &path, Some(&body), None).await?;
let response = expect_success(Method::POST, &path, response)?;
let jit: JitResponse = decode(&path, &response.bytes)?;
Ok((jit.runner, JitConfig(jit.encoded_jit_config)))
}
async fn delete_runner(&self, repository: &str, id: u64) -> Result<Deleted, Error> {
let path = format!("/repos/{repository}/actions/runners/{id}");
let response = self.send(Method::DELETE, &path, None, None).await?;
match response.status {
StatusCode::NOT_FOUND => Ok(Deleted::NotFound),
StatusCode::UNPROCESSABLE_ENTITY => Ok(Deleted::Busy),
_ => expect_success(Method::DELETE, &path, response).map(|_| Deleted::Deleted),
}
}
async fn job(&self, repository: &str, id: u64) -> Result<JobDetails, Error> {
let path = format!("/repos/{repository}/actions/jobs/{id}");
let mut job: JobDetails = self.get(&path).await?;
if job.status == "completed" {
lock(&self.etags).remove(&path);
}
match self.workflow_name(repository, job.run_id).await {
Ok(Some(name)) => job.workflow_name = Some(name),
Ok(None) => {}
Err(Unresolved) => lock(&self.workflows).owe(repository, job.run_id),
}
Ok(job)
}
async fn run_trigger(&self, repository: &str, run: u64) -> Result<RunTrigger, Error> {
let run_id = run;
let path = format!("/repos/{repository}/actions/runs/{run}");
let run: Result<RunBody, Error> = self.get(&path).await;
lock(&self.etags).remove(&path);
let run = run?;
if let Some(workflow) = run.workflow_id {
lock(&self.workflows).run(repository, run_id, Some(workflow));
}
Ok(RunTrigger {
event: run.event,
head_branch: run.head_branch,
head_repository: run.head_repository.map(|head| head.full_name),
head_sha: run.head_sha,
by_user: Account::is_user(run.actor.as_ref())
&& Account::is_user(run.triggering_actor.as_ref()),
pull_requests: run
.pull_requests
.into_iter()
.filter(|pull| pull.targets(repository))
.map(|pull| pull.number)
.collect(),
})
}
async fn pull_request(&self, repository: &str, number: u64) -> Result<PullRequest, Error> {
let path = format!("/repos/{repository}/pulls/{number}");
let pull: Result<PullRequestBody, Error> = self.get(&path).await;
lock(&self.etags).remove(&path);
let pull = pull?;
Ok(PullRequest {
by_user: Account::is_user(pull.user.as_ref()),
head_repository: pull.head.repo.map(|repo| repo.full_name),
head_branch: pull.head.branch,
base_branch: pull.base.branch,
})
}
async fn repository(&self, repository: &str) -> Result<RepositoryDetails, Error> {
let body: RepositoryBody = self.get(&format!("/repos/{repository}")).await?;
Ok(RepositoryDetails {
default_branch: body.default_branch,
visibility: body.visibility,
})
}
async fn on_branch(&self, repository: &str, branch: &str, sha: &str) -> Result<bool, Error> {
let commit =
|sha: &str| !sha.is_empty() && sha.bytes().all(|byte| byte.is_ascii_hexdigit());
if !commit(sha) {
return Ok(false);
}
let base: String = branch
.bytes()
.map(|byte| {
if byte.is_ascii_alphanumeric() || matches!(byte, b'/' | b'-' | b'_' | b'.') {
char::from(byte).to_string()
} else {
format!("%{byte:02X}")
}
})
.collect();
let path = format!("/repos/{repository}/branches/{base}");
let head: Result<BranchBody, Error> = self.get(&path).await;
lock(&self.etags).remove(&path);
let head = head?.commit.sha;
if !commit(&head) {
return Ok(false);
}
let path = format!("/repos/{repository}/compare/{head}...{sha}?per_page=1");
let body: Result<CompareBody, Error> = self.get(&path).await;
lock(&self.etags).remove(&path);
Ok(matches!(body?.status.as_str(), "identical" | "behind"))
}
async fn cancel_run(&self, repository: &str, run: u64) -> Result<(), Error> {
let path = format!("/repos/{repository}/actions/runs/{run}/cancel");
let response = self.send(Method::POST, &path, None, None).await?;
if response.status == StatusCode::CONFLICT {
return Ok(());
}
expect_success(Method::POST, &path, response).map(|_| ())
}
async fn job_action(
&self,
repository: &str,
request: JobActionRequest,
permit: &Permit<'_>,
) -> Result<u16, ActionFailure> {
let path = request.path(repository);
let allowed = || async {
if permit().await {
Ok(())
} else {
Err(Withdrawn)
}
};
let answered = match self
.exchange(&Method::POST, &path, None, None, allowed)
.await
{
Ok(Ok(answered)) => answered,
Ok(Err(Withdrawn)) => return Err(ActionFailure::Withdrawn),
Err(error) => return Err(ActionFailure::of(&error)),
};
let status = answered.response.status();
if let Some(until) = answered.limited
&& matches!(
status,
StatusCode::FORBIDDEN | StatusCode::TOO_MANY_REQUESTS
)
{
return Err(ActionFailure::RateLimited {
status: Some(status.as_u16()),
until,
});
}
if status.is_success() {
Ok(status.as_u16())
} else {
Err(ActionFailure::Status {
status: status.as_u16(),
})
}
}
async fn action_run(&self, repository: &str, run: u64) -> Result<RunNow, ActionFailure> {
let body: RunAttemptBody = self
.get(&format!("/repos/{repository}/actions/runs/{run}"))
.await
.map_err(|error| ActionFailure::of(&error))?;
Ok(RunNow {
attempt: body.run_attempt,
completed: body.status.as_deref() == Some("completed"),
})
}
async fn named_runs(&self) -> Vec<NamedRun> {
let owed = lock(&self.workflows).owed();
let mut named = Vec::new();
for (repository, runs) in owed {
for run in runs {
match self.workflow_name(&repository, run).await {
Ok(Some(workflow)) => named.push(NamedRun {
repository: repository.clone(),
run_id: run,
workflow,
}),
Ok(None) => {}
Err(Unresolved) => break,
}
lock(&self.workflows).paid(&repository, run);
}
}
named
}
fn spend(&self) -> Spend {
lock(&self.requests).spend()
}
}
fn jobs_path(repository: &str, run: u64) -> String {
format!("/repos/{repository}/actions/runs/{run}/jobs?filter=latest&per_page={PER_PAGE}")
}
struct Response {
status: StatusCode,
etag: Option<HeaderValue>,
next: bool,
bytes: Vec<u8>,
}
struct Page<T> {
body: T,
next: bool,
}
fn names_next_page(headers: &HeaderMap) -> bool {
headers
.get_all(header::LINK)
.iter()
.filter_map(|value| value.to_str().ok())
.flat_map(|value| value.split(','))
.any(|link| {
link.split(';')
.skip(1)
.any(|param| param.trim() == r#"rel="next""#)
})
}
#[derive(Clone)]
struct Cached {
etag: HeaderValue,
body: Arc<[u8]>,
next: bool,
}
struct Conditional {
entries: HashMap<String, Use>,
bytes: usize,
limit: usize,
fresh: Duration,
}
struct Use {
cached: Cached,
used: Instant,
}
impl Conditional {
fn new(limit: usize, fresh: Duration) -> Conditional {
Conditional {
entries: HashMap::new(),
bytes: 0,
limit,
fresh,
}
}
fn size(path: &str, cached: &Cached) -> usize {
path.len() + cached.body.len()
}
fn get(&mut self, path: &str, now: Instant) -> Option<Cached> {
let entry = self.entries.get_mut(path)?;
entry.used = now;
Some(entry.cached.clone())
}
fn insert(&mut self, path: &str, cached: Cached, now: Instant) {
self.remove(path);
let size = Conditional::size(path, &cached);
if size > self.limit {
return;
}
while self.bytes + size > self.limit {
let stale = self
.entries
.iter()
.filter(|(_, entry)| now.saturating_duration_since(entry.used) >= self.fresh)
.min_by_key(|(_, entry)| entry.used)
.map(|(path, _)| path.clone());
let Some(stale) = stale else {
return;
};
self.remove(&stale);
}
self.bytes += size;
self.entries
.insert(path.to_owned(), Use { cached, used: now });
}
fn remove(&mut self, path: &str) {
if let Some(entry) = self.entries.remove(path) {
self.bytes -= Conditional::size(path, &entry.cached);
}
}
fn retain(&mut self, mut keep: impl FnMut(&str) -> bool) {
let bytes = &mut self.bytes;
self.entries.retain(|path, entry| {
let kept = keep(path);
if !kept {
*bytes -= Conditional::size(path, &entry.cached);
}
kept
});
}
}
fn expect_success(method: Method, path: &str, response: Response) -> Result<Response, Error> {
if response.status.is_success() {
return Ok(response);
}
let message = serde_json::from_slice::<ErrorBody>(&response.bytes).map_or_else(
|_| String::from_utf8_lossy(&response.bytes).into_owned(),
|body| body.message,
);
Err(Error::Status {
method,
path: path.to_owned(),
status: response.status,
message,
})
}
fn decode<T: DeserializeOwned>(path: &str, bytes: &[u8]) -> Result<T, Error> {
serde_json::from_slice(bytes).map_err(|source| Error::Decode {
path: path.to_owned(),
source,
})
}
fn header_number(headers: &HeaderMap, name: &str) -> Option<u64> {
headers
.get(name)
.and_then(|value| value.to_str().ok())
.and_then(|value| value.trim().parse().ok())
}
fn rate_limited_until(
status: StatusCode,
headers: &HeaderMap,
now: SystemTime,
) -> Option<SystemTime> {
let number = |name: &str| header_number(headers, name);
if number("x-ratelimit-remaining") == Some(0)
&& let Some(reset) = number("x-ratelimit-reset")
{
return Some(SystemTime::UNIX_EPOCH + Duration::from_secs(reset));
}
if matches!(
status,
StatusCode::FORBIDDEN | StatusCode::TOO_MANY_REQUESTS
) && let Some(seconds) = number("retry-after")
{
return Some(now + Duration::from_secs(seconds));
}
None
}
#[cfg(test)]
mod tests;