started gitea api impl #2

Merged
qpismont merged 9 commits from gitea_api into main 2026-06-07 10:23:31 +02:00
3 changed files with 61 additions and 7 deletions
Showing only changes of commit 4966d08d18 - Show all commits
+18 -2
View File
@@ -1,3 +1,5 @@
use std::time::Duration;
use crate::{ use crate::{
env::EnvConfig, env::EnvConfig,
errors::AppError, errors::AppError,
@@ -12,7 +14,7 @@ pub struct Bot {
impl Bot { impl Bot {
pub fn new(config: EnvConfig) -> Self { pub fn new(config: EnvConfig) -> Self {
Self { Self {
gitea_api: GiteaAPI::new(&config.gitea_url), gitea_api: GiteaAPI::new(&config.gitea_url, &config.gitea_token),
config, config,
} }
} }
@@ -36,11 +38,25 @@ impl Bot {
match exec_result { match exec_result {
Ok(_) => println!("Task completed"), Ok(_) => println!("Task completed"),
Err(_) => println!("Task errored"), Err(err) => println!("{}", err),
} }
} }
pub async fn exec_review(&self, review_payload: ReviewPayload) -> Result<(), AppError> { 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(()) Ok(())
} }
} }
+3
View File
@@ -8,6 +8,7 @@ pub struct EnvConfig {
pub open_router_api_key: String, pub open_router_api_key: String,
pub bot_name: String, pub bot_name: String,
pub gitea_url: String, pub gitea_url: String,
pub gitea_token: String,
} }
pub fn load_config() -> anyhow::Result<EnvConfig> { pub fn load_config() -> anyhow::Result<EnvConfig> {
@@ -18,6 +19,7 @@ pub fn load_config() -> anyhow::Result<EnvConfig> {
let webhook_secret = try_get_env("WEBHOOK_SIG_HEADER_SECRET")?; let webhook_secret = try_get_env("WEBHOOK_SIG_HEADER_SECRET")?;
let open_router_api_key = try_get_env("OPEN_ROUTER_API_KEY")?; let open_router_api_key = try_get_env("OPEN_ROUTER_API_KEY")?;
let gitea_url = try_get_env("GITEA_URL")?; let gitea_url = try_get_env("GITEA_URL")?;
let gitea_token = try_get_env("GITEA_TOKEN")?;
Ok(EnvConfig { Ok(EnvConfig {
http_port, http_port,
@@ -25,6 +27,7 @@ pub fn load_config() -> anyhow::Result<EnvConfig> {
bot_name, bot_name,
open_router_api_key, open_router_api_key,
gitea_url, gitea_url,
gitea_token,
}) })
} }
+40 -5
View File
@@ -1,24 +1,58 @@
use serde::Deserialize; use serde::Deserialize;
use serde_json::Value; use serde_json::{Value, json};
use crate::errors::AppError; use crate::errors::AppError;
pub struct GiteaAPI { pub struct GiteaAPI {
base_url: String, base_url: String,
token: String,
} }
impl GiteaAPI { impl GiteaAPI {
pub fn new(base_url: &str) -> Self { pub fn new(base_url: &str, token: &str) -> Self {
Self { Self {
base_url: String::from(base_url), base_url: String::from(base_url),
token: String::from(token),
} }
} }
pub async fn comment(&self, full_name: &str, index: u64) -> anyhow::Result<u64> { pub async fn comment(&self, full_name: &str, index: u64) -> anyhow::Result<Comment> {
Ok(1) 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::<Comment>().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(()) Ok(())
} }
} }
@@ -40,6 +74,7 @@ pub struct ReviewPayload {
pub struct PullRequest { pub struct PullRequest {
pub id: u64, pub id: u64,
pub diff_url: String, pub diff_url: String,
pub number: u64,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]