mirror of
https://github.com/zed-industries/zed.git
synced 2026-06-01 03:14:56 +07:00
collab: Remove seeding infrastructure (#56562)
This PR removes the seeding infrastructure from Collab. We're already set up to just-in-time create users in local development through Cloud. Also updated the liveness probe for the health endpoint to use a different query. Closes CLO-763. Release Notes: - N/A
This commit is contained in:
parent
048831a453
commit
f1a7567791
6 changed files with 4 additions and 281 deletions
|
|
@ -13,7 +13,6 @@ BLOB_STORE_BUCKET = "the-extensions-bucket"
|
|||
BLOB_STORE_URL = "http://127.0.0.1:9000"
|
||||
BLOB_STORE_REGION = "the-region"
|
||||
ZED_CLIENT_CHECKSUM_SEED = "development-checksum-seed"
|
||||
SEED_PATH = "crates/collab/seed.default.json"
|
||||
|
||||
# RUST_LOG=info
|
||||
# LOG_JSON=true
|
||||
|
|
|
|||
|
|
@ -1,9 +1,8 @@
|
|||
use chrono::NaiveDateTime;
|
||||
|
||||
use super::*;
|
||||
|
||||
impl Database {
|
||||
/// Creates a new user.
|
||||
#[cfg(feature = "test-support")]
|
||||
pub async fn create_user(
|
||||
&self,
|
||||
email_address: &str,
|
||||
|
|
@ -38,126 +37,6 @@ impl Database {
|
|||
.await
|
||||
}
|
||||
|
||||
pub async fn update_or_create_user_by_github_account(
|
||||
&self,
|
||||
github_login: &str,
|
||||
github_user_id: i32,
|
||||
github_email: Option<&str>,
|
||||
github_name: Option<&str>,
|
||||
github_user_created_at: DateTimeUtc,
|
||||
initial_channel_id: Option<ChannelId>,
|
||||
) -> Result<user::Model> {
|
||||
self.transaction(|tx| async move {
|
||||
self.update_or_create_user_by_github_account_tx(
|
||||
github_login,
|
||||
github_user_id,
|
||||
github_email,
|
||||
github_name,
|
||||
github_user_created_at.naive_utc(),
|
||||
initial_channel_id,
|
||||
&tx,
|
||||
)
|
||||
.await
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn update_or_create_user_by_github_account_tx(
|
||||
&self,
|
||||
github_login: &str,
|
||||
github_user_id: i32,
|
||||
github_email: Option<&str>,
|
||||
github_name: Option<&str>,
|
||||
github_user_created_at: NaiveDateTime,
|
||||
initial_channel_id: Option<ChannelId>,
|
||||
tx: &DatabaseTransaction,
|
||||
) -> Result<user::Model> {
|
||||
if let Some(existing_user) = self
|
||||
.get_user_by_github_user_id_or_github_login(github_user_id, github_login, tx)
|
||||
.await?
|
||||
{
|
||||
let mut existing_user = existing_user.into_active_model();
|
||||
existing_user.github_login = ActiveValue::set(github_login.into());
|
||||
existing_user.github_user_created_at = ActiveValue::set(Some(github_user_created_at));
|
||||
|
||||
if let Some(github_email) = github_email {
|
||||
existing_user.email_address = ActiveValue::set(Some(github_email.into()));
|
||||
}
|
||||
|
||||
if let Some(github_name) = github_name {
|
||||
existing_user.name = ActiveValue::set(Some(github_name.into()));
|
||||
}
|
||||
|
||||
Ok(existing_user.update(tx).await?)
|
||||
} else {
|
||||
let user = user::Entity::insert(user::ActiveModel {
|
||||
email_address: ActiveValue::set(github_email.map(|email| email.into())),
|
||||
name: ActiveValue::set(github_name.map(|name| name.into())),
|
||||
github_login: ActiveValue::set(github_login.into()),
|
||||
github_user_id: ActiveValue::set(github_user_id),
|
||||
github_user_created_at: ActiveValue::set(Some(github_user_created_at)),
|
||||
admin: ActiveValue::set(false),
|
||||
..Default::default()
|
||||
})
|
||||
.exec_with_returning(tx)
|
||||
.await?;
|
||||
if let Some(channel_id) = initial_channel_id {
|
||||
channel_member::Entity::insert(channel_member::ActiveModel {
|
||||
id: ActiveValue::NotSet,
|
||||
channel_id: ActiveValue::Set(channel_id),
|
||||
user_id: ActiveValue::Set(user.id),
|
||||
accepted: ActiveValue::Set(true),
|
||||
role: ActiveValue::Set(ChannelRole::Guest),
|
||||
})
|
||||
.exec(tx)
|
||||
.await?;
|
||||
}
|
||||
Ok(user)
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to retrieve a user, first by their GitHub user ID, and then by their GitHub login.
|
||||
///
|
||||
/// Returns `None` if a user is not found with this GitHub user ID or GitHub login.
|
||||
pub async fn get_user_by_github_user_id_or_github_login(
|
||||
&self,
|
||||
github_user_id: i32,
|
||||
github_login: &str,
|
||||
tx: &DatabaseTransaction,
|
||||
) -> Result<Option<user::Model>> {
|
||||
if let Some(user_by_github_user_id) = user::Entity::find()
|
||||
.filter(user::Column::GithubUserId.eq(github_user_id))
|
||||
.one(tx)
|
||||
.await?
|
||||
{
|
||||
return Ok(Some(user_by_github_user_id));
|
||||
}
|
||||
|
||||
if let Some(user_by_github_login) = user::Entity::find()
|
||||
.filter(user::Column::GithubLogin.eq(github_login))
|
||||
.one(tx)
|
||||
.await?
|
||||
{
|
||||
return Ok(Some(user_by_github_login));
|
||||
}
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// get_all_users returns the next page of users. To get more call again with
|
||||
/// the same limit and the page incremented by 1.
|
||||
pub async fn get_all_users(&self, page: u32, limit: u32) -> Result<Vec<user::Model>> {
|
||||
self.transaction(|tx| async move {
|
||||
Ok(user::Entity::find()
|
||||
.order_by_asc(user::Column::GithubLogin)
|
||||
.limit(limit as u64)
|
||||
.offset(page as u64 * limit as u64)
|
||||
.all(&*tx)
|
||||
.await?)
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// Sets "connected_once" on the user for analytics.
|
||||
pub async fn set_user_connected_once(&self, id: UserId, connected_once: bool) -> Result<()> {
|
||||
self.transaction(|tx| async move {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ pub mod entities;
|
|||
pub mod env;
|
||||
pub mod executor;
|
||||
pub mod rpc;
|
||||
pub mod seed;
|
||||
pub mod services;
|
||||
|
||||
use anyhow::Context as _;
|
||||
|
|
@ -17,7 +16,7 @@ use axum::{
|
|||
use db::Database;
|
||||
use executor::Executor;
|
||||
use serde::Deserialize;
|
||||
use std::{path::PathBuf, sync::Arc};
|
||||
use std::sync::Arc;
|
||||
use util::ResultExt;
|
||||
|
||||
use crate::services::{CloudUserService, UserService};
|
||||
|
|
@ -122,7 +121,6 @@ impl std::error::Error for Error {}
|
|||
pub struct Config {
|
||||
pub http_port: u16,
|
||||
pub database_url: String,
|
||||
pub seed_path: Option<PathBuf>,
|
||||
pub database_max_connections: u32,
|
||||
pub livekit_server: Option<String>,
|
||||
pub livekit_key: Option<String>,
|
||||
|
|
@ -184,7 +182,6 @@ impl Config {
|
|||
blob_store_secret_key: None,
|
||||
blob_store_bucket: None,
|
||||
zed_client_checksum_seed: None,
|
||||
seed_path: None,
|
||||
kinesis_region: None,
|
||||
kinesis_access_key: None,
|
||||
kinesis_secret_key: None,
|
||||
|
|
|
|||
|
|
@ -43,24 +43,13 @@ async fn main() -> Result<()> {
|
|||
Some("version") => {
|
||||
println!("collab v{} ({})", VERSION, REVISION.unwrap_or("unknown"));
|
||||
}
|
||||
Some("seed") => {
|
||||
let config = envy::from_env::<Config>().expect("error loading config");
|
||||
let db_options = db::ConnectOptions::new(config.database_url.clone());
|
||||
|
||||
let mut db = Database::new(db_options).await?;
|
||||
db.initialize_notification_kinds().await?;
|
||||
|
||||
collab::seed::seed(&config, &db, false).await?;
|
||||
}
|
||||
Some("serve") => {
|
||||
let mode = match args.next().as_deref() {
|
||||
Some("collab") => ServiceMode::Collab,
|
||||
Some("api") => ServiceMode::Api,
|
||||
Some("all") => ServiceMode::All,
|
||||
_ => {
|
||||
return Err(anyhow!(
|
||||
"usage: collab <version | seed | serve <api|collab|all>>"
|
||||
))?;
|
||||
return Err(anyhow!("usage: collab <version | serve <api|collab|all>>"))?;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -200,10 +189,6 @@ async fn setup_app_database(config: &Config) -> Result<()> {
|
|||
|
||||
db.initialize_notification_kinds().await?;
|
||||
|
||||
if config.seed_path.is_some() {
|
||||
collab::seed::seed(config, &db, false).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
@ -213,7 +198,7 @@ async fn handle_root(Extension(mode): Extension<ServiceMode>) -> String {
|
|||
|
||||
async fn handle_liveness_probe(app_state: Option<Extension<Arc<AppState>>>) -> Result<String> {
|
||||
if let Some(state) = app_state {
|
||||
state.db.get_all_users(0, 1).await?;
|
||||
state.db.project_count_excluding_admins().await?;
|
||||
}
|
||||
|
||||
Ok("ok".to_string())
|
||||
|
|
|
|||
|
|
@ -1,136 +0,0 @@
|
|||
use crate::db::{self, ChannelRole, NewUserParams};
|
||||
|
||||
use anyhow::Context as _;
|
||||
use chrono::{DateTime, Utc};
|
||||
use db::Database;
|
||||
use serde::{Deserialize, de::DeserializeOwned};
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use crate::Config;
|
||||
|
||||
/// A GitHub user.
|
||||
///
|
||||
/// This representation corresponds to the entries in the `seed/github_users.json` file.
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct GithubUser {
|
||||
id: i32,
|
||||
login: String,
|
||||
email: Option<String>,
|
||||
name: Option<String>,
|
||||
created_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct SeedConfig {
|
||||
/// Which users to create as admins.
|
||||
admins: Vec<String>,
|
||||
/// Which channels to create (all admins are invited to all channels).
|
||||
channels: Vec<String>,
|
||||
}
|
||||
|
||||
pub async fn seed(config: &Config, db: &Database, force: bool) -> anyhow::Result<()> {
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
if !db.get_all_users(0, 1).await?.is_empty() && !force {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let seed_path = config
|
||||
.seed_path
|
||||
.as_ref()
|
||||
.context("called seed with no SEED_PATH")?;
|
||||
|
||||
let seed_config = load_admins(seed_path)
|
||||
.context(format!("failed to load {}", seed_path.to_string_lossy()))?;
|
||||
|
||||
let mut first_user = None;
|
||||
let mut others = vec![];
|
||||
|
||||
for admin_login in seed_config.admins {
|
||||
let user = fetch_github::<GithubUser>(
|
||||
&client,
|
||||
&format!("https://api.github.com/users/{admin_login}"),
|
||||
)
|
||||
.await;
|
||||
let user = db
|
||||
.create_user(
|
||||
&user.email.unwrap_or(format!("{admin_login}@example.com")),
|
||||
user.name.as_deref(),
|
||||
true,
|
||||
NewUserParams {
|
||||
github_login: user.login,
|
||||
github_user_id: user.id,
|
||||
},
|
||||
)
|
||||
.await
|
||||
.context("failed to create admin user")?;
|
||||
if first_user.is_none() {
|
||||
first_user = Some(user.user_id);
|
||||
} else {
|
||||
others.push(user.user_id)
|
||||
}
|
||||
}
|
||||
|
||||
for channel in seed_config.channels {
|
||||
let (channel, _) = db
|
||||
.create_channel(&channel, None, first_user.unwrap())
|
||||
.await
|
||||
.context("failed to create channel")?;
|
||||
|
||||
for user_id in &others {
|
||||
db.invite_channel_member(
|
||||
channel.id,
|
||||
*user_id,
|
||||
first_user.unwrap(),
|
||||
ChannelRole::Admin,
|
||||
)
|
||||
.await
|
||||
.context("failed to add user to channel")?;
|
||||
}
|
||||
}
|
||||
|
||||
let github_users_filepath = seed_path.parent().unwrap().join("seed/github_users.json");
|
||||
let github_users: Vec<GithubUser> =
|
||||
serde_json::from_str(&fs::read_to_string(github_users_filepath)?)?;
|
||||
|
||||
for github_user in github_users {
|
||||
log::info!("Seeding {:?} from GitHub", github_user.login);
|
||||
|
||||
db.update_or_create_user_by_github_account(
|
||||
&github_user.login,
|
||||
github_user.id,
|
||||
github_user.email.as_deref(),
|
||||
github_user.name.as_deref(),
|
||||
github_user.created_at,
|
||||
None,
|
||||
)
|
||||
.await
|
||||
.expect("failed to insert user");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn load_admins(path: impl AsRef<Path>) -> anyhow::Result<SeedConfig> {
|
||||
let file_content = fs::read_to_string(path)?;
|
||||
Ok(serde_json::from_str(&file_content)?)
|
||||
}
|
||||
|
||||
async fn fetch_github<T: DeserializeOwned>(client: &reqwest::Client, url: &str) -> T {
|
||||
let mut request_builder = client.get(url);
|
||||
if let Ok(github_token) = std::env::var("GITHUB_TOKEN") {
|
||||
request_builder =
|
||||
request_builder.header("Authorization", format!("Bearer {}", github_token));
|
||||
}
|
||||
let response = request_builder
|
||||
.header("user-agent", "zed")
|
||||
.send()
|
||||
.await
|
||||
.unwrap_or_else(|error| panic!("failed to fetch '{url}': {error}"));
|
||||
let response_text = response.text().await.unwrap_or_else(|error| {
|
||||
panic!("failed to fetch '{url}': {error}");
|
||||
});
|
||||
serde_json::from_str(&response_text).unwrap_or_else(|error| {
|
||||
panic!("failed to deserialize github user from '{url}'. Error: '{error}', text: '{response_text}'");
|
||||
})
|
||||
}
|
||||
|
|
@ -599,7 +599,6 @@ impl TestServer {
|
|||
blob_store_secret_key: None,
|
||||
blob_store_bucket: None,
|
||||
zed_client_checksum_seed: None,
|
||||
seed_path: None,
|
||||
kinesis_region: None,
|
||||
kinesis_stream: None,
|
||||
kinesis_access_key: None,
|
||||
|
|
|
|||
Loading…
Reference in a new issue