Skip to content

Commit

Permalink
fix(db): set error to null when integration is updated (#2345)
Browse files Browse the repository at this point in the history
* fix(db): set error to null when integration is updated

* Sync provider when credentials are updated

* Add unit test

* Also test sync event

* Document test

* Apply suggestions

* Sync if api_base changes

* Document test

* Fix

* Update test to check unchanged access token
  • Loading branch information
boxbeam authored Jun 5, 2024
1 parent a9c347f commit f573527
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/tabby-common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl Config {
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct RepositoryConfig {
pub git_url: String,
}
Expand Down
2 changes: 1 addition & 1 deletion ee/tabby-db/src/integrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl DbConn {
};

let res = query!(
"UPDATE integrations SET display_name = ?, access_token = ?, api_base = ?, updated_at = DATETIME('now'), synced = false WHERE id = ? AND kind = ?;",
"UPDATE integrations SET display_name = $1, access_token = $2, api_base = $3, updated_at = DATETIME('now'), synced = false, error = IIF(access_token != $2 OR api_base != $3, NULL, error) WHERE id = $4 AND kind = $5;",
display_name,
access_token,
api_base,
Expand Down
1 change: 1 addition & 0 deletions ee/tabby-webserver/src/service/background_job/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use self::{
db::DbMaintainanceJob, scheduler::SchedulerJob, third_party_integration::SyncIntegrationJob,
};

#[derive(PartialEq, Debug)]
pub enum BackgroundJobEvent {
Scheduler(RepositoryConfig),
SyncThirdPartyRepositories(ID),
Expand Down
98 changes: 98 additions & 0 deletions ee/tabby-webserver/src/service/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ impl IntegrationService for IntegrationServiceImpl {
access_token: Option<String>,
api_base: Option<String>,
) -> Result<()> {
let integration = self.get_integration(id.clone()).await?;
let access_token_is_changed = access_token
.as_ref()
.is_some_and(|token| token != &integration.access_token);
let api_base_is_changed = integration.api_base != api_base;

self.db
.update_integration(
id.as_rowid()?,
Expand All @@ -71,6 +77,13 @@ impl IntegrationService for IntegrationServiceImpl {
api_base,
)
.await?;

if access_token_is_changed || api_base_is_changed {
let _ = self
.background_job
.send(BackgroundJobEvent::SyncThirdPartyRepositories(id.clone()));
}

Ok(())
}

Expand Down Expand Up @@ -201,4 +214,89 @@ mod tests {
.len()
);
}

#[tokio::test]
async fn test_update_integration_should_reset_status() {
let (background, mut recv) = tokio::sync::mpsc::unbounded_channel();
let db = DbConn::new_in_memory().await.unwrap();
let integration = Arc::new(create(db, background));

let id = integration
.create_integration(IntegrationKind::Github, "gh".into(), "token".into(), None)
.await
.unwrap();

// Test event is sent to re-sync provider after provider is created
let event = recv.recv().await.unwrap();
assert_eq!(
event,
BackgroundJobEvent::SyncThirdPartyRepositories(id.clone())
);

// Test integration status is failed after updating sync status with an error
integration
.update_integration_sync_status(id.clone(), Some("error".into()))
.await
.unwrap();

let provider = integration.get_integration(id.clone()).await.unwrap();

assert_eq!(provider.status, IntegrationStatus::Failed);

// Test integration status is not changed if token has not been updated
integration
.update_integration(
id.clone(),
IntegrationKind::Github,
"gh".into(),
Some("token".into()),
None,
)
.await
.unwrap();

let provider = integration.get_integration(id.clone()).await.unwrap();

assert_eq!(provider.status, IntegrationStatus::Failed);

// Test integration status is pending after updating token
integration
.update_integration(
id.clone(),
IntegrationKind::Github,
"gh".into(),
Some("token2".into()),
None,
)
.await
.unwrap();

let provider = integration.get_integration(id.clone()).await.unwrap();

assert_eq!(provider.status, IntegrationStatus::Pending);

// Test event is sent to re-sync provider after credentials are updated
let event = recv.recv().await.unwrap();
assert_eq!(
event,
BackgroundJobEvent::SyncThirdPartyRepositories(id.clone())
);

// Test sync event is not sent if no fields are updated
integration
.update_integration(
id.clone(),
IntegrationKind::Github,
"gh".into(),
Some("token2".into()),
None,
)
.await
.unwrap();

assert_eq!(
recv.try_recv(),
Err(tokio::sync::mpsc::error::TryRecvError::Empty)
);
}
}

0 comments on commit f573527

Please sign in to comment.