Compare commits

..

2 Commits

Author SHA1 Message Date
cuddle-please
59fd954130 chore(release): 0.2.0
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
2024-09-13 18:56:53 +00:00
a62fbe70fb
fix: don't have to use user for basic auth
All checks were successful
continuous-integration/drone/push Build is passing
Signed-off-by: kjuulh <contact@kjuulh.io>
2024-09-13 20:56:28 +02:00
5 changed files with 140 additions and 32 deletions

View File

@ -15,6 +15,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Docs
- add readme
### Fixed
- don't have to use user for basic auth
### Other
- move projects list into separate file
- separate files

View File

@ -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());
}

View File

@ -69,6 +69,17 @@ impl<'a> From<&'a GiteaUser> for &'a str {
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct GiteaOrganisation(String);
impl From<GiteaOrganisation> 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<Config> {

View File

@ -23,20 +23,7 @@ impl GiteaProvider {
) -> anyhow::Result<Vec<super::Repository>> {
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<Vec<super::Repository>> {
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<Vec<super::Repository>> {
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<Vec<gitea_rs::models::Repository>> {
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<Configuration> {
// 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<Configuration> {
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)
}
}

View File

@ -13,6 +13,16 @@ impl ProjectsList {
}
pub async fn get_projects(&self) -> anyhow::Result<Vec<Repository>> {
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<Vec<Repository>> {
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)
}