Skip to content

Commit

Permalink
refactor(webserver): simplify implementation of github provider using…
Browse files Browse the repository at this point in the history
… PAT (#1961)

* refactor(webserver): simplify implementation of github provider using PAT

* update

* update db schema

* revert schema

* update

* update

* update

* revert

* update

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
wsxiaoys and autofix-ci[bot] authored Apr 25, 2024
1 parent 2bc855c commit 0420bde
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 387 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DROP TABLE github_repository_provider;

CREATE TABLE github_repository_provider(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
display_name TEXT NOT NULL,
access_token TEXT
);
Binary file modified ee/tabby-db/schema.sqlite
Binary file not shown.
13 changes: 5 additions & 8 deletions ee/tabby-db/schema/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,6 @@ CREATE INDEX user_completions_completion_id_idx ON user_completions(
);
CREATE INDEX idx_job_created_at ON job_runs(job, created_at);
CREATE INDEX idx_repository_name ON repositories(name);
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`)
);
CREATE INDEX idx_user_completion_user_id_created_at_language ON user_completions(
user_id,
created_at,
Expand Down Expand Up @@ -152,3 +144,8 @@ CREATE TABLE password_reset(
created_at TIMESTAMP NOT NULL DEFAULT(DATETIME('now')),
FOREIGN KEY(user_id) REFERENCES users(id)
);
CREATE TABLE github_repository_provider(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
display_name TEXT NOT NULL,
access_token TEXT
);
32 changes: 12 additions & 20 deletions ee/tabby-db/schema/schema.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 19 additions & 41 deletions ee/tabby-db/src/github_repository_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ use anyhow::{anyhow, Result};
use sqlx::{prelude::FromRow, query, query_as};
use tabby_db_macros::query_paged_as;

use crate::{DateTimeUtc, DbConn, SQLXResultExt};
use crate::{DateTimeUtc, DbConn};

#[derive(FromRow)]
pub struct GithubRepositoryProviderDAO {
pub id: i64,
pub display_name: String,
pub application_id: String,
pub secret: String,
pub access_token: Option<String>,
}

Expand All @@ -24,24 +22,21 @@ pub struct GithubProvidedRepositoryDAO {
}

impl DbConn {
pub async fn create_github_provider(
&self,
name: String,
application_id: String,
secret: String,
) -> Result<i64> {
let res = query!("INSERT INTO github_repository_provider (display_name, application_id, secret) VALUES ($1, $2, $3);",
pub async fn create_github_provider(&self, name: String, access_token: String) -> Result<i64> {
let res = query!(
"INSERT INTO github_repository_provider (display_name, access_token) VALUES ($1, $2);",
name,
application_id,
secret
).execute(&self.pool).await.unique_error("GitHub Application ID already exists")?;
access_token
)
.execute(&self.pool)
.await?;
Ok(res.last_insert_rowid())
}

pub async fn get_github_provider(&self, id: i64) -> Result<GithubRepositoryProviderDAO> {
let provider = query_as!(
GithubRepositoryProviderDAO,
"SELECT id, display_name, application_id, secret, access_token FROM github_repository_provider WHERE id = ?;",
"SELECT id, display_name, access_token FROM github_repository_provider WHERE id = ?;",
id
)
.fetch_one(&self.pool)
Expand All @@ -59,14 +54,9 @@ impl DbConn {
Ok(())
}

pub async fn update_github_provider_access_token(
&self,
id: i64,
access_token: Option<String>,
) -> Result<()> {
pub async fn reset_github_provider_access_token(&self, id: i64) -> Result<()> {
let res = query!(
"UPDATE github_repository_provider SET access_token = ? WHERE id = ?",
access_token,
"UPDATE github_repository_provider SET access_token = NULL WHERE id = ?",
id
)
.execute(&self.pool)
Expand All @@ -85,27 +75,21 @@ impl DbConn {
&self,
id: i64,
display_name: String,
application_id: String,
secret: Option<String>,
access_token: String,
) -> Result<()> {
let secret = match secret {
Some(secret) => secret,
None => self.get_github_provider(id).await?.secret,
};

let res = query!(
"UPDATE github_repository_provider SET display_name = ?, application_id = ?, secret = ? WHERE id = ?;",
"UPDATE github_repository_provider SET display_name = ?, access_token=? WHERE id = ?;",
display_name,
application_id,
secret,
access_token,
id
)
.execute(&self.pool)
.await
.unique_error("A provider with that application ID already exists")?;
.await?;

if res.rows_affected() != 1 {
return Err(anyhow!("Provider does not exist"));
return Err(anyhow!(
"The specified Github repository provider does not exist"
));
}

Ok(())
Expand All @@ -129,13 +113,7 @@ impl DbConn {
let providers = query_paged_as!(
GithubRepositoryProviderDAO,
"github_repository_provider",
[
"id",
"display_name",
"application_id",
"secret",
"access_token"
],
["id", "display_name", "access_token"],
limit,
skip_id,
backwards,
Expand Down
7 changes: 2 additions & 5 deletions ee/tabby-webserver/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ type ServerInfo {
type GithubRepositoryProvider {
id: ID!
displayName: String!
applicationId: String!
connected: Boolean!
}

Expand Down Expand Up @@ -214,8 +213,7 @@ type RepositoryEdge {
input UpdateGithubRepositoryProviderInput {
id: ID!
displayName: String!
applicationId: String!
secret: String
accessToken: String!
}

"DateTime"
Expand Down Expand Up @@ -338,8 +336,7 @@ input PasswordResetInput {

input CreateGithubRepositoryProviderInput {
displayName: String!
applicationId: String!
secret: String!
accessToken: String!
}

type User {
Expand Down
3 changes: 2 additions & 1 deletion ee/tabby-webserver/src/cron/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{sync::Arc, time::Duration};
use anyhow::Result;
use futures::Future;
use tokio_cron_scheduler::Job;
use tracing::error;
use tracing::{debug, error};

use super::github::refresh_all_repositories;
use crate::schema::{
Expand Down Expand Up @@ -59,6 +59,7 @@ pub async fn update_integrated_github_repositories_job(
EVERY_TEN_MINUTES,
github_repository_provider,
|github_repository_provider| async move {
debug!("Syncing github repositories...");
refresh_all_repositories(github_repository_provider).await
},
)
Expand Down
2 changes: 1 addition & 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
.update_github_repository_provider_access_token(provider.id.clone(), None)
.reset_github_repository_provider_access_token(provider.id.clone())
.await?;
warn!(
"GitHub credentials for provider {} are expired or invalid",
Expand Down
6 changes: 1 addition & 5 deletions ee/tabby-webserver/src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::{
axum::{extract::AuthBearer, graphql},
cron,
hub::{self, HubState},
integrations, oauth,
oauth,
path::db_file,
repositories,
schema::{
Expand Down Expand Up @@ -121,10 +121,6 @@ impl WebserverHandle {
// FIXME(boxbeam): repositories routes should support both git / github repositories, but currently only git repositories are supported.
repositories::routes(ctx.repository().git(), ctx.auth()),
)
.nest(
"/integrations/github",
integrations::github::routes(ctx.auth(), ctx.setting(), ctx.repository().github()),
)
.route(
"/avatar/:id",
routing::get(avatar)
Expand Down
Loading

0 comments on commit 0420bde

Please sign in to comment.