Skip to content

Commit

Permalink
feat(webserver): support filtering github/gitlab repos by active stat…
Browse files Browse the repository at this point in the history
…us (#2006)

* feat(webserver): support filtering github/gitlab repos by active status

* Fix inconsistency

* [autofix.ci] apply automated fixes

* Filter in repository_list

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
boxbeam and autofix-ci[bot] authored Apr 29, 2024
1 parent c16d393 commit b1184a4
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 32 deletions.
15 changes: 13 additions & 2 deletions ee/tabby-db/src/github_repository_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,27 @@ impl DbConn {
pub async fn list_github_provided_repositories(
&self,
provider_ids: Vec<i64>,
active: Option<bool>,
limit: Option<usize>,
skip_id: Option<i32>,
backwards: bool,
) -> Result<Vec<GithubProvidedRepositoryDAO>> {
let mut conditions = vec![];

let provider_ids = provider_ids
.into_iter()
.map(|id| id.to_string())
.collect::<Vec<_>>()
.join(", ");
if !provider_ids.is_empty() {
conditions.push(format!("github_repository_provider_id IN ({provider_ids})"));
}

let active_filter = active.map(|active| format!("active = {active}"));
conditions.extend(active_filter);

let condition = (!conditions.is_empty()).then(|| conditions.join(" AND "));

let repos = query_paged_as!(
GithubProvidedRepositoryDAO,
"github_provided_repositories",
Expand All @@ -203,8 +215,7 @@ impl DbConn {
limit,
skip_id,
backwards,
(!provider_ids.is_empty())
.then(|| format!("github_repository_provider_id IN ({provider_ids})"))
condition
)
.fetch_all(&self.pool)
.await?;
Expand Down
15 changes: 13 additions & 2 deletions ee/tabby-db/src/gitlab_repository_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,27 @@ impl DbConn {
pub async fn list_gitlab_provided_repositories(
&self,
provider_ids: Vec<i64>,
active: Option<bool>,
limit: Option<usize>,
skip_id: Option<i32>,
backwards: bool,
) -> Result<Vec<GitlabProvidedRepositoryDAO>> {
let mut conditions = vec![];

let provider_ids = provider_ids
.into_iter()
.map(|id| id.to_string())
.collect::<Vec<_>>()
.join(", ");
if !provider_ids.is_empty() {
conditions.push(format!("gitlab_repository_provider_id IN ({provider_ids})"));
}

let active_filter = active.map(|active| format!("active = {active}"));
conditions.extend(active_filter);

let condition = (!conditions.is_empty()).then(|| conditions.join(" AND "));

let repos = query_paged_as!(
GitlabProvidedRepositoryDAO,
"gitlab_provided_repositories",
Expand All @@ -203,8 +215,7 @@ impl DbConn {
limit,
skip_id,
backwards,
(!provider_ids.is_empty())
.then(|| format!("gitlab_repository_provider_id IN ({provider_ids})"))
condition
)
.fetch_all(&self.pool)
.await?;
Expand Down
4 changes: 2 additions & 2 deletions ee/tabby-webserver/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ type Query {
users(after: String, before: String, first: Int, last: Int): UserConnection!
invitations(after: String, before: String, first: Int, last: Int): InvitationConnection!
githubRepositoryProviders(ids: [ID!], after: String, before: String, first: Int, last: Int): GithubRepositoryProviderConnection!
githubRepositories(providerIds: [ID!]!, after: String, before: String, first: Int, last: Int): GithubProvidedRepositoryConnection!
githubRepositories(providerIds: [ID!]!, active: Boolean, after: String, before: String, first: Int, last: Int): GithubProvidedRepositoryConnection!
gitlabRepositoryProviders(ids: [ID!], after: String, before: String, first: Int, last: Int): GitlabRepositoryProviderConnection!
gitlabRepositories(providerIds: [ID!]!, after: String, before: String, first: Int, last: Int): GitlabProvidedRepositoryConnection!
gitlabRepositories(providerIds: [ID!]!, active: Boolean, after: String, before: String, first: Int, last: Int): GitlabProvidedRepositoryConnection!
jobRuns(ids: [ID!], jobs: [String!], after: String, before: String, first: Int, last: Int): JobRunConnection!
jobRunStats(jobs: [String!]): JobStats!
emailSetting: EmailSetting
Expand Down
6 changes: 4 additions & 2 deletions ee/tabby-webserver/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ impl Query {
async fn github_repositories(
ctx: &Context,
provider_ids: Vec<ID>,
active: Option<bool>,
after: Option<String>,
before: Option<String>,
first: Option<i32>,
Expand All @@ -258,7 +259,7 @@ impl Query {
ctx.locator
.repository()
.github()
.list_repositories(provider_ids, after, before, first, last)
.list_repositories(provider_ids, active, after, before, first, last)
.await
},
)
Expand Down Expand Up @@ -293,6 +294,7 @@ impl Query {
async fn gitlab_repositories(
ctx: &Context,
provider_ids: Vec<ID>,
active: Option<bool>,
after: Option<String>,
before: Option<String>,
first: Option<i32>,
Expand All @@ -308,7 +310,7 @@ impl Query {
ctx.locator
.repository()
.gitlab()
.list_repositories(provider_ids, after, before, first, last)
.list_repositories(provider_ids, active, after, before, first, last)
.await
},
)
Expand Down
1 change: 1 addition & 0 deletions ee/tabby-webserver/src/schema/repository/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub trait GithubRepositoryService: Send + Sync + RepositoryProvider {
async fn list_repositories(
&self,
provider: Vec<ID>,
active: Option<bool>,
after: Option<String>,
before: Option<String>,
first: Option<usize>,
Expand Down
1 change: 1 addition & 0 deletions ee/tabby-webserver/src/schema/repository/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ pub trait GitlabRepositoryService: Send + Sync + RepositoryProvider {
async fn list_repositories(
&self,
provider: Vec<ID>,
active: Option<bool>,
after: Option<String>,
before: Option<String>,
first: Option<usize>,
Expand Down
36 changes: 24 additions & 12 deletions ee/tabby-webserver/src/service/repository/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl GithubRepositoryService for GithubRepositoryProviderServiceImpl {
async fn list_repositories(
&self,
providers: Vec<ID>,
active: Option<bool>,
after: Option<String>,
before: Option<String>,
first: Option<usize>,
Expand All @@ -85,7 +86,7 @@ impl GithubRepositoryService for GithubRepositoryProviderServiceImpl {
let (limit, skip_id, backwards) = graphql_pagination_to_filter(after, before, first, last)?;
let repos = self
.db
.list_github_provided_repositories(providers, limit, skip_id, backwards)
.list_github_provided_repositories(providers, active, limit, skip_id, backwards)
.await?;

Ok(repos
Expand Down Expand Up @@ -140,17 +141,14 @@ impl GithubRepositoryService for GithubRepositoryProviderServiceImpl {
.collect();

let mut repos = self
.list_repositories(vec![], None, None, None, None)
.list_repositories(vec![], Some(true), None, None, None, None)
.await?;

deduplicate_github_repositories(&mut repos);

let urls = repos
.into_iter()
.filter_map(|repo| {
if !repo.active {
return None;
}
let mut url = Url::parse(&repo.git_url).ok()?;
url.set_username(tokens.get(&repo.github_repository_provider_id.to_string())?)
.ok()?;
Expand Down Expand Up @@ -184,10 +182,9 @@ impl GithubRepositoryService for GithubRepositoryProviderServiceImpl {
impl RepositoryProvider for GithubRepositoryProviderServiceImpl {
async fn repository_list(&self) -> Result<Vec<Repository>> {
Ok(self
.list_repositories(vec![], None, None, None, None)
.list_repositories(vec![], Some(true), None, None, None, None)
.await?
.into_iter()
.filter(|x| x.active)
.map(|x| x.into())
.collect())
}
Expand Down Expand Up @@ -251,7 +248,7 @@ mod tests {

// Test listing with no filter on providers
let repos = service
.list_repositories(vec![], None, None, None, None)
.list_repositories(vec![], None, None, None, None, None)
.await
.unwrap();

Expand All @@ -261,13 +258,28 @@ mod tests {

// Test listing with a filter on providers
let repos = service
.list_repositories(vec![provider_id1.as_id()], None, None, None, None)
.list_repositories(vec![provider_id1.as_id()], None, None, None, None, None)
.await
.unwrap();

assert_eq!(repos.len(), 1);
assert_eq!(repos[0].name, "test_repo1");

// Test listing with a filter on active status
let repos = service
.list_repositories(vec![], Some(true), None, None, None, None)
.await
.unwrap();

assert_eq!(0, repos.len());

let repos = service
.list_repositories(vec![], Some(false), None, None, None, None)
.await
.unwrap();

assert_eq!(2, repos.len());

// Test deletion and toggling active status
db.delete_github_provided_repository(repo_id1)
.await
Expand All @@ -278,7 +290,7 @@ mod tests {
.unwrap();

let repos = service
.list_repositories(vec![], None, None, None, None)
.list_repositories(vec![], None, None, None, None, None)
.await
.unwrap();

Expand Down Expand Up @@ -420,7 +432,7 @@ mod tests {
assert_eq!(
1,
service
.list_repositories(vec![], None, None, None, None)
.list_repositories(vec![], None, None, None, None, None)
.await
.unwrap()
.len()
Expand All @@ -436,7 +448,7 @@ mod tests {
assert_eq!(
0,
service
.list_repositories(vec![], None, None, None, None)
.list_repositories(vec![], None, None, None, None, None)
.await
.unwrap()
.len()
Expand Down
36 changes: 24 additions & 12 deletions ee/tabby-webserver/src/service/repository/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ impl GitlabRepositoryService for GitlabRepositoryProviderServiceImpl {
async fn list_repositories(
&self,
providers: Vec<ID>,
active: Option<bool>,
after: Option<String>,
before: Option<String>,
first: Option<usize>,
Expand All @@ -85,7 +86,7 @@ impl GitlabRepositoryService for GitlabRepositoryProviderServiceImpl {
let (limit, skip_id, backwards) = graphql_pagination_to_filter(after, before, first, last)?;
let repos = self
.db
.list_gitlab_provided_repositories(providers, limit, skip_id, backwards)
.list_gitlab_provided_repositories(providers, active, limit, skip_id, backwards)
.await?;

Ok(repos
Expand Down Expand Up @@ -140,17 +141,14 @@ impl GitlabRepositoryService for GitlabRepositoryProviderServiceImpl {
.collect();

let mut repos = self
.list_repositories(vec![], None, None, None, None)
.list_repositories(vec![], Some(true), None, None, None, None)
.await?;

deduplicate_gitlab_repositories(&mut repos);

let urls = repos
.into_iter()
.filter_map(|repo| {
if !repo.active {
return None;
}
let mut url = Url::parse(&repo.git_url).ok()?;
url.set_username("oauth2").ok()?;
url.set_password(Some(
Expand Down Expand Up @@ -192,10 +190,9 @@ fn deduplicate_gitlab_repositories(repositories: &mut Vec<GitlabProvidedReposito
impl RepositoryProvider for GitlabRepositoryProviderServiceImpl {
async fn repository_list(&self) -> Result<Vec<Repository>> {
Ok(self
.list_repositories(vec![], None, None, None, None)
.list_repositories(vec![], Some(true), None, None, None, None)
.await?
.into_iter()
.filter(|x| x.active)
.map(|x| x.into())
.collect())
}
Expand Down Expand Up @@ -254,7 +251,7 @@ mod tests {

// Test listing with no filter on providers
let repos = service
.list_repositories(vec![], None, None, None, None)
.list_repositories(vec![], None, None, None, None, None)
.await
.unwrap();

Expand All @@ -264,13 +261,28 @@ mod tests {

// Test listing with a filter on providers
let repos = service
.list_repositories(vec![provider_id1.as_id()], None, None, None, None)
.list_repositories(vec![provider_id1.as_id()], None, None, None, None, None)
.await
.unwrap();

assert_eq!(repos.len(), 1);
assert_eq!(repos[0].name, "test_repo1");

// Test listing with a filter on active status
let repos = service
.list_repositories(vec![], Some(true), None, None, None, None)
.await
.unwrap();

assert_eq!(0, repos.len());

let repos = service
.list_repositories(vec![], Some(false), None, None, None, None)
.await
.unwrap();

assert_eq!(2, repos.len());

// Test deletion and toggling active status
db.delete_gitlab_provided_repository(repo_id1)
.await
Expand All @@ -281,7 +293,7 @@ mod tests {
.unwrap();

let repos = service
.list_repositories(vec![], None, None, None, None)
.list_repositories(vec![], None, None, None, None, None)
.await
.unwrap();

Expand Down Expand Up @@ -423,7 +435,7 @@ mod tests {
assert_eq!(
1,
service
.list_repositories(vec![], None, None, None, None)
.list_repositories(vec![], None, None, None, None, None)
.await
.unwrap()
.len()
Expand All @@ -439,7 +451,7 @@ mod tests {
assert_eq!(
0,
service
.list_repositories(vec![], None, None, None, None)
.list_repositories(vec![], None, None, None, None, None)
.await
.unwrap()
.len()
Expand Down

0 comments on commit b1184a4

Please sign in to comment.