Skip to content

Commit

Permalink
feat: create pipeline automatically in /bump
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegec committed May 12, 2024
1 parent 725d164 commit 26e8486
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 47 deletions.
8 changes: 6 additions & 2 deletions buildit-utils/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ pub enum OpenPRError {
Anyhow(#[from] anyhow::Error),
}

// return (pr number, pr url)
#[tracing::instrument(skip(app_private_key_path, access_token, app_id))]
pub async fn open_pr(
app_private_key_path: &Path,
access_token: &str,
app_id: u64,
openpr_request: OpenPRRequest<'_>,
) -> Result<String, OpenPRError> {
) -> Result<(u64, String), OpenPRError> {
let key = tokio::fs::read(app_private_key_path).await?;
let key = tokio::task::spawn_blocking(move || jsonwebtoken::EncodingKey::from_rsa_pem(&key))
.await??;
Expand Down Expand Up @@ -136,7 +137,10 @@ pub async fn open_pr(
})
.await?;

Ok(pr.html_url.map(|x| x.to_string()).unwrap_or_else(|| pr.url))
Ok((
pr.id.0,
pr.html_url.map(|x| x.to_string()).unwrap_or_else(|| pr.url),
))
}

/// `packages` should have no groups nor modifiers
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async fn main() -> eyre::Result<()> {
)
.await
{
Ok(url) => println!("{url}"),
Ok((_id, url)) => println!("{url}"),
Err(e) => {
eprintln!("{e}");
}
Expand Down
93 changes: 49 additions & 44 deletions server/src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ use crate::{
DbPool, ALL_ARCH, ARGS,
};
use anyhow::{bail, Context};
use buildit_utils::{
find_update_and_update_checksum,
github::{OpenPRError, OpenPRRequest},
};
use buildit_utils::{find_update_and_update_checksum, github::OpenPRRequest};
use chrono::Local;
use diesel::{Connection, ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl};
use serde::Deserialize;
Expand Down Expand Up @@ -246,6 +243,44 @@ async fn get_user(pool: DbPool, chat_id: ChatId, access_token: String) -> anyhow
bail!("Failed to get user info")
}

async fn create_pipeline_from_pr(
pool: DbPool,
pr_number: u64,
archs: Option<&str>,
msg: &Message,
bot: &Bot,
) -> ResponseResult<()> {
match pipeline_new_pr(pool, pr_number, archs, &JobSource::Telegram(msg.chat.id.0)).await {
Ok(pipeline) => {
bot.send_message(
msg.chat.id,
to_html_new_pipeline_summary(
pipeline.id,
&pipeline.git_branch,
&pipeline.git_sha,
pipeline.github_pr.map(|n| n as u64),
&pipeline.archs.split(',').collect::<Vec<_>>(),
&pipeline.packages.split(',').collect::<Vec<_>>(),
),
)
.parse_mode(ParseMode::Html)
.disable_web_page_preview(true)
.send()
.instrument(tracing::info_span!("send_message"))
.await?;
}
Err(err) => {
bot.send_message(
msg.chat.id,
format!("Failed to create pipeline from pr: {err:?}"),
)
.await?;
}
}

Ok(())
}

#[tracing::instrument(skip(bot, msg, pool))]
pub async fn answer(bot: Bot, msg: Message, cmd: Command, pool: DbPool) -> ResponseResult<()> {
bot.send_chat_action(msg.chat.id, ChatAction::Typing)
Expand Down Expand Up @@ -298,40 +333,7 @@ pub async fn answer(bot: Bot, msg: Message, cmd: Command, pool: DbPool) -> Respo
Some(parts[1])
};
for pr_number in pr_numbers {
match pipeline_new_pr(
pool.clone(),
pr_number,
archs,
&JobSource::Telegram(msg.chat.id.0),
)
.await
{
Ok(pipeline) => {
bot.send_message(
msg.chat.id,
to_html_new_pipeline_summary(
pipeline.id,
&pipeline.git_branch,
&pipeline.git_sha,
pipeline.github_pr.map(|n| n as u64),
&pipeline.archs.split(',').collect::<Vec<_>>(),
&pipeline.packages.split(',').collect::<Vec<_>>(),
),
)
.parse_mode(ParseMode::Html)
.disable_web_page_preview(true)
.send()
.instrument(tracing::info_span!("send_message"))
.await?;
}
Err(err) => {
bot.send_message(
msg.chat.id,
format!("Failed to create pipeline from pr: {err:?}"),
)
.await?;
}
}
create_pipeline_from_pr(pool.clone(), pr_number, archs, &msg, &bot).await?;
}
}
}
Expand Down Expand Up @@ -466,7 +468,7 @@ pub async fn answer(bot: Bot, msg: Message, cmd: Command, pool: DbPool) -> Respo
)
.await
{
Ok(url) => {
Ok((_id, url)) => {
bot.send_message(msg.chat.id, format!("Successfully opened PR: {url}"))
.await?;
return Ok(());
Expand Down Expand Up @@ -714,7 +716,7 @@ pub async fn answer(bot: Bot, msg: Message, cmd: Command, pool: DbPool) -> Respo
}
};

let user = match get_user(pool, msg.chat.id, token.clone()).await {
let user = match get_user(pool.clone(), msg.chat.id, token.clone()).await {
Ok(user) => user,
Err(err) => {
bot.send_message(msg.chat.id, format!("Failed to get user info: {}", err))
Expand Down Expand Up @@ -753,19 +755,22 @@ pub async fn answer(bot: Bot, msg: Message, cmd: Command, pool: DbPool) -> Respo
)
.await
{
Ok(url) => {
Ok((pr_number, url)) => {
bot.send_message(msg.chat.id, format!("Successfully opened PR: {url}"))
.await?
.await?;

create_pipeline_from_pr(pool.clone(), pr_number, None, &msg, &bot)
.await?;
}
Err(e) => {
bot.send_message(msg.chat.id, format!("Failed to open PR: {}", e))
.await?
.await?;
}
}
}
Err(e) => {
bot.send_message(msg.chat.id, format!("Failed to find update: {}", e))
.await?
.await?;
}
};
}
Expand Down

0 comments on commit 26e8486

Please sign in to comment.