From 4966d08d18191ab6c71af72532da613cb7c23d1b Mon Sep 17 00:00:00 2001 From: qpismont Date: Tue, 2 Jun 2026 20:30:02 +0000 Subject: [PATCH] first comment ! :D --- src/bot.rs | 20 ++++++++++++++++++-- src/env.rs | 3 +++ src/gitea.rs | 45 ++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/bot.rs b/src/bot.rs index cccdd35..ac104eb 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,3 +1,5 @@ +use std::time::Duration; + use crate::{ env::EnvConfig, errors::AppError, @@ -12,7 +14,7 @@ pub struct Bot { impl Bot { pub fn new(config: EnvConfig) -> Self { Self { - gitea_api: GiteaAPI::new(&config.gitea_url), + gitea_api: GiteaAPI::new(&config.gitea_url, &config.gitea_token), config, } } @@ -36,11 +38,25 @@ impl Bot { match exec_result { Ok(_) => println!("Task completed"), - Err(_) => println!("Task errored"), + Err(err) => println!("{}", err), } } pub async fn exec_review(&self, review_payload: ReviewPayload) -> Result<(), AppError> { + let new_comment = self + .gitea_api + .comment( + &review_payload.repository.full_name, + review_payload.pull_request.number, + ) + .await?; + + tokio::time::sleep(Duration::from_secs(10)).await; + + self.gitea_api + .edit_comment(&review_payload.repository.full_name, new_comment.id) + .await?; + Ok(()) } } diff --git a/src/env.rs b/src/env.rs index 9be6803..1001e62 100644 --- a/src/env.rs +++ b/src/env.rs @@ -8,6 +8,7 @@ pub struct EnvConfig { pub open_router_api_key: String, pub bot_name: String, pub gitea_url: String, + pub gitea_token: String, } pub fn load_config() -> anyhow::Result { @@ -18,6 +19,7 @@ pub fn load_config() -> anyhow::Result { let webhook_secret = try_get_env("WEBHOOK_SIG_HEADER_SECRET")?; let open_router_api_key = try_get_env("OPEN_ROUTER_API_KEY")?; let gitea_url = try_get_env("GITEA_URL")?; + let gitea_token = try_get_env("GITEA_TOKEN")?; Ok(EnvConfig { http_port, @@ -25,6 +27,7 @@ pub fn load_config() -> anyhow::Result { bot_name, open_router_api_key, gitea_url, + gitea_token, }) } diff --git a/src/gitea.rs b/src/gitea.rs index 4e716df..63a816e 100644 --- a/src/gitea.rs +++ b/src/gitea.rs @@ -1,24 +1,58 @@ use serde::Deserialize; -use serde_json::Value; +use serde_json::{Value, json}; use crate::errors::AppError; pub struct GiteaAPI { base_url: String, + token: String, } impl GiteaAPI { - pub fn new(base_url: &str) -> Self { + pub fn new(base_url: &str, token: &str) -> Self { Self { base_url: String::from(base_url), + token: String::from(token), } } - pub async fn comment(&self, full_name: &str, index: u64) -> anyhow::Result { - Ok(1) + pub async fn comment(&self, full_name: &str, index: u64) -> anyhow::Result { + let url = format!( + "{}/api/v1/repos/{}/issues/{}/comments?access_token={}", + self.base_url, full_name, index, self.token + ); + + let client = reqwest::Client::new(); + let res = client + .post(url) + .json(&json!({ + "body": "Hello world :)" + })) + .send() + .await?; + + println!("{}", res.status()); + + res.json::().await.map_err(anyhow::Error::from) } - pub async fn edit_comment(&self, full_name: &str, id: u64) -> anyhow::Result<()> { + pub async fn edit_comment(&self, full_name: &str, comment_id: u64) -> anyhow::Result<()> { + let url = format!( + "{}/api/v1/repos/{}/issues/comments/{}?access_token={}", + self.base_url, full_name, comment_id, self.token + ); + + let client = reqwest::Client::new(); + let res = client + .patch(url) + .json(&json!({ + "body": "Updated Hello world :)" + })) + .send() + .await?; + + println!("{}", res.status()); + Ok(()) } } @@ -40,6 +74,7 @@ pub struct ReviewPayload { pub struct PullRequest { pub id: u64, pub diff_url: String, + pub number: u64, } #[derive(Deserialize, Debug)]