Gitea webhook reciver
This commit is contained in:
parent
38d1cb289e
commit
11880ac138
|
@ -26,6 +26,7 @@ lazy_static = "1.4"
|
||||||
sedregex = "0.2"
|
sedregex = "0.2"
|
||||||
rusqlite = { version = "0.26", features = ["bundled"] }
|
rusqlite = { version = "0.26", features = ["bundled"] }
|
||||||
hyper = { version = "0.14", features = ["server"] }
|
hyper = { version = "0.14", features = ["server"] }
|
||||||
|
warp = "0.3"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
tls = ["async-circe/tls"]
|
tls = ["async-circe/tls"]
|
||||||
|
|
64
src/bots/git.rs
Normal file
64
src/bots/git.rs
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
use serde_json::Value::Null;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use tokio::sync::mpsc::Sender;
|
||||||
|
use warp::Filter;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
struct Tx {
|
||||||
|
tx: Sender<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Tx {
|
||||||
|
fn new(tx: Sender<String>) -> Self {
|
||||||
|
Tx { tx }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_post(json: serde_json::Value, tx: Tx) -> Result<impl warp::Reply, warp::Rejection> {
|
||||||
|
if json["commits"] != Null {
|
||||||
|
let commits = json["commits"].as_array().unwrap();
|
||||||
|
let repo = &json["repository"]["full_name"].as_str().unwrap().trim();
|
||||||
|
if commits.len() != 1 {
|
||||||
|
tx.tx
|
||||||
|
.send(format!("{} new commits on {}:", commits.len(), repo))
|
||||||
|
.await
|
||||||
|
.expect("Failed to send string to main thread");
|
||||||
|
for commit in commits {
|
||||||
|
let author = &commit["author"]["name"].as_str().unwrap().trim();
|
||||||
|
let message = &commit["message"].as_str().unwrap().trim();
|
||||||
|
tx.tx
|
||||||
|
.send(format!("{} - {}", author, message))
|
||||||
|
.await
|
||||||
|
.expect("Failed to send string to main thread");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let author = &json["commits"][0]["author"]["name"]
|
||||||
|
.as_str()
|
||||||
|
.unwrap()
|
||||||
|
.trim();
|
||||||
|
let message = &json["commits"][0]["message"].as_str().unwrap().trim();
|
||||||
|
tx.tx
|
||||||
|
.send(format!("New commit on {}: {} - {}", repo, message, author))
|
||||||
|
.await
|
||||||
|
.expect("Failed to send string to main thread");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(warp::reply::with_status("Ok", warp::http::StatusCode::OK))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run(tx: Sender<String>, listen: SocketAddr) -> Result<(), tokio::io::Error> {
|
||||||
|
//let addr = SocketAddr::from((Ipv4Addr::new(192, 168, 1, 66), 51001));
|
||||||
|
//println!("{:?}", addr);
|
||||||
|
let tx = Tx::new(tx);
|
||||||
|
let tx_filter = warp::any().map(move || tx.clone());
|
||||||
|
|
||||||
|
let filter = warp::post()
|
||||||
|
.and(warp::body::json())
|
||||||
|
.and(tx_filter)
|
||||||
|
.and_then(handle_post);
|
||||||
|
|
||||||
|
warp::serve(filter).run(listen).await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
pub mod git;
|
||||||
pub mod leek;
|
pub mod leek;
|
||||||
pub mod misc;
|
pub mod misc;
|
||||||
pub mod sed;
|
pub mod sed;
|
||||||
|
|
22
src/main.rs
22
src/main.rs
|
@ -12,10 +12,11 @@ use serde::Deserialize;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
use std::net::SocketAddr;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::{collections::HashMap, env};
|
use std::{collections::HashMap, env};
|
||||||
use std::net::SocketAddr;
|
|
||||||
use tokio::select;
|
use tokio::select;
|
||||||
|
use tokio::sync::mpsc::{channel, Receiver};
|
||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
|
|
||||||
// this will be displayed when the help command is used
|
// this will be displayed when the help command is used
|
||||||
|
@ -54,6 +55,8 @@ pub struct AppState {
|
||||||
last_eval: HashMap<String, f64>,
|
last_eval: HashMap<String, f64>,
|
||||||
titlebot: Titlebot,
|
titlebot: Titlebot,
|
||||||
db: ExecutorConnection,
|
db: ExecutorConnection,
|
||||||
|
git_channel: String,
|
||||||
|
git_recv: Receiver<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
@ -68,7 +71,8 @@ struct ClientConf {
|
||||||
spotify_client_secret: String,
|
spotify_client_secret: String,
|
||||||
prefix: String,
|
prefix: String,
|
||||||
db_path: Option<String>,
|
db_path: Option<String>,
|
||||||
http_listen: Option<SocketAddr>
|
http_listen: Option<SocketAddr>,
|
||||||
|
git_channel: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main(flavor = "current_thread")]
|
#[tokio::main(flavor = "current_thread")]
|
||||||
|
@ -96,6 +100,13 @@ async fn main() -> anyhow::Result<()> {
|
||||||
&client_config.spotify_client_secret,
|
&client_config.spotify_client_secret,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let http_listen = client_config
|
||||||
|
.http_listen
|
||||||
|
.unwrap_or_else(|| SocketAddr::from(([127, 0, 0, 1], 5000)));
|
||||||
|
|
||||||
|
let (git_tx, git_recv) = channel(512);
|
||||||
|
bots::git::run(git_tx, http_listen).await?;
|
||||||
|
|
||||||
let config = Config::runtime_config(
|
let config = Config::runtime_config(
|
||||||
client_config.channels,
|
client_config.channels,
|
||||||
client_config.host,
|
client_config.host,
|
||||||
|
@ -115,9 +126,10 @@ async fn main() -> anyhow::Result<()> {
|
||||||
last_eval: HashMap::new(),
|
last_eval: HashMap::new(),
|
||||||
titlebot: Titlebot::create(spotify_creds).await?,
|
titlebot: Titlebot::create(spotify_creds).await?,
|
||||||
db: db_conn,
|
db: db_conn,
|
||||||
|
git_channel: client_config.git_channel,
|
||||||
|
git_recv,
|
||||||
};
|
};
|
||||||
|
|
||||||
let http_listen = client_config.http_listen.unwrap_or_else(|| SocketAddr::from(([127, 0, 0, 1], 5000)));
|
|
||||||
if let Err(e) = executor(state, http_listen).await {
|
if let Err(e) = executor(state, http_listen).await {
|
||||||
tracing::error!("Error in message loop: {}", e);
|
tracing::error!("Error in message loop: {}", e);
|
||||||
}
|
}
|
||||||
|
@ -153,6 +165,10 @@ async fn message_loop(state: &mut AppState) -> anyhow::Result<()> {
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(s) = state.git_recv.recv().await {
|
||||||
|
state.client.privmsg(&state.git_channel, &s).await?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,29 @@
|
||||||
use std::net::SocketAddr;
|
|
||||||
use crate::ExecutorConnection;
|
use crate::ExecutorConnection;
|
||||||
use std::convert::Infallible;
|
|
||||||
use std::sync::Arc;
|
|
||||||
use hyper::{Body, Request, Response, Server};
|
|
||||||
use hyper::service::{make_service_fn, service_fn};
|
use hyper::service::{make_service_fn, service_fn};
|
||||||
|
use hyper::{Body, Request, Response, Server};
|
||||||
|
use std::convert::Infallible;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub async fn run(db: ExecutorConnection, listen: SocketAddr) -> anyhow::Result<()> {
|
pub async fn run(db: ExecutorConnection, listen: SocketAddr) -> anyhow::Result<()> {
|
||||||
let db = Arc::new(db);
|
let db = Arc::new(db);
|
||||||
|
|
||||||
Server::bind(&listen).serve(make_service_fn(|_| {
|
Server::bind(&listen)
|
||||||
let db = Arc::clone(&db);
|
.serve(make_service_fn(|_| {
|
||||||
async move {
|
let db = Arc::clone(&db);
|
||||||
Ok::<_, Infallible>(service_fn(move |r| handle(r, Arc::clone(&db))))
|
async move { Ok::<_, Infallible>(service_fn(move |r| handle(r, Arc::clone(&db)))) }
|
||||||
}
|
}))
|
||||||
})).await?;
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle(req: Request<Body>, db: Arc<ExecutorConnection>) -> Result<Response<Body>, Infallible> {
|
async fn handle(
|
||||||
Ok(Response::new(Body::from(format!("{:?}", db.get_quote(None).await))))
|
_req: Request<Body>,
|
||||||
|
db: Arc<ExecutorConnection>,
|
||||||
|
) -> Result<Response<Body>, Infallible> {
|
||||||
|
Ok(Response::new(Body::from(format!(
|
||||||
|
"{:?}",
|
||||||
|
db.get_quote(None).await
|
||||||
|
))))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue