sourcehut-mirror-bridge/src/gitea.rs

50 lines
1.4 KiB
Rust

use anyhow::Result;
use base64::{engine::general_purpose, Engine as _};
use log::debug;
pub struct GiteaAPI {
api_endpoint: String,
auth: String,
}
impl GiteaAPI {
pub fn new(api_endpoint: &str, auth: &str) -> Self {
Self {
api_endpoint: api_endpoint.to_string(),
auth: basic_auth(auth),
}
}
pub fn check_repositories(&self, project_ids: &[&str]) -> Result<()> {
for repo in project_ids {
let api_url = format!("{}/v1/repos/{}", self.api_endpoint, repo);
let res = ureq::get(&api_url)
.set("Authorization", &self.auth)
.call()?;
if res.status() != 200 {
let body = res.into_string()?;
return Err(anyhow::anyhow!(
"Error fetching repository {}: {}",
repo,
body
));
}
debug!("Repository {} exists", repo);
}
Ok(())
}
pub fn sync(&self, project_id: &str) -> Result<()> {
let api_url = format!("{}/v1/repos/{}/mirror-sync", &self.api_endpoint, project_id);
ureq::post(&api_url)
.set("Authorization", &self.auth)
.call()?;
debug!("Synced repository {}", project_id);
Ok(())
}
}
fn basic_auth(auth: &str) -> String {
format!("Basic {}", general_purpose::STANDARD.encode(auth))
}