Skip to content

Commit

Permalink
feat(webserver, db): track github/gitlab provider status with synced_at
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbeam committed Apr 26, 2024
1 parent 4ab1959 commit 1d53952
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 74 deletions.
4 changes: 1 addition & 3 deletions ee/tabby-db/migrations/0022_github-provider.up.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
CREATE TABLE github_repository_provider(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
display_name TEXT NOT NULL,
application_id TEXT NOT NULL,
secret TEXT NOT NULL,
access_token TEXT,
CONSTRAINT `idx_application_id` UNIQUE (`application_id`)
synced_at TIMESTAMP
);
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
CREATE TABLE gitlab_repository_provider(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
display_name TEXT NOT NULL,
access_token TEXT
access_token TEXT,
synced_at TIMESTAMP
);

CREATE TABLE gitlab_provided_repositories(
Expand Down
Empty file.

This file was deleted.

Binary file modified ee/tabby-db/schema.sqlite
Binary file not shown.
17 changes: 15 additions & 2 deletions ee/tabby-db/src/github_repository_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct GithubRepositoryProviderDAO {
pub id: i64,
pub display_name: String,
pub access_token: Option<String>,
pub synced_at: Option<DateTimeUtc>,
}

#[derive(FromRow)]
Expand Down Expand Up @@ -36,7 +37,7 @@ impl DbConn {
pub async fn get_github_provider(&self, id: i64) -> Result<GithubRepositoryProviderDAO> {
let provider = query_as!(
GithubRepositoryProviderDAO,
"SELECT id, display_name, access_token FROM github_repository_provider WHERE id = ?;",
r#"SELECT id, display_name, access_token, synced_at AS "synced_at: DateTimeUtc" FROM github_repository_provider WHERE id = ?;"#,
id
)
.fetch_one(&self.pool)
Expand Down Expand Up @@ -95,6 +96,18 @@ impl DbConn {
Ok(())
}

pub async fn update_github_provider_synced_at(&self, id: i64, success: bool) -> Result<()> {
let time = success.then_some(DateTimeUtc::now());
query!(
"UPDATE github_repository_provider SET synced_at = ? WHERE id = ?",
time,
id
)
.execute(&self.pool)
.await?;
Ok(())
}

Check warning on line 109 in ee/tabby-db/src/github_repository_provider.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/github_repository_provider.rs#L99-L109

Added lines #L99 - L109 were not covered by tests

pub async fn list_github_repository_providers(
&self,
ids: Vec<i64>,
Expand All @@ -113,7 +126,7 @@ impl DbConn {
let providers = query_paged_as!(
GithubRepositoryProviderDAO,
"github_repository_provider",
["id", "display_name", "access_token"],
["id", "display_name", "access_token", "synced_at" as "synced_at: DateTimeUtc"],

Check warning on line 129 in ee/tabby-db/src/github_repository_provider.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/github_repository_provider.rs#L129

Added line #L129 was not covered by tests
limit,
skip_id,
backwards,
Expand Down
17 changes: 15 additions & 2 deletions ee/tabby-db/src/gitlab_repository_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct GitlabRepositoryProviderDAO {
pub id: i64,
pub display_name: String,
pub access_token: Option<String>,
pub synced_at: Option<DateTimeUtc>,
}

#[derive(FromRow)]
Expand Down Expand Up @@ -36,7 +37,7 @@ impl DbConn {
pub async fn get_gitlab_provider(&self, id: i64) -> Result<GitlabRepositoryProviderDAO> {
let provider = query_as!(
GitlabRepositoryProviderDAO,
"SELECT id, display_name, access_token FROM gitlab_repository_provider WHERE id = ?;",
r#"SELECT id, display_name, access_token, synced_at AS "synced_at: DateTimeUtc" FROM gitlab_repository_provider WHERE id = ?;"#,
id
)
.fetch_one(&self.pool)
Expand Down Expand Up @@ -95,6 +96,18 @@ impl DbConn {
Ok(())
}

pub async fn update_gitlab_provider_synced_at(&self, id: i64, success: bool) -> Result<()> {
let time = success.then_some(DateTimeUtc::now());
query!(
"UPDATE gitlab_repository_provider SET synced_at = ? WHERE id = ?",
time,
id
)
.execute(&self.pool)
.await?;
Ok(())
}

Check warning on line 109 in ee/tabby-db/src/gitlab_repository_provider.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/gitlab_repository_provider.rs#L99-L109

Added lines #L99 - L109 were not covered by tests

pub async fn list_gitlab_repository_providers(
&self,
ids: Vec<i64>,
Expand All @@ -113,7 +126,7 @@ impl DbConn {
let providers = query_paged_as!(
GitlabRepositoryProviderDAO,
"gitlab_repository_provider",
["id", "display_name", "access_token"],
["id", "display_name", "access_token", "synced_at" as "synced_at: DateTimeUtc"],

Check warning on line 129 in ee/tabby-db/src/gitlab_repository_provider.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-db/src/gitlab_repository_provider.rs#L129

Added line #L129 was not covered by tests
limit,
skip_id,
backwards,
Expand Down
5 changes: 4 additions & 1 deletion ee/tabby-webserver/src/cron/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async fn refresh_repositories_for_provider(
..
}) if source.status_code.is_client_error() => {
service
.reset_github_repository_provider_access_token(provider.id.clone())
.update_github_repository_provider_sync_status(provider.id.clone(), false)

Check warning on line 41 in ee/tabby-webserver/src/cron/github.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/github.rs#L41

Added line #L41 was not covered by tests
.await?;
warn!(
"GitHub credentials for provider {} are expired or invalid",
Expand Down Expand Up @@ -66,6 +66,9 @@ async fn refresh_repositories_for_provider(
.upsert_github_provided_repository(provider.id.clone(), id, repo.name, url)
.await?;
}
service
.update_github_repository_provider_sync_status(provider.id.clone(), true)
.await?;

Check warning on line 71 in ee/tabby-webserver/src/cron/github.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/github.rs#L69-L71

Added lines #L69 - L71 were not covered by tests

Ok(())
}
Expand Down
5 changes: 4 additions & 1 deletion ee/tabby-webserver/src/cron/gitlab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async fn refresh_repositories_for_provider(
Ok(repos) => repos,
Err(e) if e.to_string().contains("401 Unauthorized") => {
service
.reset_gitlab_repository_provider_access_token(provider.id.clone())
.update_gitlab_repository_provider_sync_status(provider.id.clone(), false)

Check warning on line 42 in ee/tabby-webserver/src/cron/gitlab.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/gitlab.rs#L42

Added line #L42 was not covered by tests
.await?;
warn!(
"GitLab credentials for provider {} are expired or invalid",
Expand All @@ -64,6 +64,9 @@ async fn refresh_repositories_for_provider(
)
.await?;
}
service
.update_gitlab_repository_provider_sync_status(provider.id.clone(), true)
.await?;

Check warning on line 69 in ee/tabby-webserver/src/cron/gitlab.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/cron/gitlab.rs#L67-L69

Added lines #L67 - L69 were not covered by tests

Ok(())
}
Expand Down
10 changes: 7 additions & 3 deletions ee/tabby-webserver/src/schema/github_repository_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use async_trait::async_trait;
use chrono::{DateTime, Utc};
use juniper::{GraphQLObject, ID};

use super::{repository::RepositoryProvider, Context};
use super::{repository::RepositoryProvider, types::RepositoryProviderStatus, Context};
use crate::{juniper::relay::NodeType, schema::Result};

#[derive(GraphQLObject, Debug, PartialEq)]
Expand All @@ -11,7 +11,7 @@ pub struct GithubRepositoryProvider {
pub id: ID,
pub display_name: String,

pub connected: bool,
pub status: RepositoryProviderStatus,

#[graphql(skip)]
pub access_token: Option<String>,
Expand Down Expand Up @@ -75,7 +75,11 @@ pub trait GithubRepositoryProviderService: Send + Sync + RepositoryProvider {
display_name: String,
access_token: String,
) -> Result<()>;
async fn reset_github_repository_provider_access_token(&self, id: ID) -> Result<()>;
async fn update_github_repository_provider_sync_status(
&self,
id: ID,
success: bool,
) -> Result<()>;

async fn list_github_repository_providers(
&self,
Expand Down
10 changes: 7 additions & 3 deletions ee/tabby-webserver/src/schema/gitlab_repository_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use async_trait::async_trait;
use chrono::{DateTime, Utc};
use juniper::{GraphQLObject, ID};

use super::{repository::RepositoryProvider, Context};
use super::{repository::RepositoryProvider, types::RepositoryProviderStatus, Context};
use crate::{juniper::relay::NodeType, schema::Result};

#[derive(GraphQLObject, Debug, PartialEq)]
Expand All @@ -11,7 +11,7 @@ pub struct GitlabRepositoryProvider {
pub id: ID,
pub display_name: String,

pub connected: bool,
pub status: RepositoryProviderStatus,

#[graphql(skip)]
pub access_token: Option<String>,
Expand Down Expand Up @@ -75,7 +75,11 @@ pub trait GitlabRepositoryProviderService: Send + Sync + RepositoryProvider {
display_name: String,
access_token: String,
) -> Result<()>;
async fn reset_gitlab_repository_provider_access_token(&self, id: ID) -> Result<()>;
async fn update_gitlab_repository_provider_sync_status(
&self,
id: ID,
success: bool,
) -> Result<()>;

async fn list_gitlab_repository_providers(
&self,
Expand Down
19 changes: 18 additions & 1 deletion ee/tabby-webserver/src/schema/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use juniper::{GraphQLInputObject, ID};
use juniper::{GraphQLEnum, GraphQLInputObject, ID};
use validator::Validate;

#[derive(GraphQLInputObject, Validate)]
Expand All @@ -23,3 +23,20 @@ pub struct UpdateRepositoryProviderInput {
#[validate(length(code = "access_token", min = 10))]
pub access_token: String,
}

#[derive(GraphQLEnum, Debug, PartialEq)]

Check warning on line 27 in ee/tabby-webserver/src/schema/types.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/types.rs#L27

Added line #L27 was not covered by tests
pub enum RepositoryProviderStatus {
Ready,
Pending,
Error,
}

impl RepositoryProviderStatus {
pub fn get_status(access_token_set: bool, synced_at_set: bool) -> Self {
match (access_token_set, synced_at_set) {
(true, true) => RepositoryProviderStatus::Ready,

Check warning on line 37 in ee/tabby-webserver/src/schema/types.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/types.rs#L37

Added line #L37 was not covered by tests
(true, false) => RepositoryProviderStatus::Pending,
_ => RepositoryProviderStatus::Error,

Check warning on line 39 in ee/tabby-webserver/src/schema/types.rs

View check run for this annotation

Codecov / codecov/patch

ee/tabby-webserver/src/schema/types.rs#L39

Added line #L39 was not covered by tests
}
}
}
11 changes: 9 additions & 2 deletions ee/tabby-webserver/src/service/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
gitlab_repository_provider::{GitlabProvidedRepository, GitlabRepositoryProvider},
job,
setting::{NetworkSetting, SecuritySetting},
types::RepositoryProviderStatus,
user_event::{EventKind, UserEvent},
CoreError,
},
Expand Down Expand Up @@ -130,7 +131,10 @@ impl From<GithubRepositoryProviderDAO> for GithubRepositoryProvider {
Self {
display_name: value.display_name,
id: value.id.as_id(),
connected: value.access_token.is_some(),
status: RepositoryProviderStatus::get_status(
value.access_token.is_some(),
value.synced_at.is_some(),
),
access_token: value.access_token,
}
}
Expand All @@ -154,7 +158,10 @@ impl From<GitlabRepositoryProviderDAO> for GitlabRepositoryProvider {
Self {
display_name: value.display_name,
id: value.id.as_id(),
connected: value.access_token.is_some(),
status: RepositoryProviderStatus::get_status(
value.access_token.is_some(),
value.synced_at.is_some(),
),
access_token: value.access_token,
}
}
Expand Down
41 changes: 17 additions & 24 deletions ee/tabby-webserver/src/service/github_repository_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,6 @@ impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl {
Ok(())
}

async fn reset_github_repository_provider_access_token(&self, id: ID) -> Result<()> {
self.db
.reset_github_provider_access_token(id.as_rowid()?)
.await?;
Ok(())
}

async fn list_github_repository_providers(
&self,
ids: Vec<ID>,
Expand Down Expand Up @@ -183,6 +176,21 @@ impl GithubRepositoryProviderService for GithubRepositoryProviderServiceImpl {
.await?;
Ok(())
}

async fn update_github_repository_provider_sync_status(
&self,
id: ID,
success: bool,
) -> Result<()> {
let id = id.as_rowid()?;
if !success {
self.db.reset_github_provider_access_token(id).await?;
}
self.db
.update_github_provider_synced_at(id, success)
.await?;
Ok(())
}

Check warning on line 193 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#L180-L193

Added lines #L180 - L193 were not covered by tests
}

#[async_trait]
Expand Down Expand Up @@ -217,7 +225,7 @@ mod tests {
use chrono::Duration;

use super::*;
use crate::service::AsID;
use crate::{schema::types::RepositoryProviderStatus, service::AsID};

#[tokio::test]
async fn test_github_provided_repositories() {
Expand Down Expand Up @@ -318,7 +326,7 @@ mod tests {
id: id.clone(),
display_name: "id".into(),
access_token: Some("secret".into()),
connected: true,
status: RepositoryProviderStatus::Pending,
}
);

Expand All @@ -330,21 +338,6 @@ mod tests {
assert_eq!(providers.len(), 1);
assert_eq!(providers[0].access_token, Some("secret".into()));

// Test resetgithub provider tokens
service
.reset_github_repository_provider_access_token(id.clone())
.await
.unwrap();

assert_eq!(
service
.get_github_repository_provider(id.clone())
.await
.unwrap()
.access_token,
None
);

// Test deleting github provider
service
.delete_github_repository_provider(id.clone())
Expand Down
Loading

0 comments on commit 1d53952

Please sign in to comment.