fix: check forge type before spidiring
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/pr/woodpecker Pipeline was successful
ci/woodpecker/pull_request_closed/woodpecker Pipeline was successful

This commit is contained in:
Aravinth Manivannan 2025-03-18 16:59:55 +05:30
parent 977796f56d
commit 0a98911129
Signed by: realaravinth
GPG key ID: F8F50389936984FF
7 changed files with 91 additions and 63 deletions

View file

@ -60,7 +60,16 @@ impl PartialEq for ForgeFlux {
#[async_trait]
impl SCForge for ForgeFlux {
async fn is_forge(&self) -> bool {
true
let u = self.instance_url.clone();
let mut node_info_url = self.instance_url.clone();
node_info_url.set_path(FORGEFLUX_NODEINFO);
let resp = self.client.get(node_info_url).send().await.unwrap();
if resp.status() == 200 {
let res: schema::Nodeinfo = resp.json().await.unwrap();
return res.software.name == FORGEFLUX_IDENTIFIER;
} else {
false
}
}
fn get_url(&self) -> &Url {
@ -81,9 +90,9 @@ impl SCForge for ForgeFlux {
}
}
let mut tags = Tags::default();
let tags = Tags::default();
let mut users = UserMap::default();
let mut internal_users = UserMap::default();
let internal_users = UserMap::default();
let mut repos = Repositories::default();
let instance_url = self.instance_url.clone();

View file

@ -46,6 +46,16 @@ pub struct Repository {
pub summary: String,
}
#[derive(Debug, Clone, PartialEq, Hash, Eq, Serialize, Deserialize)]
pub struct Software {
pub name: String,
}
#[derive(Debug, Clone, PartialEq, Hash, Eq, Serialize, Deserialize)]
pub struct Nodeinfo {
pub software: Software,
}
#[cfg(test)]
mod tests {
use super::*;

View file

@ -105,7 +105,7 @@ mod tests {
use crate::errors::ServiceError;
use crate::pages::auth::add::{AddChallenge, AddChallengePayload, ReadableError};
use crate::pages::errors::*;
use crate::settings::Settings;
#[actix_rt::test]
async fn add_page_works() {

View file

@ -63,6 +63,12 @@ pub struct Auth {
pub verify: &'static str,
}
impl Default for Auth {
fn default() -> Self {
Self::new()
}
}
impl Auth {
/// create new instance of Authentication route
pub const fn new() -> Auth {

View file

@ -318,60 +318,6 @@ fn set_separator_field(mut s: ConfigBuilder<DefaultState>) -> ConfigBuilder<Defa
s
}
#[cfg(test)]
mod tests {
use super::*;
use crate::utils::get_random;
#[test]
fn root_dir_is_created_test() {
let dir;
loop {
let mut tmp = env::temp_dir();
tmp = tmp.join(get_random(10));
if tmp.exists() {
continue;
} else {
dir = tmp;
break;
}
}
let repo = Repository {
root: dir.to_str().unwrap().to_owned(),
};
repo.create_root_dir();
assert!(dir.exists());
assert!(dir.is_dir());
let file = dir.join("foo");
fs::write(&file, "foo").unwrap();
repo.create_root_dir();
assert!(dir.exists());
assert!(dir.is_dir());
assert!(file.exists());
assert!(file.is_file());
let repo = Repository {
root: file.to_str().unwrap().to_owned(),
};
repo.create_root_dir();
assert!(file.exists());
assert!(file.is_dir());
let mut license_path = Path::new(&repo.root).to_path_buf();
license_path.push(LICENSE_FILE);
assert!(license_path.exists());
assert!(license_path.is_file());
assert!(fs::read_to_string(license_path)
.unwrap()
.contains(CC0_LICENSE_TXT));
}
}
const CC0_LICENSE_TXT: &str = r#"
Creative Commons Legal Code
@ -497,3 +443,57 @@ express Statement of Purpose.
"#;
const LICENSE_FILE: &str = "LICENSE.txt";
#[cfg(test)]
mod tests {
use super::*;
use crate::utils::get_random;
#[test]
fn root_dir_is_created_test() {
let dir;
loop {
let mut tmp = env::temp_dir();
tmp = tmp.join(get_random(10));
if tmp.exists() {
continue;
} else {
dir = tmp;
break;
}
}
let repo = Repository {
root: dir.to_str().unwrap().to_owned(),
};
repo.create_root_dir();
assert!(dir.exists());
assert!(dir.is_dir());
let file = dir.join("foo");
fs::write(&file, "foo").unwrap();
repo.create_root_dir();
assert!(dir.exists());
assert!(dir.is_dir());
assert!(file.exists());
assert!(file.is_file());
let repo = Repository {
root: file.to_str().unwrap().to_owned(),
};
repo.create_root_dir();
assert!(file.exists());
assert!(file.is_dir());
let mut license_path = Path::new(&repo.root).to_path_buf();
license_path.push(LICENSE_FILE);
assert!(license_path.exists());
assert!(license_path.is_file());
assert!(fs::read_to_string(license_path)
.unwrap()
.contains(CC0_LICENSE_TXT));
}
}

View file

@ -37,13 +37,13 @@ impl Ctx {
pub async fn crawl(&self, instance_url: &Url, db: &BoxDB, federate: &ArcFederate) {
info!("[crawl][{instance_url}] Init crawling");
let forge: Box<dyn SCForge> =
Box::new(Gitea::new(instance_url.clone(), self.client.clone()));
Box::new(ForgeFlux::new(instance_url.clone(), self.client.clone()));
let forge = if forge.is_forge().await {
forge
} else {
let forgeflux = Box::new(ForgeFlux::new(instance_url.clone(), self.client.clone()));
if forgeflux.is_forge().await {
forgeflux
let gitea = Box::new(Gitea::new(instance_url.clone(), self.client.clone()));
if gitea.is_forge().await {
gitea
} else {
unimplemented!("Forge type unimplemented");
}

View file

@ -17,7 +17,6 @@
*/
use std::env;
pub use std::sync::Arc;
use crate::ctx::Ctx;
pub use crate::db::BoxDB;
@ -58,6 +57,10 @@ pub mod sqlx_sqlite {
env::set_var("DATABASE_URL", &url);
println!("found db url: {url}");
let tmp_dir = Temp::new_dir().unwrap();
let d = tmp_dir.as_path();
let _ = std::fs::remove_dir_all(d);
let _ = std::fs::create_dir(d);
env::set_var("STARCHART__REPOSITORY__ROOT", tmp_dir.to_str().unwrap());
let mut settings = Settings::new().unwrap();