1use std::collections::{HashMap, HashSet};
8use std::fmt;
9use std::sync::{Arc, Mutex};
10use std::time::{Duration, Instant, SystemTime};
11
12use reqwest::header::{self, HeaderMap, HeaderValue};
13use reqwest::{Method, StatusCode};
14use serde::Deserialize;
15use serde::de::DeserializeOwned;
16use tracing::info;
17
18use crate::api;
19use crate::docker::JitConfig;
20use crate::sync::lock;
21use crate::time;
22
23mod requests;
24
25use requests::Counter;
26pub use requests::{Class, RateLimit, Report, Spend, Tally};
27
28pub const API_URL: &str = "https://api.github.com";
30
31const REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
34
35const PER_PAGE: u32 = 100;
37
38const MAX_PAGES: u32 = 10;
42
43const JOB_REQUESTS: usize = 200;
49
50const CONDITIONAL_BYTES: usize = 32 * 1024 * 1024;
53
54const CONDITIONAL_FRESH: Duration = Duration::from_secs(600);
60
61#[derive(Clone)]
63pub struct Token(String);
64
65impl fmt::Debug for Token {
66 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 f.write_str("Token(<redacted>)")
68 }
69}
70
71pub type TokenSource = Box<dyn Fn() -> Result<Token, Error> + Send + Sync>;
73
74pub fn gh_token() -> Result<Token, Error> {
76 let output = std::process::Command::new("gh")
77 .args(["auth", "token", "--hostname", "github.com"])
78 .output()
79 .map_err(|error| Error::Token(format!("cannot run gh: {error}")))?;
80 if !output.status.success() {
81 return Err(Error::Token(format!(
82 "gh auth token failed: {}",
83 String::from_utf8_lossy(&output.stderr).trim()
84 )));
85 }
86 let token = String::from_utf8(output.stdout)
87 .map_err(|_| Error::Token("gh auth token printed a token that is not UTF-8".to_owned()))?;
88 Ok(Token(token.trim().to_owned()))
89}
90
91pub fn install_crypto_provider() {
94 let _ = rustls::crypto::ring::default_provider().install_default();
96}
97
98#[derive(Debug, thiserror::Error)]
100pub enum Error {
101 #[error("GitHub's rate limit is used up until {}s after the epoch", secs(*until))]
103 RateLimited {
104 until: SystemTime,
106 },
107 #[error("GitHub answered {status} to {method} {path}: {message}")]
109 Status {
110 method: Method,
112 path: String,
114 status: StatusCode,
116 message: String,
118 },
119 #[error("{method} {path}: {source}")]
121 Http {
122 method: Method,
124 path: String,
126 source: reqwest::Error,
128 },
129 #[error("{path}: unexpected response: {source}")]
131 Decode {
132 path: String,
134 source: serde_json::Error,
136 },
137 #[error("no GitHub token: {0}")]
139 Token(String),
140}
141
142impl Error {
143 pub fn is_gone(&self) -> bool {
146 matches!(
147 self,
148 Error::Status {
149 status: StatusCode::NOT_FOUND | StatusCode::GONE,
150 ..
151 }
152 )
153 }
154}
155
156fn secs(time: SystemTime) -> u64 {
157 time.duration_since(SystemTime::UNIX_EPOCH)
158 .unwrap_or_default()
159 .as_secs()
160}
161
162#[derive(Debug, Clone, Default, PartialEq, Eq)]
164pub struct QueuedJob {
165 pub id: u64,
167 pub run_id: u64,
169 pub name: String,
171 pub labels: Vec<String>,
173 pub created_at: u64,
175 pub run_attempt: u32,
177 pub workflow: Option<String>,
179 pub branch: Option<String>,
181 pub url: Option<String>,
183}
184
185#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
187pub struct JobDetails {
188 pub id: u64,
190 pub run_id: u64,
192 #[serde(default = "first_attempt")]
194 pub run_attempt: u32,
195 pub name: String,
197 pub workflow_name: Option<String>,
199 pub head_branch: Option<String>,
201 pub html_url: Option<String>,
203 #[serde(default)]
205 pub labels: Vec<String>,
206 pub created_at: Option<String>,
208 pub status: String,
210 pub conclusion: Option<String>,
212 pub started_at: Option<String>,
214 pub completed_at: Option<String>,
216 pub runner_name: Option<String>,
218 #[serde(default)]
220 pub steps: Vec<JobStep>,
221}
222
223#[derive(Debug, Clone, PartialEq, Eq)]
225pub struct RunTrigger {
226 pub event: String,
228 pub head_branch: Option<String>,
230 pub head_repository: Option<String>,
232 pub head_sha: String,
235 pub by_user: bool,
238 pub pull_requests: Vec<u64>,
242}
243
244#[derive(Debug, Clone, PartialEq, Eq)]
246pub struct PullRequest {
247 pub by_user: bool,
249 pub head_repository: Option<String>,
251 pub head_branch: String,
253 pub base_branch: String,
255}
256
257#[derive(Debug, Clone, PartialEq, Eq)]
259pub struct RepositoryDetails {
260 pub default_branch: String,
262 pub visibility: api::Visibility,
264}
265
266fn first_attempt() -> u32 {
267 1
268}
269
270#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
272pub struct JobStep {
273 pub number: u32,
275 pub name: String,
277 pub status: String,
279 pub conclusion: Option<String>,
281 pub started_at: Option<String>,
283 pub completed_at: Option<String>,
285}
286
287#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
289pub struct Runner {
290 pub id: u64,
292 pub name: String,
294 pub status: String,
296 pub busy: bool,
298}
299
300#[derive(Debug, Clone, Copy, PartialEq, Eq)]
302pub enum Deleted {
303 Deleted,
305 NotFound,
307 Busy,
309}
310
311pub trait GithubApi: Send + Sync {
313 fn queued_jobs(
315 &self,
316 repository: &str,
317 ) -> impl Future<Output = Result<Vec<QueuedJob>, Error>> + Send;
318 fn runners(&self, repository: &str) -> impl Future<Output = Result<Vec<Runner>, Error>> + Send;
320 fn jit_config(
322 &self,
323 repository: &str,
324 name: &str,
325 labels: &[String],
326 ) -> impl Future<Output = Result<(Runner, JitConfig), Error>> + Send;
327 fn delete_runner(
329 &self,
330 repository: &str,
331 id: u64,
332 ) -> impl Future<Output = Result<Deleted, Error>> + Send;
333 fn job(
335 &self,
336 repository: &str,
337 id: u64,
338 ) -> impl Future<Output = Result<JobDetails, Error>> + Send;
339 fn run_trigger(
341 &self,
342 repository: &str,
343 run: u64,
344 ) -> impl Future<Output = Result<RunTrigger, Error>> + Send;
345 fn pull_request(
347 &self,
348 repository: &str,
349 number: u64,
350 ) -> impl Future<Output = Result<PullRequest, Error>> + Send;
351 fn repository(
353 &self,
354 repository: &str,
355 ) -> impl Future<Output = Result<RepositoryDetails, Error>> + Send;
356 fn on_branch(
359 &self,
360 repository: &str,
361 branch: &str,
362 sha: &str,
363 ) -> impl Future<Output = Result<bool, Error>> + Send;
364 fn cancel_run(
367 &self,
368 repository: &str,
369 run: u64,
370 ) -> impl Future<Output = Result<(), Error>> + Send;
371 fn spend(&self) -> Spend;
373}
374
375pub struct Github {
378 http: reqwest::Client,
379 base: String,
380 token_source: Arc<dyn Fn() -> Result<Token, Error> + Send + Sync>,
382 token: tokio::sync::Mutex<Option<Token>>,
385 etags: Mutex<Conditional>,
387 blocked_until: Mutex<Option<SystemTime>>,
388 requests: Mutex<Counter>,
390}
391
392impl Drop for Github {
393 fn drop(&mut self) {
395 let report = lock(&self.requests).take(Instant::now());
396 if !report.is_empty() {
397 info!("{}", report.line(SystemTime::now()));
398 }
399 }
400}
401
402impl fmt::Debug for Github {
403 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
404 f.debug_struct("Github")
405 .field("base", &self.base)
406 .finish_non_exhaustive()
407 }
408}
409
410#[derive(Deserialize)]
411struct Runs {
412 workflow_runs: Vec<Run>,
413}
414
415#[derive(Deserialize)]
416struct Run {
417 id: u64,
418}
419
420#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
422enum RunState {
423 Waiting,
424 Started,
425}
426
427#[derive(Deserialize)]
428struct Jobs {
429 jobs: Vec<Job>,
430}
431
432#[derive(Deserialize)]
433struct Job {
434 id: u64,
435 run_id: u64,
436 name: String,
437 status: String,
438 labels: Vec<String>,
439 created_at: Option<String>,
440 run_attempt: Option<u32>,
441 workflow_name: Option<String>,
442 head_branch: Option<String>,
443 html_url: Option<String>,
444}
445
446#[derive(Deserialize)]
447struct RunBody {
448 event: String,
449 head_branch: Option<String>,
450 head_repository: Option<RepositoryName>,
451 head_sha: String,
452 actor: Option<Account>,
453 triggering_actor: Option<Account>,
454 #[serde(default)]
455 pull_requests: Vec<PullRequestNumber>,
456}
457
458#[derive(Deserialize)]
459struct Account {
460 #[serde(rename = "type")]
461 kind: String,
462}
463
464impl Account {
465 fn is_user(account: Option<&Account>) -> bool {
467 account.is_some_and(|account| account.kind == "User")
468 }
469}
470
471#[derive(Deserialize)]
472struct PullRequestNumber {
473 number: u64,
474 base: Option<PullRequestBase>,
475}
476
477#[derive(Deserialize)]
478struct PullRequestBase {
479 repo: Option<RepositoryUrl>,
480}
481
482#[derive(Deserialize)]
483struct RepositoryUrl {
484 url: String,
485}
486
487impl PullRequestNumber {
488 fn targets(&self, repository: &str) -> bool {
491 let Some(url) = self
492 .base
493 .as_ref()
494 .and_then(|base| base.repo.as_ref())
495 .map(|repo| repo.url.to_ascii_lowercase())
496 else {
497 return false;
498 };
499 url.ends_with(&format!("/repos/{}", repository.to_ascii_lowercase()))
500 }
501}
502
503#[derive(Deserialize)]
504struct PullRequestBody {
505 user: Option<Account>,
506 head: PullRequestHead,
507 base: PullRequestBaseRef,
508}
509
510#[derive(Deserialize)]
511struct PullRequestBaseRef {
512 #[serde(rename = "ref")]
513 branch: String,
514}
515
516#[derive(Deserialize)]
517struct PullRequestHead {
518 #[serde(rename = "ref")]
519 branch: String,
520 repo: Option<RepositoryName>,
521}
522
523#[derive(Deserialize)]
524struct RepositoryName {
525 full_name: String,
526}
527
528#[derive(Deserialize)]
529struct CompareBody {
530 status: String,
531}
532
533#[derive(Deserialize)]
534struct BranchBody {
535 commit: CommitBody,
536}
537
538#[derive(Deserialize)]
539struct CommitBody {
540 sha: String,
541}
542
543#[derive(Deserialize)]
544struct RepositoryBody {
545 default_branch: String,
546 visibility: api::Visibility,
547}
548
549#[derive(Deserialize)]
550struct Runners {
551 runners: Vec<Runner>,
552}
553
554#[derive(Deserialize)]
555struct JitResponse {
556 runner: Runner,
557 encoded_jit_config: String,
558}
559
560#[derive(Deserialize)]
561struct ErrorBody {
562 message: String,
563}
564
565impl Github {
566 pub fn new(base: &str, token_source: TokenSource) -> Result<Github, Error> {
568 install_crypto_provider();
569 let mut headers = HeaderMap::new();
570 headers.insert(
571 header::ACCEPT,
572 HeaderValue::from_static("application/vnd.github+json"),
573 );
574 headers.insert(
575 "x-github-api-version",
576 HeaderValue::from_static("2022-11-28"),
577 );
578 let http = reqwest::Client::builder()
579 .user_agent(concat!("runnerd/", env!("CARGO_PKG_VERSION")))
580 .default_headers(headers)
581 .timeout(REQUEST_TIMEOUT)
582 .build()
583 .map_err(|source| Error::Http {
584 method: Method::GET,
585 path: base.to_owned(),
586 source,
587 })?;
588 Ok(Github {
589 http,
590 base: base.trim_end_matches('/').to_owned(),
591 token_source: Arc::from(token_source),
592 token: tokio::sync::Mutex::new(None),
593 etags: Mutex::new(Conditional::new(CONDITIONAL_BYTES, CONDITIONAL_FRESH)),
594 blocked_until: Mutex::new(None),
595 requests: Mutex::new(Counter::new(Instant::now(), requests::WINDOW)),
596 })
597 }
598
599 pub fn requests(&self) -> Report {
601 lock(&self.requests).peek(Instant::now())
602 }
603
604 fn count(&self, class: Class, status: StatusCode, headers: &HeaderMap) {
606 let due = {
607 let mut requests = lock(&self.requests);
608 requests.record(class, status, headers);
609 requests.due(Instant::now())
610 };
611 if let Some(report) = due {
612 info!("{}", report.line(SystemTime::now()));
613 }
614 }
615
616 pub fn blocked_until(&self) -> Option<SystemTime> {
618 let blocked = *lock(&self.blocked_until);
619 blocked.filter(|until| *until > SystemTime::now())
620 }
621
622 async fn get<T: DeserializeOwned>(&self, path: &str) -> Result<T, Error> {
623 self.get_page(path).await.map(|page| page.body)
624 }
625
626 async fn get_page<T: DeserializeOwned>(&self, path: &str) -> Result<Page<T>, Error> {
628 let cached = lock(&self.etags).get(path, Instant::now());
630 let etag = cached.as_ref().map(|cached| &cached.etag);
631 let response = self.send(Method::GET, path, None, etag).await?;
632 let (bytes, next) = if response.status == StatusCode::NOT_MODIFIED {
633 match cached {
634 Some(cached) => (cached.body, cached.next),
635 None => {
636 return Err(Error::Status {
637 method: Method::GET,
638 path: path.to_owned(),
639 status: response.status,
640 message: "not modified, but nothing is cached".to_owned(),
641 });
642 }
643 }
644 } else {
645 let etag = response.etag.clone();
646 let response = expect_success(Method::GET, path, response)?;
647 let body: Arc<[u8]> = response.bytes.into();
648 if let Some(etag) = etag {
649 lock(&self.etags).insert(
650 path,
651 Cached {
652 etag,
653 body: body.clone(),
654 next: response.next,
655 },
656 Instant::now(),
657 );
658 }
659 (body, response.next)
660 };
661 Ok(Page {
662 body: decode(path, &bytes)?,
663 next,
664 })
665 }
666
667 async fn get_pages<T: DeserializeOwned>(&self, path: &str) -> Result<Vec<T>, Error> {
672 let mut pages = Vec::new();
673 for page in 1..=MAX_PAGES {
674 let path = if page == 1 {
675 path.to_owned()
676 } else {
677 format!("{path}&page={page}")
678 };
679 let answer: Page<T> = self.get_page(&path).await?;
680 pages.push(answer.body);
681 if !answer.next {
682 break;
683 }
684 }
685 Ok(pages)
686 }
687
688 async fn send(
691 &self,
692 method: Method,
693 path: &str,
694 body: Option<&serde_json::Value>,
695 etag: Option<&HeaderValue>,
696 ) -> Result<Response, Error> {
697 if let Some(until) = self.blocked_until() {
698 return Err(Error::RateLimited { until });
699 }
700 let class = Class::of(&method, path);
701 let mut refused: Option<Token> = None;
703 loop {
704 let token = self.token(refused.as_ref()).await?;
705 let mut request = self
706 .http
707 .request(method.clone(), format!("{}{path}", self.base))
708 .bearer_auth(&token.0);
709 if let Some(body) = body {
710 request = request
711 .header(header::CONTENT_TYPE, "application/json")
712 .body(body.to_string());
713 }
714 if let Some(etag) = etag {
715 request = request.header(header::IF_NONE_MATCH, etag.clone());
716 }
717 let response = request.send().await.map_err(|source| Error::Http {
718 method: method.clone(),
719 path: path.to_owned(),
720 source: source.without_url(),
721 })?;
722 let status = response.status();
723 self.count(class, status, response.headers());
724 if status == StatusCode::UNAUTHORIZED && refused.is_none() {
725 refused = Some(token);
726 continue;
727 }
728 if let Some(until) = rate_limited_until(status, response.headers(), SystemTime::now()) {
729 *lock(&self.blocked_until) = Some(until);
730 if matches!(
731 status,
732 StatusCode::FORBIDDEN | StatusCode::TOO_MANY_REQUESTS
733 ) {
734 return Err(Error::RateLimited { until });
735 }
736 }
737 let etag = response.headers().get(header::ETAG).cloned();
738 let next = names_next_page(response.headers());
739 let bytes = response.bytes().await.map_err(|source| Error::Http {
740 method: method.clone(),
741 path: path.to_owned(),
742 source: source.without_url(),
743 })?;
744 return Ok(Response {
745 status,
746 etag,
747 next,
748 bytes: bytes.to_vec(),
749 });
750 }
751 }
752
753 async fn token(&self, refused: Option<&Token>) -> Result<Token, Error> {
756 let mut token = self.token.lock().await;
757 let stale = match (&*token, refused) {
758 (None, _) => true,
759 (Some(current), Some(refused)) => current.0 == refused.0,
760 (Some(_), None) => false,
761 };
762 if stale {
763 let source = self.token_source.clone();
764 *token = Some(
765 tokio::task::spawn_blocking(move || source())
766 .await
767 .map_err(|error| {
768 Error::Token(format!("the token source stopped: {error}"))
769 })??,
770 );
771 }
772 token
773 .clone()
774 .ok_or_else(|| Error::Token("the token source returned nothing".to_owned()))
775 }
776}
777
778impl GithubApi for Github {
779 async fn queued_jobs(&self, repository: &str) -> Result<Vec<QueuedJob>, Error> {
780 let mut runs = Vec::new();
782 for (state, status) in [
783 (RunState::Waiting, "queued"),
784 (RunState::Started, "in_progress"),
785 ] {
786 let pages: Vec<Runs> = self
787 .get_pages(&format!(
788 "/repos/{repository}/actions/runs?status={status}&per_page={PER_PAGE}"
789 ))
790 .await?;
791 runs.extend(
792 pages
793 .into_iter()
794 .flat_map(|page| page.workflow_runs)
795 .map(|run| (state, run.id)),
796 );
797 }
798 runs.sort_unstable_by_key(|&(state, id)| (id, state));
800 runs.dedup_by_key(|&mut (_, id)| id);
801 runs.sort_unstable();
804 let listed: HashSet<u64> = runs.iter().map(|&(_, id)| id).collect();
805 let mut jobs = Vec::new();
806 let mut requests = 0;
807 for &(_, run) in &runs {
808 if requests >= JOB_REQUESTS {
809 break;
810 }
811 let pages: Vec<Jobs> = self.get_pages(&jobs_path(repository, run)).await?;
812 requests += pages.len();
813 jobs.extend(
814 pages
815 .into_iter()
816 .flat_map(|page| page.jobs)
817 .filter(|job| job.status == "queued")
818 .map(|job| QueuedJob {
819 id: job.id,
820 run_id: job.run_id,
821 name: job.name,
822 labels: job.labels,
823 created_at: job
824 .created_at
825 .as_deref()
826 .and_then(time::parse_ms)
827 .unwrap_or(0),
828 run_attempt: job.run_attempt.unwrap_or(1),
829 workflow: job.workflow_name,
830 branch: job.head_branch,
831 url: job.html_url,
832 }),
833 );
834 }
835 let runs_prefix = format!("/repos/{repository}/actions/runs/");
837 lock(&self.etags).retain(|path| {
838 match path
839 .strip_prefix(&runs_prefix)
840 .and_then(|rest| rest.split_once("/jobs?"))
841 {
842 Some((run, _)) => run.parse().is_ok_and(|run: u64| listed.contains(&run)),
843 None => true,
844 }
845 });
846 jobs.sort_by_key(|job| job.id);
847 jobs.dedup_by_key(|job| job.id);
848 Ok(jobs)
849 }
850
851 async fn runners(&self, repository: &str) -> Result<Vec<Runner>, Error> {
852 let pages: Vec<Runners> = self
853 .get_pages(&format!(
854 "/repos/{repository}/actions/runners?per_page={PER_PAGE}"
855 ))
856 .await?;
857 let mut seen = HashSet::new();
858 Ok(pages
859 .into_iter()
860 .flat_map(|page| page.runners)
861 .filter(|runner| seen.insert(runner.id))
862 .collect())
863 }
864
865 async fn jit_config(
866 &self,
867 repository: &str,
868 name: &str,
869 labels: &[String],
870 ) -> Result<(Runner, JitConfig), Error> {
871 let body = serde_json::json!({
872 "name": name,
873 "runner_group_id": 1,
875 "labels": labels,
876 "work_folder": "_work",
877 });
878 let path = format!("/repos/{repository}/actions/runners/generate-jitconfig");
879 let response = self.send(Method::POST, &path, Some(&body), None).await?;
880 let response = expect_success(Method::POST, &path, response)?;
881 let jit: JitResponse = decode(&path, &response.bytes)?;
882 Ok((jit.runner, JitConfig(jit.encoded_jit_config)))
883 }
884
885 async fn delete_runner(&self, repository: &str, id: u64) -> Result<Deleted, Error> {
886 let path = format!("/repos/{repository}/actions/runners/{id}");
887 let response = self.send(Method::DELETE, &path, None, None).await?;
888 match response.status {
889 StatusCode::NOT_FOUND => Ok(Deleted::NotFound),
890 StatusCode::UNPROCESSABLE_ENTITY => Ok(Deleted::Busy),
891 _ => expect_success(Method::DELETE, &path, response).map(|_| Deleted::Deleted),
892 }
893 }
894
895 async fn job(&self, repository: &str, id: u64) -> Result<JobDetails, Error> {
896 let path = format!("/repos/{repository}/actions/jobs/{id}");
897 let job: JobDetails = self.get(&path).await?;
898 if job.status == "completed" {
900 lock(&self.etags).remove(&path);
901 }
902 Ok(job)
903 }
904
905 async fn run_trigger(&self, repository: &str, run: u64) -> Result<RunTrigger, Error> {
906 let path = format!("/repos/{repository}/actions/runs/{run}");
907 let run: Result<RunBody, Error> = self.get(&path).await;
908 lock(&self.etags).remove(&path);
910 let run = run?;
911 Ok(RunTrigger {
912 event: run.event,
913 head_branch: run.head_branch,
914 head_repository: run.head_repository.map(|head| head.full_name),
915 head_sha: run.head_sha,
916 by_user: Account::is_user(run.actor.as_ref())
917 && Account::is_user(run.triggering_actor.as_ref()),
918 pull_requests: run
919 .pull_requests
920 .into_iter()
921 .filter(|pull| pull.targets(repository))
922 .map(|pull| pull.number)
923 .collect(),
924 })
925 }
926
927 async fn pull_request(&self, repository: &str, number: u64) -> Result<PullRequest, Error> {
928 let path = format!("/repos/{repository}/pulls/{number}");
929 let pull: Result<PullRequestBody, Error> = self.get(&path).await;
930 lock(&self.etags).remove(&path);
931 let pull = pull?;
932 Ok(PullRequest {
933 by_user: Account::is_user(pull.user.as_ref()),
934 head_repository: pull.head.repo.map(|repo| repo.full_name),
935 head_branch: pull.head.branch,
936 base_branch: pull.base.branch,
937 })
938 }
939
940 async fn repository(&self, repository: &str) -> Result<RepositoryDetails, Error> {
941 let body: RepositoryBody = self.get(&format!("/repos/{repository}")).await?;
942 Ok(RepositoryDetails {
943 default_branch: body.default_branch,
944 visibility: body.visibility,
945 })
946 }
947
948 async fn on_branch(&self, repository: &str, branch: &str, sha: &str) -> Result<bool, Error> {
949 let commit =
950 |sha: &str| !sha.is_empty() && sha.bytes().all(|byte| byte.is_ascii_hexdigit());
951 if !commit(sha) {
952 return Ok(false);
953 }
954 let base: String = branch
956 .bytes()
957 .map(|byte| {
958 if byte.is_ascii_alphanumeric() || matches!(byte, b'/' | b'-' | b'_' | b'.') {
959 char::from(byte).to_string()
960 } else {
961 format!("%{byte:02X}")
962 }
963 })
964 .collect();
965 let path = format!("/repos/{repository}/branches/{base}");
968 let head: Result<BranchBody, Error> = self.get(&path).await;
969 lock(&self.etags).remove(&path);
970 let head = head?.commit.sha;
971 if !commit(&head) {
972 return Ok(false);
973 }
974 let path = format!("/repos/{repository}/compare/{head}...{sha}?per_page=1");
976 let body: Result<CompareBody, Error> = self.get(&path).await;
977 lock(&self.etags).remove(&path);
978 Ok(matches!(body?.status.as_str(), "identical" | "behind"))
980 }
981
982 async fn cancel_run(&self, repository: &str, run: u64) -> Result<(), Error> {
983 let path = format!("/repos/{repository}/actions/runs/{run}/cancel");
984 let response = self.send(Method::POST, &path, None, None).await?;
985 if response.status == StatusCode::CONFLICT {
987 return Ok(());
988 }
989 expect_success(Method::POST, &path, response).map(|_| ())
990 }
991
992 fn spend(&self) -> Spend {
993 lock(&self.requests).spend()
994 }
995}
996
997fn jobs_path(repository: &str, run: u64) -> String {
998 format!("/repos/{repository}/actions/runs/{run}/jobs?filter=latest&per_page={PER_PAGE}")
999}
1000
1001struct Response {
1002 status: StatusCode,
1003 etag: Option<HeaderValue>,
1004 next: bool,
1006 bytes: Vec<u8>,
1007}
1008
1009struct Page<T> {
1011 body: T,
1012 next: bool,
1013}
1014
1015fn names_next_page(headers: &HeaderMap) -> bool {
1017 headers
1018 .get_all(header::LINK)
1019 .iter()
1020 .filter_map(|value| value.to_str().ok())
1021 .flat_map(|value| value.split(','))
1022 .any(|link| {
1023 link.split(';')
1024 .skip(1)
1025 .any(|param| param.trim() == r#"rel="next""#)
1026 })
1027}
1028
1029#[derive(Clone)]
1031struct Cached {
1032 etag: HeaderValue,
1033 body: Arc<[u8]>,
1034 next: bool,
1036}
1037
1038struct Conditional {
1041 entries: HashMap<String, Use>,
1042 bytes: usize,
1043 limit: usize,
1044 fresh: Duration,
1045}
1046
1047struct Use {
1048 cached: Cached,
1049 used: Instant,
1050}
1051
1052impl Conditional {
1053 fn new(limit: usize, fresh: Duration) -> Conditional {
1054 Conditional {
1055 entries: HashMap::new(),
1056 bytes: 0,
1057 limit,
1058 fresh,
1059 }
1060 }
1061
1062 fn size(path: &str, cached: &Cached) -> usize {
1063 path.len() + cached.body.len()
1064 }
1065
1066 fn get(&mut self, path: &str, now: Instant) -> Option<Cached> {
1068 let entry = self.entries.get_mut(path)?;
1069 entry.used = now;
1070 Some(entry.cached.clone())
1071 }
1072
1073 fn insert(&mut self, path: &str, cached: Cached, now: Instant) {
1075 self.remove(path);
1076 let size = Conditional::size(path, &cached);
1077 if size > self.limit {
1078 return;
1079 }
1080 while self.bytes + size > self.limit {
1081 let stale = self
1082 .entries
1083 .iter()
1084 .filter(|(_, entry)| now.saturating_duration_since(entry.used) >= self.fresh)
1085 .min_by_key(|(_, entry)| entry.used)
1086 .map(|(path, _)| path.clone());
1087 let Some(stale) = stale else {
1088 return;
1089 };
1090 self.remove(&stale);
1091 }
1092 self.bytes += size;
1093 self.entries
1094 .insert(path.to_owned(), Use { cached, used: now });
1095 }
1096
1097 fn remove(&mut self, path: &str) {
1098 if let Some(entry) = self.entries.remove(path) {
1099 self.bytes -= Conditional::size(path, &entry.cached);
1100 }
1101 }
1102
1103 fn retain(&mut self, mut keep: impl FnMut(&str) -> bool) {
1105 let bytes = &mut self.bytes;
1106 self.entries.retain(|path, entry| {
1107 let kept = keep(path);
1108 if !kept {
1109 *bytes -= Conditional::size(path, &entry.cached);
1110 }
1111 kept
1112 });
1113 }
1114}
1115
1116fn expect_success(method: Method, path: &str, response: Response) -> Result<Response, Error> {
1117 if response.status.is_success() {
1118 return Ok(response);
1119 }
1120 let message = serde_json::from_slice::<ErrorBody>(&response.bytes).map_or_else(
1121 |_| String::from_utf8_lossy(&response.bytes).into_owned(),
1122 |body| body.message,
1123 );
1124 Err(Error::Status {
1125 method,
1126 path: path.to_owned(),
1127 status: response.status,
1128 message,
1129 })
1130}
1131
1132fn decode<T: DeserializeOwned>(path: &str, bytes: &[u8]) -> Result<T, Error> {
1133 serde_json::from_slice(bytes).map_err(|source| Error::Decode {
1134 path: path.to_owned(),
1135 source,
1136 })
1137}
1138
1139fn header_number(headers: &HeaderMap, name: &str) -> Option<u64> {
1141 headers
1142 .get(name)
1143 .and_then(|value| value.to_str().ok())
1144 .and_then(|value| value.trim().parse().ok())
1145}
1146
1147fn rate_limited_until(
1150 status: StatusCode,
1151 headers: &HeaderMap,
1152 now: SystemTime,
1153) -> Option<SystemTime> {
1154 let number = |name: &str| header_number(headers, name);
1155 if number("x-ratelimit-remaining") == Some(0)
1156 && let Some(reset) = number("x-ratelimit-reset")
1157 {
1158 return Some(SystemTime::UNIX_EPOCH + Duration::from_secs(reset));
1159 }
1160 if matches!(
1161 status,
1162 StatusCode::FORBIDDEN | StatusCode::TOO_MANY_REQUESTS
1163 ) && let Some(seconds) = number("retry-after")
1164 {
1165 return Some(now + Duration::from_secs(seconds));
1166 }
1167 None
1168}
1169
1170#[cfg(test)]
1171mod tests;