-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
draft: begin implementing integration / third party repository service
- Loading branch information
Showing
13 changed files
with
223 additions
and
12 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use async_trait::async_trait; | ||
use chrono::{DateTime, Utc}; | ||
use juniper::ID; | ||
use strum::EnumIter; | ||
|
||
use crate::Result; | ||
|
||
#[derive(Clone, EnumIter)] | ||
pub enum IntegrationKind { | ||
Github, | ||
Gitlab, | ||
} | ||
|
||
pub enum IntegrationStatus { | ||
Ready, | ||
Pending, | ||
Failed, | ||
} | ||
|
||
pub struct IntegrationAccessToken { | ||
pub id: ID, | ||
pub kind: IntegrationKind, | ||
pub display_name: String, | ||
pub access_token: String, | ||
pub created_at: DateTime<Utc>, | ||
pub updated_at: DateTime<Utc>, | ||
pub status: IntegrationStatus, | ||
} | ||
|
||
#[async_trait] | ||
pub trait IntegrationService: Send + Sync { | ||
async fn create_integration( | ||
&self, | ||
kind: IntegrationKind, | ||
display_name: String, | ||
access_token: String, | ||
) -> Result<ID>; | ||
|
||
async fn delete_integration(&self, id: ID) -> Result<()>; | ||
async fn update_integration(&self, id: ID, display_name: String, access_token: Option<String>); | ||
async fn list_integrations( | ||
&self, | ||
ids: Option<Vec<ID>>, | ||
kind: Option<IntegrationKind>, | ||
after: Option<String>, | ||
before: Option<String>, | ||
first: Option<usize>, | ||
last: Option<usize>, | ||
) -> Result<Vec<IntegrationAccessToken>>; | ||
|
||
async fn sync_resources(&self, id: ID) -> Result<()>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use crate::{integration::IntegrationKind, schema::Result}; | ||
use async_trait::async_trait; | ||
use chrono::{DateTime, Utc}; | ||
use juniper::ID; | ||
|
||
pub struct ProvidedRepository { | ||
pub integration_id: ID, | ||
pub active: bool, | ||
pub kind: IntegrationKind, | ||
pub display_name: String, | ||
pub git_url: String, | ||
pub vendor_id: String, | ||
pub created_at: DateTime<Utc>, | ||
pub updated_at: DateTime<Utc>, | ||
} | ||
|
||
#[async_trait] | ||
pub trait ThirdPartyRepositoryService: Send + Sync { | ||
async fn list_repositories( | ||
&self, | ||
integration_ids: Option<Vec<ID>>, | ||
kind: Option<IntegrationKind>, | ||
active: Option<bool>, | ||
after: Option<String>, | ||
before: Option<String>, | ||
first: Option<usize>, | ||
last: Option<usize>, | ||
) -> Result<Vec<ProvidedRepository>>; | ||
|
||
async fn update_repository_active(&self, id: ID, active: bool) -> Result<()>; | ||
async fn list_active_git_urls(&self) -> Result<Vec<String>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
mod git; | ||
mod github; | ||
mod gitlab; | ||
mod third_party; | ||
|
||
use std::sync::Arc; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
use juniper::ID; | ||
use std::marker::PhantomData; | ||
use strum::IntoEnumIterator; | ||
use tabby_schema::{ | ||
integration::IntegrationKind, repository::ProvidedRepository, AsRowid, DbEnum, Result, | ||
}; | ||
use url::Url; | ||
|
||
use async_trait::async_trait; | ||
use tabby_db::DbConn; | ||
use tabby_schema::repository::{RepositoryProvider, ThirdPartyRepositoryService}; | ||
|
||
use crate::service::graphql_pagination_to_filter; | ||
|
||
struct ThirdPartyRepositoryServiceImpl { | ||
db: DbConn, | ||
} | ||
|
||
#[async_trait] | ||
impl ThirdPartyRepositoryService for ThirdPartyRepositoryServiceImpl { | ||
async fn list_repositories( | ||
&self, | ||
integration_ids: Option<Vec<ID>>, | ||
kind: Option<IntegrationKind>, | ||
active: Option<bool>, | ||
after: Option<String>, | ||
before: Option<String>, | ||
first: Option<usize>, | ||
last: Option<usize>, | ||
) -> Result<Vec<ProvidedRepository>> { | ||
let (limit, skip_id, backwards) = graphql_pagination_to_filter(after, before, first, last)?; | ||
|
||
let integration_ids = integration_ids | ||
.into_iter() | ||
.flatten() | ||
.map(|id| id.as_rowid()) | ||
.collect::<Result<Vec<_>, _>>()?; | ||
|
||
let kind = kind.map(|kind| kind.as_enum_str().to_string()); | ||
|
||
Ok(self | ||
.db | ||
.list_provided_repositories(integration_ids, kind, active, limit, skip_id, backwards) | ||
.await? | ||
.into_iter() | ||
.map(ProvidedRepository::try_from) | ||
.collect::<Result<_, _>>()?) | ||
} | ||
|
||
async fn update_repository_active(&self, id: ID, active: bool) -> Result<()> { | ||
self.db | ||
.update_provided_repository_active(id.as_rowid()?, active) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
async fn list_active_git_urls(&self) -> Result<Vec<String>> { | ||
let mut urls = vec![]; | ||
} | ||
} | ||
|
||
fn format_authenticated_url( | ||
kind: &IntegrationKind, | ||
git_url: &str, | ||
access_token: &str, | ||
) -> Result<String> { | ||
let mut url = Url::parse(git_url).map_err(anyhow::Error::from)?; | ||
match kind { | ||
IntegrationKind::Github => { | ||
let _ = url.set_username(access_token); | ||
} | ||
IntegrationKind::Gitlab => { | ||
let _ = url.set_username("oauth2"); | ||
let _ = url.set_password(Some(access_token)); | ||
} | ||
} | ||
Ok(url.to_string()) | ||
} |