Integrate OpenRouter for AI-powered code review
Add openrouter-rs dependency, review prompt, and markdown formatting. Update comment API to accept dynamic body. Adjust devcontainer for podman compatibility.
This commit is contained in:
+135
-6
@@ -1,22 +1,81 @@
|
||||
use std::time::Duration;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::{
|
||||
consts::{BOT_PROCESS_MSG, REVIEW_PROMPT},
|
||||
env::EnvConfig,
|
||||
errors::AppError,
|
||||
gitea::{GiteaAPI, ReviewPayload, WebhookType},
|
||||
open_router::OpenRouterClient,
|
||||
};
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct ReviewResult {
|
||||
reviews: Vec<ReviewItem>,
|
||||
comment: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct ReviewItem {
|
||||
filename: String,
|
||||
line: Option<u64>,
|
||||
code: String,
|
||||
message: String,
|
||||
}
|
||||
|
||||
/// Map a filename to a markdown language identifier for syntax highlighting.
|
||||
fn lang_from_filename(filename: &str) -> &str {
|
||||
match std::path::Path::new(filename)
|
||||
.extension()
|
||||
.and_then(|e| e.to_str())
|
||||
.unwrap_or("")
|
||||
{
|
||||
"rs" => "rust",
|
||||
"py" => "python",
|
||||
"js" | "mjs" => "javascript",
|
||||
"ts" => "typescript",
|
||||
"jsx" => "jsx",
|
||||
"tsx" => "tsx",
|
||||
"go" => "go",
|
||||
"java" => "java",
|
||||
"kt" | "kts" => "kotlin",
|
||||
"scala" => "scala",
|
||||
"c" | "h" => "c",
|
||||
"cpp" | "cc" | "cxx" | "hpp" | "hxx" => "cpp",
|
||||
"rb" => "ruby",
|
||||
"php" => "php",
|
||||
"swift" => "swift",
|
||||
"sh" | "bash" | "zsh" => "bash",
|
||||
"sql" => "sql",
|
||||
"html" | "htm" => "html",
|
||||
"css" => "css",
|
||||
"scss" | "sass" => "scss",
|
||||
"json" => "json",
|
||||
"yaml" | "yml" => "yaml",
|
||||
"xml" => "xml",
|
||||
"toml" => "toml",
|
||||
"md" | "mdx" => "markdown",
|
||||
"dockerfile" | "Dockerfile" => "dockerfile",
|
||||
"Makefile" => "makefile",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Bot {
|
||||
config: EnvConfig,
|
||||
gitea_api: GiteaAPI,
|
||||
open_router_client: OpenRouterClient,
|
||||
}
|
||||
|
||||
impl Bot {
|
||||
pub fn new(config: EnvConfig) -> Self {
|
||||
Self {
|
||||
pub fn new(config: EnvConfig) -> anyhow::Result<Self> {
|
||||
Ok(Self {
|
||||
gitea_api: GiteaAPI::new(&config.gitea_url, &config.gitea_token),
|
||||
open_router_client: OpenRouterClient::new(
|
||||
&config.open_router_api_key,
|
||||
&config.open_router_model,
|
||||
)?,
|
||||
config,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn start(
|
||||
@@ -46,17 +105,87 @@ impl Bot {
|
||||
let new_comment = self
|
||||
.gitea_api
|
||||
.comment(
|
||||
&BOT_PROCESS_MSG.replace("{model}", &self.config.open_router_model),
|
||||
&review_payload.repository.full_name,
|
||||
review_payload.pull_request.number,
|
||||
)
|
||||
.await?;
|
||||
|
||||
tokio::time::sleep(Duration::from_secs(10)).await;
|
||||
let bot_result: Result<String, anyhow::Error> = async {
|
||||
let git_diff = self
|
||||
.download_git_diff(&review_payload.pull_request.diff_url)
|
||||
.await?;
|
||||
|
||||
let bot_request = &&REVIEW_PROMPT
|
||||
.replace("{subject}", &review_payload.pull_request.title)
|
||||
.replace("{comment}", &review_payload.comment.body)
|
||||
.replace("{diff}", &git_diff);
|
||||
|
||||
self.open_router_client.chat(&bot_request).await
|
||||
}
|
||||
.await;
|
||||
|
||||
let edit_msg = match bot_result {
|
||||
Ok(bot_result) => self.review_result_to_markdown(&bot_result),
|
||||
Err(e) => format!("Error while reviewing: {}", e),
|
||||
};
|
||||
|
||||
self.gitea_api
|
||||
.edit_comment(&review_payload.repository.full_name, new_comment.id)
|
||||
.edit_comment(
|
||||
&edit_msg,
|
||||
&review_payload.repository.full_name,
|
||||
new_comment.id,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn review_result_to_markdown(&self, result: &str) -> String {
|
||||
let review_result: ReviewResult = match serde_json::from_str(result) {
|
||||
Ok(review_result) => review_result,
|
||||
Err(_) => {
|
||||
return format!(
|
||||
"Failed to parse review result. Raw output:\n\n```json\n{}\n```",
|
||||
result
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
if review_result.reviews.is_empty() {
|
||||
return String::from("No issues found. ✅");
|
||||
}
|
||||
|
||||
let mut md = String::from("## Review Feedback\n\n");
|
||||
for (i, item) in review_result.reviews.iter().enumerate() {
|
||||
if i > 0 {
|
||||
md.push_str("\n---\n\n");
|
||||
}
|
||||
md.push_str(&format!("### `{}`\n\n", item.filename));
|
||||
if let Some(line) = item.line {
|
||||
md.push_str(&format!("> **Line {}**\n\n", line));
|
||||
}
|
||||
if !item.code.is_empty() {
|
||||
let lang = lang_from_filename(&item.filename);
|
||||
md.push_str(&format!("```{}\n{}\n```\n\n", lang, item.code));
|
||||
}
|
||||
md.push_str(&item.message);
|
||||
md.push('\n');
|
||||
}
|
||||
|
||||
if !review_result.comment.is_empty() {
|
||||
md.push_str("\n---\n\n");
|
||||
md.push_str("### Summary\n\n");
|
||||
md.push_str(&review_result.comment);
|
||||
md.push('\n');
|
||||
}
|
||||
|
||||
md
|
||||
}
|
||||
|
||||
async fn download_git_diff(&self, url: &str) -> anyhow::Result<String> {
|
||||
let response = reqwest::get(url).await?;
|
||||
let body = response.text().await?;
|
||||
Ok(body)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user