Skip to content

Commit

Permalink
Implement suggestions and RepositoryAccess listing github-provided repos
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbeam committed Apr 10, 2024
1 parent fafa445 commit 9a9b631
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 13 deletions.
5 changes: 2 additions & 3 deletions ee/tabby-db/migrations/0024_github-provided-repos.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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,
FOREIGN KEY (github_repository_provider_id) REFERENCES github_repository_provider(id),
CONSTRAINT provided_repository_unique_name UNIQUE (name)
active BOOLEAN NOT NULL DEFAULT FALSE,
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.
10 changes: 8 additions & 2 deletions ee/tabby-db/src/github_repository_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,16 @@ impl DbConn {

pub async fn list_github_provided_repositories(
&self,
provider_id: i64,
provider_ids: Vec<i64>,
limit: Option<usize>,
skip_id: Option<i32>,
backwards: bool,
) -> Result<Vec<GithubProvidedRepositoryDAO>> {
let provider_ids = provider_ids
.into_iter()
.map(|id| id.to_string())
.collect::<Vec<_>>()
.join(", ");
let repos = query_paged_as!(
GithubProvidedRepositoryDAO,
"github_provided_repositories",
Expand All @@ -150,7 +155,8 @@ impl DbConn {
limit,
skip_id,
backwards,
Some(format!("github_repository_provider_id = {provider_id}"))
(!provider_ids.is_empty())
.then(|| format!("github_repository_provider_id IN ({provider_ids})"))
)
.fetch_all(&self.pool)
.await?;
Expand Down
76 changes: 74 additions & 2 deletions ee/tabby-webserver/src/hub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ impl Hub for Arc<HubImpl> {
}
}
}

async fn list_repositories(self, _context: tarpc::context::Context) -> Vec<RepositoryConfig> {
let mut repositories = vec![];

Check warning on line 149 in ee/tabby-webserver/src/hub/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/hub/mod.rs#L149

Added line #L149 was not covered by tests

let result = self
.ctx
.repository()
Expand All @@ -156,9 +159,78 @@ impl Hub for Arc<HubImpl> {
.map(|r| RepositoryConfig::new_named(r.name, r.git_url))
.collect()
});
result.unwrap_or_else(|e| {
repositories.extend(result.unwrap_or_else(|e| {

Check warning on line 162 in ee/tabby-webserver/src/hub/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/hub/mod.rs#L162

Added line #L162 was not covered by tests
warn!("Failed to fetch repositories: {e}");
vec![]
})
}));

let provider_service = self.ctx.github_repository_provider();
let repository_providers = provider_service
.list_github_repository_providers(None, None, Some(1024), None)
.await
.unwrap_or_else(|e| {
warn!("Failed to fetch GitHub repository providers: {e}");
vec![]
});

Check warning on line 174 in ee/tabby-webserver/src/hub/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/hub/mod.rs#L165-L174

Added lines #L165 - L174 were not covered by tests

for provider in repository_providers {
let Ok(access_token) = provider_service
.read_github_repository_provider_access_token(provider.id.clone())
.await

Check warning on line 179 in ee/tabby-webserver/src/hub/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/hub/mod.rs#L176-L179

Added lines #L176 - L179 were not covered by tests
else {
continue;

Check warning on line 181 in ee/tabby-webserver/src/hub/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/hub/mod.rs#L181

Added line #L181 was not covered by tests
};

let repos = match provider_service
.list_github_provided_repositories_by_provider(
vec![provider.id.clone()],
None,
None,
Some(1024),
None,
)
.await

Check warning on line 192 in ee/tabby-webserver/src/hub/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/hub/mod.rs#L184-L192

Added lines #L184 - L192 were not covered by tests
{
Ok(repos) => repos,
Err(e) => {
warn!(
"Failed to retrieve repositories provided by {name}: {e}",
name = provider.display_name
);
continue;

Check warning on line 200 in ee/tabby-webserver/src/hub/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/hub/mod.rs#L194-L200

Added lines #L194 - L200 were not covered by tests
}
};
repositories.extend(repos.into_iter().filter(|repo| repo.active).map(|repo| {
RepositoryConfig::new_named(
repo.name,
format_authenticated_git_url(repo.git_url, &access_token),
)
}))

Check warning on line 208 in ee/tabby-webserver/src/hub/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/hub/mod.rs#L203-L208

Added lines #L203 - L208 were not covered by tests
}

repositories

Check warning on line 211 in ee/tabby-webserver/src/hub/mod.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/hub/mod.rs#L211

Added line #L211 was not covered by tests
}
}

