Skip to content

Commit

Permalink
Update db spec
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbeam committed Apr 9, 2024
1 parent 468db09 commit 14b8e6f
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 2 deletions.
2 changes: 2 additions & 0 deletions ee/tabby-db/migrations/0022_github-provider.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CREATE TABLE github_repository_provider(
display_name TEXT NOT NULL,
application_id TEXT NOT NULL,
secret TEXT NOT NULL,
access_token TEXT,
CONSTRAINT `idx_application_id` UNIQUE (`application_id`)
);

Expand All @@ -13,6 +14,7 @@ CREATE TABLE github_provided_repositories(
vendor_id TEXT NOT NULL,
name TEXT NOT NULL,
git_url TEXT NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE,
CONSTRAINT `idx_vendor_id` UNIQUE (`vendor_id`),
FOREIGN KEY (github_repository_provider_id) REFERENCES github_repository_provider(id) ON DELETE CASCADE
);
Binary file modified ee/tabby-db/schema.sqlite
Binary file not shown.
20 changes: 20 additions & 0 deletions ee/tabby-webserver/src/schema/github_repository_provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use anyhow::Result;

Check failure on line 1 in ee/tabby-webserver/src/schema/github_repository_provider.rs

View workflow job for this annotation

GitHub Actions / ast-grep-lint

use-schema-result

Use schema::Result as API interface
use async_trait::async_trait;
use juniper::{GraphQLObject, ID};

#[derive(GraphQLObject)]
pub struct GithubRepositoryProvider {
pub display_name: String,
pub application_id: String,
}

#[async_trait]
pub trait GithubRepositoryProviderService: Send + Sync {
async fn get_github_repository_provider(&self, id: ID) -> Result<GithubRepositoryProvider>;
async fn read_github_repository_provider_secret(&self, id: ID) -> Result<String>;
async fn set_github_repository_provider_token(
&self,
id: ID,
access_token: String,
) -> Result<()>;
}
3 changes: 3 additions & 0 deletions ee/tabby-webserver/src/schema/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod auth;
pub mod email;
pub mod github_repository_provider;
pub mod job;
pub mod license;
pub mod repository;
Expand Down Expand Up @@ -33,6 +34,7 @@ use self::{
RequestPasswordResetEmailInput, UpdateOAuthCredentialInput,
},
email::{EmailService, EmailSetting, EmailSettingInput},
github_repository_provider::GithubRepositoryProviderService,
license::{IsLicenseValid, LicenseInfo, LicenseService, LicenseType},
repository::{Repository, RepositoryService},
setting::{
Expand All @@ -54,6 +56,7 @@ pub trait ServiceLocator: Send + Sync {
fn email(&self) -> Arc<dyn EmailService>;
fn setting(&self) -> Arc<dyn SettingService>;
fn license(&self) -> Arc<dyn LicenseService>;
fn github_repository_provider(&self) -> Arc<dyn GithubRepositoryProviderService>;
}

pub struct Context {
Expand Down
45 changes: 45 additions & 0 deletions ee/tabby-webserver/src/service/github_repository_provider.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use anyhow::Result;
use async_trait::async_trait;
use juniper::ID;
use tabby_db::DbConn;

use crate::schema::github_repository_provider::{
GithubRepositoryProvider, GithubRepositoryProviderService,
};

use super::AsRowid;

struct GithubRepositoryProviderServiceImpl {
db: DbConn,
}

pub fn new_github_repository_provider_service(db: DbConn) -> impl GithubRepositoryProviderService {
GithubRepositoryProviderServiceImpl { db }
}

#[async_trait]
impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl {
async fn get_github_repository_provider(&self, id: ID) -> Result<GithubRepositoryProvider> {
let provider = self.db.get_github_provider(id.as_rowid()? as i64).await?;
Ok(GithubRepositoryProvider {
display_name: provider.display_name,
application_id: provider.application_id,
})
}

async fn read_github_repository_provider_secret(&self, id: ID) -> Result<String> {
let provider = self.db.get_github_provider(id.as_rowid()? as i64).await?;
Ok(provider.secret)
}

async fn set_github_repository_provider_token(
&self,
id: ID,
access_token: String,
) -> Result<()> {
self.db
.update_github_provider_token(id.as_rowid()? as i64, access_token)
.await?;
Ok(())
}
}
19 changes: 17 additions & 2 deletions ee/tabby-webserver/src/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod auth;
mod dao;
mod email;
pub mod event_logger;
mod github_repository_provider;
mod job;
mod license;
mod proxy;
Expand All @@ -28,11 +29,14 @@ use tabby_db::DbConn;
use tracing::{info, warn};

use self::{
auth::new_authentication_service, email::new_email_service, license::new_license_service,
auth::new_authentication_service, email::new_email_service,
github_repository_provider::new_github_repository_provider_service,
license::new_license_service,
};
use crate::schema::{
auth::AuthenticationService,
email::EmailService,
github_repository_provider::GithubRepositoryProviderService,
job::JobService,
license::{IsLicenseValid, LicenseService},
repository::RepositoryService,
Expand All @@ -49,6 +53,7 @@ struct ServerContext {
mail: Arc<dyn EmailService>,
auth: Arc<dyn AuthenticationService>,
license: Arc<dyn LicenseService>,
github_repository_provider: Arc<dyn GithubRepositoryProviderService>,

logger: Arc<dyn EventLogger>,
code: Arc<dyn CodeSearch>,
Expand All @@ -63,11 +68,12 @@ impl ServerContext {
db_conn: DbConn,
is_chat_enabled_locally: bool,
) -> Self {
let mail = Arc::new(
let arc = Arc::new(
new_email_service(db_conn.clone())
.await
.expect("failed to initialize mail service"),
);
let mail = arc;
let license = Arc::new(
new_license_service(db_conn.clone())
.await
Expand All @@ -84,6 +90,9 @@ impl ServerContext {
license.clone(),
)),
license,
github_repository_provider: Arc::new(new_github_repository_provider_service(
db_conn.clone(),
)),
db_conn,
logger,
code,
Expand Down Expand Up @@ -287,6 +296,12 @@ impl ServiceLocator for Arc<ServerContext> {
fn license(&self) -> Arc<dyn LicenseService> {
self.license.clone()
}

fn github_repository_provider(
&self,
) -> Arc<dyn crate::schema::github_repository_provider::GithubRepositoryProviderService> {
self.github_repository_provider.clone()
}
}

pub async fn create_service_locator(
Expand Down

0 comments on commit 14b8e6f

Please sign in to comment.