From a62fbe70fb7836e62d25f2acd73c825beaa5ebba Mon Sep 17 00:00:00 2001 From: kjuulh Date: Fri, 13 Sep 2024 20:56:28 +0200 Subject: [PATCH] fix: don't have to use user for basic auth Signed-off-by: kjuulh --- crates/gitnow/src/commands/root.rs | 1 - crates/gitnow/src/config.rs | 11 ++ crates/gitnow/src/git_provider/gitea.rs | 133 ++++++++++++++++++------ crates/gitnow/src/projects_list.rs | 24 ++++- 4 files changed, 137 insertions(+), 32 deletions(-) diff --git a/crates/gitnow/src/commands/root.rs b/crates/gitnow/src/commands/root.rs index 472e008..d8896f9 100644 --- a/crates/gitnow/src/commands/root.rs +++ b/crates/gitnow/src/commands/root.rs @@ -22,7 +22,6 @@ impl RootCommand { let repositories = self.app.projects_list().get_projects().await?; - //let github_provider = self.app.github_provider(); for repo in &repositories { tracing::info!("repo: {}", repo.to_rel_path().display()); } diff --git a/crates/gitnow/src/config.rs b/crates/gitnow/src/config.rs index 0f4f108..c1db09c 100644 --- a/crates/gitnow/src/config.rs +++ b/crates/gitnow/src/config.rs @@ -69,6 +69,17 @@ impl<'a> From<&'a GiteaUser> for &'a str { #[derive(Debug, Serialize, Deserialize, PartialEq)] pub struct GiteaOrganisation(String); +impl From for String { + fn from(value: GiteaOrganisation) -> Self { + value.0 + } +} + +impl<'a> From<&'a GiteaOrganisation> for &'a str { + fn from(value: &'a GiteaOrganisation) -> Self { + value.0.as_str() + } +} impl Config { pub async fn from_file(file_path: &Path) -> anyhow::Result { diff --git a/crates/gitnow/src/git_provider/gitea.rs b/crates/gitnow/src/git_provider/gitea.rs index 4a08f0f..3d1ce5b 100644 --- a/crates/gitnow/src/git_provider/gitea.rs +++ b/crates/gitnow/src/git_provider/gitea.rs @@ -23,20 +23,7 @@ impl GiteaProvider { ) -> anyhow::Result> { tracing::debug!("fetching gitea repositories for user"); - let mut config = gitea_rs::apis::configuration::Configuration::new(); - config.base_path = api.into(); - match access_token { - Some(GiteaAccessToken::Env { env }) => { - let token = - std::env::var(env).context(format!("{env} didn't have a valid value"))?; - - config.basic_auth = Some((user.into(), Some(token))); - } - Some(GiteaAccessToken::Direct(var)) => { - config.bearer_access_token = Some(var.to_owned()); - } - None => {} - } + let config = self.get_config(api, access_token)?; let mut repositories = Vec::new(); let mut page = 1; @@ -102,20 +89,7 @@ impl GiteaProvider { ) -> anyhow::Result> { tracing::debug!("fetching gitea repositories for user"); - let mut config = gitea_rs::apis::configuration::Configuration::new(); - config.base_path = api.into(); - match access_token { - Some(GiteaAccessToken::Env { env }) => { - let token = - std::env::var(env).context(format!("{env} didn't have a valid value"))?; - - config.basic_auth = Some((user.into(), Some(token))); - } - Some(GiteaAccessToken::Direct(var)) => { - config.bearer_access_token = Some(var.to_owned()); - } - None => {} - } + let config = self.get_config(api, access_token)?; let mut repositories = Vec::new(); let mut page = 1; @@ -169,8 +143,109 @@ impl GiteaProvider { pub async fn list_repositories_for_organisation( &self, organisation: &str, + api: &str, + access_token: Option<&GiteaAccessToken>, ) -> anyhow::Result> { - todo!() + let config = self.get_config(api, access_token)?; + + let mut repositories = Vec::new(); + let mut page = 1; + loop { + let mut repos = self + .list_repositories_for_organisation_with_page(organisation, &config, page) + .await?; + + if repos.is_empty() { + break; + } + + repositories.append(&mut repos); + page += 1; + } + + let provider = &Self::get_domain(api)?; + + Ok(repositories + .into_iter() + .map(|repo| super::Repository { + provider: provider.into(), + owner: repo + .owner + .map(|user| user.login.unwrap_or_default()) + .unwrap_or_default(), + repo_name: repo.name.unwrap_or_default(), + ssh_url: repo + .ssh_url + .expect("ssh url to be set for gitea repository"), + }) + .collect()) + } + + #[tracing::instrument(skip(self))] + pub async fn list_repositories_for_organisation_with_page( + &self, + organisation: &str, + config: &Configuration, + page: usize, + ) -> anyhow::Result> { + let repos = gitea_rs::apis::organization_api::org_list_repos( + config, + organisation, + Some(page as i32), + None, + ) + .await + .context("failed to fetch repos for users")?; + + Ok(repos) + } + + // fn get_config_from_user( + // &self, + // api: &str, + // user: &str, + // access_token: Option<&GiteaAccessToken>, + // ) -> anyhow::Result { + // let mut config = gitea_rs::apis::configuration::Configuration::new(); + // config.base_path = api.into(); + // match access_token { + // Some(GiteaAccessToken::Env { env }) => { + // let token = + // std::env::var(env).context(format!("{env} didn't have a valid value"))?; + + // // config.basic_auth = Some((user.into(), Some(token))); + // config.basic_auth = Some(("".into(), Some(token))); + // } + // Some(GiteaAccessToken::Direct(var)) => { + // config.basic_auth = Some(("".into(), Some(var.to_owned()))); + // } + // None => {} + // } + + // Ok(config) + // } + + fn get_config( + &self, + api: &str, + access_token: Option<&GiteaAccessToken>, + ) -> anyhow::Result { + let mut config = gitea_rs::apis::configuration::Configuration::new(); + config.base_path = api.into(); + match access_token { + Some(GiteaAccessToken::Env { env }) => { + let token = + std::env::var(env).context(format!("{env} didn't have a valid value"))?; + + config.basic_auth = Some(("".into(), Some(token))); + } + Some(GiteaAccessToken::Direct(var)) => { + config.bearer_access_token = Some(var.to_owned()); + } + None => {} + } + + Ok(config) } } diff --git a/crates/gitnow/src/projects_list.rs b/crates/gitnow/src/projects_list.rs index 0f1b5c7..54edb89 100644 --- a/crates/gitnow/src/projects_list.rs +++ b/crates/gitnow/src/projects_list.rs @@ -13,6 +13,16 @@ impl ProjectsList { } pub async fn get_projects(&self) -> anyhow::Result> { + let mut repositories = Vec::new(); + + repositories.extend(self.get_gitea_projects().await?); + + repositories.collect_unique(); + + Ok(repositories) + } + + async fn get_gitea_projects(&self) -> anyhow::Result> { let gitea_provider = self.app.gitea_provider(); let mut repositories = Vec::new(); @@ -40,9 +50,19 @@ impl ProjectsList { repositories.append(&mut repos); } - } - repositories.collect_unique(); + for gitea_org in gitea.organisations.iter() { + let mut repos = gitea_provider + .list_repositories_for_organisation( + gitea_org.into(), + &gitea.url, + gitea.access_token.as_ref(), + ) + .await?; + + repositories.append(&mut repos); + } + } Ok(repositories) }