Skip to content

Commit

Permalink
feat: deserialize github /user api by ourselves due to missing fields…
Browse files Browse the repository at this point in the history
… in octocrab
  • Loading branch information
jiegec committed May 13, 2024
1 parent 0b0efb8 commit 86ad00a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
43 changes: 41 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ chrono = "0.4.34"
clap = { version = "4.5.1", features = ["derive", "env"] }
common = { path = "../common" }
dotenv = "0.15.0"
octocrab = "0.35.0"
octocrab = "0.38.0"
once_cell = "1.19.0"
reqwest = "0.11.24"
serde = { version = "1.0.196", features = ["derive"] }
Expand Down
20 changes: 15 additions & 5 deletions server/src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use anyhow::{bail, Context};
use buildit_utils::{find_update_and_update_checksum, github::OpenPRRequest};
use chrono::Local;
use diesel::{Connection, ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl};
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use teloxide::{
prelude::*,
Expand Down Expand Up @@ -152,6 +152,15 @@ async fn pipeline_new_and_report(
Ok(())
}

#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize)]
pub struct GitHubUser {
pub login: String,
pub id: i64,
pub email: Option<String>,
pub avatar_url: String,
pub name: String,
}

#[tracing::instrument(skip(pool, access_token))]
async fn sync_github_info_inner(
pool: DbPool,
Expand All @@ -161,7 +170,7 @@ async fn sync_github_info_inner(
let crab = octocrab::Octocrab::builder()
.user_access_token(access_token)
.build()?;
let author = crab.current().user().await?;
let author: GitHubUser = crab.get("/user", None::<&()>).await?;
let mut conn = pool
.get()
.context("Failed to get db connection from pool")?;
Expand All @@ -177,17 +186,18 @@ async fn sync_github_info_inner(
diesel::update(users.find(user.id))
.set((
github_login.eq(author.login),
github_id.eq(author.id.0 as i64),
github_id.eq(author.id),
github_avatar_url.eq(author.avatar_url.to_string()),
github_email.eq(author.email),
github_name.eq(author.name),
))
.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_id: Some(author.id),
github_name: Some(author.name),
github_avatar_url: Some(author.avatar_url.to_string()),
github_email: author.email,
telegram_chat_id: Some(telegram_chat.0),
Expand Down

0 comments on commit 86ad00a

Please sign in to comment.