Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upload dickens-topic report larger than 32KiB to pastebin #25

Merged
merged 2 commits into from
Dec 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 76 additions & 2 deletions server/src/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use crate::{
models::{NewUser, User},
DbPool, ALL_ARCH, ARGS,
};
use anyhow::{bail, Context};
use anyhow::{bail, Context, Result};
use buildit_utils::{find_update_and_update_checksum, github::OpenPRRequest};
use chrono::Local;
use chrono::{Datelike, Days, Local};
use diesel::{Connection, ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl};
use rand::prelude::SliceRandom;
use rand::thread_rng;
Expand Down Expand Up @@ -618,6 +618,26 @@ pub async fn answer(bot: Bot, msg: Message, cmd: Command, pool: DbPool) -> Respo
.await
{
Ok(report) => {
let report = if report.len() > 32 * 1024 {
// paste to aosc.io pastebin first
match paste_to_aosc_io(&format!("Dickens-topic report for PR {pr_number}"), &report).await {
Ok(id) => {
format!("Dickens-topic report has been uploaded to pastebin as [paste {id}](https://aosc.io/paste/detail?id={id}).")
}
Err(err) => {
bot.send_message(
msg.chat.id,
truncate(&format!(
"Failed to upload report to aosc.io pastebin: {err:?}."
)),
)
.await?;
return Ok(());
}
}
} else {
report
};
// post report as github comment
match wait_with_send_typing(
crab.issues("AOSC-Dev", "aosc-os-abbs")
Expand Down Expand Up @@ -967,6 +987,60 @@ fn split_open_pr_message(arguments: &str) -> (Option<&str>, Vec<&str>) {
(title, parts)
}

async fn paste_to_aosc_io(title: &str, text: &str) -> Result<String> {
if text.len() > 10485760 {
bail!("text is too large to be pasted to https://aosc.io/paste")
}
let client = ClientBuilder::new().user_agent("buildit").build()?;
let exp_date = chrono::Utc::now()
.checked_add_days(Days::new(7))
.context("failed to generate expDate")?;
let exp_date = format!(
"{:04}-{:02}-{:02}",
exp_date.year(),
exp_date.month(),
exp_date.day()
);
let resp = client
.post("https://aosc.io/pasteApi/paste")
.form(&[
("title", title),
("language", "plaintext"),
("content", text),
("expDate", &exp_date),
])
.send()
.await?
.error_for_status()?
.json::<serde_json::Value>()
.await?;
if resp.get("code").and_then(|v| v.as_u64()) != Some(0) {
let msg = resp
.get("message")
.and_then(|v| v.as_str())
.unwrap_or("(no message field)");
bail!("aosc.io/paste error: {}", msg)
} else {
let id = resp
.get("data")
.and_then(|v| v.get("id"))
.and_then(|v| v.as_str())
.context("$.data.id not found from paste response")?;
Ok(id.to_string())
}
}

#[tokio::test]
async fn test_paste_to_aosc_io() {
let id = paste_to_aosc_io(
"Test message for test_paste_to_aosc_io",
"Some random texts here",
)
.await
.unwrap();
dbg!(id);
}

#[test]
fn test_split_open_pr_message() {
let t = split_open_pr_message("clutter fix ftbfs;clutter-fix-ftbfs;clutter");
Expand Down
Loading