Skip to content

Commit

Permalink
feat: save github and telegram user association in db
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegec committed Mar 15, 2024
1 parent 86a59dc commit 74767d6
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 2 deletions.
2 changes: 2 additions & 0 deletions server/migrations/2024-03-15-050949_create_users/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE users;
10 changes: 10 additions & 0 deletions server/migrations/2024-03-15-050949_create_users/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- Your SQL goes here
CREATE TABLE users (
id SERIAL PRIMARY KEY,
github_login TEXT,
github_id BIGINT,
github_name TEXT,
github_avatar_url TEXT,
github_email TEXT,
telegram_chat_id BIGINT
);
66 changes: 66 additions & 0 deletions server/src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ use crate::{
api::{pipeline_new, pipeline_new_pr, pipeline_status, worker_status, JobSource},
formatter::to_html_new_pipeline_summary,
github::{get_github_token, login_github},
models::{NewUser, User},
DbPool, ALL_ARCH, ARGS,
};
use anyhow::Context;
use buildit_utils::github::{get_archs, OpenPRError, OpenPRRequest};
use chrono::Local;
use diesel::{Connection, ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl};
use serde::Deserialize;
use tracing::warn;

use std::borrow::Cow;
use teloxide::{
Expand Down Expand Up @@ -142,6 +146,65 @@ async fn pipeline_new_and_report(
Ok(())
}

async fn sync_github_info_inner(
pool: DbPool,
telegram_chat: ChatId,
access_token: String,
) -> anyhow::Result<()> {
let crab = octocrab::Octocrab::builder()
.user_access_token(access_token)
.build()?;
let author = crab.current().user().await?;
let mut conn = pool
.get()
.context("Failed to get db connection from pool")?;

conn.transaction::<(), diesel::result::Error, _>(|conn| {
use crate::schema::users::dsl::*;
match users
.filter(telegram_chat_id.eq(&telegram_chat.0))
.first::<User>(conn)
.optional()?
{
Some(user) => {
diesel::update(users.find(user.id))
.set((
github_login.eq(author.login),
github_id.eq(author.id.0 as i64),
github_avatar_url.eq(author.avatar_url.to_string()),
github_email.eq(author.email),
))
.execute(conn)?;
}
None => {
let new_user = NewUser {
github_login: Some(author.login),
github_id: Some(author.id.0 as i64),
github_name: None, // TODO
github_avatar_url: Some(author.avatar_url.to_string()),
github_email: author.email,
telegram_chat_id: Some(telegram_chat.0),
};
diesel::insert_into(crate::schema::users::table)
.values(&new_user)
.execute(conn)?;
}
}

Ok(())
})?;
Ok(())
}

async fn sync_github_info(pool: DbPool, telegram_chat_id: ChatId, access_token: String) {
if let Err(err) = sync_github_info_inner(pool, telegram_chat_id, access_token).await {
warn!(
"Failed to sync github info for telegram chat {}: {}",
telegram_chat_id, err
);
}
}

pub async fn answer(bot: Bot, msg: Message, cmd: Command, pool: DbPool) -> ResponseResult<()> {
bot.send_chat_action(msg.chat.id, ChatAction::Typing)
.await?;
Expand Down Expand Up @@ -287,6 +350,9 @@ pub async fn answer(bot: Bot, msg: Message, cmd: Command, pool: DbPool) -> Respo
}
};

// sync github info, but do not wait for result
tokio::spawn(sync_github_info(pool, msg.chat.id, token.clone()));

if (3..=5).contains(&parts.len()) {
let tags = if parts.len() >= 4 {
if parts[3].is_empty() {
Expand Down
3 changes: 2 additions & 1 deletion server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use diesel::r2d2::Pool;
use server::bot::{answer, Command};
use server::recycler::recycler_worker;
use server::routes::{
dashboard_status, job_info, job_list, job_restart, ping, pipeline_info, pipeline_list, pipeline_new_pr, worker_info, worker_job_update, worker_list, worker_poll, AppState
dashboard_status, job_info, job_list, job_restart, ping, pipeline_info, pipeline_list,
pipeline_new_pr, worker_info, worker_job_update, worker_list, worker_poll, AppState,
};
use server::routes::{pipeline_new, worker_heartbeat};
use server::routes::{pipeline_status, worker_status};
Expand Down
25 changes: 25 additions & 0 deletions server/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,28 @@ pub struct NewWorker {
pub logical_cores: i32,
pub last_heartbeat_time: chrono::DateTime<chrono::Utc>,
}

#[derive(Queryable, Selectable)]
#[diesel(table_name = crate::schema::users)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct User {
pub id: i32,
pub github_login: Option<String>,
pub github_id: Option<i64>,
pub github_name: Option<String>,
pub github_avatar_url: Option<String>,
pub github_email: Option<String>,
pub telegram_chat_id: Option<i64>,
}

#[derive(Insertable, AsChangeset)]
#[diesel(table_name = crate::schema::users)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct NewUser {
pub github_login: Option<String>,
pub github_id: Option<i64>,
pub github_name: Option<String>,
pub github_avatar_url: Option<String>,
pub github_email: Option<String>,
pub telegram_chat_id: Option<i64>,
}
14 changes: 13 additions & 1 deletion server/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ diesel::table! {
}
}

diesel::table! {
users (id) {
id -> Int4,
github_login -> Nullable<Text>,
github_id -> Nullable<Int8>,
github_name -> Nullable<Text>,
github_avatar_url -> Nullable<Text>,
github_email -> Nullable<Text>,
telegram_chat_id -> Nullable<Int8>,
}
}

diesel::table! {
workers (id) {
id -> Int4,
Expand All @@ -51,4 +63,4 @@ diesel::table! {

diesel::joinable!(jobs -> pipelines (pipeline_id));

diesel::allow_tables_to_appear_in_same_query!(jobs, pipelines, workers,);
diesel::allow_tables_to_appear_in_same_query!(jobs, pipelines, users, workers,);

0 comments on commit 74767d6

Please sign in to comment.