diff --git a/ee/tabby-webserver/src/cron/db/github.rs b/ee/tabby-webserver/src/cron/db/github.rs index 09044ecf1a2e..1f184ca891c9 100644 --- a/ee/tabby-webserver/src/cron/db/github.rs +++ b/ee/tabby-webserver/src/cron/db/github.rs @@ -6,31 +6,27 @@ use juniper::ID; use octocrab::{models::Repository, GitHubError, Octocrab}; use tracing::warn; -use crate::schema::github_repository_provider::{ - GithubRepositoryProvider, GithubRepositoryProviderService, -}; +use crate::schema::repository::{GithubRepositoryProvider, GithubRepositoryService}; -pub async fn refresh_all_repositories( - service: Arc, -) -> Result<()> { +pub async fn refresh_all_repositories(service: Arc) -> Result<()> { for provider in service - .list_github_repository_providers(vec![], None, None, None, None) + .list_providers(vec![], None, None, None, None) .await? { let start = Utc::now(); refresh_repositories_for_provider(service.clone(), provider.id.clone()).await?; service - .delete_outdated_github_provided_repositories(provider.id, start) + .delete_outdated_repositories(provider.id, start) .await?; } Ok(()) } async fn refresh_repositories_for_provider( - service: Arc, + service: Arc, provider_id: ID, ) -> Result<()> { - let provider = service.get_github_repository_provider(provider_id).await?; + let provider = service.get_provider(provider_id).await?; let repos = match fetch_all_repos(&provider).await { Ok(repos) => repos, Err(octocrab::Error::GitHub { @@ -38,7 +34,7 @@ async fn refresh_repositories_for_provider( .. }) if source.status_code.is_client_error() => { service - .update_github_repository_provider_sync_status(provider.id.clone(), false) + .update_provider_status(provider.id.clone(), false) .await?; warn!( "GitHub credentials for provider {} are expired or invalid", @@ -64,7 +60,7 @@ async fn refresh_repositories_for_provider( let url = url.strip_suffix(".git").unwrap_or(&url); service - .upsert_github_provided_repository( + .upsert_repository( provider.id.clone(), id, repo.full_name.unwrap_or(repo.name), @@ -73,7 +69,7 @@ async fn refresh_repositories_for_provider( .await?; } service - .update_github_repository_provider_sync_status(provider.id.clone(), true) + .update_provider_status(provider.id.clone(), true) .await?; Ok(()) diff --git a/ee/tabby-webserver/src/cron/db/gitlab.rs b/ee/tabby-webserver/src/cron/db/gitlab.rs index 834ac7d2d1fb..00d00f7c2bed 100644 --- a/ee/tabby-webserver/src/cron/db/gitlab.rs +++ b/ee/tabby-webserver/src/cron/db/gitlab.rs @@ -10,36 +10,32 @@ use juniper::ID; use serde::Deserialize; use tracing::warn; -use crate::schema::gitlab_repository_provider::{ - GitlabRepositoryProvider, GitlabRepositoryProviderService, -}; +use crate::schema::repository::{GitlabRepositoryProvider, GitlabRepositoryService}; -pub async fn refresh_all_repositories( - service: Arc, -) -> Result<()> { +pub async fn refresh_all_repositories(service: Arc) -> Result<()> { for provider in service - .list_gitlab_repository_providers(vec![], None, None, None, None) + .list_providers(vec![], None, None, None, None) .await? { let start = Utc::now(); refresh_repositories_for_provider(service.clone(), provider.id.clone()).await?; service - .delete_outdated_gitlab_provided_repositories(provider.id, start) + .delete_outdated_repositories(provider.id, start) .await?; } Ok(()) } async fn refresh_repositories_for_provider( - service: Arc, + service: Arc, provider_id: ID, ) -> Result<()> { - let provider = service.get_gitlab_repository_provider(provider_id).await?; + let provider = service.get_provider(provider_id).await?; let repos = match fetch_all_repos(&provider).await { Ok(repos) => repos, Err(e) if e.to_string().contains("401 Unauthorized") => { service - .update_gitlab_repository_provider_sync_status(provider.id.clone(), false) + .update_provider_status(provider.id.clone(), false) .await?; warn!( "GitLab credentials for provider {} are expired or invalid", @@ -58,7 +54,7 @@ async fn refresh_repositories_for_provider( let url = url.strip_suffix(".git").unwrap_or(&url); service - .upsert_gitlab_provided_repository( + .upsert_repository( provider.id.clone(), id, repo.name_with_namespace, @@ -67,7 +63,7 @@ async fn refresh_repositories_for_provider( .await?; } service - .update_gitlab_repository_provider_sync_status(provider.id.clone(), true) + .update_provider_status(provider.id.clone(), true) .await?; Ok(()) diff --git a/ee/tabby-webserver/src/cron/db/mod.rs b/ee/tabby-webserver/src/cron/db/mod.rs index d0fabff5f811..8f52796b70de 100644 --- a/ee/tabby-webserver/src/cron/db/mod.rs +++ b/ee/tabby-webserver/src/cron/db/mod.rs @@ -11,8 +11,9 @@ use tokio_cron_scheduler::Job; use tracing::{debug, error}; use crate::schema::{ - auth::AuthenticationService, github_repository_provider::GithubRepositoryProviderService, - gitlab_repository_provider::GitlabRepositoryProviderService, job::JobService, + auth::AuthenticationService, + job::JobService, + repository::{GithubRepositoryService, GitlabRepositoryService}, }; const EVERY_TWO_HOURS: &str = "0 0 1/2 * * * *"; @@ -64,7 +65,7 @@ pub async fn password_reset_job(auth: Arc) -> Result< } pub async fn update_integrated_github_repositories_job( - github_repository_provider: Arc, + github_repository_provider: Arc, ) -> Result { service_job( "sync github repositories", @@ -79,7 +80,7 @@ pub async fn update_integrated_github_repositories_job( } pub async fn update_integrated_gitlab_repositories_job( - gitlab_repository_provider: Arc, + gitlab_repository_provider: Arc, ) -> Result { service_job( "sync gitlab repositories", diff --git a/ee/tabby-webserver/src/schema/mod.rs b/ee/tabby-webserver/src/schema/mod.rs index 45caeeca312c..ad07b55c413c 100644 --- a/ee/tabby-webserver/src/schema/mod.rs +++ b/ee/tabby-webserver/src/schema/mod.rs @@ -2,14 +2,10 @@ pub mod analytic; pub mod auth; pub mod constants; pub mod email; -pub mod git_repository; -pub mod github_repository_provider; -pub mod gitlab_repository_provider; pub mod job; pub mod license; pub mod repository; pub mod setting; -pub mod types; pub mod user_event; pub mod worker; @@ -38,16 +34,12 @@ use self::{ RequestInvitationInput, RequestPasswordResetEmailInput, UpdateOAuthCredentialInput, }, email::{EmailService, EmailSetting, EmailSettingInput}, - git_repository::GitRepository, - github_repository_provider::{GithubProvidedRepository, GithubRepositoryProvider}, - gitlab_repository_provider::{GitlabProvidedRepository, GitlabRepositoryProvider}, job::JobStats, license::{IsLicenseValid, LicenseInfo, LicenseService, LicenseType}, repository::{FileEntrySearchResult, Repository, RepositoryKind, RepositoryService}, setting::{ NetworkSetting, NetworkSettingInput, SecuritySetting, SecuritySettingInput, SettingService, }, - types::{CreateRepositoryProviderInput, UpdateRepositoryProviderInput}, user_event::{UserEvent, UserEventService}, }; use crate::{ @@ -230,7 +222,7 @@ impl Query { before: Option, first: Option, last: Option, - ) -> Result> { + ) -> Result> { check_admin(ctx).await?; relay::query_async( after, @@ -241,13 +233,7 @@ impl Query { ctx.locator .repository() .github() - .list_github_repository_providers( - ids.unwrap_or_default(), - after, - before, - first, - last, - ) + .list_providers(ids.unwrap_or_default(), after, before, first, last) .await }, ) @@ -261,7 +247,7 @@ impl Query { before: Option, first: Option, last: Option, - ) -> Result> { + ) -> Result> { check_admin(ctx).await?; relay::query_async( after, @@ -272,13 +258,7 @@ impl Query { ctx.locator .repository() .github() - .list_github_provided_repositories_by_provider( - provider_ids, - after, - before, - first, - last, - ) + .list_repositories(provider_ids, after, before, first, last) .await }, ) @@ -292,7 +272,7 @@ impl Query { before: Option, first: Option, last: Option, - ) -> Result> { + ) -> Result> { check_admin(ctx).await?; relay::query_async( after, @@ -303,13 +283,7 @@ impl Query { ctx.locator .repository() .gitlab() - .list_gitlab_repository_providers( - ids.unwrap_or_default(), - after, - before, - first, - last, - ) + .list_providers(ids.unwrap_or_default(), after, before, first, last) .await }, ) @@ -323,7 +297,7 @@ impl Query { before: Option, first: Option, last: Option, - ) -> Result> { + ) -> Result> { check_admin(ctx).await?; relay::query_async( after, @@ -334,13 +308,7 @@ impl Query { ctx.locator .repository() .gitlab() - .list_gitlab_provided_repositories_by_provider( - provider_ids, - after, - before, - first, - last, - ) + .list_repositories(provider_ids, after, before, first, last) .await }, ) @@ -398,7 +366,7 @@ impl Query { before: Option, first: Option, last: Option, - ) -> Result> { + ) -> Result> { check_admin(ctx).await?; relay::query_async( after, @@ -706,7 +674,7 @@ impl Mutation { async fn create_git_repository(ctx: &Context, name: String, git_url: String) -> Result { check_admin(ctx).await?; - let input = git_repository::CreateGitRepositoryInput { name, git_url }; + let input = repository::CreateGitRepositoryInput { name, git_url }; input.validate()?; ctx.locator .repository() @@ -798,7 +766,7 @@ impl Mutation { async fn create_github_repository_provider( ctx: &Context, - input: CreateRepositoryProviderInput, + input: repository::CreateRepositoryProviderInput, ) -> Result { check_admin(ctx).await?; input.validate()?; @@ -806,7 +774,7 @@ impl Mutation { .locator .repository() .github() - .create_github_repository_provider(input.display_name, input.access_token) + .create_provider(input.display_name, input.access_token) .await?; Ok(id) } @@ -816,21 +784,21 @@ impl Mutation { ctx.locator .repository() .github() - .delete_github_repository_provider(id) + .delete_provider(id) .await?; Ok(true) } async fn update_github_repository_provider( ctx: &Context, - input: UpdateRepositoryProviderInput, + input: repository::UpdateRepositoryProviderInput, ) -> Result { check_admin(ctx).await?; input.validate()?; ctx.locator .repository() .github() - .update_github_repository_provider(input.id, input.display_name, input.access_token) + .update_provider(input.id, input.display_name, input.access_token) .await?; Ok(true) } @@ -843,14 +811,14 @@ impl Mutation { ctx.locator .repository() .github() - .update_github_provided_repository_active(id, active) + .update_repository_active(id, active) .await?; Ok(true) } async fn create_gitlab_repository_provider( ctx: &Context, - input: CreateRepositoryProviderInput, + input: repository::CreateRepositoryProviderInput, ) -> Result { check_admin(ctx).await?; input.validate()?; @@ -858,7 +826,7 @@ impl Mutation { .locator .repository() .gitlab() - .create_gitlab_repository_provider(input.display_name, input.access_token) + .create_provider(input.display_name, input.access_token) .await?; Ok(id) } @@ -868,21 +836,21 @@ impl Mutation { ctx.locator .repository() .gitlab() - .delete_gitlab_repository_provider(id) + .delete_provider(id) .await?; Ok(true) } async fn update_gitlab_repository_provider( ctx: &Context, - input: UpdateRepositoryProviderInput, + input: repository::UpdateRepositoryProviderInput, ) -> Result { check_admin(ctx).await?; input.validate()?; ctx.locator .repository() .gitlab() - .update_gitlab_repository_provider(input.id, input.display_name, input.access_token) + .update_provider(input.id, input.display_name, input.access_token) .await?; Ok(true) } @@ -895,7 +863,7 @@ impl Mutation { ctx.locator .repository() .gitlab() - .update_gitlab_provided_repository_active(id, active) + .update_repository_active(id, active) .await?; Ok(true) } diff --git a/ee/tabby-webserver/src/schema/git_repository.rs b/ee/tabby-webserver/src/schema/repository/git.rs similarity index 92% rename from ee/tabby-webserver/src/schema/git_repository.rs rename to ee/tabby-webserver/src/schema/repository/git.rs index 18a27c9a9549..478b38f934da 100644 --- a/ee/tabby-webserver/src/schema/git_repository.rs +++ b/ee/tabby-webserver/src/schema/repository/git.rs @@ -2,8 +2,11 @@ use async_trait::async_trait; use juniper::{GraphQLObject, ID}; use validator::Validate; -use super::{repository::RepositoryProvider, Context, Result}; -use crate::juniper::relay::NodeType; +use super::RepositoryProvider; +use crate::{ + juniper::relay::NodeType, + schema::{Context, Result}, +}; #[derive(Validate)] pub struct CreateGitRepositoryInput { diff --git a/ee/tabby-webserver/src/schema/github_repository_provider.rs b/ee/tabby-webserver/src/schema/repository/github.rs similarity index 65% rename from ee/tabby-webserver/src/schema/github_repository_provider.rs rename to ee/tabby-webserver/src/schema/repository/github.rs index 3412729f2b6e..a2ebf3907dc9 100644 --- a/ee/tabby-webserver/src/schema/github_repository_provider.rs +++ b/ee/tabby-webserver/src/schema/repository/github.rs @@ -2,8 +2,11 @@ use async_trait::async_trait; use chrono::{DateTime, Utc}; use juniper::{GraphQLObject, ID}; -use super::{repository::RepositoryProvider, types::RepositoryProviderStatus, Context}; -use crate::{juniper::relay::NodeType, schema::Result}; +use super::{RepositoryProvider, RepositoryProviderStatus}; +use crate::{ + juniper::relay::NodeType, + schema::{Context, Result}, +}; #[derive(GraphQLObject, Debug, PartialEq)] #[graphql(context = Context)] @@ -61,27 +64,19 @@ impl NodeType for GithubProvidedRepository { } #[async_trait] -pub trait GithubRepositoryProviderService: Send + Sync + RepositoryProvider { - async fn create_github_repository_provider( - &self, - display_name: String, - access_token: String, - ) -> Result; - async fn get_github_repository_provider(&self, id: ID) -> Result; - async fn delete_github_repository_provider(&self, id: ID) -> Result<()>; - async fn update_github_repository_provider( +pub trait GithubRepositoryService: Send + Sync + RepositoryProvider { + async fn create_provider(&self, display_name: String, access_token: String) -> Result; + async fn get_provider(&self, id: ID) -> Result; + async fn delete_provider(&self, id: ID) -> Result<()>; + async fn update_provider( &self, id: ID, display_name: String, access_token: String, ) -> Result<()>; - async fn update_github_repository_provider_sync_status( - &self, - id: ID, - success: bool, - ) -> Result<()>; + async fn update_provider_status(&self, id: ID, success: bool) -> Result<()>; - async fn list_github_repository_providers( + async fn list_providers( &self, ids: Vec, after: Option, @@ -90,7 +85,7 @@ pub trait GithubRepositoryProviderService: Send + Sync + RepositoryProvider { last: Option, ) -> Result>; - async fn list_github_provided_repositories_by_provider( + async fn list_repositories( &self, provider: Vec, after: Option, @@ -99,18 +94,18 @@ pub trait GithubRepositoryProviderService: Send + Sync + RepositoryProvider { last: Option, ) -> Result>; - async fn upsert_github_provided_repository( + async fn upsert_repository( &self, provider_id: ID, vendor_id: String, display_name: String, git_url: String, ) -> Result<()>; - async fn update_github_provided_repository_active(&self, id: ID, active: bool) -> Result<()>; - async fn list_provided_git_urls(&self) -> Result>; - async fn delete_outdated_github_provided_repositories( + async fn update_repository_active(&self, id: ID, active: bool) -> Result<()>; + async fn delete_outdated_repositories( &self, provider_id: ID, cutoff_timestamp: DateTime, ) -> Result<()>; + async fn list_active_git_urls(&self) -> Result>; } diff --git a/ee/tabby-webserver/src/schema/gitlab_repository_provider.rs b/ee/tabby-webserver/src/schema/repository/gitlab.rs similarity index 65% rename from ee/tabby-webserver/src/schema/gitlab_repository_provider.rs rename to ee/tabby-webserver/src/schema/repository/gitlab.rs index b446ddbe80ca..be72a7df60e5 100644 --- a/ee/tabby-webserver/src/schema/gitlab_repository_provider.rs +++ b/ee/tabby-webserver/src/schema/repository/gitlab.rs @@ -2,8 +2,11 @@ use async_trait::async_trait; use chrono::{DateTime, Utc}; use juniper::{GraphQLObject, ID}; -use super::{repository::RepositoryProvider, types::RepositoryProviderStatus, Context}; -use crate::{juniper::relay::NodeType, schema::Result}; +use super::{RepositoryProvider, RepositoryProviderStatus}; +use crate::{ + juniper::relay::NodeType, + schema::{Context, Result}, +}; #[derive(GraphQLObject, Debug, PartialEq)] #[graphql(context = Context)] @@ -61,27 +64,19 @@ impl NodeType for GitlabProvidedRepository { } #[async_trait] -pub trait GitlabRepositoryProviderService: Send + Sync + RepositoryProvider { - async fn create_gitlab_repository_provider( - &self, - display_name: String, - access_token: String, - ) -> Result; - async fn get_gitlab_repository_provider(&self, id: ID) -> Result; - async fn delete_gitlab_repository_provider(&self, id: ID) -> Result<()>; - async fn update_gitlab_repository_provider( +pub trait GitlabRepositoryService: Send + Sync + RepositoryProvider { + async fn create_provider(&self, display_name: String, access_token: String) -> Result; + async fn get_provider(&self, id: ID) -> Result; + async fn delete_provider(&self, id: ID) -> Result<()>; + async fn update_provider( &self, id: ID, display_name: String, access_token: String, ) -> Result<()>; - async fn update_gitlab_repository_provider_sync_status( - &self, - id: ID, - success: bool, - ) -> Result<()>; + async fn update_provider_status(&self, id: ID, success: bool) -> Result<()>; - async fn list_gitlab_repository_providers( + async fn list_providers( &self, ids: Vec, after: Option, @@ -90,7 +85,7 @@ pub trait GitlabRepositoryProviderService: Send + Sync + RepositoryProvider { last: Option, ) -> Result>; - async fn list_gitlab_provided_repositories_by_provider( + async fn list_repositories( &self, provider: Vec, after: Option, @@ -99,16 +94,16 @@ pub trait GitlabRepositoryProviderService: Send + Sync + RepositoryProvider { last: Option, ) -> Result>; - async fn upsert_gitlab_provided_repository( + async fn upsert_repository( &self, provider_id: ID, vendor_id: String, display_name: String, git_url: String, ) -> Result<()>; - async fn update_gitlab_provided_repository_active(&self, id: ID, active: bool) -> Result<()>; - async fn list_provided_git_urls(&self) -> Result>; - async fn delete_outdated_gitlab_provided_repositories( + async fn update_repository_active(&self, id: ID, active: bool) -> Result<()>; + async fn list_active_git_urls(&self) -> Result>; + async fn delete_outdated_repositories( &self, provider_id: ID, cutoff_timestamp: DateTime, diff --git a/ee/tabby-webserver/src/schema/repository.rs b/ee/tabby-webserver/src/schema/repository/mod.rs similarity index 84% rename from ee/tabby-webserver/src/schema/repository.rs rename to ee/tabby-webserver/src/schema/repository/mod.rs index 0a466c932341..84ab79a2d044 100644 --- a/ee/tabby-webserver/src/schema/repository.rs +++ b/ee/tabby-webserver/src/schema/repository/mod.rs @@ -1,17 +1,23 @@ +mod types; +pub use types::*; + +mod git; +pub use git::{CreateGitRepositoryInput, GitRepository, GitRepositoryService}; + +mod github; +pub use github::{GithubProvidedRepository, GithubRepositoryProvider, GithubRepositoryService}; + +mod gitlab; use std::{path::PathBuf, sync::Arc}; use async_trait::async_trait; +pub use gitlab::{GitlabProvidedRepository, GitlabRepositoryProvider, GitlabRepositoryService}; use juniper::{GraphQLEnum, GraphQLObject, ID}; use serde::Deserialize; use tabby_common::config::{RepositoryAccess, RepositoryConfig}; use tabby_search::FileSearch; -use super::{ - git_repository::{GitRepository, GitRepositoryService}, - github_repository_provider::{GithubProvidedRepository, GithubRepositoryProviderService}, - gitlab_repository_provider::{GitlabProvidedRepository, GitlabRepositoryProviderService}, - Result, -}; +use super::Result; #[derive(GraphQLObject, Debug)] pub struct FileEntrySearchResult { @@ -102,7 +108,7 @@ pub trait RepositoryService: Send + Sync + RepositoryAccess { ) -> Result>; fn git(&self) -> Arc; - fn github(&self) -> Arc; - fn gitlab(&self) -> Arc; + fn github(&self) -> Arc; + fn gitlab(&self) -> Arc; fn access(self: Arc) -> Arc; } diff --git a/ee/tabby-webserver/src/schema/types.rs b/ee/tabby-webserver/src/schema/repository/types.rs similarity index 100% rename from ee/tabby-webserver/src/schema/types.rs rename to ee/tabby-webserver/src/schema/repository/types.rs diff --git a/ee/tabby-webserver/src/service/dao.rs b/ee/tabby-webserver/src/service/dao.rs index 620abd705e57..7b93e37f094b 100644 --- a/ee/tabby-webserver/src/service/dao.rs +++ b/ee/tabby-webserver/src/service/dao.rs @@ -11,12 +11,12 @@ use crate::{ schema::{ auth::{self, OAuthCredential, OAuthProvider}, email::{AuthMethod, EmailSetting, Encryption}, - git_repository::GitRepository, - github_repository_provider::{GithubProvidedRepository, GithubRepositoryProvider}, - gitlab_repository_provider::{GitlabProvidedRepository, GitlabRepositoryProvider}, job, + repository::{ + GitRepository, GithubProvidedRepository, GithubRepositoryProvider, + GitlabProvidedRepository, GitlabRepositoryProvider, RepositoryProviderStatus, + }, setting::{NetworkSetting, SecuritySetting}, - types::RepositoryProviderStatus, user_event::{EventKind, UserEvent}, CoreError, }, diff --git a/ee/tabby-webserver/src/service/mod.rs b/ee/tabby-webserver/src/service/mod.rs index b851a0f100f5..3eb366766887 100644 --- a/ee/tabby-webserver/src/service/mod.rs +++ b/ee/tabby-webserver/src/service/mod.rs @@ -3,9 +3,6 @@ mod auth; mod dao; mod email; pub mod event_logger; -mod git_repository; -mod github_repository_provider; -mod gitlab_repository_provider; mod job; mod license; mod proxy; diff --git a/ee/tabby-webserver/src/service/git_repository.rs b/ee/tabby-webserver/src/service/repository/git.rs similarity index 94% rename from ee/tabby-webserver/src/service/git_repository.rs rename to ee/tabby-webserver/src/service/repository/git.rs index 72185f04bc7e..3f82f6e95da4 100644 --- a/ee/tabby-webserver/src/service/git_repository.rs +++ b/ee/tabby-webserver/src/service/repository/git.rs @@ -2,11 +2,12 @@ use async_trait::async_trait; use juniper::ID; use tabby_db::DbConn; -use super::{graphql_pagination_to_filter, AsID, AsRowid}; -use crate::schema::{ - git_repository::{GitRepository, GitRepositoryService}, - repository::{Repository, RepositoryProvider}, - Result, +use crate::{ + schema::{ + repository::{GitRepository, GitRepositoryService, Repository, RepositoryProvider}, + Result, + }, + service::{graphql_pagination_to_filter, AsID, AsRowid}, }; #[async_trait] diff --git a/ee/tabby-webserver/src/service/github_repository_provider.rs b/ee/tabby-webserver/src/service/repository/github.rs similarity index 77% rename from ee/tabby-webserver/src/service/github_repository_provider.rs rename to ee/tabby-webserver/src/service/repository/github.rs index b5d764929599..43c7ca5e8a77 100644 --- a/ee/tabby-webserver/src/service/github_repository_provider.rs +++ b/ee/tabby-webserver/src/service/repository/github.rs @@ -6,33 +6,28 @@ use juniper::ID; use tabby_db::DbConn; use url::Url; -use super::{AsID, AsRowid}; use crate::{ schema::{ - github_repository_provider::{ - GithubProvidedRepository, GithubRepositoryProvider, GithubRepositoryProviderService, + repository::{ + GithubProvidedRepository, GithubRepositoryProvider, GithubRepositoryService, + Repository, RepositoryProvider, }, - repository::{Repository, RepositoryProvider}, Result, }, - service::graphql_pagination_to_filter, + service::{graphql_pagination_to_filter, AsID, AsRowid}, }; struct GithubRepositoryProviderServiceImpl { db: DbConn, } -pub fn create(db: DbConn) -> impl GithubRepositoryProviderService { +pub fn create(db: DbConn) -> impl GithubRepositoryService { GithubRepositoryProviderServiceImpl { db } } #[async_trait] -impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl { - async fn create_github_repository_provider( - &self, - display_name: String, - access_token: String, - ) -> Result { +impl GithubRepositoryService for GithubRepositoryProviderServiceImpl { + async fn create_provider(&self, display_name: String, access_token: String) -> Result { let id = self .db .create_github_provider(display_name, access_token) @@ -40,17 +35,17 @@ impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl { Ok(id.as_id()) } - async fn get_github_repository_provider(&self, id: ID) -> Result { + async fn get_provider(&self, id: ID) -> Result { let provider = self.db.get_github_provider(id.as_rowid()?).await?; Ok(provider.into()) } - async fn delete_github_repository_provider(&self, id: ID) -> Result<()> { + async fn delete_provider(&self, id: ID) -> Result<()> { self.db.delete_github_provider(id.as_rowid()?).await?; Ok(()) } - async fn list_github_repository_providers( + async fn list_providers( &self, ids: Vec, after: Option, @@ -75,7 +70,7 @@ impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl { .collect()) } - async fn list_github_provided_repositories_by_provider( + async fn list_repositories( &self, providers: Vec, after: Option, @@ -99,7 +94,7 @@ impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl { .collect()) } - async fn upsert_github_provided_repository( + async fn upsert_repository( &self, provider_id: ID, vendor_id: String, @@ -117,14 +112,14 @@ impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl { Ok(()) } - async fn update_github_provided_repository_active(&self, id: ID, active: bool) -> Result<()> { + async fn update_repository_active(&self, id: ID, active: bool) -> Result<()> { self.db .update_github_provided_repository_active(id.as_rowid()?, active) .await?; Ok(()) } - async fn update_github_repository_provider( + async fn update_provider( &self, id: ID, display_name: String, @@ -136,16 +131,16 @@ impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl { Ok(()) } - async fn list_provided_git_urls(&self) -> Result> { + async fn list_active_git_urls(&self) -> Result> { let tokens: HashMap = self - .list_github_repository_providers(vec![], None, None, None, None) + .list_providers(vec![], None, None, None, None) .await? .into_iter() .filter_map(|provider| Some((provider.id.to_string(), provider.access_token?))) .collect(); let mut repos = self - .list_github_provided_repositories_by_provider(vec![], None, None, None, None) + .list_repositories(vec![], None, None, None, None) .await?; deduplicate_github_repositories(&mut repos); @@ -166,7 +161,7 @@ impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl { Ok(urls) } - async fn delete_outdated_github_provided_repositories( + async fn delete_outdated_repositories( &self, provider_id: ID, cutoff_timestamp: DateTime, @@ -177,11 +172,7 @@ impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl { Ok(()) } - async fn update_github_repository_provider_sync_status( - &self, - id: ID, - success: bool, - ) -> Result<()> { + async fn update_provider_status(&self, id: ID, success: bool) -> Result<()> { self.db .update_github_provider_sync_status(id.as_rowid()?, success) .await?; @@ -193,7 +184,7 @@ impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl { impl RepositoryProvider for GithubRepositoryProviderServiceImpl { async fn repository_list(&self) -> Result> { Ok(self - .list_github_provided_repositories_by_provider(vec![], None, None, None, None) + .list_repositories(vec![], None, None, None, None) .await? .into_iter() .filter(|x| x.active) @@ -221,7 +212,7 @@ mod tests { use chrono::Duration; use super::*; - use crate::{schema::types::RepositoryProviderStatus, service::AsID}; + use crate::{schema::repository::RepositoryProviderStatus, service::AsID}; #[tokio::test] async fn test_github_provided_repositories() { @@ -260,7 +251,7 @@ mod tests { // Test listing with no filter on providers let repos = service - .list_github_provided_repositories_by_provider(vec![], None, None, None, None) + .list_repositories(vec![], None, None, None, None) .await .unwrap(); @@ -270,13 +261,7 @@ mod tests { // Test listing with a filter on providers let repos = service - .list_github_provided_repositories_by_provider( - vec![provider_id1.as_id()], - None, - None, - None, - None, - ) + .list_repositories(vec![provider_id1.as_id()], None, None, None, None) .await .unwrap(); @@ -293,7 +278,7 @@ mod tests { .unwrap(); let repos = service - .list_github_provided_repositories_by_provider(vec![], None, None, None, None) + .list_repositories(vec![], None, None, None, None) .await .unwrap(); @@ -307,15 +292,12 @@ mod tests { let service = super::create(db.clone()); let id = service - .create_github_repository_provider("id".into(), "secret".into()) + .create_provider("id".into(), "secret".into()) .await .unwrap(); // Test retrieving github provider by ID - let provider1 = service - .get_github_repository_provider(id.clone()) - .await - .unwrap(); + let provider1 = service.get_provider(id.clone()).await.unwrap(); assert_eq!( provider1, GithubRepositoryProvider { @@ -328,22 +310,19 @@ mod tests { // Test listing github providers let providers = service - .list_github_repository_providers(vec![], None, None, None, None) + .list_providers(vec![], None, None, None, None) .await .unwrap(); assert_eq!(providers.len(), 1); assert_eq!(providers[0].access_token, Some("secret".into())); // Test deleting github provider - service - .delete_github_repository_provider(id.clone()) - .await - .unwrap(); + service.delete_provider(id.clone()).await.unwrap(); assert_eq!( 0, service - .list_github_repository_providers(vec![], None, None, None, None) + .list_providers(vec![], None, None, None, None) .await .unwrap() .len() @@ -374,7 +353,7 @@ mod tests { .await .unwrap(); - let git_urls = service.list_provided_git_urls().await.unwrap(); + let git_urls = service.list_active_git_urls().await.unwrap(); assert_eq!( git_urls, ["https://token@github.com/TabbyML/tabby".to_string()] @@ -392,7 +371,7 @@ mod tests { .unwrap(); service - .update_github_repository_provider_sync_status(provider_id.as_id(), true) + .update_provider_status(provider_id.as_id(), true) .await .unwrap(); @@ -402,7 +381,7 @@ mod tests { assert!(provider.synced_at.is_some()); service - .update_github_repository_provider_sync_status(provider_id.as_id(), false) + .update_provider_status(provider_id.as_id(), false) .await .unwrap(); @@ -434,14 +413,14 @@ mod tests { .unwrap(); service - .delete_outdated_github_provided_repositories(provider_id.as_id(), time) + .delete_outdated_repositories(provider_id.as_id(), time) .await .unwrap(); assert_eq!( 1, service - .list_github_provided_repositories_by_provider(vec![], None, None, None, None) + .list_repositories(vec![], None, None, None, None) .await .unwrap() .len() @@ -450,14 +429,14 @@ mod tests { let time = time + Duration::minutes(1); service - .delete_outdated_github_provided_repositories(provider_id.as_id(), time) + .delete_outdated_repositories(provider_id.as_id(), time) .await .unwrap(); assert_eq!( 0, service - .list_github_provided_repositories_by_provider(vec![], None, None, None, None) + .list_repositories(vec![], None, None, None, None) .await .unwrap() .len() diff --git a/ee/tabby-webserver/src/service/gitlab_repository_provider.rs b/ee/tabby-webserver/src/service/repository/gitlab.rs similarity index 77% rename from ee/tabby-webserver/src/service/gitlab_repository_provider.rs rename to ee/tabby-webserver/src/service/repository/gitlab.rs index a948f9377ee0..dac9089a4b68 100644 --- a/ee/tabby-webserver/src/service/gitlab_repository_provider.rs +++ b/ee/tabby-webserver/src/service/repository/gitlab.rs @@ -6,33 +6,28 @@ use juniper::ID; use tabby_db::DbConn; use url::Url; -use super::{AsID, AsRowid}; use crate::{ schema::{ - gitlab_repository_provider::{ - GitlabProvidedRepository, GitlabRepositoryProvider, GitlabRepositoryProviderService, + repository::{ + GitlabProvidedRepository, GitlabRepositoryProvider, GitlabRepositoryService, + Repository, RepositoryProvider, }, - repository::{Repository, RepositoryProvider}, Result, }, - service::graphql_pagination_to_filter, + service::{graphql_pagination_to_filter, AsID, AsRowid}, }; struct GitlabRepositoryProviderServiceImpl { db: DbConn, } -pub fn create(db: DbConn) -> impl GitlabRepositoryProviderService { +pub fn create(db: DbConn) -> impl GitlabRepositoryService { GitlabRepositoryProviderServiceImpl { db } } #[async_trait] -impl GitlabRepositoryProviderService for GitlabRepositoryProviderServiceImpl { - async fn create_gitlab_repository_provider( - &self, - display_name: String, - access_token: String, - ) -> Result { +impl GitlabRepositoryService for GitlabRepositoryProviderServiceImpl { + async fn create_provider(&self, display_name: String, access_token: String) -> Result { let id = self .db .create_gitlab_provider(display_name, access_token) @@ -40,17 +35,17 @@ impl GitlabRepositoryProviderService for GitlabRepositoryProviderServiceImpl { Ok(id.as_id()) } - async fn get_gitlab_repository_provider(&self, id: ID) -> Result { + async fn get_provider(&self, id: ID) -> Result { let provider = self.db.get_gitlab_provider(id.as_rowid()?).await?; Ok(provider.into()) } - async fn delete_gitlab_repository_provider(&self, id: ID) -> Result<()> { + async fn delete_provider(&self, id: ID) -> Result<()> { self.db.delete_gitlab_provider(id.as_rowid()?).await?; Ok(()) } - async fn list_gitlab_repository_providers( + async fn list_providers( &self, ids: Vec, after: Option, @@ -75,7 +70,7 @@ impl GitlabRepositoryProviderService for GitlabRepositoryProviderServiceImpl { .collect()) } - async fn list_gitlab_provided_repositories_by_provider( + async fn list_repositories( &self, providers: Vec, after: Option, @@ -99,7 +94,7 @@ impl GitlabRepositoryProviderService for GitlabRepositoryProviderServiceImpl { .collect()) } - async fn upsert_gitlab_provided_repository( + async fn upsert_repository( &self, provider_id: ID, vendor_id: String, @@ -117,14 +112,14 @@ impl GitlabRepositoryProviderService for GitlabRepositoryProviderServiceImpl { Ok(()) } - async fn update_gitlab_provided_repository_active(&self, id: ID, active: bool) -> Result<()> { + async fn update_repository_active(&self, id: ID, active: bool) -> Result<()> { self.db .update_gitlab_provided_repository_active(id.as_rowid()?, active) .await?; Ok(()) } - async fn update_gitlab_repository_provider( + async fn update_provider( &self, id: ID, display_name: String, @@ -136,16 +131,16 @@ impl GitlabRepositoryProviderService for GitlabRepositoryProviderServiceImpl { Ok(()) } - async fn list_provided_git_urls(&self) -> Result> { + async fn list_active_git_urls(&self) -> Result> { let tokens: HashMap = self - .list_gitlab_repository_providers(vec![], None, None, None, None) + .list_providers(vec![], None, None, None, None) .await? .into_iter() .filter_map(|provider| Some((provider.id.to_string(), provider.access_token?))) .collect(); let mut repos = self - .list_gitlab_provided_repositories_by_provider(vec![], None, None, None, None) + .list_repositories(vec![], None, None, None, None) .await?; deduplicate_gitlab_repositories(&mut repos); @@ -169,7 +164,7 @@ impl GitlabRepositoryProviderService for GitlabRepositoryProviderServiceImpl { Ok(urls) } - async fn delete_outdated_gitlab_provided_repositories( + async fn delete_outdated_repositories( &self, provider_id: ID, cutoff_timestamp: DateTime, @@ -180,11 +175,7 @@ impl GitlabRepositoryProviderService for GitlabRepositoryProviderServiceImpl { Ok(()) } - async fn update_gitlab_repository_provider_sync_status( - &self, - id: ID, - success: bool, - ) -> Result<()> { + async fn update_provider_status(&self, id: ID, success: bool) -> Result<()> { self.db .update_gitlab_provider_sync_status(id.as_rowid()?, success) .await?; @@ -201,7 +192,7 @@ fn deduplicate_gitlab_repositories(repositories: &mut Vec Result> { Ok(self - .list_gitlab_provided_repositories_by_provider(vec![], None, None, None, None) + .list_repositories(vec![], None, None, None, None) .await? .into_iter() .filter(|x| x.active) @@ -224,7 +215,7 @@ mod tests { use chrono::Duration; use super::*; - use crate::{schema::types::RepositoryProviderStatus, service::AsID}; + use crate::{schema::repository::RepositoryProviderStatus, service::AsID}; #[tokio::test] async fn test_gitlab_provided_repositories() { @@ -263,7 +254,7 @@ mod tests { // Test listing with no filter on providers let repos = service - .list_gitlab_provided_repositories_by_provider(vec![], None, None, None, None) + .list_repositories(vec![], None, None, None, None) .await .unwrap(); @@ -273,13 +264,7 @@ mod tests { // Test listing with a filter on providers let repos = service - .list_gitlab_provided_repositories_by_provider( - vec![provider_id1.as_id()], - None, - None, - None, - None, - ) + .list_repositories(vec![provider_id1.as_id()], None, None, None, None) .await .unwrap(); @@ -296,7 +281,7 @@ mod tests { .unwrap(); let repos = service - .list_gitlab_provided_repositories_by_provider(vec![], None, None, None, None) + .list_repositories(vec![], None, None, None, None) .await .unwrap(); @@ -310,15 +295,12 @@ mod tests { let service = super::create(db.clone()); let id = service - .create_gitlab_repository_provider("id".into(), "secret".into()) + .create_provider("id".into(), "secret".into()) .await .unwrap(); // Test retrieving gitlab provider by ID - let provider1 = service - .get_gitlab_repository_provider(id.clone()) - .await - .unwrap(); + let provider1 = service.get_provider(id.clone()).await.unwrap(); assert_eq!( provider1, GitlabRepositoryProvider { @@ -331,22 +313,19 @@ mod tests { // Test listing gitlab providers let providers = service - .list_gitlab_repository_providers(vec![], None, None, None, None) + .list_providers(vec![], None, None, None, None) .await .unwrap(); assert_eq!(providers.len(), 1); assert_eq!(providers[0].access_token, Some("secret".into())); // Test deleting gitlab provider - service - .delete_gitlab_repository_provider(id.clone()) - .await - .unwrap(); + service.delete_provider(id.clone()).await.unwrap(); assert_eq!( 0, service - .list_gitlab_repository_providers(vec![], None, None, None, None) + .list_providers(vec![], None, None, None, None) .await .unwrap() .len() @@ -364,7 +343,7 @@ mod tests { .unwrap(); service - .update_gitlab_repository_provider_sync_status(provider_id.as_id(), true) + .update_provider_status(provider_id.as_id(), true) .await .unwrap(); @@ -374,7 +353,7 @@ mod tests { assert!(provider.synced_at.is_some()); service - .update_gitlab_repository_provider_sync_status(provider_id.as_id(), false) + .update_provider_status(provider_id.as_id(), false) .await .unwrap(); @@ -408,7 +387,7 @@ mod tests { .await .unwrap(); - let git_urls = service.list_provided_git_urls().await.unwrap(); + let git_urls = service.list_active_git_urls().await.unwrap(); assert_eq!( git_urls, ["https://oauth2:token@gitlab.com/TabbyML/tabby".to_string()] @@ -437,14 +416,14 @@ mod tests { .unwrap(); service - .delete_outdated_gitlab_provided_repositories(provider_id.as_id(), time) + .delete_outdated_repositories(provider_id.as_id(), time) .await .unwrap(); assert_eq!( 1, service - .list_gitlab_provided_repositories_by_provider(vec![], None, None, None, None) + .list_repositories(vec![], None, None, None, None) .await .unwrap() .len() @@ -453,14 +432,14 @@ mod tests { let time = time + Duration::minutes(1); service - .delete_outdated_gitlab_provided_repositories(provider_id.as_id(), time) + .delete_outdated_repositories(provider_id.as_id(), time) .await .unwrap(); assert_eq!( 0, service - .list_gitlab_provided_repositories_by_provider(vec![], None, None, None, None) + .list_repositories(vec![], None, None, None, None) .await .unwrap() .len() diff --git a/ee/tabby-webserver/src/service/repository.rs b/ee/tabby-webserver/src/service/repository/mod.rs similarity index 80% rename from ee/tabby-webserver/src/service/repository.rs rename to ee/tabby-webserver/src/service/repository/mod.rs index e8b643240626..4813a91e9c6d 100644 --- a/ee/tabby-webserver/src/service/repository.rs +++ b/ee/tabby-webserver/src/service/repository/mod.rs @@ -1,3 +1,7 @@ +mod git; +mod github; +mod gitlab; + use std::sync::Arc; use async_trait::async_trait; @@ -5,25 +9,25 @@ use juniper::ID; use tabby_common::config::{RepositoryAccess, RepositoryConfig}; use tabby_db::DbConn; -use super::{github_repository_provider, gitlab_repository_provider, Result}; use crate::schema::{ - git_repository::GitRepositoryService, - github_repository_provider::GithubRepositoryProviderService, - gitlab_repository_provider::GitlabRepositoryProviderService, - repository::{FileEntrySearchResult, Repository, RepositoryKind, RepositoryService}, + repository::{ + FileEntrySearchResult, GitRepositoryService, GithubRepositoryService, + GitlabRepositoryService, Repository, RepositoryKind, RepositoryService, + }, + Result, }; struct RepositoryServiceImpl { git: Arc, - github: Arc, - gitlab: Arc, + github: Arc, + gitlab: Arc, } pub fn create(db: DbConn) -> Arc { Arc::new(RepositoryServiceImpl { git: Arc::new(db.clone()), - github: Arc::new(github_repository_provider::create(db.clone())), - gitlab: Arc::new(gitlab_repository_provider::create(db.clone())), + github: Arc::new(github::create(db.clone())), + gitlab: Arc::new(gitlab::create(db.clone())), }) } @@ -40,7 +44,7 @@ impl RepositoryAccess for RepositoryServiceImpl { repos.extend( self.github - .list_provided_git_urls() + .list_active_git_urls() .await .unwrap_or_default() .into_iter() @@ -49,7 +53,7 @@ impl RepositoryAccess for RepositoryServiceImpl { repos.extend( self.gitlab - .list_provided_git_urls() + .list_active_git_urls() .await .unwrap_or_default() .into_iter() @@ -66,11 +70,11 @@ impl RepositoryService for RepositoryServiceImpl { self.git.clone() } - fn github(&self) -> Arc { + fn github(&self) -> Arc { self.github.clone() } - fn gitlab(&self) -> Arc { + fn gitlab(&self) -> Arc { self.gitlab.clone() }