From 86ad00ab0ec2fd3c9f0d541ab079b90aa7d9e9bb Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Mon, 13 May 2024 14:33:31 +0800 Subject: [PATCH] feat: deserialize github /user api by ourselves due to missing fields in octocrab --- Cargo.lock | 43 +++++++++++++++++++++++++++++++++++++++++-- server/Cargo.toml | 2 +- server/src/bot.rs | 20 +++++++++++++++----- 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5fb98c1..bea2325 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -419,7 +419,7 @@ dependencies = [ "fancy-regex", "gix", "jsonwebtoken", - "octocrab", + "octocrab 0.35.0", "once_cell", "thiserror", "tokio", @@ -2398,6 +2398,45 @@ dependencies = [ "url", ] +[[package]] +name = "octocrab" +version = "0.38.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68a8a3df00728324ad654ecd1ed449a60157c55b7ff8c109af3a35989687c367" +dependencies = [ + "arc-swap", + "async-trait", + "base64 0.22.0", + "bytes", + "cfg-if", + "chrono", + "either", + "futures", + "futures-util", + "http 1.1.0", + "http-body 1.0.0", + "http-body-util", + "hyper 1.2.0", + "hyper-rustls", + "hyper-timeout 0.5.1", + "hyper-util", + "jsonwebtoken", + "once_cell", + "percent-encoding", + "pin-project", + "secrecy", + "serde", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "snafu", + "tokio", + "tower", + "tower-http", + "tracing", + "url", +] + [[package]] name = "oma-debcontrol" version = "0.3.1" @@ -3254,7 +3293,7 @@ dependencies = [ "diesel", "dotenv", "jsonwebtoken", - "octocrab", + "octocrab 0.38.0", "once_cell", "opentelemetry", "opentelemetry-otlp", diff --git a/server/Cargo.toml b/server/Cargo.toml index 12fd6fc..d5de367 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -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"] } diff --git a/server/src/bot.rs b/server/src/bot.rs index 297dd0a..c3c8dfa 100644 --- a/server/src/bot.rs +++ b/server/src/bot.rs @@ -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::*, @@ -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, + pub avatar_url: String, + pub name: String, +} + #[tracing::instrument(skip(pool, access_token))] async fn sync_github_info_inner( pool: DbPool, @@ -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")?; @@ -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),