|
| 1 | +use anyhow::{bail, Context, Result}; |
| 2 | +use serde::Deserialize; |
| 3 | +use ureq::Agent; |
| 4 | + |
| 5 | +/// Name of the github organization for this repo. |
| 6 | +const ORG: &str = "rust-osdev"; |
| 7 | + |
| 8 | +/// Name of this repo on github. |
| 9 | +const REPO: &str = "ovmf-prebuilt"; |
| 10 | + |
| 11 | +/// User-Agent header to send with requests. |
| 12 | +const USER_AGENT: &str = "https://github.com/rust-osdev/ovmf-prebuilt"; |
| 13 | + |
| 14 | +#[derive(Debug, Deserialize)] |
| 15 | +struct Asset { |
| 16 | + digest: Option<String>, |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Debug, Deserialize)] |
| 20 | +pub struct Release { |
| 21 | + pub tag_name: String, |
| 22 | + name: Option<String>, |
| 23 | + assets: Vec<Asset>, |
| 24 | +} |
| 25 | + |
| 26 | +impl Release { |
| 27 | + pub fn sha256(&self) -> Result<&str> { |
| 28 | + if self.assets.len() != 1 { |
| 29 | + bail!("expected exactly one asset, got {}", self.assets.len()); |
| 30 | + } |
| 31 | + let asset = &self.assets[0]; |
| 32 | + let digest = asset.digest.as_ref().context("asset is missing digest")?; |
| 33 | + digest |
| 34 | + .strip_prefix("sha256:") |
| 35 | + .context("digest format is not sha256") |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +pub struct Github { |
| 40 | + agent: Agent, |
| 41 | +} |
| 42 | + |
| 43 | +impl Github { |
| 44 | + pub fn new() -> Self { |
| 45 | + let config = Agent::config_builder().user_agent(USER_AGENT).build(); |
| 46 | + let agent = Agent::new_with_config(config); |
| 47 | + Github { agent } |
| 48 | + } |
| 49 | + |
| 50 | + pub fn get_releases(&self) -> Result<Vec<Release>> { |
| 51 | + let url = format!("https://api.github.com/repos/{ORG}/{REPO}/releases"); |
| 52 | + println!("downloading release list"); |
| 53 | + let mut releases: Vec<Release> = self |
| 54 | + .agent |
| 55 | + .get(url) |
| 56 | + .header("Accept", "application/vnd.github+json") |
| 57 | + .header("X-GitHub-Api-Version", "2022-11-28") |
| 58 | + .call()? |
| 59 | + .body_mut() |
| 60 | + .read_json()?; |
| 61 | + // Remove releases with a null `name`. This serves to exclude older |
| 62 | + // releases (with tags like "v0.20211216.194+gcc2db6ebfb"), from |
| 63 | + // before the repo was reworked to its current form. |
| 64 | + releases.retain(|release| release.name.is_some()); |
| 65 | + Ok(releases) |
| 66 | + } |
| 67 | +} |
0 commit comments