started gitea api impl #2
+18
-2
@@ -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(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<EnvConfig> {
|
||||
@@ -18,6 +19,7 @@ pub fn load_config() -> anyhow::Result<EnvConfig> {
|
||||
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<EnvConfig> {
|
||||
bot_name,
|
||||
open_router_api_key,
|
||||
gitea_url,
|
||||
gitea_token,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+40
-5
@@ -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<u64> {
|
||||
Ok(1)
|
||||
pub async fn comment(&self, full_name: &str, index: u64) -> anyhow::Result<Comment> {
|
||||
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(())
|
||||
}
|
||||
}
|
||||
@@ -40,6 +74,7 @@ pub struct ReviewPayload {
|
||||
pub struct PullRequest {
|
||||
pub id: u64,
|
||||
pub diff_url: String,
|
||||
pub number: u64,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
|
||||
Reference in New Issue
Block a user