support fetching the latest revision for gitea

This commit is contained in:
figsoda 2023-01-08 13:37:36 -05:00
parent 33c92eb6eb
commit 07f23a7970

View File

@ -1,3 +1,6 @@
use anyhow::{anyhow, Result};
use serde::Deserialize;
use crate::{
impl_fetcher,
simple::{SimpleFetcher, SimpleUrlFetcher},
@ -6,6 +9,11 @@ use crate::{
pub struct FetchFromGitea<'a>(pub &'a str);
impl_fetcher!(FetchFromGitea<'a>);
#[derive(Deserialize)]
struct Commit {
sha: String,
}
impl<'a> SimpleFetcher<'a, 2> for FetchFromGitea<'a> {
const KEYS: [&'static str; 2] = ["owner", "repo"];
const NAME: &'static str = "fetchFromGitea";
@ -13,6 +21,17 @@ impl<'a> SimpleFetcher<'a, 2> for FetchFromGitea<'a> {
fn host(&'a self) -> Option<&'a str> {
Some(self.0)
}
fn fetch_rev(&self, [owner, repo]: &[&str; 2]) -> Result<String> {
let url = format!("https://{}/api/v1/repos/{owner}/{repo}/commits", self.0);
Ok(ureq::get(&url)
.call()?
.into_json::<Vec<Commit>>()?
.into_iter()
.next()
.ok_or_else(|| anyhow!("no commits found for https://{}/{owner}/{repo}", self.0))?
.sha)
}
}
impl<'a> SimpleUrlFetcher<'a, 2> for FetchFromGitea<'a> {