bot: implement github event handling

This commit is contained in:
Antoine Martin 2021-09-12 20:13:34 +02:00
parent a402029dd2
commit cd6badb9a0
3 changed files with 73 additions and 12 deletions

View file

@ -7,13 +7,19 @@ use std::{
use anyhow::Context; use anyhow::Context;
use matrix_sdk::{ use matrix_sdk::{
room::Room, room::Room,
ruma::events::{room::member::MemberEventContent, StrippedStateEvent}, ruma::events::{
room::member::MemberEventContent, room::message::MessageEventContent,
AnyMessageEventContent, StrippedStateEvent,
},
Client, ClientConfig, Session, SyncSettings, Client, ClientConfig, Session, SyncSettings,
}; };
use tokio::sync::mpsc::UnboundedReceiver; use tokio::sync::mpsc::UnboundedReceiver;
use tracing::{debug, info}; use tracing::{debug, info, warn};
use crate::{config::ProloloConfig, webhooks::Event}; use crate::{
config::ProloloConfig,
webhooks::{github::RefType, Event, GitHubEvent},
};
mod handlers; mod handlers;
use handlers::autojoin::autojoin_authorized_rooms; use handlers::autojoin::autojoin_authorized_rooms;
@ -86,6 +92,54 @@ impl Prololo {
} }
}; };
debug!("received event: {:?}", event); debug!("received event: {:?}", event);
Self::handle_event(event, &client, &config).await
}
}
async fn handle_event(event: Event, client: &Client, config: &ProloloConfig) {
match event {
Event::GitHub(event) => Self::handle_github_event(event, client, config).await,
}
}
const SEPARATOR: &'static str = "";
async fn handle_github_event(event: GitHubEvent, client: &Client, config: &ProloloConfig) {
let message = match event {
GitHubEvent::Create(event) => match event.ref_type {
RefType::Branch => return, // new branches are handled by the Push event
RefType::Tag => {
format!(
"[{}] {} created tag {} {} {}",
event.repository.name,
event.sender.login,
event.r#ref,
Self::SEPARATOR,
event.repository.ref_url(&event.r#ref)
)
}
},
GitHubEvent::Issues => todo!(),
GitHubEvent::IssueComment => todo!(),
GitHubEvent::Push => todo!(),
};
let message = AnyMessageEventContent::RoomMessage(MessageEventContent::text_plain(message));
let room = match client.get_joined_room(&config.matrix_room_id) {
Some(room) => room,
None => {
warn!(
"room {} isn't joined yet, can't send message",
config.matrix_room_id
);
return;
}
};
if let Err(e) = room.send(message, None).await {
warn!("encountered error while sending message: {}", e);
} }
} }

View file

@ -1,5 +1,6 @@
mod github; pub mod github;
pub use github::{github_webhook, GitHubEvent}; pub use github::{github_webhook, GitHubEvent};
use tokio::sync::mpsc::UnboundedSender; use tokio::sync::mpsc::UnboundedSender;
pub struct EventSender(pub UnboundedSender<Event>); pub struct EventSender(pub UnboundedSender<Event>);

View file

@ -120,20 +120,26 @@ pub enum RefType {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct GitHubUser { pub struct GitHubUser {
login: String, pub login: String,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Repository { pub struct Repository {
name: String, pub name: String,
full_name: String, pub full_name: String,
html_url: Url, pub html_url: Url,
}
impl Repository {
pub fn ref_url(&self, r#ref: &str) -> String {
format!("https://github.com/{}/tree/{}", self.full_name, r#ref)
}
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct CreateEvent { pub struct CreateEvent {
r#ref: String, pub r#ref: String,
ref_type: RefType, pub ref_type: RefType,
repository: Repository, pub repository: Repository,
sender: GitHubUser, pub sender: GitHubUser,
} }