Skip to content

Commit

Permalink
chore: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegec committed Mar 11, 2024
1 parent cc95c89 commit fbe8063
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 37 deletions.
2 changes: 1 addition & 1 deletion server/src/api.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down
29 changes: 29 additions & 0 deletions server/src/github.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::ARGS;
use octocrab::models::pulls::PullRequest;
use octocrab::{models::InstallationId, Octocrab};
use serde::{Deserialize, Serialize};
use teloxide::prelude::*;

Check warning on line 5 in server/src/github.rs

View workflow job for this annotation

GitHub Actions / build

unused import: `teloxide::prelude`
use teloxide::types::{ChatId, Message};

#[derive(Deserialize, Serialize, Debug)]
Expand Down Expand Up @@ -62,3 +65,29 @@ pub fn get_packages_from_pr(pr: &PullRequest) -> Vec<String> {
})
.unwrap_or_default()
}

/// Create octocrab instance authenticated as github installation
pub async fn get_crab_github_installation() -> anyhow::Result<Option<Octocrab>> {
if let Some(id) = ARGS
.github_app_id
.as_ref()
.and_then(|x| x.parse::<u64>().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);
}
31 changes: 0 additions & 31 deletions server/src/job.rs

This file was deleted.

1 change: 0 additions & 1 deletion server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion server/src/routes.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down
2 changes: 1 addition & 1 deletion worker/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions worker/src/heartbeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down

0 comments on commit fbe8063

Please sign in to comment.