fn format_authenticated_git_url(mut git_url: String, access_token: &str) -> String {
let split_pos = git_url.find("://").map(|i| i + 3).unwrap_or(0);
git_url.insert_str(split_pos, &format!("{access_token}@"));
git_url
}

#[cfg(test)]
mod tests {
use crate::hub::format_authenticated_git_url;

#[test]
fn test_format_authenticated_git_url() {
assert_eq!(
format_authenticated_git_url("https://github.com/TabbyML/tabby".into(), "token".into()),
"https://[email protected]/TabbyML/tabby"
);
assert_eq!(
format_authenticated_git_url("github.com/TabbyML/tabby".into(), "token".into()),
"[email protected]/TabbyML/tabby"
);
}
}
4 changes: 3 additions & 1 deletion ee/tabby-webserver/src/schema/github_repository_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub struct GithubProvidedRepository {
pub github_repository_provider_id: ID,
pub name: String,
pub git_url: String,
pub active: bool,
}

impl NodeType for GithubProvidedRepository {
Expand Down Expand Up @@ -65,6 +66,7 @@ pub trait GithubRepositoryProviderService: Send + Sync {
) -> Result<ID>;
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 read_github_repository_provider_access_token(&self, id: ID) -> Result<String>;
async fn update_github_repository_provider_access_token(
&self,
id: ID,
Expand All @@ -81,7 +83,7 @@ pub trait GithubRepositoryProviderService: Send + Sync {

async fn list_github_provided_repositories_by_provider(
&self,
provider: ID,
provider: Vec<ID>,
after: Option<String>,
before: Option<String>,
first: Option<usize>,
Expand Down
6 changes: 3 additions & 3 deletions ee/tabby-webserver/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,9 @@ impl Query {
.await
}

async fn github_repositories_by_provider(
async fn github_repositories(
ctx: &Context,
github_repository_provider_id: ID,
provider_ids: Vec<ID>,
after: Option<String>,
before: Option<String>,
first: Option<i32>,
Expand All @@ -275,7 +275,7 @@ impl Query {
.locator
.github_repository_provider()
.list_github_provided_repositories_by_provider(
github_repository_provider_id,
provider_ids,
after,
before,
first,
Expand Down
1 change: 1 addition & 0 deletions ee/tabby-webserver/src/service/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ impl From<GithubProvidedRepositoryDAO> for GithubProvidedRepository {
name: value.name,
git_url: value.git_url,
vendor_id: value.vendor_id,
active: value.active,
}
}

Check warning on line 143 in ee/tabby-webserver/src/service/dao.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/dao.rs#L134-L143

Added lines #L134 - L143 were not covered by tests
}
Expand Down
17 changes: 15 additions & 2 deletions ee/tabby-webserver/src/service/github_repository_provider.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::anyhow;
use async_trait::async_trait;
use juniper::ID;
use tabby_db::DbConn;
Expand Down Expand Up @@ -46,6 +47,14 @@ impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl {
Ok(provider.secret)
}

async fn read_github_repository_provider_access_token(&self, id: ID) -> Result<String> {
let provider = self.db.get_github_provider(id.as_rowid()?).await?;
let Some(access_token) = provider.access_token else {
return Err(anyhow!("Provider has no access token").into());

Check warning on line 53 in ee/tabby-webserver/src/service/github_repository_provider.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/github_repository_provider.rs#L50-L53

Added lines #L50 - L53 were not covered by tests
};
Ok(access_token)
}

Check warning on line 56 in ee/tabby-webserver/src/service/github_repository_provider.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/github_repository_provider.rs#L55-L56

Added lines #L55 - L56 were not covered by tests

async fn update_github_repository_provider_access_token(
&self,
id: ID,
Expand Down Expand Up @@ -77,16 +86,20 @@ impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl {

async fn list_github_provided_repositories_by_provider(
&self,
provider: ID,
providers: Vec<ID>,
after: Option<String>,
before: Option<String>,
first: Option<usize>,
last: Option<usize>,
) -> Result<Vec<GithubProvidedRepository>> {
let providers = providers
.into_iter()
.map(|i| i.as_rowid())
.collect::<Result<Vec<_>, _>>()?;
let (limit, skip_id, backwards) = graphql_pagination_to_filter(after, before, last, first)?;
let repos = self
.db
.list_github_provided_repositories(provider.as_rowid()?, limit, skip_id, backwards)
.list_github_provided_repositories(providers, limit, skip_id, backwards)
.await?;

Check warning on line 103 in ee/tabby-webserver/src/service/github_repository_provider.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/service/github_repository_provider.rs#L87-L103

Added lines #L87 - L103 were not covered by tests

Ok(repos
Expand Down

0 comments on commit 9a9b631

Please sign in to comment.