Skip to content

Commit

Permalink
fix(background_job): ensure repositories from config.toml got synced …
Browse files Browse the repository at this point in the history
…in background job
  • Loading branch information
wsxiaoys committed Jan 15, 2025
1 parent 5aa27b5 commit 3eda8d7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
18 changes: 17 additions & 1 deletion ee/tabby-webserver/src/service/background_job/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ use std::sync::Arc;

use anyhow::Context;
use chrono::{DateTime, Utc};
use futures::SinkExt;
use octocrab::models::repos;
use serde::{Deserialize, Serialize};
use tabby_common::config::CodeRepository;
use tabby_common::config::{config_index_to_id, CodeRepository, Config};
use tabby_index::public::CodeIndexer;
use tabby_inference::Embedding;
use tabby_schema::{job::JobService, repository::GitRepositoryService};
use tracing::debug;

use super::{helper::Job, BackgroundJobEvent};

Expand Down Expand Up @@ -42,17 +45,30 @@ impl SchedulerGitJob {
git_repository: Arc<dyn GitRepositoryService>,
job: Arc<dyn JobService>,
) -> tabby_schema::Result<()> {
// Read git repositories from config file.
let config_repositories = Config::load()
.map(|config| config.repositories)
.unwrap_or_default()
.into_iter()
.enumerate()
.map(|(index, x)| CodeRepository::new(x.git_url(), &config_index_to_id(index)))
.collect::<Vec<_>>();

// Read git repositories from database.
let repositories = git_repository
.repository_list()
.await
.context("Must be able to retrieve repositories for sync")?;

// Combine git repositories from config file and database.
let repositories: Vec<_> = repositories
.into_iter()
.map(|repo| CodeRepository::new(&repo.git_url, &repo.source_id))
.chain(config_repositories.into_iter())
.collect();

for repository in repositories {
debug!("Scheduling git repository sync for {:?}", repository);
let _ = job
.trigger(BackgroundJobEvent::SchedulerGitRepository(repository).to_command())
.await;
Expand Down
14 changes: 8 additions & 6 deletions ee/tabby-webserver/src/service/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ impl RepositoryService for RepositoryServiceImpl {
let mut all = vec![];
all.extend(self.git().repository_list().await?);
all.extend(self.third_party().repository_list().await?);

all.extend(
self.config
.iter()
.enumerate()
.map(|(index, repo)| repository_config_to_repository(index, repo))
.collect::<Result<Vec<_>>>()?,
.collect::<Vec<_>>(),
);

if let Some(policy) = policy {
Expand All @@ -106,7 +107,7 @@ impl RepositoryService for RepositoryServiceImpl {
RepositoryKind::GitConfig => {
let index = config_id_to_index(id)?;
let config = &self.config[index];
return repository_config_to_repository(index, config);
return Ok(repository_config_to_repository(index, config));
}
RepositoryKind::Git => self.git().get_repository(id).await,
RepositoryKind::Github
Expand Down Expand Up @@ -238,23 +239,24 @@ fn to_repository(kind: RepositoryKind, repo: ProvidedRepository) -> Repository {
}
}

fn repository_config_to_repository(index: usize, config: &RepositoryConfig) -> Result<Repository> {
fn repository_config_to_repository(index: usize, config: &RepositoryConfig) -> Repository {
let source_id = config_index_to_id(index);
Ok(Repository {
Repository {
id: ID::new(source_id.clone()),
source_id,
name: config.display_name(),
kind: RepositoryKind::GitConfig,
dir: config.dir(),
refs: tabby_git::list_refs(&config.dir())?
refs: tabby_git::list_refs(&config.dir())
.unwrap_or_default()
.into_iter()
.map(|r| GitReference {
name: r.name,
commit: r.commit,
})
.collect(),
git_url: config.git_url().to_owned(),
})
}
}

#[cfg(test)]
Expand Down

0 comments on commit 3eda8d7

Please sign in to comment.