Compare commits
11 Commits
9175f9b3a2
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 7f24d7657c | |||
| a613fdb99e | |||
| 975581093a | |||
| 00d46ce968 | |||
| 3f6c5b5559 | |||
| d4666fb36e | |||
| cf59455d4a | |||
| c7387a3b28 | |||
| 433021d607 | |||
| 3c32cd20b6 | |||
| a30d7c5d90 |
@@ -0,0 +1,4 @@
|
|||||||
|
target/
|
||||||
|
.env
|
||||||
|
.devcontainer/
|
||||||
|
docs/
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
HTTP_PORT=3000
|
||||||
|
BOT_NAME=Herald
|
||||||
|
|
||||||
|
WEBHOOK_SIG_HEADER_SECRET=
|
||||||
|
|
||||||
|
OPEN_ROUTER_API_KEY=
|
||||||
|
OPEN_ROUTER_MODEL=deepseek/deepseek-v4-flash
|
||||||
|
OPEN_ROUTER_TIMEOUT=120
|
||||||
|
|
||||||
|
BOT_MAX_CONCURRENT=5
|
||||||
|
|
||||||
|
GITEA_URL=https://gitea.example.com
|
||||||
|
GITEA_TOKEN=
|
||||||
|
GITEA_TIMEOUT=30
|
||||||
|
|
||||||
|
# Optional
|
||||||
|
SENTRY_DSN=
|
||||||
|
RUST_LOG=info
|
||||||
|
RUST_BACKTRACE=1
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
when:
|
||||||
|
event:
|
||||||
|
- tag
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: release-docker
|
||||||
|
image: quay.io/buildah/stable
|
||||||
|
privileged: true
|
||||||
|
volumes:
|
||||||
|
- /data/woodpecker-builds:/data
|
||||||
|
commands:
|
||||||
|
- echo $DOCKER_PASSWORD | buildah login docker.io -u $DOCKER_USERNAME --password-stdin
|
||||||
|
- chmod +x scripts/build.sh
|
||||||
|
- bash scripts/build.sh
|
||||||
|
environment:
|
||||||
|
DOCKER_USERNAME:
|
||||||
|
from_secret: docker_username
|
||||||
|
DOCKER_PASSWORD:
|
||||||
|
from_secret: docker_password
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
when:
|
||||||
|
event:
|
||||||
|
- push
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: clippy
|
||||||
|
image: rust:1.96
|
||||||
|
commands:
|
||||||
|
- rustup component add clippy
|
||||||
|
- cargo clippy
|
||||||
|
|
||||||
|
- name: test
|
||||||
|
image: rust:1.96
|
||||||
|
commands:
|
||||||
|
- cargo test
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
{
|
||||||
|
"lsp": {
|
||||||
|
"rust-analyzer": {
|
||||||
|
"initialization_options": {
|
||||||
|
"check": {
|
||||||
|
"command": "clippy"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Generated
+1
-1
@@ -786,7 +786,7 @@ checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "herald"
|
name = "herald"
|
||||||
version = "0.1.0"
|
version = "1.0.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"axum",
|
"axum",
|
||||||
|
|||||||
+6
-3
@@ -1,8 +1,11 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "herald"
|
name = "herald"
|
||||||
version = "0.1.0"
|
version = "1.0.1"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
debug = 1
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
|
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
|
||||||
tokio = { version = "1.52", features = ["full"] }
|
tokio = { version = "1.52", features = ["full"] }
|
||||||
@@ -12,7 +15,7 @@ futures-util = "0.3"
|
|||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
sentry = { version = "0.48", features = ["tower-axum-matched-path"] }
|
sentry = { version = "0.48", features = ["tower-axum-matched-path"] }
|
||||||
sentry-anyhow = "0.48"
|
sentry-anyhow = { version = "0.48", features = ["backtrace"] }
|
||||||
openrouter-rs = "0.10"
|
openrouter-rs = "0.10"
|
||||||
dotenvy = "0.15"
|
dotenvy = "0.15"
|
||||||
tower = "0.5"
|
tower = "0.5"
|
||||||
@@ -20,7 +23,7 @@ tower-http = {version = "0.6", features = ["trace"] }
|
|||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-subscriber = { version = "0.3", features=["env-filter"] }
|
tracing-subscriber = { version = "0.3", features=["env-filter"] }
|
||||||
axum = "0.8"
|
axum = "0.8"
|
||||||
anyhow = "1.0"
|
anyhow = { version = "1.0", features = ["backtrace"] }
|
||||||
thiserror = "2.0"
|
thiserror = "2.0"
|
||||||
hmac = "0.13"
|
hmac = "0.13"
|
||||||
sha2 = "0.11"
|
sha2 = "0.11"
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
FROM rust:1.96 as builder
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
COPY . .
|
||||||
|
RUN cargo build --release
|
||||||
|
|
||||||
|
|
||||||
|
FROM debian:trixie-slim
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=builder /app/target/release/herald .
|
||||||
|
CMD [ "./herald" ]
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
# Herald
|
||||||
|
|
||||||
|
Herald is a Gitea bot that performs automated AI-powered code reviews on pull requests using [OpenRouter](https://openrouter.ai/).
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Listens for Gitea webhook events and triggers code reviews on pull request comments
|
||||||
|
- Streams reviews back to Gitea as PR comments
|
||||||
|
- Concurrent review processing with configurable parallelism
|
||||||
|
- Graceful shutdown — in-progress reviews finish before the process exits
|
||||||
|
- Error tracking via Sentry
|
||||||
|
- Tiny memory footprint (~4MB) thanks to Rust
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
**Requirements:** Rust toolchain ([rustup.rs](https://rustup.rs))
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cargo build --release
|
||||||
|
./target/release/herald
|
||||||
|
```
|
||||||
|
|
||||||
|
Herald reads its configuration from environment variables (a `.env` file is supported):
|
||||||
|
|
||||||
|
| Variable | Description |
|
||||||
|
|---|---|
|
||||||
|
| `HTTP_PORT` | Port to listen on |
|
||||||
|
| `BOT_NAME` | The bot's Gitea username (used to detect mentions) |
|
||||||
|
| `WEBHOOK_SIG_HEADER_SECRET` | Gitea webhook secret for signature verification |
|
||||||
|
| `OPEN_ROUTER_API_KEY` | OpenRouter API key |
|
||||||
|
| `OPEN_ROUTER_MODEL` | Model to use (e.g. `deepseek/deepseek-v4-flash`) |
|
||||||
|
| `OPEN_ROUTER_TIMEOUT` | OpenRouter request timeout in seconds |
|
||||||
|
| `BOT_MAX_CONCURRENT` | Maximum number of concurrent reviews |
|
||||||
|
| `GITEA_URL` | Base URL of your Gitea instance |
|
||||||
|
| `GITEA_TOKEN` | Gitea API token |
|
||||||
|
| `GITEA_TIMEOUT` | Gitea API request timeout in seconds |
|
||||||
|
| `SENTRY_DSN` | *(optional)* Sentry DSN for error tracking |
|
||||||
|
| `RUST_LOG` | *(optional)* Log level, defaults to `info` |
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
The easiest way to get started is with the provided [Dev Container](https://containers.dev/) (VS Code or Zed with the dev container extension).
|
||||||
|
|
||||||
|
Open the project and reopen it in the container — the Rust toolchain is pre-installed.
|
||||||
|
|
||||||
|
**Without Dev Container**, you just need a Rust toolchain:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
rustup toolchain install stable
|
||||||
|
cargo run
|
||||||
|
```
|
||||||
|
|
||||||
|
Copy `.env.example` to `.env` and fill in your values before running.
|
||||||
Executable
+22
@@ -0,0 +1,22 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
IMAGE="tintounn/herald"
|
||||||
|
|
||||||
|
if [ -z "${CI_COMMIT_TAG:-}" ]; then
|
||||||
|
echo "Error: CI_COMMIT_TAG is not set" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
TAG="${CI_COMMIT_TAG}"
|
||||||
|
|
||||||
|
echo "Building ${IMAGE}:${TAG}..."
|
||||||
|
buildah build \
|
||||||
|
--file Containerfile \
|
||||||
|
--tag "docker.io/${IMAGE}:${TAG}" \
|
||||||
|
.
|
||||||
|
|
||||||
|
echo "Pushing ${IMAGE}:${TAG}..."
|
||||||
|
buildah push "${IMAGE}:${TAG}"
|
||||||
|
|
||||||
|
echo "Done: ${IMAGE}:${TAG}"
|
||||||
+17
-7
@@ -1,5 +1,4 @@
|
|||||||
use anyhow::anyhow;
|
use axum::body::{Bytes, to_bytes};
|
||||||
use axum::body::{Body, Bytes, to_bytes};
|
|
||||||
use axum::extract::{FromRef, FromRequest, State};
|
use axum::extract::{FromRef, FromRequest, State};
|
||||||
use axum::http::Request;
|
use axum::http::Request;
|
||||||
use axum::response::IntoResponse;
|
use axum::response::IntoResponse;
|
||||||
@@ -15,12 +14,14 @@ use tower::ServiceBuilder;
|
|||||||
use tower_http::trace::TraceLayer;
|
use tower_http::trace::TraceLayer;
|
||||||
use tracing::{info, instrument};
|
use tracing::{info, instrument};
|
||||||
|
|
||||||
|
use tokio_util::sync::CancellationToken;
|
||||||
|
|
||||||
use crate::consts::{GITEA_EVENT_TYPE_HEADER_NAME, GITEA_SIG_HEADER_NAME, MAX_WEBHOOK_BODY_SIZE};
|
use crate::consts::{GITEA_EVENT_TYPE_HEADER_NAME, GITEA_SIG_HEADER_NAME, MAX_WEBHOOK_BODY_SIZE};
|
||||||
use crate::errors::AppError;
|
use crate::errors::AppError;
|
||||||
use crate::gitea::WebhookType;
|
use crate::gitea::WebhookType;
|
||||||
use crate::state::AppState;
|
use crate::state::AppState;
|
||||||
|
|
||||||
pub async fn start(app_state: AppState) -> anyhow::Result<()> {
|
pub async fn start(app_state: AppState, shutdown: CancellationToken) -> anyhow::Result<()> {
|
||||||
let http_port = app_state.config.http_port;
|
let http_port = app_state.config.http_port;
|
||||||
|
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
@@ -38,8 +39,12 @@ pub async fn start(app_state: AppState) -> anyhow::Result<()> {
|
|||||||
info!("Listening API on port {}", http_port);
|
info!("Listening API on port {}", http_port);
|
||||||
|
|
||||||
axum::serve(listener, app)
|
axum::serve(listener, app)
|
||||||
|
.with_graceful_shutdown(async move { shutdown.cancelled().await })
|
||||||
.await
|
.await
|
||||||
.map_err(anyhow::Error::from)
|
.map_err(anyhow::Error::from)
|
||||||
|
.inspect(|_| info!("API shutting down complete"))?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn root() -> &'static str {
|
async fn root() -> &'static str {
|
||||||
@@ -53,10 +58,15 @@ async fn webhook(
|
|||||||
) -> Result<impl IntoResponse, AppError> {
|
) -> Result<impl IntoResponse, AppError> {
|
||||||
tracing::Span::current().record("webhook_type", tracing::field::debug(&wb));
|
tracing::Span::current().record("webhook_type", tracing::field::debug(&wb));
|
||||||
|
|
||||||
app_state
|
let event_id = wb.event_id();
|
||||||
.bot_tx
|
if app_state.bot.check_and_mark(event_id).await {
|
||||||
.try_send(wb)
|
return Err(AppError::AlreadyProcessedErr);
|
||||||
.map_err(|_| AppError::ChannelFullErr)?;
|
}
|
||||||
|
|
||||||
|
if app_state.bot_tx.try_send(wb).is_err() {
|
||||||
|
app_state.bot.unmark(event_id).await;
|
||||||
|
return Err(AppError::ChannelFullErr);
|
||||||
|
}
|
||||||
|
|
||||||
Ok((StatusCode::CREATED, "Task started"))
|
Ok((StatusCode::CREATED, "Task started"))
|
||||||
}
|
}
|
||||||
|
|||||||
+108
-8
@@ -4,7 +4,9 @@ use crate::{
|
|||||||
open_router::OpenRouterClient,
|
open_router::OpenRouterClient,
|
||||||
};
|
};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::{sync::Arc, time::Duration};
|
use std::{collections::HashSet, sync::Arc, time::Duration};
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
use tokio_util::sync::CancellationToken;
|
||||||
use tracing::{error, info, instrument};
|
use tracing::{error, info, instrument};
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
@@ -29,6 +31,7 @@ pub struct Bot {
|
|||||||
open_router_client: OpenRouterClient,
|
open_router_client: OpenRouterClient,
|
||||||
http_client: reqwest::Client,
|
http_client: reqwest::Client,
|
||||||
max_concurrent: usize,
|
max_concurrent: usize,
|
||||||
|
actions_handled: Arc<Mutex<HashSet<u64>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bot {
|
impl Bot {
|
||||||
@@ -45,6 +48,7 @@ impl Bot {
|
|||||||
)?,
|
)?,
|
||||||
max_concurrent: config.bot_max_concurrent,
|
max_concurrent: config.bot_max_concurrent,
|
||||||
config,
|
config,
|
||||||
|
actions_handled: Arc::new(Mutex::new(HashSet::new())),
|
||||||
http_client: reqwest::Client::builder()
|
http_client: reqwest::Client::builder()
|
||||||
.timeout(Duration::from_secs(gitea_timeout))
|
.timeout(Duration::from_secs(gitea_timeout))
|
||||||
.build()?,
|
.build()?,
|
||||||
@@ -54,14 +58,23 @@ impl Bot {
|
|||||||
pub async fn start(
|
pub async fn start(
|
||||||
&self,
|
&self,
|
||||||
mut rx: tokio::sync::mpsc::Receiver<WebhookType>,
|
mut rx: tokio::sync::mpsc::Receiver<WebhookType>,
|
||||||
|
shutdown: CancellationToken,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
info!("Bot started");
|
info!("Bot started");
|
||||||
|
|
||||||
let sem = Arc::new(tokio::sync::Semaphore::new(self.max_concurrent));
|
let sem = Arc::new(tokio::sync::Semaphore::new(self.max_concurrent));
|
||||||
let mut tasks = tokio::task::JoinSet::new();
|
let mut tasks = tokio::task::JoinSet::new();
|
||||||
|
|
||||||
while let Some(wb) = rx.recv().await {
|
loop {
|
||||||
// Drain completed tasks to avoid the JoinSet growing unbounded
|
let wb = tokio::select! {
|
||||||
|
biased;
|
||||||
|
_ = shutdown.cancelled() => break,
|
||||||
|
msg = rx.recv() => match msg {
|
||||||
|
Some(wb) => wb,
|
||||||
|
None => break,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
while let Some(res) = tasks.try_join_next() {
|
while let Some(res) = tasks.try_join_next() {
|
||||||
if let Err(e) = res {
|
if let Err(e) = res {
|
||||||
error!("Task panicked: {e}");
|
error!("Task panicked: {e}");
|
||||||
@@ -78,17 +91,21 @@ impl Bot {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// When all webhook tasks have completed, we can safely exit
|
|
||||||
// properly before returning
|
|
||||||
tasks.join_all().await;
|
tasks.join_all().await;
|
||||||
|
|
||||||
info!("Bot shutting down, channel closed");
|
info!("Bot shutting down complete");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip(self))]
|
#[instrument(skip(self, webhook), fields(repo, pr))]
|
||||||
pub async fn exec(&self, webhook: WebhookType) {
|
pub async fn exec(&self, webhook: WebhookType) {
|
||||||
tracing::Span::current().record("webhook_type", tracing::field::debug(&webhook));
|
match &webhook {
|
||||||
|
WebhookType::Review(p) => {
|
||||||
|
tracing::Span::current().record("repo", &p.repository.full_name);
|
||||||
|
tracing::Span::current().record("pr", p.pull_request.number);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let exec_result = match webhook {
|
let exec_result = match webhook {
|
||||||
WebhookType::Review(review_payload) => crate::bot_actions::review::exec_review(
|
WebhookType::Review(review_payload) => crate::bot_actions::review::exec_review(
|
||||||
&self.gitea_api,
|
&self.gitea_api,
|
||||||
@@ -108,4 +125,87 @@ impl Bot {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn check_and_mark(&self, event_id: u64) -> bool {
|
||||||
|
let mut action_handled_lock = self.actions_handled.lock().await;
|
||||||
|
|
||||||
|
!action_handled_lock.insert(event_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn unmark(&self, event_id: u64) {
|
||||||
|
let mut action_handled_lock = self.actions_handled.lock().await;
|
||||||
|
|
||||||
|
action_handled_lock.remove(&event_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn make_actions_handled() -> Arc<Mutex<HashSet<u64>>> {
|
||||||
|
Arc::new(Mutex::new(HashSet::new()))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn check_and_mark(actions_handled: &Arc<Mutex<HashSet<u64>>>, event_id: u64) -> bool {
|
||||||
|
let mut lock = actions_handled.lock().await;
|
||||||
|
!lock.insert(event_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn unmark(actions_handled: &Arc<Mutex<HashSet<u64>>>, event_id: u64) {
|
||||||
|
let mut lock = actions_handled.lock().await;
|
||||||
|
lock.remove(&event_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_check_and_mark_first_call_returns_false() {
|
||||||
|
let actions_handled = make_actions_handled();
|
||||||
|
assert!(!check_and_mark(&actions_handled, 1).await);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_check_and_mark_second_call_returns_true() {
|
||||||
|
let actions_handled = make_actions_handled();
|
||||||
|
check_and_mark(&actions_handled, 1).await;
|
||||||
|
assert!(check_and_mark(&actions_handled, 1).await);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_check_and_mark_different_ids_are_independent() {
|
||||||
|
let actions_handled = make_actions_handled();
|
||||||
|
check_and_mark(&actions_handled, 1).await;
|
||||||
|
assert!(!check_and_mark(&actions_handled, 2).await);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_unmark_allows_reprocessing() {
|
||||||
|
let actions_handled = make_actions_handled();
|
||||||
|
check_and_mark(&actions_handled, 1).await;
|
||||||
|
unmark(&actions_handled, 1).await;
|
||||||
|
assert!(!check_and_mark(&actions_handled, 1).await);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_unmark_nonexistent_id_is_noop() {
|
||||||
|
let actions_handled = make_actions_handled();
|
||||||
|
unmark(&actions_handled, 99).await;
|
||||||
|
assert!(!check_and_mark(&actions_handled, 99).await);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_check_and_mark_concurrent_same_id() {
|
||||||
|
let actions_handled = make_actions_handled();
|
||||||
|
let actions_handled2 = Arc::clone(&actions_handled);
|
||||||
|
|
||||||
|
let t1 = tokio::spawn(async move { check_and_mark(&actions_handled, 42).await });
|
||||||
|
let t2 = tokio::spawn(async move { check_and_mark(&actions_handled2, 42).await });
|
||||||
|
|
||||||
|
let (r1, r2) = tokio::join!(t1, t2);
|
||||||
|
let results = [r1.unwrap(), r2.unwrap()];
|
||||||
|
|
||||||
|
// exactement un seul des deux doit retourner false (non traité)
|
||||||
|
assert_eq!(results.iter().filter(|&&r| !r).count(), 1);
|
||||||
|
// l'autre doit retourner true (déjà traité)
|
||||||
|
assert_eq!(results.iter().filter(|&&r| r).count(), 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ use crate::{
|
|||||||
open_router::OpenRouterClient,
|
open_router::OpenRouterClient,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[instrument(skip(gitea_api, open_router_client, http_client, review_payload), err)]
|
#[instrument(skip(gitea_api, open_router_client, http_client, review_payload))]
|
||||||
pub async fn exec_review(
|
pub async fn exec_review(
|
||||||
gitea_api: &GiteaAPI,
|
gitea_api: &GiteaAPI,
|
||||||
open_router_client: &OpenRouterClient,
|
open_router_client: &OpenRouterClient,
|
||||||
@@ -35,7 +35,7 @@ pub async fn exec_review(
|
|||||||
|
|
||||||
let bot_result: Result<ReviewResult, anyhow::Error> = async {
|
let bot_result: Result<ReviewResult, anyhow::Error> = async {
|
||||||
let git_diff =
|
let git_diff =
|
||||||
download_git_diff(&http_client, &review_payload.pull_request.diff_url).await?;
|
download_git_diff(http_client, &review_payload.pull_request.diff_url).await?;
|
||||||
|
|
||||||
let diff_for_llm = format_diff_for_review(&git_diff);
|
let diff_for_llm = format_diff_for_review(&git_diff);
|
||||||
|
|
||||||
|
|||||||
+34
-3
@@ -41,11 +41,42 @@ pub fn load_config() -> anyhow::Result<EnvConfig> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn try_get_env(key: &str) -> anyhow::Result<String> {
|
pub fn try_get_env(key: &str) -> anyhow::Result<String> {
|
||||||
let env = std::env::var(key)?;
|
let env_value = std::env::var(key).map_err(|e| anyhow::anyhow!("{}: {}", key, e))?;
|
||||||
|
|
||||||
if env.trim().is_empty() {
|
if env_value.trim().is_empty() {
|
||||||
return Err(anyhow!(format!("env var {} is empty", key)));
|
return Err(anyhow!(format!("env var {} is empty", key)));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(env)
|
Ok(env_value)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_try_get_env_returns_value() {
|
||||||
|
unsafe { std::env::set_var("TEST_ENV_PRESENT", "hello") };
|
||||||
|
assert_eq!(try_get_env("TEST_ENV_PRESENT").unwrap(), "hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_try_get_env_missing_var_returns_error() {
|
||||||
|
unsafe { std::env::remove_var("TEST_ENV_MISSING") };
|
||||||
|
assert!(try_get_env("TEST_ENV_MISSING").is_err());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_try_get_env_empty_value_returns_error() {
|
||||||
|
unsafe { std::env::set_var("TEST_ENV_EMPTY", "") };
|
||||||
|
let err = try_get_env("TEST_ENV_EMPTY").unwrap_err();
|
||||||
|
assert!(err.to_string().contains("TEST_ENV_EMPTY"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_try_get_env_whitespace_only_returns_error() {
|
||||||
|
unsafe { std::env::set_var("TEST_ENV_WHITESPACE", " ") };
|
||||||
|
let err = try_get_env("TEST_ENV_WHITESPACE").unwrap_err();
|
||||||
|
assert!(err.to_string().contains("TEST_ENV_WHITESPACE"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,9 @@ pub enum AppError {
|
|||||||
#[error("Channel full")]
|
#[error("Channel full")]
|
||||||
ChannelFullErr,
|
ChannelFullErr,
|
||||||
|
|
||||||
|
#[error("Already processed")]
|
||||||
|
AlreadyProcessedErr,
|
||||||
|
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
BadJsonStructErr(#[from] serde_json::Error),
|
BadJsonStructErr(#[from] serde_json::Error),
|
||||||
|
|
||||||
@@ -58,6 +61,7 @@ impl IntoResponse for AppError {
|
|||||||
StatusCode::UNAUTHORIZED,
|
StatusCode::UNAUTHORIZED,
|
||||||
"WebHook sig header is invalid".to_string(),
|
"WebHook sig header is invalid".to_string(),
|
||||||
),
|
),
|
||||||
|
AppError::AlreadyProcessedErr => (StatusCode::OK, "Already processed".to_string()),
|
||||||
AppError::ChannelFullErr => {
|
AppError::ChannelFullErr => {
|
||||||
sentry_anyhow::capture_anyhow(&anyhow!("Max concurrent tasks reached"));
|
sentry_anyhow::capture_anyhow(&anyhow!("Max concurrent tasks reached"));
|
||||||
(
|
(
|
||||||
|
|||||||
+50
-17
@@ -29,7 +29,7 @@ impl GiteaAPI {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip(self), err)]
|
#[instrument(skip(self))]
|
||||||
pub async fn comment(
|
pub async fn comment(
|
||||||
&self,
|
&self,
|
||||||
body: &str,
|
body: &str,
|
||||||
@@ -57,7 +57,7 @@ impl GiteaAPI {
|
|||||||
res.json::<Comment>().await.map_err(anyhow::Error::from)
|
res.json::<Comment>().await.map_err(anyhow::Error::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip(self), err)]
|
#[instrument(skip(self))]
|
||||||
pub async fn edit_comment(
|
pub async fn edit_comment(
|
||||||
&self,
|
&self,
|
||||||
body: &str,
|
body: &str,
|
||||||
@@ -85,7 +85,7 @@ impl GiteaAPI {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip(self), err)]
|
#[instrument(skip(self))]
|
||||||
pub async fn delete_comment(&self, full_name: &str, comment_id: u64) -> anyhow::Result<()> {
|
pub async fn delete_comment(&self, full_name: &str, comment_id: u64) -> anyhow::Result<()> {
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"{}/api/v1/repos/{}/issues/comments/{}",
|
"{}/api/v1/repos/{}/issues/comments/{}",
|
||||||
@@ -104,7 +104,7 @@ impl GiteaAPI {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip(self, review_result), err)]
|
#[instrument(skip(self, review_result))]
|
||||||
pub async fn post_pull_request_review(
|
pub async fn post_pull_request_review(
|
||||||
&self,
|
&self,
|
||||||
review_result: &ReviewResult,
|
review_result: &ReviewResult,
|
||||||
@@ -158,6 +158,14 @@ pub enum WebhookType {
|
|||||||
Review(ReviewPayload),
|
Review(ReviewPayload),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl WebhookType {
|
||||||
|
pub fn event_id(&self) -> u64 {
|
||||||
|
match self {
|
||||||
|
WebhookType::Review(payload) => payload.comment.id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct ReviewPayload {
|
pub struct ReviewPayload {
|
||||||
pub action: String,
|
pub action: String,
|
||||||
@@ -198,14 +206,6 @@ impl WebhookType {
|
|||||||
_ => Err(AppError::UnknownEventErr),
|
_ => Err(AppError::UnknownEventErr),
|
||||||
}?;
|
}?;
|
||||||
|
|
||||||
let pr_body = match &wb {
|
|
||||||
WebhookType::Review(review_payload) => &review_payload.comment.body,
|
|
||||||
};
|
|
||||||
|
|
||||||
if !pr_body.starts_with(&format!("@{}", bot_name)) {
|
|
||||||
return Err(AppError::UnauthorizedUserErr);
|
|
||||||
}
|
|
||||||
|
|
||||||
let action = match &wb {
|
let action = match &wb {
|
||||||
WebhookType::Review(review_payload) => &review_payload.action,
|
WebhookType::Review(review_payload) => &review_payload.action,
|
||||||
};
|
};
|
||||||
@@ -214,6 +214,14 @@ impl WebhookType {
|
|||||||
return Err(AppError::InvalidActionErr);
|
return Err(AppError::InvalidActionErr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let pr_body = match &wb {
|
||||||
|
WebhookType::Review(review_payload) => &review_payload.comment.body,
|
||||||
|
};
|
||||||
|
|
||||||
|
if !pr_body.starts_with(&format!("@{}", bot_name)) {
|
||||||
|
return Err(AppError::UnauthorizedUserErr);
|
||||||
|
}
|
||||||
|
|
||||||
Ok(wb)
|
Ok(wb)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -229,7 +237,12 @@ mod tests {
|
|||||||
"action": "created",
|
"action": "created",
|
||||||
"pull_request": {
|
"pull_request": {
|
||||||
"id": 42,
|
"id": 42,
|
||||||
"diff_url": "https://mydiff.fr"
|
"diff_url": "https://mydiff.fr",
|
||||||
|
"number": 1,
|
||||||
|
"title": "My PR"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"full_name": "owner/repo"
|
||||||
},
|
},
|
||||||
"comment": {
|
"comment": {
|
||||||
"id": 7,
|
"id": 7,
|
||||||
@@ -288,7 +301,12 @@ mod tests {
|
|||||||
"action": "edited",
|
"action": "edited",
|
||||||
"pull_request": {
|
"pull_request": {
|
||||||
"id": 1,
|
"id": 1,
|
||||||
"diff_url": "https://mydiff.fr"
|
"diff_url": "https://mydiff.fr",
|
||||||
|
"number": 1,
|
||||||
|
"title": "My PR"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"full_name": "owner/repo"
|
||||||
},
|
},
|
||||||
"comment": {
|
"comment": {
|
||||||
"id": 1,
|
"id": 1,
|
||||||
@@ -314,7 +332,12 @@ mod tests {
|
|||||||
"action": "created",
|
"action": "created",
|
||||||
"pull_request": {
|
"pull_request": {
|
||||||
"id": 99,
|
"id": 99,
|
||||||
"diff_url": "https://mydiff.fr"
|
"diff_url": "https://mydiff.fr",
|
||||||
|
"number": 5,
|
||||||
|
"title": "My PR"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"full_name": "owner/repo"
|
||||||
},
|
},
|
||||||
"comment": {
|
"comment": {
|
||||||
"id": 12,
|
"id": 12,
|
||||||
@@ -346,7 +369,12 @@ mod tests {
|
|||||||
"action": "created",
|
"action": "created",
|
||||||
"pull_request": {
|
"pull_request": {
|
||||||
"id": 1,
|
"id": 1,
|
||||||
"diff_url": "https://mydiff.fr"
|
"diff_url": "https://mydiff.fr",
|
||||||
|
"number": 1,
|
||||||
|
"title": "My PR"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"full_name": "owner/repo"
|
||||||
},
|
},
|
||||||
"comment": {
|
"comment": {
|
||||||
"id": 1,
|
"id": 1,
|
||||||
@@ -367,7 +395,12 @@ mod tests {
|
|||||||
"action": "created",
|
"action": "created",
|
||||||
"pull_request": {
|
"pull_request": {
|
||||||
"id": 1,
|
"id": 1,
|
||||||
"diff_url": "https://mydiff.fr"
|
"diff_url": "https://mydiff.fr",
|
||||||
|
"number": 1,
|
||||||
|
"title": "My PR"
|
||||||
|
},
|
||||||
|
"repository": {
|
||||||
|
"full_name": "owner/repo"
|
||||||
},
|
},
|
||||||
"comment": {
|
"comment": {
|
||||||
"id": 1,
|
"id": 1,
|
||||||
|
|||||||
+32
-2
@@ -1,5 +1,10 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::{bot::Bot, gitea::WebhookType, state::AppState};
|
use crate::{bot::Bot, gitea::WebhookType, state::AppState};
|
||||||
|
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
|
use tokio::signal::unix::{SignalKind, signal};
|
||||||
|
use tokio_util::sync::CancellationToken;
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};
|
use tracing_subscriber::{EnvFilter, fmt, layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
|
|
||||||
@@ -54,11 +59,36 @@ async fn run() -> anyhow::Result<()> {
|
|||||||
"Starting Herald"
|
"Starting Herald"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let shutdown = CancellationToken::new();
|
||||||
|
|
||||||
let bot = Bot::new(config.clone())?;
|
let bot = Bot::new(config.clone())?;
|
||||||
let (tx, rx) = tokio::sync::mpsc::channel::<WebhookType>(config.bot_max_concurrent * 2);
|
let (tx, rx) = tokio::sync::mpsc::channel::<WebhookType>(config.bot_max_concurrent * 2);
|
||||||
let app_state = AppState { bot_tx: tx, config };
|
let app_state = AppState {
|
||||||
|
bot_tx: tx,
|
||||||
|
bot: bot.clone(),
|
||||||
|
config,
|
||||||
|
};
|
||||||
|
|
||||||
tokio::try_join!(bot.start(rx), api::start(app_state))?;
|
let signal = async {
|
||||||
|
let mut sigterm = signal(SignalKind::terminate())?;
|
||||||
|
let mut sigint = signal(SignalKind::interrupt())?;
|
||||||
|
tokio::select! {
|
||||||
|
_ = sigterm.recv() => info!("Received SIGTERM"),
|
||||||
|
_ = sigint.recv() => info!("Received SIGINT"),
|
||||||
|
}
|
||||||
|
|
||||||
|
info!("Shutting down...");
|
||||||
|
shutdown.cancel();
|
||||||
|
anyhow::Ok(())
|
||||||
|
};
|
||||||
|
|
||||||
|
tokio::try_join!(
|
||||||
|
bot.start(rx, shutdown.clone()),
|
||||||
|
api::start(app_state, shutdown.clone()),
|
||||||
|
signal
|
||||||
|
)?;
|
||||||
|
|
||||||
|
info!("Shutdown complete");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -42,7 +42,7 @@ impl OpenRouterClient {
|
|||||||
Ok(ChatResult {
|
Ok(ChatResult {
|
||||||
message: response.choices[0]
|
message: response.choices[0]
|
||||||
.content()
|
.content()
|
||||||
.map(|msg| String::from(msg))
|
.map(String::from)
|
||||||
.ok_or(anyhow::anyhow!("No content"))?,
|
.ok_or(anyhow::anyhow!("No content"))?,
|
||||||
cost: response.usage.and_then(|u| u.cost),
|
cost: response.usage.and_then(|u| u.cost),
|
||||||
})
|
})
|
||||||
|
|||||||
+2
-1
@@ -1,7 +1,8 @@
|
|||||||
use crate::{env::EnvConfig, gitea::WebhookType};
|
use crate::{bot::Bot, env::EnvConfig, gitea::WebhookType};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub bot_tx: tokio::sync::mpsc::Sender<WebhookType>,
|
pub bot_tx: tokio::sync::mpsc::Sender<WebhookType>,
|
||||||
|
pub bot: Bot,
|
||||||
pub config: EnvConfig,
|
pub config: EnvConfig,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user