diff --git a/src/admin.rs b/src/admin.rs index 6c08138..f824d68 100644 --- a/src/admin.rs +++ b/src/admin.rs @@ -1,7 +1,7 @@ use super::*; -use std::fmt::Write; use std::collections::BTreeMap; +use std::fmt::Write; impl Forgejo { pub async fn admin_get_crons(&self, query: CronQuery) -> Result, ForgejoError> { @@ -12,11 +12,17 @@ impl Forgejo { self.post(&format!("admin/cron/{name}"), &()).await } - pub async fn admin_get_emails(&self, query: EmailListQuery) -> Result, ForgejoError> { + pub async fn admin_get_emails( + &self, + query: EmailListQuery, + ) -> Result, ForgejoError> { self.get(&query.path()).await } - pub async fn admin_search_emails(&self, query: EmailSearchQuery) -> Result, ForgejoError> { + pub async fn admin_search_emails( + &self, + query: EmailSearchQuery, + ) -> Result, ForgejoError> { self.get(&query.path()).await } @@ -36,24 +42,40 @@ impl Forgejo { self.delete(&format!("admin/hooks/{id}")).await } - pub async fn admin_edit_hook(&self, id: u64, opt: EditHookOption) -> Result { + pub async fn admin_edit_hook( + &self, + id: u64, + opt: EditHookOption, + ) -> Result { self.patch(&format!("admin/hooks/{id}"), &opt).await } - pub async fn admin_get_orgs(&self, query: AdminOrganizationQuery) -> Result, ForgejoError> { + pub async fn admin_get_orgs( + &self, + query: AdminOrganizationQuery, + ) -> Result, ForgejoError> { self.get(&query.path()).await } - pub async fn admin_unadopted_repos(&self, query: UnadoptedRepoQuery) -> Result, ForgejoError> { + pub async fn admin_unadopted_repos( + &self, + query: UnadoptedRepoQuery, + ) -> Result, ForgejoError> { self.get(&query.path()).await } - + pub async fn admin_adopt(&self, owner: &str, repo: &str) -> Result<(), ForgejoError> { - self.post(&format!("admin/unadopted/{owner}/{repo}"), &()).await + self.post(&format!("admin/unadopted/{owner}/{repo}"), &()) + .await } - pub async fn admin_delete_unadopted(&self, owner: &str, repo: &str) -> Result<(), ForgejoError> { - self.delete(&format!("admin/unadopted/{owner}/{repo}")).await + pub async fn admin_delete_unadopted( + &self, + owner: &str, + repo: &str, + ) -> Result<(), ForgejoError> { + self.delete(&format!("admin/unadopted/{owner}/{repo}")) + .await } pub async fn admin_users(&self, query: AdminUserQuery) -> Result, ForgejoError> { @@ -65,14 +87,23 @@ impl Forgejo { } pub async fn admin_delete_user(&self, user: &str, purge: bool) -> Result<(), ForgejoError> { - self.delete(&format!("admin/users/{user}?purge={purge}")).await + self.delete(&format!("admin/users/{user}?purge={purge}")) + .await } - pub async fn admin_edit_user(&self, user: &str, opt: CreateUserOption) -> Result { + pub async fn admin_edit_user( + &self, + user: &str, + opt: CreateUserOption, + ) -> Result { self.patch(&format!("admin/users/{user}"), &opt).await } - pub async fn admin_add_key(&self, user: &str, opt: CreateKeyOption) -> Result { + pub async fn admin_add_key( + &self, + user: &str, + opt: CreateKeyOption, + ) -> Result { self.patch(&format!("admin/users/{user}/keys"), &opt).await } @@ -80,15 +111,27 @@ impl Forgejo { self.delete(&format!("admin/users/{user}/keys/{id}")).await } - pub async fn admin_create_org(&self, owner: &str, opt: CreateOrgOption) -> Result { + pub async fn admin_create_org( + &self, + owner: &str, + opt: CreateOrgOption, + ) -> Result { self.post(&format!("admin/users/{owner}/orgs"), &opt).await } - - pub async fn admin_rename_user(&self, user: &str, opt: RenameUserOption) -> Result<(), ForgejoError> { + + pub async fn admin_rename_user( + &self, + user: &str, + opt: RenameUserOption, + ) -> Result<(), ForgejoError> { self.post(&format!("admin/users/{user}/rename"), &opt).await } - - pub async fn admin_create_repo(&self, owner: &str, opt: CreateRepoOption) -> Result { + + pub async fn admin_create_repo( + &self, + owner: &str, + opt: CreateRepoOption, + ) -> Result { self.post(&format!("admin/users/{owner}/repos"), &opt).await } } @@ -115,12 +158,14 @@ impl CronQuery { let mut s = String::from("admin/cron?"); if let Some(page) = self.page { s.push_str("page="); - s.write_fmt(format_args!("{page}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{page}")) + .expect("writing to string can't fail"); s.push('&'); } if let Some(limit) = self.limit { s.push_str("limit="); - s.write_fmt(format_args!("{limit}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{limit}")) + .expect("writing to string can't fail"); s.push('&'); } s @@ -147,12 +192,14 @@ impl EmailListQuery { let mut s = String::from("admin/emails?"); if let Some(page) = self.page { s.push_str("page="); - s.write_fmt(format_args!("{page}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{page}")) + .expect("writing to string can't fail"); s.push('&'); } if let Some(limit) = self.limit { s.push_str("limit="); - s.write_fmt(format_args!("{limit}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{limit}")) + .expect("writing to string can't fail"); s.push('&'); } s @@ -176,12 +223,14 @@ impl EmailSearchQuery { } if let Some(page) = self.page { s.push_str("page="); - s.write_fmt(format_args!("{page}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{page}")) + .expect("writing to string can't fail"); s.push('&'); } if let Some(limit) = self.limit { s.push_str("limit="); - s.write_fmt(format_args!("{limit}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{limit}")) + .expect("writing to string can't fail"); s.push('&'); } s @@ -208,17 +257,17 @@ pub struct Hook { #[non_exhaustive] #[serde(rename_all = "lowercase")] pub enum HookType { - Forgejo, - Dingtalk, - Discord, - Gitea, - Gogs, - Msteams, - Slack, - Telegram, - Feishu, - Wechatwork, - Packagist + Forgejo, + Dingtalk, + Discord, + Gitea, + Gogs, + Msteams, + Slack, + Telegram, + Feishu, + Wechatwork, + Packagist, } #[derive(Default, Debug)] @@ -232,12 +281,14 @@ impl HookQuery { let mut s = String::from("admin/hooks?"); if let Some(page) = self.page { s.push_str("page="); - s.write_fmt(format_args!("{page}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{page}")) + .expect("writing to string can't fail"); s.push('&'); } if let Some(limit) = self.limit { s.push_str("limit="); - s.write_fmt(format_args!("{limit}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{limit}")) + .expect("writing to string can't fail"); s.push('&'); } s @@ -283,12 +334,14 @@ impl AdminOrganizationQuery { let mut s = String::from("admin/orgs?"); if let Some(page) = self.page { s.push_str("page="); - s.write_fmt(format_args!("{page}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{page}")) + .expect("writing to string can't fail"); s.push('&'); } if let Some(limit) = self.limit { s.push_str("limit="); - s.write_fmt(format_args!("{limit}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{limit}")) + .expect("writing to string can't fail"); s.push('&'); } s @@ -307,12 +360,14 @@ impl UnadoptedRepoQuery { let mut s = String::from("admin/unadopted?"); if let Some(page) = self.page { s.push_str("page="); - s.write_fmt(format_args!("{page}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{page}")) + .expect("writing to string can't fail"); s.push('&'); } if let Some(limit) = self.limit { s.push_str("limit="); - s.write_fmt(format_args!("{limit}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{limit}")) + .expect("writing to string can't fail"); s.push('&'); } if !self.pattern.is_empty() { @@ -337,7 +392,8 @@ impl AdminUserQuery { let mut s = String::from("admin/users?"); if let Some(source_id) = self.source_id { s.push_str("source_id="); - s.write_fmt(format_args!("{source_id}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{source_id}")) + .expect("writing to string can't fail"); s.push('&'); } if !self.login_name.is_empty() { @@ -347,12 +403,14 @@ impl AdminUserQuery { } if let Some(page) = self.page { s.push_str("page="); - s.write_fmt(format_args!("{page}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{page}")) + .expect("writing to string can't fail"); s.push('&'); } if let Some(limit) = self.limit { s.push_str("limit="); - s.write_fmt(format_args!("{limit}")).expect("writing to string can't fail"); + s.write_fmt(format_args!("{limit}")) + .expect("writing to string can't fail"); s.push('&'); } s diff --git a/src/lib.rs b/src/lib.rs index 6a86d3e..21b0a45 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,8 +8,8 @@ pub struct Forgejo { client: Client, } -mod issue; mod admin; +mod issue; mod misc; mod notification; mod organization; @@ -17,8 +17,8 @@ mod package; mod repository; mod user; -pub use issue::*; pub use admin::*; +pub use issue::*; pub use misc::*; pub use notification::*; pub use organization::*; diff --git a/tests/ci_test.rs b/tests/ci_test.rs index 91b42eb..e059799 100644 --- a/tests/ci_test.rs +++ b/tests/ci_test.rs @@ -249,12 +249,30 @@ async fn admin(api: &forgejo_api::Forgejo) -> eyre::Result<()> { username: "Pipis".into(), visibility: "public".into(), }; - let _ = api.admin_create_user(user_opt).await.wrap_err("failed to create user")?; + let _ = api + .admin_create_user(user_opt) + .await + .wrap_err("failed to create user")?; - let users = api.admin_users(forgejo_api::AdminUserQuery::default()).await.wrap_err("failed to search users")?; - ensure!(users.iter().find(|u| u.login == "Pipis").is_some(), "could not find new user"); - let users = api.admin_get_emails(forgejo_api::EmailListQuery::default()).await.wrap_err("failed to search emails")?; - ensure!(users.iter().find(|u| u.email == "user@noreply.example.org").is_some(), "could not find new user"); + let users = api + .admin_users(forgejo_api::AdminUserQuery::default()) + .await + .wrap_err("failed to search users")?; + ensure!( + users.iter().find(|u| u.login == "Pipis").is_some(), + "could not find new user" + ); + let users = api + .admin_get_emails(forgejo_api::EmailListQuery::default()) + .await + .wrap_err("failed to search emails")?; + ensure!( + users + .iter() + .find(|u| u.email == "user@noreply.example.org") + .is_some(), + "could not find new user" + ); let org_opt = forgejo_api::CreateOrgOption { description: None, @@ -265,8 +283,16 @@ async fn admin(api: &forgejo_api::Forgejo) -> eyre::Result<()> { visibility: forgejo_api::OrgVisibility::Public, website: None, }; - let _ = api.admin_create_org("Pipis", org_opt).await.wrap_err("failed to create org")?; - ensure!(!api.admin_get_orgs(forgejo_api::AdminOrganizationQuery::default()).await?.is_empty(), "org list empty"); + let _ = api + .admin_create_org("Pipis", org_opt) + .await + .wrap_err("failed to create org")?; + ensure!( + !api.admin_get_orgs(forgejo_api::AdminOrganizationQuery::default()) + .await? + .is_empty(), + "org list empty" + ); let key_opt = forgejo_api::CreateKeyOption { key: "ssh-ed25519 00000000000000000000000000000000000000000000000000000000000000000000 user@noreply.example.org".into(), @@ -279,12 +305,24 @@ async fn admin(api: &forgejo_api::Forgejo) -> eyre::Result<()> { let rename_opt = forgejo_api::RenameUserOption { new_username: "Bepis".into(), }; - api.admin_rename_user("Pipis", rename_opt).await.wrap_err("failed to rename user")?; - api.admin_delete_user("Bepis", true).await.wrap_err("failed to delete user")?; - ensure!(api.admin_delete_user("Ghost", true).await.is_err(), "deleting fake user should fail"); + api.admin_rename_user("Pipis", rename_opt) + .await + .wrap_err("failed to rename user")?; + api.admin_delete_user("Bepis", true) + .await + .wrap_err("failed to delete user")?; + ensure!( + api.admin_delete_user("Ghost", true).await.is_err(), + "deleting fake user should fail" + ); - let crons = api.admin_get_crons(forgejo_api::CronQuery::default()).await.wrap_err("failed to get crons list")?; - api.admin_run_cron(&crons.get(0).ok_or_else(|| eyre!("no crons"))?.name).await.wrap_err("failed to run cron")?; + let crons = api + .admin_get_crons(forgejo_api::CronQuery::default()) + .await + .wrap_err("failed to get crons list")?; + api.admin_run_cron(&crons.get(0).ok_or_else(|| eyre!("no crons"))?.name) + .await + .wrap_err("failed to run cron")?; let hook_opt = forgejo_api::CreateHookOption { active: None, @@ -299,14 +337,20 @@ async fn admin(api: &forgejo_api::Forgejo) -> eyre::Result<()> { _type: forgejo_api::HookType::Forgejo, }; // yarr har har me matey this is me hook - let hook = api.admin_create_hook(hook_opt).await.wrap_err("failed to create hook")?; + let hook = api + .admin_create_hook(hook_opt) + .await + .wrap_err("failed to create hook")?; let edit_hook = forgejo_api::EditHookOption { active: Some(true), ..Default::default() }; - api.admin_edit_hook(hook.id, edit_hook).await.wrap_err("failed to edit hook")?; - api.admin_delete_hook(hook.id).await.wrap_err("failed to delete hook")?; + api.admin_edit_hook(hook.id, edit_hook) + .await + .wrap_err("failed to edit hook")?; + api.admin_delete_hook(hook.id) + .await + .wrap_err("failed to delete hook")?; Ok(()) } -