From fbe80635ff2965dffa1fd2b4851a3d8bdf0b1c90 Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Mon, 11 Mar 2024 14:11:27 +0800 Subject: [PATCH] chore: refactor code --- server/src/api.rs | 2 +- server/src/github.rs | 29 +++++++++++++++++++++++++++++ server/src/job.rs | 31 ------------------------------- server/src/lib.rs | 1 - server/src/routes.rs | 2 +- worker/src/build.rs | 2 +- worker/src/heartbeat.rs | 4 ++-- 7 files changed, 34 insertions(+), 37 deletions(-) delete mode 100644 server/src/job.rs diff --git a/server/src/api.rs b/server/src/api.rs index 78fa7d5..3f4bca8 100644 --- a/server/src/api.rs +++ b/server/src/api.rs @@ -1,6 +1,6 @@ use crate::{ + github::get_crab_github_installation, github::get_packages_from_pr, - job::get_crab_github_installation, models::{NewJob, NewPipeline, Pipeline, Worker}, DbPool, ALL_ARCH, ARGS, }; diff --git a/server/src/github.rs b/server/src/github.rs index 0bcb181..545167e 100644 --- a/server/src/github.rs +++ b/server/src/github.rs @@ -1,5 +1,8 @@ +use crate::ARGS; use octocrab::models::pulls::PullRequest; +use octocrab::{models::InstallationId, Octocrab}; use serde::{Deserialize, Serialize}; +use teloxide::prelude::*; use teloxide::types::{ChatId, Message}; #[derive(Deserialize, Serialize, Debug)] @@ -62,3 +65,29 @@ pub fn get_packages_from_pr(pr: &PullRequest) -> Vec { }) .unwrap_or_default() } + +/// Create octocrab instance authenticated as github installation +pub async fn get_crab_github_installation() -> anyhow::Result> { + if let Some(id) = ARGS + .github_app_id + .as_ref() + .and_then(|x| x.parse::().ok()) + { + if let Some(app_private_key) = ARGS.github_app_key.as_ref() { + let key = tokio::fs::read(app_private_key).await?; + let key = + tokio::task::spawn_blocking(move || jsonwebtoken::EncodingKey::from_rsa_pem(&key)) + .await??; + + let app_crab = octocrab::Octocrab::builder().app(id.into(), key).build()?; + // TODO: move to config + return Ok(Some( + app_crab + .installation_and_token(InstallationId(45135446)) + .await? + .0, + )); + } + } + return Ok(None); +} diff --git a/server/src/job.rs b/server/src/job.rs deleted file mode 100644 index 94e68c6..0000000 --- a/server/src/job.rs +++ /dev/null @@ -1,31 +0,0 @@ -use crate::ARGS; - -use octocrab::{models::InstallationId, Octocrab}; - -use teloxide::prelude::*; - -/// Create octocrab instance authenticated as github installation -pub async fn get_crab_github_installation() -> anyhow::Result> { - if let Some(id) = ARGS - .github_app_id - .as_ref() - .and_then(|x| x.parse::().ok()) - { - if let Some(app_private_key) = ARGS.github_app_key.as_ref() { - let key = tokio::fs::read(app_private_key).await?; - let key = - tokio::task::spawn_blocking(move || jsonwebtoken::EncodingKey::from_rsa_pem(&key)) - .await??; - - let app_crab = octocrab::Octocrab::builder().app(id.into(), key).build()?; - // TODO: move to config - return Ok(Some( - app_crab - .installation_and_token(InstallationId(45135446)) - .await? - .0, - )); - } - } - return Ok(None); -} diff --git a/server/src/lib.rs b/server/src/lib.rs index 00fb313..1852ab5 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -11,7 +11,6 @@ pub mod bot; pub mod formatter; pub mod github; pub mod github_webhooks; -pub mod job; pub mod models; pub mod routes; pub mod schema; diff --git a/server/src/routes.rs b/server/src/routes.rs index 541ad30..c3d05f6 100644 --- a/server/src/routes.rs +++ b/server/src/routes.rs @@ -1,7 +1,7 @@ use crate::{ api::{self, JobSource, PipelineStatus}, formatter::{to_html_build_result, to_markdown_build_result, FAILED, SUCCESS}, - job::get_crab_github_installation, + github::get_crab_github_installation, models::{Job, NewWorker, Pipeline, Worker}, DbPool, ARGS, }; diff --git a/worker/src/build.rs b/worker/src/build.rs index 3f536ee..e476046 100644 --- a/worker/src/build.rs +++ b/worker/src/build.rs @@ -333,8 +333,8 @@ async fn build_worker_inner(args: &Args) -> anyhow::Result<()> { } pub async fn build_worker(args: Args) -> ! { - info!("Starting build worker"); loop { + info!("Starting build worker"); if let Err(err) = build_worker_inner(&args).await { warn!("Got error running heartbeat worker: {}", err); } diff --git a/worker/src/heartbeat.rs b/worker/src/heartbeat.rs index 8075845..b0d4405 100644 --- a/worker/src/heartbeat.rs +++ b/worker/src/heartbeat.rs @@ -6,7 +6,7 @@ use std::time::Duration; pub async fn heartbeat_worker_inner(args: &Args) -> anyhow::Result<()> { let client = reqwest::Client::new(); loop { - info!("Sending heartbeat"); + // info!("Sending heartbeat"); client .post(format!("{}/api/worker/heartbeat", args.server)) .json(&WorkerHeartbeatRequest { @@ -23,8 +23,8 @@ pub async fn heartbeat_worker_inner(args: &Args) -> anyhow::Result<()> { } pub async fn heartbeat_worker(args: Args) -> ! { - info!("Starting heartbeat worker"); loop { + info!("Starting heartbeat worker"); if let Err(err) = heartbeat_worker_inner(&args).await { warn!("Got error running heartbeat worker: {}", err); }